# ResistEqptMappingManagerImpl.java

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

    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.ems.manager.EntityManager;
    import com.mycim.server.resist.dao.ResistEqptMappingDAO;
    import com.mycim.server.resist.manager.ResistEqptMappingManager;
    import com.mycim.server.resist.manager.ResistMappingManager;
    import com.mycim.server.security.manager.StationManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.Resist;
    import com.mycim.valueobject.ems.ResistEqptMapping;
    import com.mycim.valueobject.ems.ResistMapping;
    import com.mycim.valueobject.security.Station;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
     * @author yanbing.chen
     * @version 6.0.0
     * @date 2019/10/31
     **/
    @Service
    @Transactional
    public class ResistEqptMappingManagerImpl implements ResistEqptMappingManager {

        @Autowired
        ResistEqptMappingDAO resistEqptMappingDAO;

        @Autowired
        EntityManager entityManager;

        @Autowired
        ResistMappingManager resistMappingManager;

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        StationManager stationManager;

        @Autowired
        TransactionLogManager transactionLogManager;

        @Override
        public Page qryResistEqptMappingByPage(ResistEqptMapping mapping, Page page) {
            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                mapping.setResistNo(StringUtils.replace(mapping.getResistNo(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getEqptId())) {
                mapping.setEqptId(
                        StringUtils.trimToUpperCase(StringUtils.replace(mapping.getEqptId(), "*", "%").toString()));
            }
            if (StringUtils.isNotBlank(mapping.getPrPipe())) {
                mapping.setPrPipe(StringUtils.replace(mapping.getPrPipe(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getUnit())) {
                mapping.setUnit(StringUtils.replace(mapping.getUnit(), "*", "%").toString());
            }

            return resistEqptMappingDAO.qryResistEqptMappingByPage(mapping, page);
        }

        @Override
        public boolean isOnlyRealPipe(String eqptId, String prPipe, String unit) {
            ResistEqptMapping mapping = new ResistEqptMapping(eqptId, prPipe, unit);
            return isResistEqptMappingExist(mapping);
        }

        @Override
        public boolean isResistEqptMappingExist(ResistEqptMapping mapping) {
            Page page = new Page(1, 10);
            page = this.qryResistEqptMappingByPage(mapping, page);
            if (CollectionUtils.isNotEmpty(page.getResults())) {
                return true;
            }
            return false;
        }

        @Override
        public void addResistEqptMapping(ResistEqptMapping mapping) {
            ResistMapping resistMapping = resistMappingManager.getResistMappingByResistNo(mapping.getResistNo());
            Assert.notNull(resistMapping,
                           Errors.create().key(MessageIdList.RESIST_RESIST_MISSING).content("Resist not exist!").build());

            String entityNamedSpace = namedObjectManager
                    .getNamedSpace(LocalContext.getFacilityRrn(), ObjectList.ENTITY_KEY);
            long eqptRrn = namedObjectManager
                    .getNamedObjectRrn(mapping.getEqptId(), entityNamedSpace, ObjectList.ENTITY_KEY,
                                       ObjectList.EQUIPMENT_KEY);
            Assert.isFalse(eqptRrn <= 0,
                           Errors.create().key(MessageIdList.EQUIPMENT_ID_NOT_EXIST).content("EQP ID is not exist!")
                                 .build());

            // 设备必须经过初始化事件
            String eqptCurStatus = entityManager.getEntityCurrentStatus(eqptRrn);
            Assert.isFalse(StringUtils.isBlank(eqptCurStatus),
                           Errors.create().key(MessageIdList.EQUIPMENT_EQP_NOT_INITIALIZED)
                                 .content("The EQP is not initialized!").build());

            // 添加的设备必须是%PH%工作站的设备
            boolean stationFlag = true;
            List<Station> stationColl = stationManager.getStationsByEquips(eqptRrn);
            if (CollectionUtils.isNotEmpty(stationColl)) {
                for (Station s : stationColl) {
                    if (StringUtils.contains(s.getInstanceId(), "PH")) {
                        stationFlag = false;
                        break;
                    }
                }
            }
            Assert.isFalse(stationFlag, Errors.create().key(MessageIdList.EQUIPMENT_STATION_NOT_ALLOW)
                                              .content("The EQP station is not allow add!").build());
            String resistEqptBarcode = mapping.getEqptId()  + mapping.getPrPipe() + mapping.getResistNo();
            mapping.setResistEqptBarcode(resistEqptBarcode.toUpperCase());
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(mapping.getCreateBy(), TransactionNames.CREATE_KEY);
            resistEqptMappingDAO.addResistEqptMapping(mapping);
            resistEqptMappingDAO.addResistEqptMappingH(transactionLog.getTransRrn(), 1, mapping);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void deleteResistEqptMapping(ResistEqptMapping mapping) {
            ResistMapping resistMapping = resistMappingManager.getResistMappingByResistNo(mapping.getResistNo());
            Assert.notNull(resistMapping,
                           Errors.create().key(MessageIdList.RESIST_RESIST_MISSING).content("Resist not exist!").build());

            ResistEqptMapping existMapping = new ResistEqptMapping(mapping.getEqptId(), mapping.getPrPipe(),
                                                                   mapping.getUnit(), mapping.getResistNo());
            Assert.isTrue(isResistEqptMappingExist(existMapping),
                          Errors.create().key(MessageIdList.RESIST_RESIST_EQP_MAPPING_MISSING)
                                .content("Resist-EQP mapping is not exist!").build());
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(mapping.getModifyBy(), TransactionNames.DELETE_KEY);

            resistEqptMappingDAO.deleteResistEqptMapping(mapping);
            resistEqptMappingDAO.addResistEqptMappingH(transactionLog.getTransRrn(), 1, mapping);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void modifyResistEqptMapping(ResistEqptMapping mapping) {
            ResistMapping resistMapping = resistMappingManager.getResistMappingByResistNo(mapping.getResistNo());
            Assert.notNull(resistMapping,
                           Errors.create().key(MessageIdList.RESIST_RESIST_MISSING).content("Resist not exist!").build());

            ResistEqptMapping existMapping = new ResistEqptMapping(mapping.getEqptId(), mapping.getPrPipe(),
                                                                   mapping.getUnit(), mapping.getResistNo());
            Assert.isFalse(isResistEqptMappingExist(existMapping),
                           Errors.create().key(MessageIdList.RESIST_RESIST_EQP_MAPPING_MISSING)
                                 .content("Resist-EQP mapping is not exist!").build());
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(mapping.getModifyBy(), TransactionNames.MODIFY_KEY);
            resistEqptMappingDAO.modifyResistEqptMapping(mapping);
            resistEqptMappingDAO.addResistEqptMappingH(transactionLog.getTransRrn(), 1, mapping);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public Page qryResistEqptMappingHByPage(ResistEqptMapping mapping, Page page) {
            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                mapping.setResistNo(StringUtils.replace(mapping.getResistNo(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getEqptId())) {
                mapping.setEqptId(
                        StringUtils.trimToUpperCase(StringUtils.replace(mapping.getEqptId(), "*", "%").toString()));
            }
            if (StringUtils.isNotBlank(mapping.getPrPipe())) {
                mapping.setPrPipe(StringUtils.replace(mapping.getPrPipe(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getUnit())) {
                mapping.setUnit(StringUtils.replace(mapping.getUnit(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getTransPerformedBy())) {
                mapping.setTransPerformedBy(StringUtils.replace(mapping.getTransPerformedBy(), "*", "%").toString());
            }

            return resistEqptMappingDAO.qryResistEqptMappingHByPage(mapping, page);
        }

        @Override
        public ResistEqptMapping getResistEqptMappingByResist(Resist resist) {

            return resistEqptMappingDAO.getResistEqptMappingByResist(resist);
        }

    }