# ResistRcpMappingDAOImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.jdbc.mapper.RowMapper;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.resist.dao.ResistRcpMappingDAO;
    import com.mycim.server.resist.dao.mapper.ResistRcpMappingRowMapper;
    import com.mycim.valueobject.consts.DataBaseNames;
    import com.mycim.valueobject.ems.ResistRcpMapping;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author weike.li
     * @version 6.0.0
     * @date 2020/6/24
     **/
    @Repository
    public class ResistRcpMappingDAOImpl implements ResistRcpMappingDAO {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public Page qryResistRcpMappingByPage(ResistRcpMapping mapping, Page page) {
            StringBuilder sql = new StringBuilder("SELECT ");
            sql.append(" R.RESIST_NO, R.RECIPE, R.CREATE_BY, R.CREATE_TIME, ");
            sql.append(" R.MODIFY_BY, R.MODIFY_TIME ");
            sql.append(" FROM ");
            sql.append(DataBaseNames.RESIST_RCP_MAPPING);
            sql.append(" R WHERE 1=1 ");

            List<Object> argList = new ArrayList<>();
            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                sql.append("AND R.RESIST_NO LIKE ? ");
                argList.add(mapping.getResistNo());
            }
            if (StringUtils.isNotBlank(mapping.getCreateBy())) {
                sql.append("AND R.CREATE_BY LIKE ? ");
                argList.add(mapping.getCreateBy());
            }
            if (StringUtils.isNotBlank(mapping.getModifyBy())) {
                sql.append("AND R.MODIFY_BY LIKE ? ");
                argList.add(mapping.getModifyBy());
            }
            if (StringUtils.isNotBlank(mapping.getTransStartTime())) {
                sql.append(" AND R.CREATE_TIME > to_timestamp (?,'" + DateUtils.DATE_FORMAT24 + "')");
                argList.add(mapping.getTransStartTime() + " 00:00:00");
            }

            if (StringUtils.isNotBlank(mapping.getTransEndTime())) {
                sql.append(" AND R.CREATE_TIME <= to_timestamp (?,'" + DateUtils.DATE_FORMAT24 + "')");
                argList.add(mapping.getTransEndTime() + " 23:59:59");
            }

            sql.append("ORDER BY R.CREATE_TIME DESC ");

            return jdbcTemplate.queryForPage(page, sql.toString(), argList.toArray(), new ResistRcpMappingRowMapper());
        }

        @Override
        public void addResistRcpMapping(ResistRcpMapping mapping) {
            String sql = "INSERT INTO " + DataBaseNames.RESIST_RCP_MAPPING + " (RESIST_NO, " +
                    "RECIPE, CREATE_BY, MODIFY_BY, CREATE_TIME, " + " MODIFY_TIME) VALUES (?, ?, ?, ?, sysdate, " +
                    "sysdate) ";
            jdbcTemplate
                    .update(sql, mapping.getResistNo(), mapping.getRecipe(), mapping.getCreateBy(), mapping.getCreateBy());
        }

        @Override
        public void addResistRcpMappingHist(long transRrn, ResistRcpMapping mapping) {
            String sql = "INSERT INTO " + DataBaseNames.RESIST_RCP_MAPPING_H + " (TRANS_RRN, RESIST_NO, " +
                    " RECIPE, OLD_RECIPE) VALUES (?, ?, ?, ?) ";
            jdbcTemplate.update(sql, transRrn, mapping.getResistNo(), mapping.getRecipe(), mapping.getOldRecipe());
        }

        @Override
        public boolean checkResistRcpMappingIsExist(String resistNo, String rcpLetter) {
            String sql = "SELECT COUNT(*) FROM RESIST_RCP_MAPPING WHERE RESIST_NO = ? " + "AND RECIPE = ?";
            return jdbcTemplate.queryForObject(sql, new Object[]{resistNo, rcpLetter}, int.class) > 0;
        }

        @Override
        public void deleteResistRcpMapping(String resistNo, String rcpLetter) {
            String sql = "delete from RESIST_RCP_MAPPING WHERE RESIST_NO = ?  AND " + "RECIPE = ?";
            jdbcTemplate.update(sql, resistNo, rcpLetter);
        }

        @Override
        public Page qryResistRcpMappingHistByPage(ResistRcpMapping mapping, Page page) {
            StringBuilder sql = new StringBuilder("SELECT ");
            sql.append(" H.RESIST_NO, H.RECIPE, H.OLD_RECIPE, ");
            sql.append(" T.TRANS_PERFORMED_BY, T.TRANS_ID, T.TRANS_START_TIMESTAMP ");
            sql.append(" FROM RESIST_RCP_MAPPING_H H, TRANSACTION_LOG T ");
            sql.append(" where H.TRANS_RRN = T.TRANS_RRN(+) ");

            List<Object> argList = new ArrayList<>();
            if (StringUtils.isNotBlank(mapping.getResistNo())) {
                sql.append("AND H.RESIST_NO LIKE ? ");
                argList.add(mapping.getResistNo());
            }
            if (StringUtils.isNotBlank(mapping.getTransPerformedBy())) {
                sql.append("AND T.TRANS_PERFORMED_BY LIKE ? ");
                argList.add(mapping.getTransPerformedBy());
            }
            if (StringUtils.isNotBlank(mapping.getTransStartTime())) {
                sql.append(" AND T.TRANS_START_TIMESTAMP > to_timestamp (?,'" + DateUtils.DATE_FORMAT24 + "')");
                argList.add(mapping.getTransStartTime() + " 00:00:00");
            }

            if (StringUtils.isNotBlank(mapping.getTransEndTime())) {
                sql.append(" AND T.TRANS_START_TIMESTAMP <= to_timestamp (?,'" + DateUtils.DATE_FORMAT24 + "')");
                argList.add(mapping.getTransEndTime() + " 23:59:59");
            }

            sql.append("ORDER BY T.TRANS_START_TIMESTAMP DESC ");

            return jdbcTemplate.queryForPage(page, sql.toString(), argList.toArray(), new RowMapper<ResistRcpMapping>() {

                @Override
                public ResistRcpMapping mapRow(ResultSet rs, int rowNum) throws SQLException {
                    ResistRcpMapping mapping = new ResistRcpMapping();
                    mapping.setTransId(rs.getString("TRANS_ID"));
                    mapping.setResistNo(rs.getString("RESIST_NO"));
                    mapping.setRecipe(rs.getString("RECIPE"));
                    mapping.setTransPerformedBy(rs.getString("TRANS_PERFORMED_BY"));
                    String startTimestamp = rs.getString("TRANS_START_TIMESTAMP");
                    if (StringUtils.isNotEmptyTrim(startTimestamp)) {
                        mapping.setTransTime(DateUtils.parse(startTimestamp, DateUtils.DATE_FORMAT4DATE));
                    }
                    String oldRecipe = rs.getString("OLD_RECIPE");
                    mapping.setOldRecipe(StringUtils.isNotBlank(oldRecipe) ? oldRecipe : StringUtils.EMPTY);
                    return mapping;
                }
            });
        }

        @Override
        public void modifyResistRcpMapping(ResistRcpMapping mapping) {
            String sql = "UPDATE " + DataBaseNames.RESIST_RCP_MAPPING + " SET RECIPE = ?, " +
                    "MODIFY_BY = ?, MODIFY_TIME = sysdate " + " WHERE RESIST_NO = ? AND " + "RECIPE = ? ";
            jdbcTemplate
                    .update(sql, mapping.getRecipe(), mapping.getModifyBy(), mapping.getResistNo(), mapping.getOldRecipe());
        }

    }