package com.gree.mall.miniapp.logic.common;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;

import com.gree.mall.miniapp.bean.es.CommonListES;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.es.EsLogic;
import com.gree.mall.miniapp.plus.entity.WorkerComList;
import com.gree.mall.miniapp.plus.entity.WorkerComListWebsit;
import com.gree.mall.miniapp.plus.entity.WorkerComListWorker;
import com.gree.mall.miniapp.plus.service.AdminWebsitService;
import com.gree.mall.miniapp.plus.service.WorkerComListService;
import com.gree.mall.miniapp.plus.service.WorkerComListWebsitService;
import com.gree.mall.miniapp.plus.service.WorkerComListWorkerService;
import com.gree.mall.miniapp.utils.StringUtil;
import com.gree.mall.miniapp.utils.oss.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WorkerComListLogic {

    @Autowired
    CommonLogic commonLogic;
    @Autowired
    OSSUtil ossUtil;
    @Autowired
    EsLogic esLogic;
    @Autowired
    WorkerComListService workerComListService;
    @Autowired
    WorkerComListWorkerService workerComListWorkerService;
    @Autowired
    WorkerComListWebsitService workerComListWebsitService;
    @Autowired
    AdminWebsitService adminWebsitService;


    /**
     * 列表
     */
    public IPage<WorkerComList> list(String workerNumber, String title, Boolean status, String remark, Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        List<String> comListIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(workerNumber)) {
            //师傅编号不为空以师傅为准
            List<WorkerComListWorker> worker = workerComListWorkerService.lambdaQuery()
                    .eq(WorkerComListWorker::getWorkerComListWorkerNumber, workerNumber).list();
            if (CollectionUtils.isEmpty(worker)) {
                return new Page<>(pageNum, pageSize);
            }
            comListIds = worker.stream().map(WorkerComListWorker::getWorkerComListId).distinct().collect(Collectors.toList());
        }else {
            throw new RemoteServiceException("师傅编号未传");
        }
        IPage<WorkerComList> page = workerComListService.lambdaQuery()
                .like(StringUtils.isNotEmpty(title), WorkerComList::getTitle, title)
                .eq(status != null, WorkerComList::getStatus, status)
                .like(StringUtils.isNotEmpty(remark), WorkerComList::getRemark, remark)
                .in(!CollectionUtils.isEmpty(comListIds), WorkerComList::getId, comListIds)
                .orderByDesc(WorkerComList::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
        return page;
    }



    /**
     * 通用列表明细
     *
     * @param comListId
     * @param workerNumber
     */
    public List<CommonListES> record(String comListId, String workerNumber) {
        if (StringUtil.isEmpty(workerNumber))
         throw new RemoteServiceException("师傅编号未传");
        List<CommonListES> list = esLogic.list(Arrays.asList(workerNumber), workerNumber, comListId);
        return list;
    }



}
