# LotAutoMonitorReqDaoImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.automonitor.dao.LotAutoMonitorReqDao;
    import com.mycim.valueobject.automonitor.entity.*;
    import com.mycim.valueobject.bas.TransactionLog;
    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.*;
    import java.util.stream.Collectors;

    /**
     * @author finatice.yang
     * @date 2021/8/25
     **/
    @Repository
    public class LotAutoMonitorReqDaoImpl implements LotAutoMonitorReqDao {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void deleteLotMonitorJobStore(Long lotRrn) {
            StringBuilder sql = new StringBuilder(DataBaseNames.DELETE_FROM);
            sql.append(DataBaseNames.AUTO_MONITOR_LOT_STORE);
            sql.append(" WHERE LOT_RRN = ? ");

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

        @Override
        public void insertLotMonitorJobStore(LotMonitorJobStore store) {
            StringBuilder sql = new StringBuilder(DataBaseNames.INSERT_INTO);
            sql.append(DataBaseNames.AUTO_MONITOR_LOT_STORE);
            sql.append(" (LOT_RRN,LOT_ID, BASE_LOT_RRN,BASE_LOT_ID, QTY1, EQUIPMENT_RRN,EQUIPMENT_ID, ITEM_TYPE, ");
            sql.append(" PROCESS_RRN,PROCESS_ID, PROCESS_VERSION, JOB_STATUS, ");
            sql.append(" WORKFLOW_RRN, WORKFLOW_VERSION, WORKFLOW_ID, WFL_STEP, MERGE_WFL_STEP, ");
            sql.append(" MONITOR_CARRIER_RRN, MONITOR_CARRIER_ID) ");
            sql.append(" VALUES (:lotRrn,:lotId,:baseLotRrn,:baseLotId,:qty,:eqptRrn,:eqptId,:itemType,:processRrn,");
            sql.append(" :processId,:processVersion,:jobStatus,:workflowRrn,:workflowVersion,:workflowId,");
            sql.append(" :wflStep,:mergeWflStep,:monitorCarrierRrn,:monitorCarrierId)");

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

        @Override
        public void insertLotMonitorJobStoreHistory(TransactionLog transactionLog, LotMonitorJobStore store) {
            StringBuilder sql = new StringBuilder(DataBaseNames.INSERT_INTO);
            sql.append(DataBaseNames.AUTO_MONITOR_LOT_STORE_H);
            sql.append(" (TRANS_RRN,TRANS_ID,TRANS_TIME,TRANS_USER_ID,TRANS_COMMENTS,");
            sql.append(" LOT_RRN, LOT_ID, BASE_LOT_RRN,BASE_LOT_ID, QTY1, EQUIPMENT_RRN,EQUIPMENT_ID, ITEM_TYPE, ");
            sql.append(" PROCESS_RRN,PROCESS_ID, PROCESS_VERSION, PRODUCT_RRN,PRODUCT_ID, JOB_STATUS, ");
            sql.append(" WORKFLOW_RRN, WORKFLOW_VERSION, WORKFLOW_ID, WFL_STEP, MERGE_WFL_STEP, ");
            sql.append(" MONITOR_CARRIER_RRN, MONITOR_CARRIER_ID, CARRIER_RRN, CARRIER_ID) ");
            sql.append(" VALUES (:transRrn,:transId,:transStartTimestamp,:transPerformedBy,:comments,");
            sql.append(" :lotRrn,:lotId,:baseLotRrn,:baseLotId,:qty,:eqptRrn,:eqptId,:itemType,");
            sql.append(" :processRrn,:processId,:processVersion,:productRrn,:productId,:jobStatus,");
            sql.append(" :workflowRrn,:workflowVersion,:workflowId,:wflStep,:mergeWflStep,");
            sql.append(" :monitorCarrierRrn,:monitorCarrierId,:carrierRrn,:carrierId)");

            Map<String, Object> paramMap = BeanUtils.copyBeanToMap(store);
            BeanUtils.copyBeanToMap(paramMap, transactionLog);
            jdbcTemplate.update(sql.toString(), paramMap);
        }

        @Override
        public void updateLotMonitorJobStore(LotMonitorJobStore store) {
            Map<String, Object> paramMap = MapUtils.newHashMap();

            StringBuilder sql = new StringBuilder(DataBaseNames.UPDATE);
            sql.append(DataBaseNames.AUTO_MONITOR_LOT_STORE).append(" SET ");

            StringBuilder setSql = new StringBuilder("");
            if (StringUtils.isNotBlank(store.getJobStatus())) {
                setSql.append(" JOB_STATUS = :jobStatus ,");
                paramMap.put("jobStatus", store.getJobStatus());
            }

            if (StringUtils.isNotBlank(setSql)) {
                sql.append(setSql.substring(0, setSql.lastIndexOf(StringUtils.COMMA_SIGN)));
            }

            sql.append(" WHERE LOT_RRN = :lotRrn ");
            paramMap.put("lotRrn", store.getLotRrn());

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

        @Override
        public void insertLotMonitorStepEdcInfo(List<LotMonitorJobStepEdcInfo> stepEdcInfoList) {
            StringBuilder sql = new StringBuilder(DataBaseNames.INSERT_INTO);
            sql.append(DataBaseNames.AUTO_MONITOR_LOT_EDC_INFO);
            sql.append(" (TRANS_RRN,LOT_RRN,LOT_ID, STEP_SEQUENCE,SEQUENCE, UNIT_RRN,UNIT_ID, ");
            sql.append(" SPC_JOB_ID,SPC_JOB_NAME,DATA_VALUE,FORMULA,VIO_INFO) ");
            sql.append(" VALUES (:transRrn,:lotRrn,:lotId,:lotStepSequence,:sequence,:unitRrn,:unitId,");
            sql.append(" :spcJobId,:spcJobName,:dataValue,:formula,:vioInfo)");

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

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

        @Override
        public void insertMonitorCarrierMapping(List<MonitorCarrierMapping> monitorCarrierMappings) {
            StringBuffer sql = new StringBuffer(DataBaseNames.INSERT_INTO);
            sql.append(DataBaseNames.AUTO_MONITOR_CARRIER_MAPPING);
            sql.append(" (MONITOR_CARRIER_RRN, MONITOR_CARRIER_ID, UNIT_RRN, UNIT_ID, ");
            sql.append(" POSITION, SOURCE_POSITION, LOT_RRN, LOT_ID) ");
            sql.append(" VALUES (:monitorCarrierRrn,:monitorCarrierId,:unitRrn,:unitId,");
            sql.append(" :position,:sourcePosition,:lotRrn,:lotId) ");

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

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

        @Override
        public void deleteMonitorCarrierMappingByLot(Long lotRrn) {
            StringBuffer sql = new StringBuffer(DataBaseNames.DELETE_FROM);
            sql.append(DataBaseNames.AUTO_MONITOR_CARRIER_MAPPING);
            sql.append(" WHERE LOT_RRN = ? ");
            jdbcTemplate.update(sql.toString(), new Object[]{lotRrn});
        }

        @Override
        public void deleteMonitorCarrierMappingByCarrier(Long monitorCarrierRrn) {
            StringBuffer sql = new StringBuffer(DataBaseNames.DELETE_FROM);
            sql.append(DataBaseNames.AUTO_MONITOR_CARRIER_MAPPING);
            sql.append(" WHERE MONITOR_CARRIER_RRN = ? ");
            jdbcTemplate.update(sql.toString(), new Object[]{monitorCarrierRrn});
        }

        @Override
        public void insertAutoMonitorUnit(List<AutoMonitorUnitInfo> unitInfoList) {
            StringBuilder sql = new StringBuilder(DataBaseNames.INSERT_INTO);
            sql.append(DataBaseNames.AUTO_MONITOR_UNIT);
            sql.append(" (LOT_RRN,LOT_ID,UNIT_RRN,UNIT_ID,CARRIER_RRN,CARRIER_ID,POSITION,");
            sql.append(" MONITOR_CARRIER_RRN,MONITOR_CARRIER_ID,MONITOR_CARRIER_POSITION) ");
            sql.append(" VALUES ");
            sql.append(" (:lotRrn,:lotId,:unitRrn,:unitId,:carrierRrn,:carrierId,:position,");
            sql.append(" :monitorCarrierRrn,:monitorCarrierId,:monitorCarrierPosition) ");

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

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

    }