# PiLotReqServiceImpl.java

    package com.mycim.server.pilot.service;

    import com.alipay.sofa.runtime.api.annotation.SofaService;
    import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.framework.utils.lang.ObjectUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.pilot.manager.PiLotCheckManager;
    import com.mycim.server.pilot.manager.PiLotReqManager;
    import com.mycim.server.wip.manager.PiLotInqManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.PiLotStatusEnum;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.pilot.PiLotRcpGrp;
    import com.mycim.valueobject.ems.pilot.PiLotRcpMaping;
    import com.mycim.valueobject.ems.pilot.PiLotSetup;
    import com.mycim.valueobject.ems.pilot.PiLotView;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * @author songpy
     * @version 1.0.0
     * @date 2021/8/26
     **/
    @Service
    @SofaService(interfaceType = PiLotReqService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    public class PiLotReqServiceImpl implements PiLotReqService {
        @Autowired
        private PiLotInqManager piLotInqManager;

        @Autowired
        private PiLotReqManager piLotReqManager;

        @Autowired
        PiLotCheckManager piLotCheckManager;

        @Autowired
        private TransactionLogManager transactionLogManager;

        @Override
        public void addPiLotSetup(PiLotSetup piLotSetup) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.CREATE_KEY);
            piLotReqManager.addPiLotSetup(piLotSetup, transactionLog);
        }

        @Override
        public void updatePiLotSetup(PiLotSetup piLotSetup) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.MODIFY_KEY);

            PiLotSetup oldSetup = piLotInqManager.getPiLotSetupByRrn(piLotSetup.getPiLotRrn());

            //判断是否变更Lot
            Boolean needUpdateLot = !StringUtils.equalsIgnoreCase(piLotSetup.getBoundLotId(), oldSetup.getBoundLotId()) &&
                    StringUtils.isNotEmpty(piLotSetup.getBoundLotId());
            if (needUpdateLot && piLotSetup.getBoundViewRrn() > 0L) {
                PiLotView view = piLotInqManager.getPiLotViewByRrn(piLotSetup.getBoundViewRrn());
                int count = view.getRetryCount() - 1;
                BeanUtils.copyProperties(piLotSetup, view);
                transactionLog.setTransId(TransactionNames.PI_LOT_ASSIGN_LOT);
                // 子母批模式 assign lot
                if (StringUtils.equalsIgnoreCase(piLotSetup.getSingleLot(), PiLotStatusEnum.NO.toString())) {
                    view.setStatus(PiLotStatusEnum.WAITSPLIT.toString());
                    piLotSetup.setStatus(PiLotStatusEnum.WAITSPLIT.toString());
                }
                // 单批模式 assign lot 和retry
                if (StringUtils.equalsIgnoreCase(piLotSetup.getSingleLot(), PiLotStatusEnum.YES.toString())) {
                    view.setStatus(PiLotStatusEnum.WAITCOMPLETE.toString());
                    piLotSetup.setStatus(PiLotStatusEnum.WAITCOMPLETE.toString());
                    //设置指定站点出站  hold信息   #42577
                    piLotReqManager.setSingleLotFutureHold(view, oldSetup);
                    if (StringUtils.isNotEmpty(oldSetup.getBoundLotId())) {
                        Assert.state(count >= 0, Errors.create().key(MessageIdList.PILOT_RETRY_COUNT)
                                                       .content("Retry exceeds the set number of times").build());
                        view.setRetryCount(count);
                        transactionLog.setTransId(TransactionNames.PI_LOT_RETRY);
                    }
                }
                piLotReqManager.savePiLotView(view, transactionLog);
                piLotReqManager.createPilotFlowLotTransHistory(oldSetup, view);
            }
            piLotReqManager.updatePiLotSetup(piLotSetup, transactionLog);
        }

        @Override
        public void deletePiLotSetup(Long piLotSetupRrn) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.DELETE_KEY);
            piLotReqManager.deletePiLotSetup(piLotSetupRrn, transactionLog);
        }

        @Override
        public void savePiLotView(PiLotView piLotView) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.MODIFY_KEY);
            piLotReqManager.savePiLotView(piLotView, transactionLog);
        }

        @Override
        public void retryPilotView(PiLotView piLotView) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.PI_LOT_RETRY);
            PiLotView oldView = piLotInqManager.getPiLotViewByRrn(piLotView.getViewRrn());
            piLotReqManager.savePiLotView(piLotView, transactionLog);
            piLotView.setChildLotRrn(oldView.getChildLotRrn());
            piLotReqManager.createPilotFlowLotTransHistory(null, piLotView);
        }

        @Override
        public void closePiLotViewAndSetup(PiLotView piLotView) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.EVENT_STATUS_CLOSE);
            piLotView.setStatus(PiLotStatusEnum.CLOSE.toString());
            if (StringUtils.equalsIgnoreCase(piLotView.getSingleLot(), PiLotStatusEnum.YES.toString())) {
                piLotReqManager.deletePilotSingleLotFutureHold(piLotView);
            }
            piLotReqManager.savePiLotView(piLotView, transactionLog);

            PiLotSetup setup = piLotInqManager.getPiLotSetupByRrn(piLotView.getPiLotRrn());
            setup.setStatus(PiLotStatusEnum.CREATED.toString());
            piLotReqManager.updatePiLotSetup(setup, transactionLog);
            piLotReqManager.createPilotFlowLotTransHistory(null, piLotView);
        }

        @Override
        public void updateRcpGrp(PiLotRcpGrp piLotRcpGrp) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.MODIFY_KEY);
            piLotReqManager.updateRcpGrp(piLotRcpGrp, transactionLog);
        }

        @Override
        public void addRcpGrp(PiLotRcpGrp piLotRcpGrp) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.CREATE_KEY);
            piLotReqManager.addRcpGrp(piLotRcpGrp, transactionLog);
        }

        @Override
        public void addRcpToGrp(PiLotRcpMaping piLotRcpMaping) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.CREATE_KEY);
            piLotReqManager.addRcpToGrp(piLotRcpMaping, transactionLog);
        }

        @Override
        public void deleteRcp(PiLotRcpMaping piLotRcpMaping) {
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(LocalContext.getUserId(),
                                                                                      TransactionNames.DELETE_KEY);
            piLotReqManager.deleteRcpFromGrp(piLotRcpMaping, transactionLog);
        }

        @Override
        public void handPilotByTime() {
            piLotReqManager.handPilotByTime();
        }

    }