# EquipmentConstrainPairManagerImpl.java

    package com.mycim.server.constrain.manager.impl;

    import com.mycim.framework.oid.IDGenerators;
    import com.mycim.framework.oid.type.IDNames;
    import com.mycim.framework.oid.type.SequenceNames;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.constrain.dao.EquipmentConstrainPairDAO;
    import com.mycim.server.constrain.manager.EquipmentConstrainPairManager;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.EquipmentConstrainPairConstants;
    import com.mycim.valueobject.ems.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Date;
    import java.util.List;
    import java.util.Map;

    /**
     * @author shijie.deng
     * @version 6.0.0
     * @date 2019/9/9
     **/
    @Service
    @Transactional
    public class EquipmentConstrainPairManagerImpl implements EquipmentConstrainPairManager {

        @Autowired
        private EquipmentConstrainPairDAO equipmentConstrainPairDAO;

        @Autowired
        private TransactionLogManager transactionLogManager;

        @Override
        public EquipmentConstrainPair getConstrainPairByProductAndProcess(long productRrn, long processRrn) {
            return equipmentConstrainPairDAO.getConstrainPairByProductAndProcess(productRrn, processRrn);
        }

        @Override
        public List<EquipmentConstrainPairDetail> getConstrainPairDetailsByConstrainRrn(long constrainRrn) {
            return equipmentConstrainPairDAO.getConstrainPairDetailsByConstrainRrn(constrainRrn);
        }

        @Override
        public Long saveConstrainPair(Map<String, Object> transInfo) {

            Long processRrn = MapUtils.getLong(transInfo, "processRrn");
            String processId = MapUtils.getString(transInfo, "processId");
            Long productRrn = MapUtils.getLong(transInfo, "productRrn");
            String productId = MapUtils.getString(transInfo, "productId");
            Long lotRrn = MapUtils.getLong(transInfo, "lotRrn");
            String lotId = MapUtils.getString(transInfo, "lotId");
            Long constrainRrn = MapUtils.getLong(transInfo, "constrainRrn");
            Long facilityRrn = MapUtils.getLong(transInfo, "facilityRrn");
            String user = MapUtils.getString(transInfo, "user");
            String type = MapUtils.getString(transInfo, "type");
            String remark = MapUtils.getString(transInfo, "remark");
            Date now = new Date();
            String tansId = EquipmentConstrainPairConstants.TRANSID_UPDATE;
            if (constrainRrn == null || constrainRrn == 0) {
                tansId = EquipmentConstrainPairConstants.TRANSID_CREATE;
            }
            // 插入本次事务历史
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(user, tansId);
            long transRrn = transactionLog.getTransRrn();// 事务rrn
            List<Map<String, Object>> changeRecords = (List<Map<String, Object>>) transInfo.get("changeRecords");
            if (constrainRrn == null || constrainRrn == 0) {
                // 新增
                // 1.新增机限母表记录
                EquipmentConstrainPair constrain = new EquipmentConstrainPair();
                constrain.setCreateBy(user);
                constrain.setCreateTime(now);
                constrain.setFacilityRrn(facilityRrn);
                constrain.setLotRrn(lotRrn);
                constrain.setModifyBy(user);
                constrain.setModifyTime(now);
                constrain.setProcessRrn(processRrn);
                constrain.setProductRrn(productRrn);
                constrain.setRemark(remark);
                constrain.setStatus(EquipmentConstrainPairConstants.STATUS_ENABLE);
                constrain.setType(type);
                constrain.setVersion("1");
                constrainRrn = equipmentConstrainPairDAO.insertConstrainPair(constrain);
            } else {
                // 修改设置 目前只修改版本号
                EquipmentConstrainPair constrainPair = equipmentConstrainPairDAO.getConstrainPairByRrn(constrainRrn);
                String version = constrainPair.getVersion();
                Integer newVersion = Integer.valueOf(version) + 1;
                constrainPair.setVersion(newVersion.toString());
                equipmentConstrainPairDAO.updateConstrainPair(constrainPair);
            }
            // 2.插入机限历史
            EquipmentConstrainPairHistory constrainHistory = new EquipmentConstrainPairHistory();
            constrainHistory.setConstrainRrn(constrainRrn);
            constrainHistory.setCreateBy(user);
            constrainHistory.setCreateTime(now);
            constrainHistory.setFacilityRrn(facilityRrn);
            constrainHistory.setLotId(lotId);
            constrainHistory.setProcessId(processId);
            constrainHistory.setProductId(productId);
            constrainHistory.setRemark(remark);
            constrainHistory.setTransRrn(transRrn);
            equipmentConstrainPairDAO.insertConstrainPairH(constrainHistory);
            // 3.修改或增加详细记录及其历史信息
            for (Map<String, Object> detailMap : changeRecords) {

                Long detailRrn = MapUtils.getLong(detailMap, "detailRrn");// 详情RRN
                Long operationRrn = MapUtils.getLong(detailMap, "operationRrn");
                Long routeRrn = MapUtils.getLong(detailMap, "routeRrn");
                String operationRouteSeq = MapUtils.getString(detailMap, "operationRouteSeq");
                String routeId = MapUtils.getString(detailMap, "routeId");
                String isBenchmark = MapUtils.getString(detailMap, "isBenchmark");
                String layerId = MapUtils.getString(detailMap, "layerId");
                String operationId = MapUtils.getString(detailMap, "operationId");
                String routeProcessSeq = MapUtils.getString(detailMap, "routeProcessSeq");
                String reticlefamilyId = MapUtils.getString(detailMap, "reticlefamilyId");
                String planA = MapUtils.getString(detailMap, "PlanA");
                String planB = MapUtils.getString(detailMap, "PlanB");
                String planC = MapUtils.getString(detailMap, "PlanC");
                String planD = MapUtils.getString(detailMap, "PlanD");
                String planE = MapUtils.getString(detailMap, "PlanE");
                // 如果修改的时候存在修改基准层,那么把之前的基准层拿出来设为false 注:此代码位置必须在详情更新之前
                if (new Boolean(isBenchmark)) {
                    EquipmentConstrainPairDetail benchMark = equipmentConstrainPairDAO
                            .getBenchMarkByConstrainRrn(constrainRrn);
                    if (benchMark != null) {
                        benchMark.setIsBenchmark("false");
                        equipmentConstrainPairDAO.updateConstrainPairDetail(benchMark);
                    }
                }
                // 更新或新增
                EquipmentConstrainPairDetail detail = new EquipmentConstrainPairDetail();
                detail.setConstrainRrn(constrainRrn);
                detail.setIsBenchmark(isBenchmark);
                detail.setLayerId(layerId);
                detail.setOperationId(operationId);
                detail.setOperationRrn(operationRrn);
                detail.setOperationSeq(operationRouteSeq);
                detail.setPlanA(planA);
                detail.setPlanB(planB);
                detail.setPlanC(planC);
                detail.setPlanD(planD);
                detail.setPlanE(planE);
                detail.setProcessRrn(processRrn);
                detail.setProductRrn(productRrn);
                detail.setReticlefamilyId(reticlefamilyId);
                detail.setRouteId(routeId);
                detail.setRouteRrn(routeRrn);
                detail.setRouteSeq(routeProcessSeq);
                if (detailRrn == null || detailRrn == 0L) {
                    detailRrn = equipmentConstrainPairDAO.insertConstrainPairDetail(detail);// 工步机限配置信息
                } else {
                    detail.setConstrainDetailRrn(detailRrn);
                    equipmentConstrainPairDAO.updateConstrainPairDetail(detail);
                }
                StringBuilder operationDetailSB = new StringBuilder("");
                if (StringUtils.isNotBlank(planA)) {
                    operationDetailSB.append("PlanA:" + planA + "|");
                }
                if (StringUtils.isNotBlank(planB)) {
                    operationDetailSB.append("PlanB:" + planB + "|");
                }
                if (StringUtils.isNotBlank(planC)) {
                    operationDetailSB.append("PlanC:" + planC + "|");
                }
                if (StringUtils.isNotBlank(planD)) {
                    operationDetailSB.append("PlanD:" + planD + "|");
                }
                if (StringUtils.isNotBlank(planE)) {
                    operationDetailSB.append("PlanE:" + planE + "|");
                }

                EquipmentConstrainPairDetailH detailhistory = new EquipmentConstrainPairDetailH();
                detailhistory.setConstrainRrn(constrainRrn);
                detailhistory.setConstrainDetailRrn(detailRrn);
                detailhistory.setLotId(lotId);
                detailhistory.setOperationDetail(operationDetailSB.toString());
                detailhistory.setProcessId(processId);
                detailhistory.setProductId(productId);
                detailhistory.setRemark(remark);
                detailhistory.setTransRrn(transRrn);
                // 工步机限配置信息 历史
                equipmentConstrainPairDAO.insertConstrainPairDetailH(detailhistory);

            }
            // 事务完成
            transactionLogManager.markTransactionLog(transactionLog);
            return constrainRrn;

        }

        @Override
        public void changeConstrianStatus(long constrainRrn, String user, String status, long facilityRrn) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(user, EquipmentConstrainPairConstants.TRANSID_UPDATE);
            // 事务rrn
            long transRrn = transactionLog.getTransRrn();
            Date now = new Date();
            EquipmentConstrainPair constrainPair = equipmentConstrainPairDAO.getConstrainPairByRrn(constrainRrn);
            constrainPair.setStatus(status);
            constrainPair.setModifyBy(user);
            constrainPair.setModifyTime(now);
            equipmentConstrainPairDAO.updateConstrainPair(constrainPair);
            // 历史
            EquipmentConstrainPairHistory constrainHistory = new EquipmentConstrainPairHistory();
            constrainHistory.setConstrainRrn(constrainRrn);
            constrainHistory.setCreateBy(user);
            constrainHistory.setFacilityRrn(facilityRrn);
            constrainHistory.setCreateTime(now);
            constrainHistory.setTransRrn(transRrn);
            equipmentConstrainPairDAO.insertConstrainPairH(constrainHistory);
            // 事务完成
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public EquipmentConstrainPair getConstrainPairByLotRrn(long lotRrn) {
            return equipmentConstrainPairDAO.getConstrainPairByLot(lotRrn);
        }

        @Override
        public EquipmentConstrainPairDetail getConstrainPairDetailByRunInfo(long constrainRrn, long routeRrn,
                                                                            long operationRrn, String routeSeq,
                                                                            String operationSeq) {
            return equipmentConstrainPairDAO
                    .getConstrainPairDetailByRunInfo(constrainRrn, routeRrn, operationRrn, routeSeq, operationSeq);
        }

        @Override
        public LotConstrainTrigger getLotNewTrigger(long lotRrn, long constrainPairRrn) {
            return equipmentConstrainPairDAO.getLotNewTrigger(lotRrn, constrainPairRrn);
        }

        @Override
        public EquipmentConstrainPairDetail getBenchMarkByConstrainRrn(long constrainRrn) {
            return equipmentConstrainPairDAO.getBenchMarkByConstrainRrn(constrainRrn);
        }

        @Override
        public void insertLotTrigger(LotConstrainTrigger trigger) {
            long instanceRrn = Long
                    .valueOf(IDGenerators.get(IDNames.SEQUENCE).generateId(SequenceNames.MODULE_SEQ_OBJECT_RRN));
            trigger.setTriggerRrn(instanceRrn);
            equipmentConstrainPairDAO.insertLotTrigger(trigger);
        }

        @Override
        public void deleteLotConstrainTrigger(Long triggerRrn) {
            equipmentConstrainPairDAO.deleteLotConstrainTrigger(triggerRrn);
        }

    }