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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesConfigService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNumberRuleMatchDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPullingOrderInfoService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.model.MesSpsDetailInfo;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.model.MesSpsInfo;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.*;
import cn.estsh.i3plus.pojo.mes.repository.*;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class MesPullingOrderInfoService implements IMesPullingOrderInfoService {
    public static final Logger LOGGER = LoggerFactory.getLogger(MesPullingOrderInfoService.class);
    @Autowired
    private MesPullingOrderInfoRepository mesPullingOrderInfoRepository;

    @Autowired
    private MesPullingOrderPartInfoRepository mesPullingOrderPartInfoRepository;

    @Autowired
    private MesPartPullDetailRepository mesPartPullDetailRDao;

    @Autowired
    private IMesProduceSnExtService mesProduceSnExtService;

    @Autowired
    private IMesConfigService configService;

    @Autowired
    private IMesPrintedSnLogRepository snLogRao;

    @Autowired
    private MesConfigRepository configRao;

    @Autowired
    private MesCustomerCarModelRepository carModelRepository;

    @Autowired
    private MesWorkOrderRepository workOrderRao;


    @Override
    public ListPager<MesPullingOrderInfo> queryMesPullingOrderInfoByPager(MesPullingOrderInfo bean, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        setPackQueryBean(bean, packBean);
        if (!StringUtil.isEmpty(bean.orderBy())) {
            packBean.setOrderByStr(bean.orderBy());
        } else {
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, packBean);
        }
        //
        if (!StringUtil.isEmpty(bean.getCreateDatetime())) {
            DdlPreparedPack.getStringBiggerPack(bean.getCreateDatetime(), "createDatetime", packBean);
        }
        pager = PagerHelper.getPager(pager, mesPullingOrderInfoRepository.findByHqlWhereCount(packBean));

        List<MesPullingOrderInfo> resultList = mesPullingOrderInfoRepository.findByHqlWherePage(packBean, pager);

        return new ListPager<>(resultList, pager);
    }

    protected void setPackQueryBean(MesPullingOrderInfo bean, DdlPackBean packBean) {
        DdlPreparedPack.getStringEqualPack(bean.getPullCode(), "pullCode", packBean);
        DdlPreparedPack.getStringEqualPack(bean.getPullingOrderNo(), "pullingOrderNo", packBean);
        DdlPreparedPack.getStringEqualPack(bean.getWorkCenterCode(), "workCenterCode", packBean);
        DdlPreparedPack.getNumEqualPack(bean.getPullOrderType(), "pullOrderType", packBean);
        DdlPreparedPack.getNumEqualPack(bean.getPrintStatus(), "printStatus", packBean);
        //已确认、已扫描、已发送设备
        if (StringUtil.isEmpty(bean.getPullOrderStatus())) {
            DdlPreparedPack.getInPackList(Arrays.asList(
                    MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_10.getValue(),
                    MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue(),
                    MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_30.getValue()), "pullOrderStatus", packBean);
        } else {
            DdlPreparedPack.getNumEqualPack(bean.getPullOrderStatus(), "pullOrderStatus", packBean);
        }
        DdlPreparedPack.getStringEqualPack(bean.getWorkOrderNo(), "workOrderNo", packBean);
        DdlPreparedPack.getStringLikerPack(bean.getPartNo(), "partNo", packBean);
    }

    @Override
    public void doMesPullingOrderInfoScan(MesPullingOrderInfo bean, String userName) {
        //校验是不是第一行数据
        DdlPackBean packBeanOne = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(bean.getPullCode(), "pullCode", packBeanOne);
        DdlPreparedPack.getStringEqualPack(bean.getPullingOrderNo(), "pullingOrderNo", packBeanOne);
        DdlPreparedPack.getStringEqualPack(bean.getWorkCenterCode(), "workCenterCode", packBeanOne);
        MesPullingOrderInfo mesPullingOrderInfo = mesPullingOrderInfoRepository.getByProperty(packBeanOne);
        if (StringUtil.isEmpty(mesPullingOrderInfo)) {
            MesPcnException.throwMesBusiException("扫描拉动单号【%s】查询数据为空", bean.getPullingOrderNo());
        }
        if (mesPullingOrderInfo.getPullOrderStatus() != MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_10.getValue()) {
            MesPcnException.throwMesBusiException("扫描拉动单号【%s】拉动单状态不是【已确认】", bean.getPullingOrderNo());
        }

        //强过码 如果强过为true 不校验顺序 直接扫描完成修改状态为已扫描
        //扫完单号 扫强过码校验通过后 单号顺序之前的单子变成无序 都可扫描 43289 SPS拉动提示系统异常
//        String spsPass = configService.getCfgValue(bean.getOrganizeCode(), MesPcnExtConstWords.SPS_STRONGER_PASS);
        if ((!Objects.isNull(mesPullingOrderInfo.getIsPassCode()) && mesPullingOrderInfo.getIsPassCode() == CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue())
                || (!Objects.isNull(bean.getIsPass()) && CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == bean.getIsPass())) {
            mesPullingOrderInfo.setPullOrderStatus(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue());
            ConvertBean.serviceModelUpdate(mesPullingOrderInfo, userName);
            mesPullingOrderInfoRepository.update(mesPullingOrderInfo);
            //修改当前单号之前的为可强过
            DdlPackBean packBeanPass = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(bean.getPullCode(), "pullCode", packBeanPass);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_10.getValue(), "pullOrderStatus", packBeanPass);
            DdlPreparedPack.getStringSmallerNotEqualPack(bean.getPullingOrderNo(), "pullingOrderNo", packBeanPass);
            DdlPreparedPack.getStringEqualPack(bean.getWorkCenterCode(), "workCenterCode", packBeanPass);
            DdlPreparedPack.getNumNOEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isPassCode", packBeanPass);
            List<MesPullingOrderInfo> mesPullingOrderInfos = mesPullingOrderInfoRepository.findByHqlWhere(packBeanPass);
            if (!CollectionUtils.isEmpty(mesPullingOrderInfos)) {
                for (MesPullingOrderInfo pullingOrderInfo : mesPullingOrderInfos) {
                    pullingOrderInfo.setIsPassCode(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                    ConvertBean.serviceModelUpdate(pullingOrderInfo, userName);
                }
                mesPullingOrderInfoRepository.saveAll(mesPullingOrderInfos);
            }
        } else {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(bean.getPullCode(), "pullCode", packBean);
            DdlPreparedPack.getStringEqualPack(bean.getWorkCenterCode(), "workCenterCode", packBean);
            DdlPreparedPack.getNumNOEqualPack(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), "isPassCode", packBean);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PART_PULL_ORDER_TYPE.SPS_PULL.getValue(), "pullOrderType", packBean);
            DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_10.getValue(), "pullOrderStatus", packBean);

            //顺序校验 根据未扫描单号 1 、2 、3，如果没有强过码 必须先扫1 再扫2  pullingOrderNo修改位workOrderNo
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{"pullingOrderNo"}, packBean);
            List<MesPullingOrderInfo> pullingOrderInfos = mesPullingOrderInfoRepository.findByHqlWhere(packBean);
            if (!CollectionUtils.isEmpty(pullingOrderInfos)) {
                //按照排序 依次扫描，如果扫描不按照顺序 报错
                MesPullingOrderInfo pullingOrderInfo = pullingOrderInfos.get(0);
                if (pullingOrderInfo.getPullingOrderNo().equals(mesPullingOrderInfo.getPullingOrderNo())) {
                    mesPullingOrderInfo.setPullOrderStatus(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue());
                    ConvertBean.serviceModelUpdate(mesPullingOrderInfo, userName);
                    mesPullingOrderInfoRepository.update(mesPullingOrderInfo);
                } else {
                    MesPcnException.throwMesBusiException("扫描拉动单号【%s】SPS排序单校验顺序错误", bean.getPullingOrderNo());
                }
            } else {
                MesPcnException.throwMesBusiException("扫描拉动单号产线拉动组【%s】数据为空", bean.getPullingOrderNo());
            }
        }

    }

    @Override
    public List<MesPullingOrderInfo> doMesPullingOrderInfoPrint(MesPullingOrderInfo bean) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());

        List<String> pullCodeList = Arrays.asList(bean.getPullCode().split(MesPcnExtConstWords.COMMA));
        List<String> workCenterCodeList = Arrays.asList(bean.getWorkCenterCode().split(MesPcnExtConstWords.COMMA));

        if (pullCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(pullCodeList.get(0), MesPcnExtConstWords.PULL_CODE, packBean);
        else DdlPreparedPack.getInPackList(pullCodeList, MesPcnExtConstWords.PULL_CODE, packBean);

        if (workCenterCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(workCenterCodeList.get(0), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        else DdlPreparedPack.getInPackList(workCenterCodeList, MesPcnExtConstWords.WORK_CENTER_CODE, packBean);

        DdlPreparedPack.getStringEqualPack(bean.getPullingOrderNo(), MesPcnExtConstWords.PULLING_ORDER_NO, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PRINT_STATUS.UNPRINT.getValue(), MesPcnExtConstWords.PRINT_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.CUST_ORDER_NO}, packBean);

        List<MesPullingOrderInfo> pullingOrderInfos = mesPullingOrderInfoRepository.findByHqlTopWhere(packBean, MesPcnExtConstWords.THREE);

        log.info("打印队列查询 --- 拉动单 --- 查询到打印数据: {} ---", CollectionUtils.isEmpty(pullingOrderInfos) ? "[]" :
                pullingOrderInfos.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkOrderNo()))).map(MesPullingOrderInfo::getWorkOrderNo).collect(Collectors.toList()).toString());

        if (!CollectionUtils.isEmpty(pullingOrderInfos)) {
            //根据车型代码查询车型信息
            Map<String, List<MesCustomerCarModel>> mesCustomerCarModelMap = getMesCustomerCarModelMap(bean.getOrganizeCode(), pullingOrderInfos.stream().map(MesPullingOrderInfo::getCarModelCode).distinct().collect(Collectors.toList()));
            //把查出来的拉动单打印并修改打印状态为已打印
            List<MesPrintedSnLog> snLogList = new ArrayList<>();
            for (MesPullingOrderInfo pullingOrderInfo : pullingOrderInfos) {
                //查询对应的工单
                DdlPackBean workOrderPackBean = DdlPackBean.getDdlPackBean(pullingOrderInfo.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(pullingOrderInfo.getWorkOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, workOrderPackBean);
                List<MesWorkOrder> mesWorkOrders = workOrderRao.findByHqlTopWhere(workOrderPackBean, 1);
                MesWorkOrder mesWorkOrder;
                if (!CollectionUtils.isEmpty(mesWorkOrders)) {
                    mesWorkOrder = mesWorkOrders.get(0);
                }else {
                    mesWorkOrder = new MesWorkOrder();
                }
                //查询拉动组明细
                DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(pullingOrderInfo.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(pullingOrderInfo.getPullingOrderNo(), MesPcnExtConstWords.PULLING_ORDER_NO, partPackBean);
                List<MesPullingOrderPartInfo> pullingOrderPartInfos = mesPullingOrderPartInfoRepository.findByHqlWhere(partPackBean);
                if (!CollectionUtils.isEmpty(pullingOrderPartInfos)) {
                    pullingOrderInfo.setPartCount(pullingOrderPartInfos.size());
                    pullingOrderPartInfos.forEach(item->{
                        if(!StringUtil.isEmpty(item.getLocation()) && item.getLocation().contains(MesPcnExtConstWords.COMMA)){
                            item.setLocatAddr(item.getLocation().split(MesPcnExtConstWords.COMMA)[0]);
                            item.setLightAddr(item.getLocation().split(MesPcnExtConstWords.COMMA)[1]);
                        };
                        item.setAssemblyPartNo(pullingOrderInfo.getPartNo());
                    });
                    pullingOrderInfo.setPullingOrderPartInfos(pullingOrderPartInfos);
                }
                pullingOrderInfo.setWorkOrderNoLast(!StringUtil.isEmpty(pullingOrderInfo.getWorkOrderNo())?pullingOrderInfo.getWorkOrderNo().substring(pullingOrderInfo.getWorkOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setWorkOrderNoPre(!StringUtil.isEmpty(pullingOrderInfo.getWorkOrderNo())?pullingOrderInfo.getWorkOrderNo().substring(MesPcnExtConstWords.ZERO,pullingOrderInfo.getWorkOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setCustOrderNoLast(!StringUtil.isEmpty(pullingOrderInfo.getCustOrderNo())?pullingOrderInfo.getCustOrderNo().substring(pullingOrderInfo.getCustOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setCarModelName((Objects.isNull(mesCustomerCarModelMap) || StringUtil.isEmpty(pullingOrderInfo.getCarModelCode()) || !mesCustomerCarModelMap.containsKey(pullingOrderInfo.getCarModelCode()) ? "" : mesCustomerCarModelMap.get(pullingOrderInfo.getCarModelCode()).iterator().next().getCarModelName()));

                pullingOrderInfo.setPrintTime(TimeTool.getNowTime(true));
                pullingOrderInfo.setPrintStatus(MesExtEnumUtil.PRINT_STATUS.PRINTED.getValue());
                pullingOrderInfo.setIsPrint(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                pullingOrderInfo.setShiftName(mesWorkOrder.getShiftName());
                pullingOrderInfo.setSummaryQty(mesWorkOrder.getQty());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    pullingOrderInfo.setPlanStartDate(sdf.format(sdf.parse(mesWorkOrder.getPlanStartTime())));
                } catch (ParseException e) {
                    log.error("格式化时间错误e:{}", e.getMessage());
                }
                pullingOrderInfo.setPlanStartTime(mesWorkOrder.getPlanStartTime());
                pullingOrderInfo.setShiftName(mesWorkOrder.getShiftName());
                pullingOrderInfo.setCarModelCode(mesWorkOrder.getCarModelCode());
                pullingOrderInfo.setCustOrderNo(mesWorkOrder.getCustOrderNo());
                pullingOrderInfo.setSystemSyncStatus(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
                ConvertBean.serviceModelUpdate(pullingOrderInfo, bean.getModifyUser());

                //10-12 打印补打拉动单新增log表
                MesPrintedSnLog snLog = new MesPrintedSnLog();
                snLog.setBarcode(pullingOrderInfo.getPullingOrderNo());
                snLog.setCustPartNo(pullingOrderInfo.getCustPartNo());
                snLog.setWorkOrderNo(pullingOrderInfo.getWorkOrderNo());
                snLog.setPartNo(pullingOrderInfo.getPartNo());
                snLog.setPartName(pullingOrderInfo.getPartName());
                snLog.setSystemSyncStatus(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
                ConvertBean.serviceModelInitialize(snLog, bean.getModifyUser());
                snLog.setOrganizeCode(bean.getOrganizeCode());
                snLogList.add(snLog);

            }
            mesPullingOrderInfoRepository.saveAll(pullingOrderInfos);
            //保存打印条码记录
            snLogRao.saveAll(snLogList);
        }
        return pullingOrderInfos;
    }

    @Override
    public List<MesPullingOrderInfo> doMesPullingOrderInfoPrintNew(MesPullingOrderInfo bean) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());

        List<String> pullCodeList = Arrays.asList(bean.getPullCode().split(MesPcnExtConstWords.COMMA));
        List<String> workCenterCodeList = Arrays.asList(bean.getWorkCenterCode().split(MesPcnExtConstWords.COMMA));

        if (pullCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(pullCodeList.get(0), MesPcnExtConstWords.PULL_CODE, packBean);
        else DdlPreparedPack.getInPackList(pullCodeList, MesPcnExtConstWords.PULL_CODE, packBean);

        if (workCenterCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(workCenterCodeList.get(0), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        else DdlPreparedPack.getInPackList(workCenterCodeList, MesPcnExtConstWords.WORK_CENTER_CODE, packBean);

        DdlPreparedPack.getNumEqualPack(bean.getPullOrderType(),"pullOrderType",packBean);
        DdlPreparedPack.getStringEqualPack(bean.getPullingOrderNo(), MesPcnExtConstWords.PULLING_ORDER_NO, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PRINT_STATUS.UNPRINT.getValue(), MesPcnExtConstWords.PRINT_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.CUST_ORDER_NO}, packBean);

        List<MesPullingOrderInfo> pullingOrderInfos = mesPullingOrderInfoRepository.findByHqlTopWhere(packBean, MesPcnExtConstWords.THREE);

        log.info("打印队列查询 --- 拉动单 --- 查询到打印数据: {} ---", CollectionUtils.isEmpty(pullingOrderInfos) ? "[]" :
                pullingOrderInfos.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkOrderNo()))).map(MesPullingOrderInfo::getWorkOrderNo).collect(Collectors.toList()).toString());

        if (!CollectionUtils.isEmpty(pullingOrderInfos)) {
            //根据车型代码查询车型信息
            Map<String, List<MesCustomerCarModel>> mesCustomerCarModelMap = getMesCustomerCarModelMap(bean.getOrganizeCode(), pullingOrderInfos.stream().map(MesPullingOrderInfo::getCarModelCode).distinct().collect(Collectors.toList()));
            //把查出来的拉动单打印并修改打印状态为已打印
            List<MesPrintedSnLog> snLogList = new ArrayList<>();
            for (MesPullingOrderInfo pullingOrderInfo : pullingOrderInfos) {
                //查询拉动组明细
                DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(pullingOrderInfo.getOrganizeCode());
                DdlPreparedPack.getStringEqualPack(pullingOrderInfo.getPullingOrderNo(), MesPcnExtConstWords.PULLING_ORDER_NO, partPackBean);
                List<MesPullingOrderPartInfo> pullingOrderPartInfos = mesPullingOrderPartInfoRepository.findByHqlWhere(partPackBean);
                if (!CollectionUtils.isEmpty(pullingOrderPartInfos)) {
                    pullingOrderInfo.setPartCount(pullingOrderPartInfos.size());
                    pullingOrderPartInfos.forEach(item->{
                        String[] locationArr = StringUtils.isEmpty(item.getLocation()) ? null : item.getLocation().split(MesPcnExtConstWords.COMMA);
                        item.setLocatAddr((null != locationArr && locationArr.length >= 1 ? locationArr[0] : MesPcnExtConstWords.ZERO_STR));
                        item.setLightAddr((null != locationArr && locationArr.length >= 2 ? locationArr[1] : MesPcnExtConstWords.EMPTY));
                    });
                    pullingOrderPartInfos = pullingOrderPartInfos.stream().filter(o -> null != o).sorted(Comparator.comparing(MesPullingOrderPartInfo::getLocatAddr)).collect(Collectors.toList());
                    pullingOrderInfo.setPullingOrderPartInfos(pullingOrderPartInfos);
                }
                pullingOrderInfo.setWorkOrderNoLast(!StringUtil.isEmpty(pullingOrderInfo.getWorkOrderNo())?pullingOrderInfo.getWorkOrderNo().substring(pullingOrderInfo.getWorkOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setWorkOrderNoPre(!StringUtil.isEmpty(pullingOrderInfo.getWorkOrderNo())?pullingOrderInfo.getWorkOrderNo().substring(MesPcnExtConstWords.ZERO,pullingOrderInfo.getWorkOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setCustOrderNoLast(!StringUtil.isEmpty(pullingOrderInfo.getCustOrderNo())?pullingOrderInfo.getCustOrderNo().substring(pullingOrderInfo.getCustOrderNo().length() - MesPcnExtConstWords.FOUR):"");
                pullingOrderInfo.setCarModelName("车型：" + (Objects.isNull(mesCustomerCarModelMap) || StringUtil.isEmpty(pullingOrderInfo.getCarModelCode()) || !mesCustomerCarModelMap.containsKey(pullingOrderInfo.getCarModelCode()) ? "" : mesCustomerCarModelMap.get(pullingOrderInfo.getCarModelCode()).iterator().next().getCarModelName()));

                pullingOrderInfo.setPrintTime(TimeTool.getNowTime(true));
                pullingOrderInfo.setPrintStatus(MesExtEnumUtil.PRINT_STATUS.PRINTED.getValue());
                pullingOrderInfo.setIsPrint(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                ConvertBean.serviceModelUpdate(pullingOrderInfo, bean.getModifyUser());

                //10-12 打印补打拉动单新增log表
                MesPrintedSnLog snLog = new MesPrintedSnLog();
                snLog.setBarcode(pullingOrderInfo.getPullingOrderNo());
                snLog.setCustPartNo(pullingOrderInfo.getCustPartNo());
                snLog.setWorkOrderNo(pullingOrderInfo.getWorkOrderNo());
                snLog.setPartNo(pullingOrderInfo.getPartNo());
                snLog.setPartName(pullingOrderInfo.getPartName());
                ConvertBean.serviceModelInitialize(snLog, bean.getModifyUser());
                snLog.setOrganizeCode(bean.getOrganizeCode());
                snLogList.add(snLog);

            }
            mesPullingOrderInfoRepository.saveAll(pullingOrderInfos);
            //保存打印条码记录
            snLogRao.saveAll(snLogList);
        }
        return pullingOrderInfos;
    }

    @Override
    public ListPager queryMesPullingOrderPartInfoByPager(MesPullingOrderInfo bean, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        if (!StringUtil.isEmpty(bean.getPullCode())) {
            DdlPreparedPack.getInPackList(Arrays.asList(bean.getPullCode().split(",")), "pullCode", packBean);
        }
        //查询拉动单主表是JIS拉动且拉动状态为10
        String append = packBean.getWhereAppend() +
                " AND EXISTS (select 1 from MesPullingOrderInfo po where po.organizeCode = '"+ bean.getOrganizeCode() +"' and po.pullingOrderNo = model.pullingOrderNo" +
                " and po.pullOrderType = 20 and po.pullOrderStatus != 50 and po.workCenterCode = '" + bean.getWorkCenterCode() + "' ";
        packBean.setWhereAppend(append + " )");
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"pullingOrderNo"}, packBean);

        pager = PagerHelper.getPager(pager, mesPullingOrderPartInfoRepository.findByHqlWhereCount(packBean));

        List<MesPullingOrderPartInfo> resultList = mesPullingOrderPartInfoRepository.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(resultList)) {

            Map<String, List<MesPartPullDetail>> pullCodePartMap = null;
            List<MesPullingOrderInfo> pullingOrderInfoList = null;
            Map<String, List<MesPullingOrderInfo>> pullingOrderInfoMap = null;
            Map<String, List<MesCustomerCarModel>> customerCarModelMap = null;

            //查拉动单明细对应的拉动组规则
            List list = (resultList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getPullCode()))).map(MesPullingOrderPartInfo::getPullCode).collect(Collectors.toList())).stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(list)) {
                DdlPackBean packBeanDetail = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
                DdlPreparedPack.getInPackList(list, "pullCode", packBeanDetail);
                List<MesPartPullDetail> mesPartPullDetails = mesPartPullDetailRDao.findByHqlWhere(packBeanDetail);
                pullCodePartMap = CollectionUtils.isEmpty(mesPartPullDetails) ? null :
                        mesPartPullDetails.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesPcnExtConstWords.AND).add(o.getPullCode()).add(o.getPartNo()).toString()));
            }

            list = resultList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getPullingOrderNo()))).map(MesPullingOrderPartInfo::getPullingOrderNo).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(list)) {
                packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
                DdlPreparedPack.getInPackList(list, "pullingOrderNo", packBean);
                pullingOrderInfoList = mesPullingOrderInfoRepository.findByHqlWhere(packBean);
                pullingOrderInfoMap = CollectionUtils.isEmpty(pullingOrderInfoList) ? null :
                        pullingOrderInfoList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesPullingOrderInfo::getPullingOrderNo));
            }

            if (!CollectionUtils.isEmpty(pullingOrderInfoList)) {
                list = (pullingOrderInfoList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCarModelCode()))).map(MesPullingOrderInfo::getCarModelCode).collect(Collectors.toList())).stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(list)) {
                    packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
                    DdlPreparedPack.getInPackList(list, MesPcnExtConstWords.CAR_MODEL_CODE, packBean);
                    List<MesCustomerCarModel> customerCarModelList = carModelRepository.findByHqlWhere(packBean);
                    customerCarModelMap = CollectionUtils.isEmpty(customerCarModelList) ? null :
                            customerCarModelList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesCustomerCarModel::getCarModelCode));
                }
            }

            String key;
            for (MesPullingOrderPartInfo partInfo : resultList) {
                key = new StringJoiner(MesPcnExtConstWords.AND).add(partInfo.getPullCode()).add(partInfo.getPartNo()).toString();
                if (!CollectionUtils.isEmpty(pullCodePartMap) && pullCodePartMap.containsKey(key)) {
                    MesPartPullDetail mesPartPullDetail = pullCodePartMap.get(key).get(0);
                    partInfo.setScanValidationType(StringUtil.isEmpty(mesPartPullDetail.getScanValidationType()) ? 10 : mesPartPullDetail.getScanValidationType());
                    partInfo.setRuleValidation(StringUtil.isEmpty(mesPartPullDetail.getRuleValidation()) ? MesPcnExtConstWords.EMPTY : mesPartPullDetail.getRuleValidation());
                    if (Objects.isNull(mesPartPullDetail.getScanValidationType()) || mesPartPullDetail.getScanValidationType().compareTo(MesExtEnumUtil.PART_PULL_DETAIL_SCAN_VALIDATION_TYPE.UN_VALIDATE.getValue()) == 0) {
                        partInfo.setColor(MesExtEnumUtil.PART_PULL_DETAIL_COLOR.WHITE.getCode());
                    } else if (partInfo.getColor() == MesExtEnumUtil.PART_PULL_DETAIL_COLOR.WHITE.getCode()) {
                        partInfo.setColor(MesExtEnumUtil.PART_PULL_DETAIL_COLOR.RED.getCode());
                    }
                }
                if (!CollectionUtils.isEmpty(pullingOrderInfoMap) && pullingOrderInfoMap.containsKey(partInfo.getPullingOrderNo())) {
                    partInfo.setCarModelCode(pullingOrderInfoMap.get(partInfo.getPullingOrderNo()).get(0).getCarModelCode());
                }
                if (!StringUtils.isEmpty(partInfo.getCarModelCode()) && !CollectionUtils.isEmpty(customerCarModelMap) && customerCarModelMap.containsKey(partInfo.getCarModelCode())) {
                    partInfo.setCarModelName(customerCarModelMap.get(partInfo.getCarModelCode()).get(0).getCarModelName());
                }
            }

        }
        return new ListPager<>(resultList, pager);
    }

    @Override
    public MesPullingOrderPartInfo doMesPullingOrderInfoJisScan(MesPullingOrderPartInfo mesPullingOrderInfo, String userName) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(mesPullingOrderInfo.getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(mesPullingOrderInfo.getId(), MesPcnExtConstWords.ID, packBean);
        MesPullingOrderPartInfo partInfo = mesPullingOrderPartInfoRepository.getByProperty(packBean);
        if (!StringUtil.isEmpty(partInfo)) {
            //查拉动单明细对应的拉动组规则
            DdlPackBean packBeanDetail = DdlPackBean.getDdlPackBean(partInfo.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(partInfo.getPullCode(), "pullCode", packBeanDetail);
            DdlPreparedPack.getStringEqualPack(partInfo.getPartNo(), "partNo", packBeanDetail);
            MesPartPullDetail mesPartPullDetail = mesPartPullDetailRDao.getByProperty(packBeanDetail);
            if (StringUtil.isEmpty(mesPartPullDetail)) {
                MesPcnException.throwMesBusiException("工单【%s】拉动组数据为空", mesPullingOrderInfo.getWorkOrderNo());
            }
            //跳过码
            if (mesPullingOrderInfo.getProductSn().equals(configService.getCfgValue(partInfo.getOrganizeCode(), MesPcnExtConstWords.JIS_SKIP_CODE))) {
                //修改为已跳过
                partInfo.setStatus(MesExtEnumUtil.PULL_ORDER_PART_STATUS.SKIP.getValue());
                return updatePullingOrderPartInfo(userName, partInfo);
            }
            //校验规则
            if (checkRule(mesPullingOrderInfo, mesPartPullDetail)) {
                //校验成功修改颜色为黄色2
                partInfo.setColor(MesExtEnumUtil.PART_PULL_DETAIL_COLOR.YELLOW.getCode());
                ConvertBean.serviceModelUpdate(partInfo, userName);
                mesPullingOrderPartInfoRepository.update(partInfo);
            } else {
                MesPcnException.throwMesBusiException("工单拉动组明细【%s】校验规则失败", mesPullingOrderInfo.getWorkOrderNo());
            }
        } else {
            MesPcnException.throwMesBusiException("工单拉动组明细【%s】数据为空", mesPullingOrderInfo.getWorkOrderNo());
        }
        return partInfo;
    }

    @Override
    public MesPullingOrderInfo doSPSForEquipment100() {
        String organizeCode = "";
        String assemblyLine = "";

        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(MesPcnExtConstWords.WU_HAN_SPS_CFG_CODE,"cfgCode",packBean);
        List<MesConfig> configList = configRao.findByHqlWhere(packBean);
        for (MesConfig mesConfig : configList) {
            if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_WORK_CENTER_CODE_100)){
                assemblyLine = mesConfig.getCfgValue();
            }else if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_ORGANIZE_CODE)){
                organizeCode = mesConfig.getCfgValue();
            }
        }
        if (organizeCode.isEmpty() || assemblyLine.isEmpty()) {
            log.error("没有配置表mes_config,cfg_code为WU_HAN_SPS_CODE,key为WU_HAN_WORK_CENTER_CODE_100 或者 WU_HAN_ORGANIZE_CODE没有配置");
            return null;
        }
        return getPullingOrder(organizeCode, assemblyLine);

    }

    @Override
    public String doSPSForEquipmentString100() {
        String organizeCode = "";
        String assemblyLine = "";
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(MesPcnExtConstWords.WU_HAN_SPS_CFG_CODE,"cfgCode",packBean);
        List<MesConfig> configList = configRao.findByHqlWhere(packBean);
        for (MesConfig mesConfig : configList) {
            if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_WORK_CENTER_CODE_100)){
                assemblyLine = mesConfig.getCfgValue();
            }else if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_ORGANIZE_CODE)){
                organizeCode = mesConfig.getCfgValue();
            }
        }
        if (organizeCode.isEmpty() || assemblyLine.isEmpty()) {
            log.error("没有配置表mes_config,cfg_code为WU_HAN_SPS_CODE,key为WU_HAN_WORK_CENTER_CODE_100 或者 WU_HAN_ORGANIZE_CODE没有配置");
            return null;
        }
        MesPullingOrderInfo pullingOrder = getPullingOrder(organizeCode, assemblyLine);
        Map<String, MesSpsInfo> result = new HashMap<>();
        if (pullingOrder != null){
            MesSpsInfo mesSpsInfo = new MesSpsInfo();
            mesSpsInfo.setId(pullingOrder.getId());
            mesSpsInfo.setSeq(pullingOrder.getProductSeq());
            mesSpsInfo.setOrderCode(pullingOrder.getPullingOrderNo());
            mesSpsInfo.setCustOrderCode(pullingOrder.getCustOrderNo());

            List<MesPullingOrderPartInfo> partList = pullingOrder.getPartList();
            List<MesSpsDetailInfo> detailInfoList = new ArrayList<>();
            if (partList !=null && !partList.isEmpty()){
                for (MesPullingOrderPartInfo part : partList) {
                    MesSpsDetailInfo detailInfo = new MesSpsDetailInfo();
                    detailInfo.setPartNo(part.getPartNo());
                    detailInfo.setQty(part.getPullQty());
                    detailInfo.setPartName(part.getPartName());
                    if (part.getLocation().indexOf(",") > 0){
                        String[] locations = part.getLocation().split(",");
                        detailInfo.setSection(locations[0]);
                    }
                    detailInfo.setAddress(part.getLocation());
                    detailInfoList.add(detailInfo);
                }
            }
            mesSpsInfo.setPartList(detailInfoList);
            result.put("info", mesSpsInfo);
        }
        String jsonString = JSON.toJSONString(result);
        return jsonString.replaceAll("id", "Id")
                .replaceAll("orderCode", "OrderCode")
                .replaceAll("custOrderCode", "CustOrderCode")
                .replaceAll("seq", "Seq")
                .replaceAll("partNo", "PartNo")
                .replaceAll("partName", "PartName")
                .replaceAll("section", "Section")
                .replaceAll("address", "Address")
                .replaceAll("qty", "Qty").replaceAll("partList","PartList");
    }

    @Override
    public MesPullingOrderInfo doSPSForEquipment200() {
        String organizeCode = "";
        String assemblyLine = "";
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(MesPcnExtConstWords.WU_HAN_SPS_CFG_CODE,"cfgCode",packBean);
        List<MesConfig> configList = configRao.findByHqlWhere(packBean);
        for (MesConfig mesConfig : configList) {
            if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_WORK_CENTER_CODE_200)){
                assemblyLine = mesConfig.getCfgValue();
            }else if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_ORGANIZE_CODE)){
                organizeCode = mesConfig.getCfgValue();
            }
        }
        if (organizeCode.isEmpty() || assemblyLine.isEmpty()) {
            log.error("没有配置表mes_config,cfg_code为WU_HAN_SPS_CODE,key为WU_HAN_WORK_CENTER_CODE_100 或者 WU_HAN_ORGANIZE_CODE没有配置");
            return null;
        }
        return getPullingOrder(organizeCode, assemblyLine);
    }

    @Override
    public String doSPSForEquipmentString200() {
        String organizeCode = "";
        String assemblyLine = "";
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.getStringEqualPack(MesPcnExtConstWords.WU_HAN_SPS_CFG_CODE,"cfgCode",packBean);
        List<MesConfig> configList = configRao.findByHqlWhere(packBean);
        for (MesConfig mesConfig : configList) {
            if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_WORK_CENTER_CODE_200)){
                assemblyLine = mesConfig.getCfgValue();
            }else if (mesConfig.getCfgKey().equals(MesPcnExtConstWords.WU_HAN_ORGANIZE_CODE)){
                organizeCode = mesConfig.getCfgValue();
            }
        }
        if (organizeCode.isEmpty() || assemblyLine.isEmpty()) {
            log.error("没有配置表mes_config,cfg_code为WU_HAN_SPS_CODE,key为WU_HAN_WORK_CENTER_CODE_100 或者 WU_HAN_ORGANIZE_CODE没有配置");
            return null;
        }
        MesPullingOrderInfo pullingOrder = getPullingOrder(organizeCode, assemblyLine);
        Map<String, MesSpsInfo> result = new HashMap<>();
        if (pullingOrder != null){
            MesSpsInfo mesSpsInfo = new MesSpsInfo();
            mesSpsInfo.setId(pullingOrder.getId());
            mesSpsInfo.setSeq(pullingOrder.getProductSeq());
            mesSpsInfo.setOrderCode(pullingOrder.getPullingOrderNo());
            mesSpsInfo.setCustOrderCode(pullingOrder.getCustOrderNo());

            List<MesPullingOrderPartInfo> partList = pullingOrder.getPartList();
            List<MesSpsDetailInfo> detailInfoList = new ArrayList<>();
            if (partList !=null && !partList.isEmpty()){
                for (MesPullingOrderPartInfo part : partList) {
                    MesSpsDetailInfo detailInfo = new MesSpsDetailInfo();
                    detailInfo.setPartNo(part.getPartNo());
                    detailInfo.setQty(part.getPullQty());
                    detailInfo.setPartName(part.getPartName());
                    if (part.getLocation().indexOf(",") > 0){
                        String[] locations = part.getLocation().split(",");
                        detailInfo.setSection(locations[0]);
                    }
                    detailInfo.setAddress(part.getLocation());
                    detailInfoList.add(detailInfo);
                }
            }
            mesSpsInfo.setPartList(detailInfoList);
            result.put("info", mesSpsInfo);
        }
        String jsonString = JSON.toJSONString(result);
        return jsonString.replaceAll("id", "Id")
                .replaceAll("orderCode", "OrderCode")
                .replaceAll("custOrderCode", "CustOrderCode")
                .replaceAll("seq", "Seq")
                .replaceAll("partNo", "PartNo")
                .replaceAll("partName", "PartName")
                .replaceAll("section", "Section")
                .replaceAll("address", "Address")
                .replaceAll("qty", "Qty").replaceAll("partList","PartList");
    }

