# ActivationAttributeDaoImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.server.spec.dao.ActivationAttributeDao;
    import com.mycim.server.spec.dao.mapper.ProductSpecItemRowMapper;
    import com.mycim.valueobject.prp.ProductSpecItem;
    import com.mycim.valueobject.prp.ProductSpecInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author Qiansheng.Wang
     * @since 2020-10-26
     */
    @Repository
    public class ActivationAttributeDaoImpl implements ActivationAttributeDao {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void insert(List<ProductSpecItem> activationAttributes) {
            StringBuilder sql = new StringBuilder("insert into ");

            sql.append(" pro_context_active_dtl ");
            sql.append(" (PRODUCT_RRN, PROCESS_RRN, PROCESS_VERSION, ROUTE_SEQ, ROUTE_RRN, ");
            sql.append(" OPERATION_SEQ, OPERATION_RRN, FLOW_SEQ, OPERATION_DESCRIPTION, ");
            sql.append(" STAGE_ID, RECIPE_RRN, PARAMETER_SET_RRN, RETICLE_GROUP_RRN, PROCESS_LOCATION, ");
            sql.append(" STATUS, EFFECTIVE_TIME, TERMINATED_TIME, CREATED_USER, CREATED_TIME, ");
            sql.append(" UPDATED_USER, UPDATED_TIME, ECN_RRN, ");
            sql.append(" ATTRIBUTE_DATA1, ATTRIBUTE_DATA2, ATTRIBUTE_DATA3, ATTRIBUTE_DATA4, ATTRIBUTE_DATA5, ");
            sql.append(" PRODUCT_ID, PRODUCT_VERSION, PROCESS_ID, ROUTE_ID, ROUTE_VERSION, OPERATION_ID, ");
            sql.append(" RECIPE_ID, RETICLE_FAMILY_ID, PARAMETER_SET_ID, EQUIPMENT_GROUP_RRN, EQUIPMENT_GROUP_ID) ");
            sql.append(" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
            sql.append(" ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

            List<Object[]> batchArgs = new ArrayList<>();
            for (ProductSpecItem activationAttribute : activationAttributes) {
                batchArgs
                        .add(new Object[]{activationAttribute.getProductRrn(), activationAttribute.getProcessRrn(),
                                          activationAttribute.getProcessVersion(), activationAttribute.getRouteSeq(),
                                          activationAttribute.getRouteRrn(), activationAttribute.getOperationSeq(),
                                          activationAttribute.getOperationRrn(), activationAttribute.getFlowSeq(),
                                          activationAttribute.getOperationDesc(), activationAttribute.getStageId(),
                                          activationAttribute.getRecipeRrn(), activationAttribute.getParameterSetRrn(),
                                          activationAttribute.getReticleFamilyRrn(),
                                          activationAttribute.getProcessLocation(), activationAttribute.getStatus(),
                                          activationAttribute.getEffectiveTime(), activationAttribute.getTerminatedTime()
                                , activationAttribute.getCreatedUser(), activationAttribute.getCreatedTime(),
                                          activationAttribute.getUpdatedUser(), activationAttribute.getUpdatedTime(),
                                          activationAttribute.getEcnRrn(), activationAttribute.getPollutionLevel(),
                                          activationAttribute.getBankFlag(), activationAttribute.getWorkArea(),
                                          activationAttribute.getOperationType(), activationAttribute.getCpDefine(),
                                          activationAttribute.getProductId(), activationAttribute.getProductVersion(),
                                          activationAttribute.getProcessId(), activationAttribute.getRouteId(),
                                          activationAttribute.getRouteVersion(), activationAttribute.getOperationId(),
                                          activationAttribute.getRecipeId(), activationAttribute.getReticleFamilyId(),
                                          activationAttribute.getParameterSetId(),
                                          activationAttribute.getEquipmentGroupRrn(),
                                          activationAttribute.getEquipmentGroupId()});
            }

            jdbcTemplate.batchUpdate(sql.toString(), batchArgs);
        }

        @Override
        public void delete(List<ProductSpecInfo> productSpecInfos) {
            String sql = "delete from PRO_CONTEXT_ACTIVE_DTL where PRODUCT_RRN = ? and PROCESS_RRN = ? and " +
                    "PROCESS_VERSION = ? ";

            List<Object[]> batchArgs = new ArrayList<>();
            for (ProductSpecInfo productSpecInfo : productSpecInfos) {
                batchArgs
                        .add(new Object[]{productSpecInfo.getProductRrn(), productSpecInfo.getProcessRrn(),
                                          productSpecInfo.getProcessVersion()});
            }

            jdbcTemplate.batchUpdate(sql, batchArgs);
        }

        @Override
        public List<ProductSpecItem> getActiveProductSpec(ProductSpecItem attributeItemQueryTemp) {
            StringBuilder sql = new StringBuilder();

            sql.append("select ");
            sql.append(" PRODUCT_RRN, PROCESS_RRN, PROCESS_VERSION, ROUTE_SEQ, ROUTE_RRN, ");
            sql.append(" OPERATION_SEQ, OPERATION_RRN, FLOW_SEQ, OPERATION_DESCRIPTION, ");
            sql.append(" STAGE_ID, RECIPE_RRN, PARAMETER_SET_RRN, RETICLE_GROUP_RRN, PROCESS_LOCATION, ");
            sql.append(" STATUS, EFFECTIVE_TIME, TERMINATED_TIME, CREATED_USER, CREATED_TIME, ");
            sql.append(" UPDATED_USER, UPDATED_TIME, ECN_RRN, ");
            sql.append(" ATTRIBUTE_DATA1, ATTRIBUTE_DATA2, ATTRIBUTE_DATA3, ATTRIBUTE_DATA4, ATTRIBUTE_DATA5, ");
            sql.append(" PRODUCT_ID, PRODUCT_VERSION, PROCESS_ID, ROUTE_ID, ROUTE_VERSION, OPERATION_ID, ");
            sql.append(" RECIPE_ID, RETICLE_FAMILY_ID, PARAMETER_SET_ID, EQUIPMENT_GROUP_RRN, EQUIPMENT_GROUP_ID ");
            sql.append(" from PRO_CONTEXT_ACTIVE_DTL ");
            sql.append(" where 1 = 1 ");

            List<Object> args = new ArrayList<>();

            if (attributeItemQueryTemp.getProductRrn() != null && attributeItemQueryTemp.getProductRrn() > 0) {
                sql.append(" and PRODUCT_RRN = ?");
                args.add(attributeItemQueryTemp.getProductRrn());
            }

            if (attributeItemQueryTemp.getProductVersion() != null && attributeItemQueryTemp.getProductVersion() > 0) {
                sql.append(" and PRODUCT_VERSION = ?");
                args.add(attributeItemQueryTemp.getProductVersion());
            }

            if (attributeItemQueryTemp.getProcessRrn() != null && attributeItemQueryTemp.getProcessRrn() > 0) {
                sql.append(" and PROCESS_RRN = ?");
                args.add(attributeItemQueryTemp.getProcessRrn());
            }

            if (attributeItemQueryTemp.getProcessVersion() != null && attributeItemQueryTemp.getProcessVersion() > 0) {
                sql.append(" and PROCESS_VERSION = ?");
                args.add(attributeItemQueryTemp.getProcessVersion());
            }

            return jdbcTemplate.query(sql.toString(), new ProductSpecItemRowMapper(), args.toArray());
        }

    }