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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxCloudApiBusiService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToStatisticsOutPut;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToStatisticsOutPutParamModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdDataToStatisticsOutPutRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftProdCenterRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author yebu
 * @description 微服接口业务接口
 * @date 2023/7/20 18:16
 */
@Slf4j
@Service
public class SxCloudApiBusiServiceImpl implements ISxCloudApiBusiService {

    @Autowired
    private MesProdDataToStatisticsOutPutRepository prodDataToStatisticsOutPutRDao;

    @Autowired
    private MesShiftProdCenterRecordRepository shiftProdCenterRecordRepository;

    @Override
    public List<MesProdDataToStatisticsOutPut> queryProdDataToMonthlyStatistics(SxProdDataToStatisticsOutPutParamModel model) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(model.getDataKey(), MesExtConstWords.DATA_KEY,packBean);
        DdlPreparedPack.getStringEqualPack(model.getDataType(),MesExtConstWords.DATA_TYPE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getPartNo(),MesExtConstWords.PART_NO,packBean);
        DdlPreparedPack.getStringEqualPack(model.getPartNameRdd(),MesExtConstWords.PART_NAME_RDD,packBean);
        DdlPreparedPack.getStringEqualPack(model.getShiftCode(),MesExtConstWords.SHIFT_CODE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getStatisticsDate(),MesExtConstWords.STATISTICS_DATE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getWorkCenterCode(),MesExtConstWords.WORK_CENTER_CODE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getWorkTime(),MesExtConstWords.WORK_TIME,packBean);
        DdlPreparedPack.getNumEqualPack(model.getWorkOrderType(),MesExtConstWords.WORK_ORDER_TYPE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getCareerCode(),MesExtConstWords.CAREER_CODE,packBean);
        DdlPreparedPack.getStringEqualPack(model.getCategoryCode(),MesExtConstWords.CATEGORY_CODE,packBean);
        return prodDataToStatisticsOutPutRDao.findByHqlWhere(packBean);
    }

    @Override
    public List<MesShiftProdCenterRecord> queryNormalProdAndUnnormalProdShiftProdCenterRecord(String workTimeStart, String workTimeEnd, String workCenterCode, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.timeBuilder(workTimeStart, workTimeEnd, MesExtConstWords.WORK_TIME, packBean, false);
        DdlPreparedPack.getStringEqualPack(workCenterCode, MesExtConstWords.WORK_CENTER_CODE, packBean);
        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = shiftProdCenterRecordRepository.findByHqlWhere(packBean);

        shiftProdCenterRecordList = shiftProdCenterRecordList.stream().filter(o -> (o != null) && (o.getProdType() == MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_PROD.getValue() || o.getProdType() == MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_PROD.getValue())).collect(Collectors.toList());

        return shiftProdCenterRecordList;
    }
}
