# ActivationAttributeManagerImpl.java

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

    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.spec.dao.ActivationAttributeDao;
    import com.mycim.server.spec.manager.*;
    import com.mycim.valueobject.Pair;
    import com.mycim.valueobject.prp.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.lang.Override;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author Qiansheng.Wang
     * @since 2020-10-26
     */
    @Service
    @Transactional
    public class ActivationAttributeManagerImpl implements ActivationAttributeManager {

        @Autowired
        ActivationAttributeDao activationAttributeDao;

        @Autowired
        ProductSpecInfoManager productSpecInfoManager;

        @Autowired
        ProcessSpecItemManager processSpecItemManager;

        @Autowired
        ProductAttributeItemManager productAttributeItemManager;

        /**
         * 更新PRO_CONTEXT_ACTIVE_DTL中间表内容
         * @param processRrn
         * @param processVersion
         */
        @Override
        public void saveActivationAttributes(Long processRrn, Integer processVersion) {
            List<ProductSpecInfo> productSpecInfos = productSpecInfoManager.getProductSpecInfos(processRrn, processVersion);
            List<ProcessSpecItem> processSpecItems = processSpecItemManager.getProcessSpecItems(processRrn, processVersion);
            List<ProductAttributeItem> productAttributeItems = productAttributeItemManager
                    .getProductAttributeItems(processRrn, processVersion);

            List<ProductSpecItem> activationAttributes = new ArrayList<>();

            for (ProductSpecInfo productSpecInfo : productSpecInfos) {
                for (ProcessSpecItem processSpecItem : processSpecItems) {
                    activationAttributes
                            .add(buildActivationAttribute(productSpecInfo, processSpecItem, productAttributeItems));
                }
            }

            activationAttributeDao.delete(productSpecInfos);
            activationAttributeDao.insert(activationAttributes);
        }

        @Override
        public List<ProductSpecItem> getActiveProductSpec(Long productRrn, Integer productVersion, Long processRrn,
                                                          Integer processVersion) {
            ProductSpecItem productSpecItem = new ProductSpecItem();

            productSpecItem.setProductRrn(productRrn);
            productSpecItem.setProductVersion(productVersion);
            productSpecItem.setProcessRrn(processRrn);
            productSpecItem.setProcessVersion(processVersion);

            return getActiveProductSpec(productSpecItem);
        }

        @Override
        public List<ProductSpecItem> getActiveProductSpec(Long processRrn, Integer processVersion) {
            ProductSpecItem productSpecItem = new ProductSpecItem();

            productSpecItem.setProcessRrn(processRrn);
            productSpecItem.setProcessVersion(processVersion);

            return getActiveProductSpec(productSpecItem);
        }

        private List<ProductSpecItem> getActiveProductSpec(ProductSpecItem productSpecItem) {
            List<ProductSpecItem> productSpecItems = activationAttributeDao.getActiveProductSpec(productSpecItem);
            if (CollectionUtils.isEmpty(productSpecItems)) {
                return new ArrayList<>();
            }
            return productSpecItems;
        }

        private ProductSpecItem buildActivationAttribute(ProductSpecInfo productSpecInfo, ProcessSpecItem processSpecItem,
                                                         List<ProductAttributeItem> productAttributeItems) {
            ProductSpecItem productSpecItem = new ProductSpecItem();

            BeanUtils.copyProperties(processSpecItem, productSpecItem);

            productSpecItem.setProductRrn(productSpecInfo.getProductRrn());
            productSpecItem.setProductId(productSpecInfo.getProductId());
            productSpecItem.setProductVersion(productSpecInfo.getProductVersion());

            Pair<Long, String> pair = getAttributeRrnAndNamePair(productAttributeItems, productSpecItem,
                                                                 processSpecItem.getRecipeRrn(),
                                                                 processSpecItem.getRecipeId());

            productSpecItem.setRecipeRrn(pair.getKey());
            productSpecItem.setRecipeId(pair.getValue());

            pair = getAttributeRrnAndNamePair(productAttributeItems, productSpecItem, processSpecItem.getReticleFamilyRrn(),
                                              processSpecItem.getReticleFamilyId());

            productSpecItem.setReticleFamilyId(pair.getValue());
            productSpecItem.setReticleFamilyRrn(pair.getKey());

            pair = getAttributeRrnAndNamePair(productAttributeItems, productSpecItem, processSpecItem.getParameterSetRrn(),
                                              processSpecItem.getParameterSetId());

            productSpecItem.setParameterSetId(pair.getValue());
            productSpecItem.setParameterSetRrn(pair.getKey());

            return productSpecItem;
        }

        /**
         * Use check logic to get attribute RRN and ID (name)
         *
         * @param productAttributeItems Source value
         * @param productSpecItem       target value
         * @param attributeNameRrn      instance RRN
         * @param attributeName         instance ID
         * @return One pair, the key is the instance RRN, and the value is the instance ID
         */
        private Pair<Long, String> getAttributeRrnAndNamePair(List<ProductAttributeItem> productAttributeItems,
                                                              ProductSpecItem productSpecItem, Long attributeNameRrn,
                                                              String attributeName) {
            Pair<Long, String> pair = new Pair<>(attributeNameRrn, attributeName);

            if (attributeNameRrn != null && attributeNameRrn > 0 && ProductVariableEnum.isVariable(attributeName)) {
                ProductAttributeItem productAttributeItem = new ProductAttributeItem();

                BeanUtils.copyProperties(productSpecItem, productAttributeItem);

                productAttributeItem.setAttributeName(attributeName);
                productAttributeItem.setAttributeNameRrn(attributeNameRrn);

                String attributeValue = null;
                Long attributeValueRrn = null;

                int index = productAttributeItems.indexOf(productAttributeItem);
                if (index >= 0) {
                    productAttributeItem = productAttributeItems.get(index);
                    attributeValue = productAttributeItem.getAttributeValue();
                    attributeValueRrn = productAttributeItem.getAttributeValueRrn();
                }

                pair = new Pair<>(attributeValueRrn, attributeValue);
            }
            return pair;
        }

    }