# AutoMonitorItemReqDaoImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.server.automonitor.dao.AutoMonitorItemReqDao;
    import com.mycim.valueobject.automonitor.entity.AutoMonitorItem;
    import com.mycim.valueobject.automonitor.entity.AutoMonitorItemStep;
    import com.mycim.valueobject.automonitor.entity.AutoMonitorItemStepEdcInfo;
    import com.mycim.valueobject.consts.DataBaseNames;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
    import org.springframework.stereotype.Repository;

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

    /**
     * @author finatice.yang
     * @date 2021/8/24
     **/
    @Repository
    public class AutoMonitorItemReqDaoImpl implements AutoMonitorItemReqDao {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void insertAutoMonitorItem(AutoMonitorItem autoMonitorItem) {
            StringBuilder sql = new StringBuilder("INSERT INTO ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM);
            sql.append(" (WORKFLOW_RRN, WORKFLOW_VERSION, WORKFLOW_ID, EQUIPMENT_RRN, EQUIPMENT_ID, ITEM_TYPE, QTY, ");
            sql.append(" PRODUCT_RRN, PRODUCT_ID, PROCESS_RRN, PROCESS_ID, MAIN_EQUIPMENT_TYPE,");
            sql.append(" MONITOR_CARRIER_FLAG, MONITOR_CARRIER_TYPE, START_SPLIT_FLAG ) ");
            sql.append(" VALUES ");
            sql.append("(:workflowRrn,:workflowVersion,:workflowId,:eqptRrn,:eqptId,:itemType,:qty,");
            sql.append(" :productRrn,:productId,:processRrn,:processId,:mainEqptType,");
            sql.append(" :monitorCarrierFlag,:monitorCarrierType,:startSplitFlag) ");

            jdbcTemplate.update(sql.toString(), new BeanPropertySqlParameterSource(autoMonitorItem));
        }

        @Override
        public void updateAutoMonitorItem(AutoMonitorItem autoMonitorItem) {
            StringBuilder sql = new StringBuilder("UPDATE ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM).append(" SET ");
            sql.append(" QTY=:qty, PRODUCT_RRN=:productRrn, PRODUCT_ID=:productId, ");
            sql.append(" PROCESS_RRN=:processRrn, PROCESS_ID=:processId, ");
            sql.append(" MONITOR_CARRIER_FLAG=:monitorCarrierFlag, MONITOR_CARRIER_TYPE=:monitorCarrierType, ");
            sql.append(" START_SPLIT_FLAG=:startSplitFlag ");
            sql.append(" WHERE WORKFLOW_RRN=:workflowRrn AND WORKFLOW_VERSION=:workflowVersion ");

            jdbcTemplate.update(sql.toString(), new BeanPropertySqlParameterSource(autoMonitorItem));
        }

        @Override
        public void updateAutoMonitorItemSysInActiveFlag(Long workflowRrn, Boolean sysInActiveFlag) {
            StringBuilder sql = new StringBuilder("UPDATE ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM).append(" SET ");
            sql.append(" SYS_INACTIVE_FLAG = ? ");
            sql.append(" WHERE WORKFLOW_RRN=? ");

            Object[] args = new Object[]{sysInActiveFlag, workflowRrn};

            jdbcTemplate.update(sql.toString(), args);
        }

        @Override
        public void deleteAutoMonitorItem(AutoMonitorItem autoMonitorItem) {
            StringBuilder sql = new StringBuilder("DELETE FROM ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM);
            sql.append(" WHERE WORKFLOW_RRN = :workflowRrn AND WORKFLOW_VERSION = :workflowVersion ");
            jdbcTemplate.update(sql.toString(), new BeanPropertySqlParameterSource(autoMonitorItem));
        }

        @Override
        public void insertAutoMonitorItemSteps(List<AutoMonitorItemStep> stepList) {
            StringBuilder sql = new StringBuilder("INSERT INTO ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM_STEP);
            sql.append(" (WORKFLOW_RRN, WORKFLOW_VERSION, WORKFLOW_ID, STEP_SEQUENCE, OPERATION_RRN, OPERATION_ID, ");
            sql.append(" EQUIPMENT_TYPE,EQUIPMENT_RRN,EQUIPMENT_ID,EQUIPMENT_GROUP_RRN,EQUIPMENT_GROUP_ID,");
            sql.append(" MAIN_EQUIPMENT_FLAG,FLOW_SEQ,OPERATION_DESC,RECIPE_ID,RECIPE_RRN,RETICLE_RRN,RETICLE_ID, ");
            sql.append(" RETICLE_GROUP_RRN, RETICLE_GROUP_ID, STAGE_ID, NONRT_SLOT_TYPE  ) ");
            sql.append(" VALUES (:workflowRrn,:workflowVersion,:workflowId,:stepSequence,:operationRrn,:operationId,");
            sql.append(" :eqptType,:equipmentRrn,:equipmentId,:equipmentGroupRrn,:equipmentGroupId,:mainEqptFlag,");
            sql.append(" :flowSeq,:operationDesc,:recipeId,:recipeRrn,:reticleRrn,:reticleId,");
            sql.append(" :reticleGroupRrn,:reticleGroupId,:stageId,:nonRTSlotType)");

            Map<String, Object>[] batchValues = stepList.stream().map(step -> {
                return BeanUtils.copyBeanToMap(step);
            }).collect(Collectors.toList()).toArray(new Map[stepList.size()]);

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

        @Override
        public void deleteAutoMonitorItemSteps(Long workflowRrn, Integer workflowVersion) {
            StringBuilder sql = new StringBuilder("DELETE FROM ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM_STEP);
            sql.append(" WHERE WORKFLOW_RRN = ? AND WORKFLOW_VERSION = ?");

            Object[] args = new Object[]{workflowRrn, workflowVersion};
            jdbcTemplate.update(sql.toString(), args);
        }

        @Override
        public void insertItemStepEdcInfo(List<AutoMonitorItemStepEdcInfo> edcInfoList) {
            StringBuilder sql = new StringBuilder("INSERT INTO ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM_STEP_EDC);
            sql.append(" (WORKFLOW_RRN, WORKFLOW_VERSION, WORKFLOW_ID, STEP_SEQUENCE, WAFER_SLOT, ");
            sql.append(" NONRT_JOB_IDS, NONRT_JOB_NAMES) ");
            sql.append(" VALUES (:workflowRrn,:workflowVersion,:workflowId,:stepSequence,:waferSlot,");
            sql.append(" :nonRTJobIds,:nonRTJobNames) ");

            Map<String, Object>[] batchValues = edcInfoList.stream().map(edcInfo -> {
                return BeanUtils.copyBeanToMap(edcInfo);
            }).collect(Collectors.toList()).toArray(new Map[edcInfoList.size()]);

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

        @Override
        public void deleteItemStepEdcInfo(Long workflowRrn, Integer workflowVersion) {
            StringBuilder sql = new StringBuilder("DELETE FROM ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM_STEP_EDC);
            sql.append(" WHERE WORKFLOW_RRN = ? AND WORKFLOW_VERSION = ?");

            Object[] args = new Object[]{workflowRrn, workflowVersion};
            jdbcTemplate.update(sql.toString(), args);
        }

        @Override
        public void deleteItemStepEdcInfo(Long workflowRrn, Integer workflowVersion, Long stepSequence) {
            StringBuilder sql = new StringBuilder("DELETE FROM ");
            sql.append(DataBaseNames.AUTO_MONITOR_ITEM_STEP_EDC);
            sql.append(" WHERE WORKFLOW_RRN = ? AND WORKFLOW_VERSION = ? AND STEP_SEQUENCE = ?");

            Object[] args = new Object[]{workflowRrn, workflowVersion, stepSequence};
            jdbcTemplate.update(sql.toString(), args);
        }

    }