# ResequenceContextValueManagerDAOImpl.java

    package com.mycim.server.ctx.exec.dao.impl;

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.jdbc.mapper.RowMapper;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.server.ctx.exec.dao.ResequenceContextValueManagerDAO;
    import com.mycim.valueobject.prp.ResequenceContextValue;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

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

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/1
     **/
    @Repository
    public class ResequenceContextValueManagerDAOImpl implements ResequenceContextValueManagerDAO {

        @Autowired
        private JdbcTemplate jdbcTemplate;

        @Override
        public List<Map<String, Object>> flowSeqList(Long contextRrn, Long processRrn, Integer processVersion,
                                                     Long routeRrn, Long operationRrn, String status) {
            String sql = "SELECT RESULT_VALUE1,RESULT_VALUE2,status,CONTEXT_KEY3,CONTEXT_KEY4 from " + "CONTEXT_VALUE " +
                    "WHERE CONTEXT_RRN=? AND CONTEXT_KEY1=?";
            if (processVersion != null && processVersion != 0) {
                sql += " and CONTEXT_KEY2='" + processVersion + "'";
            }
            if (routeRrn != null && routeRrn != 0) {
                sql += " and CONTEXT_KEY3='" + routeRrn + "'";
            }
            if (operationRrn != null && operationRrn != 0) {
                sql += " and CONTEXT_KEY4='" + operationRrn + "'";
            }

            if (StringUtils.isNotBlank(status)) {
                sql += " and status='" + status + "'";
            }
            String processRrnStr = processRrn == null ? " " : processRrn.toString();
            return jdbcTemplate
                    .query(sql, new Object[]{contextRrn, processRrnStr}, (RowMapper<Map<String, Object>>) (rs, rowNum) -> {
                        Map<String, Object> m = new HashMap<>(5);
                        m.put("flowSeq", rs.getString("RESULT_VALUE1"));
                        m.put("reworkFlag", rs.getString("RESULT_VALUE2"));
                        m.put("flowSeqStatus", rs.getString("status"));
                        m.put("routeRrn", rs.getString("CONTEXT_KEY3"));
                        m.put("operationRrn", rs.getString("CONTEXT_KEY4"));
                        return m;
                    });
        }

        @Override
        public boolean isActiveFlowSeqForLastVersion(Long contextRrn, Long processRrn, Integer processVersion,
                                                     Long routeRrn, Long operationRrn, String flowSeq) {
            String sql = "SELECT COUNT(context_rrn) FROM CONTEXT_VALUE WHERE context_rrn=? AND CONTEXT_KEY1 =? AND " +
                    "context_key2=? " + " AND context_key3=? AND context_key4=? AND RESULT_VALUE1=? AND status='ACTIVE'";

            Object[] args = new Object[]{contextRrn, StringUtils.toString(processRrn), StringUtils.toString(processVersion),
                                         StringUtils.toString(routeRrn), StringUtils.toString(operationRrn), flowSeq};
            Long conut = jdbcTemplate.queryForObjectWithNull(sql, args, Long.class);

            return conut != null && conut > 0 ? true : false;
        }

        @Override
        public String getFlowSeq(ResequenceContextValue ctx) {
            String sql = "SELECT RESULT_VALUE1 FROM CONTEXT_VALUE WHERE CONTEXT_RRN=? " +
                    " AND CONTEXT_KEY1=? AND CONTEXT_KEY2=? AND CONTEXT_KEY3=? AND CONTEXT_KEY4=?";
            return jdbcTemplate.queryForObjectWithNull(sql, new Object[]{ctx.getContextRrn(), ctx.getContextKey1(),
                                                                         ctx.getContextKey2(), ctx.getContextKey3(),
                                                                         ctx.getContextKey4()}, String.class);
        }

    }