package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.ep;

import cn.estsh.i3plus.ext.mes.api.busi.ep.IEpQmsGetDataStatisticsService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.ep.IEpMesOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesRepairTask;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpOutPutStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpRepairTaskModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesRepairTaskRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-04-06 16:38
 */
@Service
public class EpQmsGetDataStatisticsService implements IEpQmsGetDataStatisticsService {

    @Autowired
    private IEpMesOutPutStatisticsDao epMesOutPutStatisticsDao;

    @Autowired
    private MesRepairTaskRepository repairTaskRepository;

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;
    @Override
    public List<EpOutPutStatisticsModel> queryMesOutPutStatisticsByTimeAndWorkOrderType(String organizeCode, String startTime, String endTime, Integer workOrderType) {
        return epMesOutPutStatisticsDao.getMesOutPutStatisticsByTimeAndWorkOrderType(organizeCode, startTime, endTime, workOrderType);
    }

    @Override
    public List<EpRepairTaskModel> queryMesRepairTaskStatistics(String organizeCode, String startTime, String endTime) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (!StringUtils.isEmpty(startTime) || !StringUtils.isEmpty(endTime)) {
            DdlPreparedPack.timeBuilder(startTime, endTime, MesExtConstWords.WORK_TIME, packBean, false);
        }
        List<MesRepairTask> mesRepairTaskDbList = repairTaskRepository.findByHqlWhere(packBean);
        List<EpRepairTaskModel> repairTaskModels = new ArrayList<>();
        mesRepairTaskDbList.stream().forEach(o->{
            EpRepairTaskModel repairTaskModel = new EpRepairTaskModel();
            BeanUtils.copyProperties(o,repairTaskModel);
            repairTaskModels.add(repairTaskModel);
        });
        return repairTaskModels;
    }

    @Override
    public List<EpOutPutStatisticsModel> queryOutPutStatisticsByTimeAndIsOqc(String organizeCode, String startTime, String endTime, Integer workOrderType) {
        return epMesOutPutStatisticsDao.queryOutPutStatisticsByTimeAndIsOqc(organizeCode, startTime, endTime, workOrderType);
    }

    @Override
    public List<EpOutPutStatisticsModel> queryMesOutPutStatisticsByWorkOrderNoList(EpOutPutStatisticsModel model) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        DdlPreparedPack.getInPack(model.getWorkOrderNoList(), MesExtConstWords.WORK_ORDER_NO, packBean);
        List<MesOutPutStatistics> outPutStatistics = outPutStatisticsRepository.findByHqlWhere(packBean);
        List<EpOutPutStatisticsModel> outPutStatisticsModelList = new ArrayList<>(outPutStatistics.size());
        if(!CollectionUtils.isEmpty(outPutStatistics)){
            outPutStatistics.stream().forEach(opt -> {
                EpOutPutStatisticsModel outPutStatisticsModel = new EpOutPutStatisticsModel();
                BeanUtils.copyProperties(opt, outPutStatisticsModel);
                outPutStatisticsModelList.add(outPutStatisticsModel);
            });
        }
        return outPutStatisticsModelList;
    }
}
