# ArgRecipeServiceImpl.java

    package com.mycim.server.rcp.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.jdbc.Page;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.rcp.manager.ArgRecipeManager;
    import com.mycim.server.wip.manager.ArgRecipeQueryManager;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.prp.ArgRecipeInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    /**
     * Diff Arg功能
     *
     * @author finatice.yang
     * @date 2021/6/23
     **/
    @Service
    @SofaService(interfaceType = ArgRecipeService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    @Transactional
    public class ArgRecipeServiceImpl implements ArgRecipeService {

        @Autowired
        TransactionLogManager transactionLogManager;

        @Autowired
        ArgRecipeManager argRecipeManager;

        @Autowired
        ArgRecipeQueryManager argRecipeQueryManager;

        @Override
        public void addArgInfo(ArgRecipeInfo argInfo) {

            List<ArgRecipeInfo> argBaseList = argRecipeQueryManager.getArgInfoList(argInfo);

            Long sequence = NumberUtils.LONG_ZERO;
            if (CollectionUtils.isNotEmpty(argBaseList)) {
                for (ArgRecipeInfo argBaseRecipeInfo : argBaseList) {
                    if (argBaseRecipeInfo.getSequence().longValue() > sequence.longValue()) {
                        sequence = argBaseRecipeInfo.getSequence();
                    }

                    Assert.isFalse(checkOverLapping(argBaseRecipeInfo, argInfo),
                                   Errors.create().content("Wafer count had overLapping").build());
                }
            }

            argInfo.setSequence(sequence + 1);

            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.CREATE_KEY);

            argRecipeManager.addArgInfo(transactionLog, argInfo);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void modifyArgInfo(ArgRecipeInfo argInfo) {

            List<ArgRecipeInfo> argBaseList = argRecipeQueryManager.getArgInfoList(argInfo);

            if (CollectionUtils.isNotEmpty(argBaseList)) {
                for (ArgRecipeInfo argBaseRecipeInfo : argBaseList) {
                    if (argBaseRecipeInfo.getSequence().longValue() == argInfo.getSequence().longValue()) {
                        continue;
                    }

                    Assert.isFalse(checkOverLapping(argBaseRecipeInfo, argInfo),
                                   Errors.create().content("Wafer count had overLapping").build());
                }
            }

            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.MODIFY_KEY);
            argRecipeManager.updateArgInfo(transactionLog, argInfo);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void deleteArgInfos(List<ArgRecipeInfo> argInfoList) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.DELETE_KEY);
            argRecipeManager.deleteArgInfos(transactionLog, argInfoList);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        private boolean checkOverLapping(ArgRecipeInfo argBaseInfo, ArgRecipeInfo argSetupInfo) {
            Integer lowerWaferCount = argSetupInfo.getLowerWaferCount();
            Integer upperWaferCount = argSetupInfo.getUpperWaferCount();

            boolean hadOverLapping = false;
            int startCount = 0;
            int endCount = 0;

            if (lowerWaferCount != null) {
                startCount = lowerWaferCount.intValue();
            }
            if (upperWaferCount != null) {
                endCount = upperWaferCount.intValue();
            }

            if (endCount == 0) {
                if (argBaseInfo.getUpperWaferCount() == null) {
                    hadOverLapping = true;
                } else if (startCount < argBaseInfo.getUpperWaferCount().intValue()) {
                    hadOverLapping = true;
                }
            } else {
                if (argBaseInfo.getLowerWaferCount() == null) {
                    hadOverLapping = true;
                } else {
                    for (int i = startCount + 1; i <= endCount; i++) {
                        if (argBaseInfo.getUpperWaferCount() == null) {
                            if (i > argBaseInfo.getLowerWaferCount().intValue()) {
                                hadOverLapping = true;
                                break;
                            }
                        } else {
                            if (i > argBaseInfo.getLowerWaferCount().intValue()
                                    && i <= argBaseInfo.getUpperWaferCount().intValue()) {
                                hadOverLapping = true;
                                break;
                            }
                        }

                    }
                }
            }

            return hadOverLapping;
        }

    }