# ResistRcpMappingManagerImpl.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.server.base.manager.TransactionLogManager;
    import com.mycim.server.resist.dao.ResistRcpMappingDAO;
    import com.mycim.server.resist.manager.ResistMappingManager;
    import com.mycim.server.resist.manager.ResistRcpMappingManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.ResistMapping;
    import com.mycim.valueobject.ems.ResistRcpMapping;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    /**
     * @author weike.li
     * @version 6.0.0
     * @date 2020/6/24
     **/

    @Service
    @Transactional
    public class ResistRcpMappingManagerImpl implements ResistRcpMappingManager {

        @Autowired
        private ResistMappingManager resistMappingManager;

        @Autowired
        private ResistRcpMappingDAO resistRcpMappingDAO;

        @Autowired
        private TransactionLogManager transactionLogManager;

        @Override
        public void addResistRcpMapping(ResistRcpMapping mapping) {
            ResistMapping resistMapping = resistMappingManager.getResistMappingByResistNo(mapping.getResistNo());
            Assert.notNull(resistMapping,
                           Errors.create().key(MessageIdList.RESIST_RESIST_MISSING).content("Resist not exist!").build());
            //检查是否有重复记录
            boolean isExist = resistRcpMappingDAO
                    .checkResistRcpMappingIsExist(mapping.getResistNo(), mapping.getRecipe());
            Assert.isFalse(isExist, Errors.create().content("This mapping is already existed!").build());
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(mapping.getCreateBy(), TransactionNames.CREATE_KEY);
            resistRcpMappingDAO.addResistRcpMapping(mapping);
            mapping.setOldRecipe(mapping.getRecipe());
            resistRcpMappingDAO.addResistRcpMappingHist(transactionLog.getTransRrn(), mapping);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public Page qryResistRcpMappingByPage(ResistRcpMapping mapping, Page page) {

            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                mapping.setResistNo(StringUtils.replace(mapping.getResistNo(), "*", "%").toString());
            }

            if (StringUtils.isNotBlank(mapping.getCreateBy())) {
                mapping.setCreateBy(StringUtils.replace(mapping.getCreateBy(), "*", "%").toString());
            }

            if (StringUtils.isNotBlank(mapping.getModifyBy())) {
                mapping.setModifyBy(StringUtils.replace(mapping.getModifyBy(), "*", "%").toString());
            }
            return resistRcpMappingDAO.qryResistRcpMappingByPage(mapping, page);
        }

        @Override
        public void deleteResistRcpMapping(String resistNo, String rcpLetter) {
            boolean isExist = resistRcpMappingDAO.checkResistRcpMappingIsExist(resistNo, rcpLetter);

            Assert.isTrue(isExist, Errors.create().content("This mapping is not existed!").build());
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.DELETE_KEY);
            resistRcpMappingDAO.deleteResistRcpMapping(resistNo, rcpLetter);
            ResistRcpMapping mapping = new ResistRcpMapping(resistNo, rcpLetter);
            mapping.setOldRecipe(rcpLetter);
            resistRcpMappingDAO.addResistRcpMappingHist(transactionLog.getTransRrn(), mapping);
            transactionLogManager.markTransactionLog(transactionLog);

        }

        @Override
        public Page qryResistRcpMappingHistByPage(ResistRcpMapping mapping, Page page) {
            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                mapping.setResistNo(StringUtils.replace(mapping.getResistNo(), "*", "%").toString());
            }
            if (StringUtils.isNotBlank(mapping.getTransPerformedBy())) {
                mapping.setTransPerformedBy(StringUtils.replace(mapping.getTransPerformedBy(), "*", "%").toString());
            }
            return resistRcpMappingDAO.qryResistRcpMappingHistByPage(mapping, page);
        }

        @Override
        public void modifyResistRcpMapping(ResistRcpMapping mapping) {
            boolean isExist = resistRcpMappingDAO
                    .checkResistRcpMappingIsExist(mapping.getResistNo(), mapping.getRecipe());
            Assert.isFalse(isExist, Errors.create().content("This mapping is already existed!").build());

            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(mapping.getModifyBy(), TransactionNames.MODIFY_KEY);
            resistRcpMappingDAO.modifyResistRcpMapping(mapping);
            resistRcpMappingDAO.addResistRcpMappingHist(transactionLog.getTransRrn(), mapping);
            transactionLogManager.markTransactionLog(transactionLog);
        }

    }