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

import cn.estsh.i3plus.ext.mes.api.base.INumberRuleExtService;
import cn.estsh.i3plus.ext.mes.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.api.base.bu.ISxBuBaseDataService;
import cn.estsh.i3plus.ext.mes.api.busi.IProductEncodeCfgExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesNumberRuleExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuNumberRuleModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuWorkCenterModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuWorkOrderModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.MesShiftExtModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductEncodeCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCenterRepository;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.log4j.Log4j2;
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-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-06-10 13:51
 */
@Service
@Log4j2
public class SxBuBaseDataService implements ISxBuBaseDataService {

    @Autowired
    private MesWorkCenterRepository workCenterRepository;

    @Autowired
    private MesWorkCenterExtRepository workCenterExtRepository;

    @Autowired
    private MesWorkOrderRepository workOrderRepository;

    @Autowired
    private MesShiftExtRepository shiftExtRepository;

    @Autowired
    private IProductEncodeCfgExtService productEncodeCfgExtService;

    @Autowired
    private INumberRuleExtService numberRuleExtService;

    @Autowired
    private IShiftExtService shiftExtService;

    @Override
    public ListPager<BuWorkCenterModel> queryWorkCenterByListPager(String organizeCode, String workCenterCode, String workCenterName, String careerCode, String categoryCode, Pager pager) {
        log.info("查询产线信息入参Pager:{}-产线编码{}-产线名称{}",pager, workCenterCode, workCenterName);
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringLikerPack(workCenterCode, MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringLikerPack(workCenterName, MesExtConstWords.WORK_CENTER_NAME, packBean);
        DdlPreparedPack.getStringEqualPack(careerCode, MesExtConstWords.CAREER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(categoryCode, MesExtConstWords.CATEGORY_CODE, packBean);
        pager = PagerHelper.getPager(pager, workCenterExtRepository.findByHqlWhereCount(packBean));
        List<MesWorkCenterExt> workCenterExtList = workCenterExtRepository.findByHqlWherePage(packBean, pager);

        List<BuWorkCenterModel> workCenterModelList = new ArrayList<>();

        if(CollectionUtils.isEmpty(workCenterExtList)){
            return new ListPager(workCenterModelList,pager);
        }

        workCenterExtList.stream().forEach(mesWorkCenter -> {
            BuWorkCenterModel workCenterModel = new BuWorkCenterModel();
            workCenterModel.setWorkCenterCode(mesWorkCenter.getWorkCenterCode());
            workCenterModel.setWorkCenterName(mesWorkCenter.getWorkCenterName());
            workCenterModelList.add(workCenterModel);
        });

        log.info("回参Pager:{}",pager);
        return new ListPager(workCenterModelList,pager);
    }

    @Override
    public List<BuWorkCenterModel> queryWorkCenters(String organizeCode, String lineType) {
        log.info("产线类型-{}",lineType);
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(lineType, MesExtConstWords.LINE_TYPE, packBean);
        packBean.setOrderByStr(" order by createDatetime asc ");
        List<MesWorkCenterExt> workCenters = workCenterExtRepository.findByHqlWhere(packBean);

        List<BuWorkCenterModel> workCenterModelList = new ArrayList<>();

        workCenters.stream().forEach(mesWorkCenter -> {
            BuWorkCenterModel workCenterModel = new BuWorkCenterModel();
            workCenterModel.setWorkCenterCode(mesWorkCenter.getWorkCenterCode());
            workCenterModel.setWorkCenterName(mesWorkCenter.getWorkCenterName());
            workCenterModelList.add(workCenterModel);
        });
        return workCenterModelList;
    }

    @Override
    public List<BuWorkOrderModel> queryWorkOrdersByPlanTime(String organizeCode, String startTime, String endTime, List<String> workOrderNoList) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (!StringUtils.isEmpty(startTime) || !StringUtils.isEmpty(endTime)) {
            DdlPreparedPack.timeBuilder(startTime, endTime, MesExtConstWords.START_TIME, packBean, true);
        }
        DdlPreparedPack.getInPackList(workOrderNoList, MesExtConstWords.ORDER_NO, packBean);
        List<MesWorkOrder> workOrderList = workOrderRepository.findByHqlWhere(packBean);

        List<BuWorkOrderModel> workOrderModels = new ArrayList<>();
        workOrderList.stream().forEach(mesWorkOrder -> {
            BuWorkOrderModel workOrderModel = new BuWorkOrderModel();
            BeanUtils.copyProperties(mesWorkOrder, workOrderModel);
            workOrderModel.setWorkOrderNo(mesWorkOrder.getOrderNo());
            workOrderModels.add(workOrderModel);
        });
        return workOrderModels;
    }

    @Override
    public MesShiftExtModel queryMesShift( String organizeCode, String workCenterCode, String shiftCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(shiftCode, MesExtConstWords.SHIFT_CODE, packBean);
        MesShiftExt shiftExt = shiftExtRepository.getByProperty(packBean);

        if(shiftExt == null){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("班次不存在")
                    .setErrorSolution("请确保产线【%s】-班次【%s】是否已维护",workCenterCode,shiftCode)
                    .build();
        }

        MesShiftExtModel model = new MesShiftExtModel();
        BeanUtils.copyProperties(shiftExt, model);
        return model;
    }

    @Override
    public BuNumberRuleModel queryMesSnRule(String organizeCode, String partNo, Integer codeType, Integer matchType) {
        MesProductEncodeCfg productEncodeCfg = new MesProductEncodeCfg();
        productEncodeCfg.setCodeType(codeType);
        productEncodeCfg.setMatchValue(partNo);
        productEncodeCfg.setMatchType(matchType);
        MesProductEncodeCfg productEncodeCfgDb = productEncodeCfgExtService.getMesProductEncodeCfg(productEncodeCfg);
        log.info("物料{}-编码类型{}-匹配类型{}",partNo, codeType, matchType);
        if(productEncodeCfgDb == null){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("编码规则不存在")
                    .setErrorSolution("请确保是否已维护 物料编码【%s】-编码类型【%s】-匹配类型【%s】 的生产编码规则", partNo, codeType, matchType)
                    .build();
        }

        MesNumberRuleExt numberRuleExt = new MesNumberRuleExt();
        numberRuleExt.setRuleCode(productEncodeCfgDb.getRuleCode());
        MesNumberRuleExt numberRuleDb = numberRuleExtService.getNumberRule(numberRuleExt);
        BuNumberRuleModel buNumberRuleModel = new BuNumberRuleModel();

        if(numberRuleDb == null){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("条码生成规则不存在")
                    .setErrorSolution("请确保规则代码【%s】是否已维护", productEncodeCfgDb.getRuleCode())
                    .build();
        }

        BeanUtils.copyProperties(numberRuleDb, buNumberRuleModel);

        return buNumberRuleModel;
    }
}
