package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.bu;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuOutPutStatisticsService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.bu.IBuOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.OqcLastCheckModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOutPutStatisticsModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOutPutStatisticsQtyModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class BuOutPutStatisticsService implements IBuOutPutStatisticsService {

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Autowired
    private IBuOutPutStatisticsDao outPutStatisticsDao;

    @Autowired
    private ISxWorkOrderExtService sxWorkOrderExtService;

    @Override
    public List<BuOutPutStatisticsQtyModel> queryOutPutStatisticsQtyList(BuOutPutStatisticsQtyModel model) {
        List<BuOutPutStatisticsQtyModel> buOutPutStatisticsQtyModels;
        if (StringUtils.isEmpty(model.getCategoryCode3()) && StringUtils.isEmpty(model.getSalesTo())) {
            buOutPutStatisticsQtyModels = queryOutPutStatisticsList(model);
        } else {
            buOutPutStatisticsQtyModels = outPutStatisticsDao.queryOutPutStatisticsListByPart(model);
        }

        if(!CollectionUtils.isEmpty(buOutPutStatisticsQtyModels)){
            List<String> workOrderNoList = buOutPutStatisticsQtyModels.stream().map(BuOutPutStatisticsQtyModel::getWorkOrderNo).distinct().collect(Collectors.toList());
            List<MesWorkOrder> mesWorkOrders = sxWorkOrderExtService.queryMesWorkOrderByOrderNoList(workOrderNoList, model.getOrganizeCode());
            Map<String, MesWorkOrder> workOrderMap = mesWorkOrders.stream().collect(Collectors.toMap(MesWorkOrder::getOrderNo, mesWorkOrder -> mesWorkOrder));
            for (BuOutPutStatisticsQtyModel buOutPutStatisticsQtyModel : buOutPutStatisticsQtyModels) {
                MesWorkOrder mesWorkOrder = workOrderMap.get(buOutPutStatisticsQtyModel.getWorkOrderNo());
                buOutPutStatisticsQtyModel.setUnFinishQty(mesWorkOrder.getQty() - mesWorkOrder.getCompleteQty());
            }
        }

        return buOutPutStatisticsQtyModels;
    }

    private List<BuOutPutStatisticsQtyModel> queryOutPutStatisticsList(BuOutPutStatisticsQtyModel model) {
        List<BuOutPutStatisticsQtyModel> resultList = null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(model.getWorkOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(model.getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringEqualPack(model.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(model.getWorkOrderType(), MesPcnExtConstWords.WORK_ORDER_TYPE, packBean);
        if (!StringUtils.isEmpty(model.getWorkTimeStart()) || !StringUtils.isEmpty(model.getWorkTimeEnd())) {
            DdlPreparedPack.timeBuilder(model.getWorkTimeStart(), model.getWorkTimeEnd(), MesPcnExtConstWords.WORK_TIME, packBean, false);
        }

        List<MesOutPutStatistics> outPutStatisticsList = outPutStatisticsRepository.findByHqlWhere(packBean);

        if (CollectionUtils.isEmpty(outPutStatisticsList)) return resultList;

        for (MesOutPutStatistics item : outPutStatisticsList) {
            if (null == item) continue;

            BuOutPutStatisticsQtyModel result = new BuOutPutStatisticsQtyModel();
            BeanUtils.copyProperties(item, result);

            if (CollectionUtils.isEmpty(resultList)) resultList = new ArrayList<>();
            resultList.add(result);
        }
        return resultList;
    }

    @Override
    public List<BuOutPutStatisticsModel> queryOutPutStatisticsModelList(OqcLastCheckModel model, String organizeCode) {
        List<MesOutPutStatistics> mesOutPutStatistics = queryOutPutStatisticsList(model, organizeCode);
        List<String> workOrderNoList = mesOutPutStatistics.stream().map(MesOutPutStatistics::getWorkOrderNo).distinct().collect(Collectors.toList());
        List<MesWorkOrder> workOrderList = sxWorkOrderExtService.queryMesWorkOrderByOrderNoList(workOrderNoList, organizeCode);
        Map<String, MesWorkOrder> workOrderByOrderNoMap =workOrderList.stream().collect(Collectors.toMap(MesWorkOrder::getOrderNo,mesWorkOrder -> mesWorkOrder));
        List<BuOutPutStatisticsModel> outPutStatisticsModels = new ArrayList<>();
        mesOutPutStatistics.stream().forEach(outPutStatistics -> {
            BuOutPutStatisticsModel outPutStatisticsModel = new BuOutPutStatisticsModel();
            BeanUtils.copyProperties(outPutStatistics, outPutStatisticsModel);
            outPutStatisticsModel.setUnFinishQty(outPutStatisticsModel.getPlanQty() - workOrderByOrderNoMap.get(outPutStatistics.getWorkOrderNo()).getCompleteQty());
            outPutStatisticsModels.add(outPutStatisticsModel);
        });
        return outPutStatisticsModels;
    }

    @Override
    public List<MesOutPutStatistics> queryOutPutStatisticsList(OqcLastCheckModel model, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(model.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(model.getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringEqualPack(model.getWorkOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(model.getShiftCode(), MesPcnExtConstWords.SHIFT_CODE, packBean);
        if (!StringUtils.isEmpty(model.getWorkTimeStart()) || !StringUtils.isEmpty(model.getWorkTimeEnd())) {
            DdlPreparedPack.timeBuilder(model.getWorkTimeStart(), model.getWorkTimeEnd(), MesPcnExtConstWords.WORK_TIME, packBean, false);
        }
        return outPutStatisticsRepository.findByHqlWhere(packBean);
    }

}