//    @Override
//    public Map<String, Object> doSPSForEquipment() {
//        return Collections.emptyMap();
//    }

    private boolean checkRule(MesPullingOrderPartInfo mesPullingOrderInfo, MesPartPullDetail mesPartPullDetail) {
        String rule = mesPartPullDetail.getRuleValidation();
        if (mesPullingOrderInfo.getScanValidationType() == MesExtEnumUtil.PART_PULL_DETAIL_SCAN_VALIDATION_TYPE.HOMEMADE_VALIDATE.getValue()) {
            List<MesProduceSn> produceSnList = mesProduceSnExtService.getProduceSnList(mesPullingOrderInfo.getOrganizeCode(), mesPullingOrderInfo.getProductSn());
            if (CollectionUtils.isEmpty(produceSnList)) {
                return false;
            }
            return mesPartPullDetail.getPullPartNo().equals(produceSnList.get(0).getPartNo());
        } else if (mesPullingOrderInfo.getScanValidationType() == MesExtEnumUtil.PART_PULL_DETAIL_SCAN_VALIDATION_TYPE.COOPERATION_VALIDATE.getValue()) {
            return (Boolean) ((IMesNumberRuleMatchDispatchService) SpringContextsUtil.getBean(MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.MATCH_TYPE_20.getStrategyClass()))
                    .matchNumberRule(mesPullingOrderInfo.getOrganizeCode(), mesPullingOrderInfo.getProductSn(), rule, rule)
                    .get(MesPcnExtConstWords.RESULT);
        } else {
            return true;
        }
    }

    @Override
    public List<MesPullingOrderPartInfo> doMesPullingOrderInfoSend(List<MesPullingOrderPartInfo> infoList, String userName) {
        List<MesPullingOrderPartInfo> result = new ArrayList<>();
        for (MesPullingOrderPartInfo mesPullingOrderPartInfo : infoList) {
            //查询拉动组明细
//            DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(mesPullingOrderPartInfo.getOrganizeCode());
//            DdlPreparedPack.getStringEqualPack(mesPullingOrderPartInfo.getPullingOrderNo(), "pullingOrderNo", partPackBean);
//            DdlPreparedPack.getStringEqualPack(mesPullingOrderPartInfo.getPartNo(), "partNo", partPackBean);
            DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(mesPullingOrderPartInfo.getOrganizeCode());
            DdlPreparedPack.getNumEqualPack(mesPullingOrderPartInfo.getId(),"id",partPackBean);
            MesPullingOrderPartInfo partInfo = mesPullingOrderPartInfoRepository.getByProperty(partPackBean);
            if (!StringUtil.isEmpty(partInfo)) {
                //修改为已送料
                partInfo.setStatus(MesExtEnumUtil.PULL_ORDER_PART_STATUS.SENTED.getValue());
                result.add(updatePullingOrderPartInfo(userName, partInfo));
            }
        }
        return result;
    }

    private MesPullingOrderPartInfo updatePullingOrderPartInfo(String userName, MesPullingOrderPartInfo partInfo) {
        partInfo.setColor(MesExtEnumUtil.PART_PULL_DETAIL_COLOR.GREEN.getCode());
        ConvertBean.serviceModelUpdate(partInfo, userName);
        mesPullingOrderPartInfoRepository.update(partInfo);
        //拉动单改为已扫描
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(partInfo.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(partInfo.getPullingOrderNo(), "pullingOrderNo", packBean);

        MesPullingOrderInfo pullingOrderInfo = mesPullingOrderInfoRepository.getByProperty(packBean);
        if (!StringUtil.isEmpty(pullingOrderInfo)) {
            pullingOrderInfo.setPullOrderStatus(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue());
            ConvertBean.serviceModelUpdate(pullingOrderInfo, userName);
            mesPullingOrderInfoRepository.update(pullingOrderInfo);
        }
        return partInfo;
    }



    private MesPullingOrderInfo getPullingOrder(String organizeCode, String assemblyLine){
        //查询拉动单状态为已扫描=20的 拉动单
        DdlPackBean seriesPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(assemblyLine,"workCenterCode", seriesPackBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PART_PULL_ORDER_TYPE.SPS_PULL.getValue(), "pullOrderType", seriesPackBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue(), "pullOrderStatus", seriesPackBean);
        seriesPackBean.setWhereAppend(seriesPackBean.getWhereAppend() + "  order by modifyDatetime desc");

        MesPullingOrderInfo mesPullingOrder = mesPullingOrderInfoRepository.getByProperty(seriesPackBean);
        log.info("castle-1-查询条件为:workCenterCode:{},pullOrderType:{},pullOrderStatus:{}查询出来的数据为:{}",assemblyLine,MesExtEnumUtil.PART_PULL_ORDER_TYPE.SPS_PULL.getValue(),MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_20.getValue(),JSON.toJSONString(mesPullingOrder));
       if (StringUtil.isEmpty(mesPullingOrder)) {
            return null;
        }
        //更新主表状态 源系统为25配料完成  现在-【已发送设备-30】
        DdlPackBean pullingOrderPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", pullingOrderPackBean);
        DdlPreparedPack.getNumEqualPack(mesPullingOrder.getId(),"id", pullingOrderPackBean);
        mesPullingOrderInfoRepository.updateByProperties(new String[]{"pullOrderStatus","modifyDatetime"},new Object[]{MesExtEnumUtil.PULL_ORDER_STATUS.PULL_ORDER_STATUS_30.getValue(),TimeTool.getNowTime(true)},pullingOrderPackBean);
        //查询拉动组明细
        DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(mesPullingOrder.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesPullingOrder.getPullingOrderNo(), "pullingOrderNo", partPackBean);
        List<MesPullingOrderPartInfo> pullingOrderPartInfos = mesPullingOrderPartInfoRepository.findByHqlWhere(partPackBean);
        if (!org.apache.shiro.util.CollectionUtils.isEmpty(pullingOrderPartInfos)) {
            mesPullingOrder.setPartList(pullingOrderPartInfos);
        }
        log.info("castle-2-查询出来的数据为:{}",JSON.toJSONString(mesPullingOrder));

        return mesPullingOrder;
    }

    public Map<String, List<MesCustomerCarModel>> getMesCustomerCarModelMap(String organizeCode, List<String> carModelCodeList) {
        if (StringUtil.isEmpty(organizeCode) || CollectionUtils.isEmpty(carModelCodeList)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (carModelCodeList.size() == MesPcnExtConstWords.ONE) {
            DdlPreparedPack.getStringEqualPack(carModelCodeList.get(MesPcnExtConstWords.ZERO), MesPcnExtConstWords.CAR_MODEL_CODE, packBean);
        } else {
            DdlPreparedPack.getInPackList(carModelCodeList, MesPcnExtConstWords.CAR_MODEL_CODE, packBean);
        }
        List<MesCustomerCarModel> customerCarModelList = carModelRepository.findByHqlWhere(packBean);
        return CollectionUtils.isEmpty(customerCarModelList) ? null : customerCarModelList.stream().collect(Collectors.groupingBy(MesCustomerCarModel::getCarModelCode));
    }
}
