# ArgRecipeQueryDAOImpl.java

    package com.mycim.server.wip.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.collections.MapUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.wip.dao.ArgRecipeQueryDAO;
    import com.mycim.valueobject.prp.ArgRecipeInfo;
    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.HashMap;
    import java.util.List;
    import java.util.Map;

    /**
     * @author finatice.yang
     * @date 2021/7/2
     **/
    @Repository
    public class ArgRecipeQueryDAOImpl implements ArgRecipeQueryDAO {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public List<ArgRecipeInfo> getArgInfoList(ArgRecipeInfo condition) {
            String sql = "SELECT ARG.EQPT_RRN,ARG.EQPT_ID,ARG.FLOW_RECIPE_ID,ARG.SEQUENCE," +
                    "ARG.WAFER_COUNT_LOWER,ARG.WAFER_COUNT_UPPER,ARG.ARG_RECIPE_ID" +
                    " FROM ARG_RECIPE ARG WHERE 1=1 ";

            List<Object> argList = new ArrayList<Object>();
            if (StringUtils.isNotBlank(condition.getEqptId())) {
                sql += " AND ARG.EQPT_ID LIKE ? ";
                argList.add(condition.getEqptId());
            }
            if (StringUtils.isNotBlank(condition.getFlowRecipeId())) {
                sql += " AND ARG.FLOW_RECIPE_ID LIKE ? ";
                argList.add(condition.getFlowRecipeId());
            }
            if (condition.getEqptRrn() != null) {
                sql += " AND ARG.EQPT_RRN = ? ";
                argList.add(condition.getEqptRrn());
            }

            List<ArgRecipeInfo> list = jdbcTemplate.query(sql, argList.toArray(), new RowMapper<ArgRecipeInfo>() {

                @Override
                public ArgRecipeInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
                    ArgRecipeInfo argInfo = new ArgRecipeInfo();
                    argInfo.setEqptId(rs.getString("EQPT_ID"));
                    argInfo.setEqptRrn(rs.getLong("EQPT_RRN"));
                    argInfo.setFlowRecipeId(rs.getString("FLOW_RECIPE_ID"));
                    argInfo.setSequence(rs.getLong("SEQUENCE"));
                    argInfo.setLowerWaferCount(
                            StringUtils.isBlank(rs.getString("WAFER_COUNT_LOWER")) ? null : rs.getInt("WAFER_COUNT_LOWER"));
                    argInfo.setUpperWaferCount(
                            StringUtils.isBlank(rs.getString("WAFER_COUNT_UPPER")) ? null : rs.getInt("WAFER_COUNT_UPPER"));
                    argInfo.setArgRecipeId(rs.getString("ARG_RECIPE_ID"));
                    String waferCountRange = "QTY";
                    if (argInfo.getLowerWaferCount() != null) {
                        waferCountRange = argInfo.getLowerWaferCount() + "&lt;" + waferCountRange;
                    }
                    if (argInfo.getUpperWaferCount() != null) {
                        waferCountRange = waferCountRange + "&lt;=" + argInfo.getUpperWaferCount();
                    }
                    argInfo.setWaferCountRange(waferCountRange);
                    return argInfo;
                }

            });

            return list;
        }

        @Override
        public Page getArgInfoListHistory(ArgRecipeInfo condition, Page page) {

            String sql = "SELECT TRANS_RRN,TRANS_ID,TRANS_TIME,TRANS_USER_ID,EQPT_ID,EQPT_RRN," +
                    "FLOW_RECIPE_ID,SEQUENCE,WAFER_COUNT_LOWER,WAFER_COUNT_UPPER,ARG_RECIPE_ID FROM ARG_RECIPE_H " +
                    "WHERE 1=1";

            List<Object> argList = new ArrayList<Object>();
            if (StringUtils.isNotBlank(condition.getEqptId())) {
                sql += " AND EQPT_ID LIKE ?";
                argList.add(condition.getEqptId());
            }
            if (StringUtils.isNotBlank(condition.getFlowRecipeId())) {
                sql += " AND FLOW_RECIPE_ID LIKE ?";
                argList.add(condition.getFlowRecipeId());
            }

            sql += " ORDER BY TRANS_TIME DESC";

            page = jdbcTemplate.queryForPage(page, sql, argList.toArray(), new RowMapper<Map<String, Object>>() {

                @Override
                public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("eqptId", rs.getString("EQPT_ID"));
                    map.put("flowRecipeId", rs.getString("FLOW_RECIPE_ID"));
                    map.put("transId", rs.getString("TRANS_ID"));

                    Integer lowerWaferCount = StringUtils.isBlank(rs.getString("WAFER_COUNT_LOWER")) ? null : rs
                            .getInt("WAFER_COUNT_LOWER");
                    Integer upperWaferCount = StringUtils.isBlank(rs.getString("WAFER_COUNT_UPPER")) ? null : rs
                            .getInt("WAFER_COUNT_UPPER");

                    map.put("argRecipeId", rs.getString("ARG_RECIPE_ID"));
                    String waferCountRange = "QTY";
                    if (lowerWaferCount != null) {
                        waferCountRange = lowerWaferCount + "&lt;" + waferCountRange;
                    }
                    if (upperWaferCount != null) {
                        waferCountRange = waferCountRange + "&lt;=" + upperWaferCount;
                    }
                    map.put("waferCountRange", waferCountRange);
                    map.put("transEndTimestamp", DateUtils.formatDate(rs.getTimestamp("TRANS_TIME")));
                    map.put("transPerformedBy", rs.getString("TRANS_USER_ID"));
                    return map;
                }

            });

            return page;
        }

    }