# ProductAttributeInfoDaoImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.server.spec.dao.ProductAttributeInfoDao;
    import com.mycim.server.spec.dao.mapper.ProductAttributeInfoMapper;
    import com.mycim.valueobject.prp.ProductAttributeInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

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

    @Repository
    public class ProductAttributeInfoDaoImpl implements ProductAttributeInfoDao {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void insertProductAttributeInfos(List<ProductAttributeInfo> productAttributeInfos) {
            StringBuilder sql = new StringBuilder("INSERT INTO ");

            sql.append(" PROD_ATTRIBUTE_INFO ");
            sql.append(" (PRODUCT_RRN, PRODUCT_ID, PRODUCT_VERSION, PROCESS_RRN, PROCESS_ID, PROCESS_VERSION, ");
            sql.append(" ACTIVE_FLAG, ACTIVE_TIME, ACTIVE_USER, CURRENT_STATUS, ");
            sql.append(" LAST_UPDATED_USER, LAST_UPDATED_TIME, CREATE_USER_ID, CREATE_TIME) ");
            sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

            List<Object[]> batchArgs = new ArrayList<>();

            for (ProductAttributeInfo attributeInfo : productAttributeInfos) {
                batchArgs
                        .add(new Object[]{attributeInfo.getProductRrn(), attributeInfo.getProductId(),
                                          attributeInfo.getProductVersion(), attributeInfo.getProcessRrn(),
                                          attributeInfo.getProcessId(), attributeInfo.getProcessVersion(),
                                          attributeInfo.getActiveFlag(), attributeInfo.getActiveTime(),
                                          attributeInfo.getActiveUser(), attributeInfo.getCurrentStatus(),
                                          attributeInfo.getLastUpdatedUser(), attributeInfo.getLastUpdatedTime(),
                                          attributeInfo.getCreateUserId(), attributeInfo.getCreateTime()});
            }

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

        @Override
        public void deleteProductAttributeInfos(List<ProductAttributeInfo> productAttributeInfos) {
            StringBuilder sql = new StringBuilder("delete from ");

            sql.append(" PROD_ATTRIBUTE_INFO ");
            sql.append(" where PRODUCT_RRN = ? and PRODUCT_VERSION = ? and PROCESS_RRN = ? and PROCESS_VERSION = ? ");

            List<Object[]> batchArgs = new ArrayList<>();
            for (ProductAttributeInfo attributeInfo : productAttributeInfos) {
                batchArgs
                        .add(new Object[]{attributeInfo.getProductRrn(), attributeInfo.getProductVersion(),
                                          attributeInfo.getProcessRrn(), attributeInfo.getProcessVersion()});

            }

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

        @Override
        public void updateStatusOfProductAttributeInfos(List<ProductAttributeInfo> productAttributeInfos) {
            StringBuilder sql = new StringBuilder("update ");

            sql.append(" PROD_ATTRIBUTE_INFO ");
            sql.append(" set ACTIVE_FLAG = ?, ACTIVE_TIME = ?, ACTIVE_USER = ?, ");
            sql.append(" CURRENT_STATUS = ?, LAST_UPDATED_USER = ?, LAST_UPDATED_TIME = ? ");
            sql.append(" where PRODUCT_RRN = ? and PRODUCT_VERSION = ? and PROCESS_RRN = ? and PROCESS_VERSION = ? ");

            List<Object[]> batchArgs = new ArrayList<>();
            for (ProductAttributeInfo attributeInfo : productAttributeInfos) {
                batchArgs
                        .add(new Object[]{attributeInfo.getActiveFlag(), attributeInfo.getActiveTime(),
                                          attributeInfo.getActiveUser(), attributeInfo.getCurrentStatus(),
                                          attributeInfo.getLastUpdatedUser(), attributeInfo.getLastUpdatedTime(),
                                          attributeInfo.getProductRrn(), attributeInfo.getProductVersion(),
                                          attributeInfo.getProcessRrn(), attributeInfo.getProcessVersion()});

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

        @Override
        public ProductAttributeInfo getProductAttributeInfoByPrimaryKey(Long productRrn, Integer productVersion,
                                                                        Long processRrn, Integer processVersion) {
            StringBuilder sql = new StringBuilder("select ");

            sql.append(" PRODUCT_RRN, PRODUCT_ID, PRODUCT_VERSION, PROCESS_RRN, PROCESS_ID, PROCESS_VERSION, ");
            sql.append(" ACTIVE_FLAG, ACTIVE_TIME, ACTIVE_USER, CURRENT_STATUS, ");
            sql.append(" LAST_UPDATED_USER, LAST_UPDATED_TIME, CREATE_USER_ID, CREATE_TIME ");
            sql.append(" from PROD_ATTRIBUTE_INFO ");
            sql.append(" where PRODUCT_RRN = ? and PRODUCT_VERSION = ? and PROCESS_RRN = ? and PROCESS_VERSION = ? ");

            return jdbcTemplate
                    .queryForObjectWithNull(sql.toString(), new ProductAttributeInfoMapper(), productRrn, productVersion,
                                            processRrn, processVersion);
        }

        @Override
        public List<ProductAttributeInfo> getProductAttributeInfoWithPrimaryKey(
                ProductAttributeInfo attributeInfoQueryTemp) {
            StringBuilder sql = new StringBuilder("select ");

            sql.append(" PRODUCT_RRN, PRODUCT_ID, PRODUCT_VERSION, PROCESS_RRN, PROCESS_ID, PROCESS_VERSION, ");
            sql.append(" ACTIVE_FLAG, ACTIVE_TIME, ACTIVE_USER, CURRENT_STATUS, ");
            sql.append(" LAST_UPDATED_USER, LAST_UPDATED_TIME, CREATE_USER_ID, CREATE_TIME ");
            sql.append(" from PROD_ATTRIBUTE_INFO WHERE 1 = 1 ");

            List<Object> args = new ArrayList<>();
            if (attributeInfoQueryTemp.getProductRrn() != null && attributeInfoQueryTemp.getProductRrn() > 0) {
                sql.append(" and PRODUCT_RRN = ?");
                args.add(attributeInfoQueryTemp.getProductRrn());
            }

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

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

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

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

    }