# ResistStatusDaoImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.resist.dao.ResistStatusDao;
    import com.mycim.server.resist.dao.mapper.ResistStatusRowMapper;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.DataBaseNames;
    import com.mycim.valueobject.ems.ResistStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    import java.util.ArrayList;
    import java.util.List;

    /**
     * @author can.yang
     */
    @Repository
    public class ResistStatusDaoImpl implements ResistStatusDao {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void insertResistStatus(Long eventRrn, ResistStatus resistStatus, TransactionLog transactionLog) {
            StringBuilder sql = new StringBuilder("insert into ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append("( RESIST_SYS_RRN,STATUS_GROUP_ID,CURRENT_STATUS,LAST_EVENT_TRANS_RRN,CREATE_TIME,UPDATE_TIME,");
            sql.append(" CREATE_USER_RRN,UPDATE_USER_RRN,LOCK_VERSION) SELECT ?, T.STATUS_GROUP_ID STATUS_GROUP_ID, ");
            sql.append("T.TARGET_STATUS CURRENT_STATUS,?,?,?,?,?,?  FROM ");
            sql.append(DataBaseNames.EVENT_MODEL);
            sql.append(" T WHERE T.EVENT_RRN= ? ");
            List args = new ArrayList();
            args.add(resistStatus.getResistSysRrn());
            args.add(transactionLog.getTransRrn());
            args.add(transactionLog.getTransStartTimestamp());
            args.add(transactionLog.getTransStartTimestamp());
            args.add(LocalContext.getUserRrn());
            args.add(LocalContext.getUserRrn());
            args.add(NumberUtils.INTEGER_ZERO);
            args.add(eventRrn);
            jdbcTemplate.update(sql.toString(), args.toArray());
        }

        @Override
        public void insertResistStatusH(Long eventRrn, ResistStatus resistStatus, TransactionLog transactionLog) {
            StringBuilder sql = new StringBuilder("INSERT INTO ");
            sql.append(DataBaseNames.RESIST_STATUS_H);
            sql.append(" (TRANS_RRN,STATUS_GROUP_ID,STATUS_BEFORE,STATUS_AFTER,TRANS_ID,TRANS_USER_RRN,TRANS_TIME, ");
            sql.append(" RESIST_SYS_RRN) VALUES(?,?,?,?,?,?,?,?) ");
            List args = new ArrayList();
            args.add(transactionLog.getTransRrn());
            args.add(resistStatus.getStatusGroupId());
            args.add(resistStatus.getStatusBefore());
            args.add(resistStatus.getCurrentStatus());
            args.add(transactionLog.getTransId());
            args.add(LocalContext.getUserRrn());
            args.add(transactionLog.getTransStartTimestamp());
            args.add(resistStatus.getResistSysRrn());
            jdbcTemplate.update(sql.toString(), args.toArray());
        }

        @Override
        public void initInsertResistStatusH(ResistStatus resistStatus, TransactionLog transactionLog) {
            StringBuilder sql = new StringBuilder("insert into ");
            sql.append(DataBaseNames.RESIST_STATUS_H);
            sql.append(" ( TRANS_RRN,STATUS_GROUP_ID,STATUS_BEFORE,TRANS_ID,TRANS_USER_RRN,TRANS_TIME,RESIST_SYS_RRN) ");
            sql.append(" SELECT ?,STATUS_GROUP_ID,T.CURRENT_STATUS, ?, ?, ?, T.RESIST_SYS_RRN FROM ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append(" T WHERE RESIST_SYS_RRN = ? ");
            List args = new ArrayList();
            args.add(transactionLog.getTransRrn());
            args.add(transactionLog.getTransId());
            args.add(LocalContext.getUserRrn());
            args.add(transactionLog.getTransStartTimestamp());
            args.add(resistStatus.getResistSysRrn());
            jdbcTemplate.update(sql.toString(), args.toArray());
        }

        @Override
        public void updateResistStatus(ResistStatus resistStatus, TransactionLog transactionLog) {
            List args = new ArrayList();
            StringBuilder sql = new StringBuilder("UPDATE ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append(" SET CURRENT_STATUS = ?, LAST_EVENT_TRANS_RRN =? ,UPDATE_USER_RRN=?, UPDATE_TIME= ? ");
            sql.append(" WHERE RESIST_SYS_RRN = ? AND STATUS_GROUP_ID = ? ");
            args.add(resistStatus.getCurrentStatus());
            args.add(transactionLog.getTransRrn());
            args.add(LocalContext.getUserRrn());
            args.add(transactionLog.getTransStartTimestamp());
            args.add(resistStatus.getResistSysRrn());
            args.add(resistStatus.getStatusGroupId());
            jdbcTemplate.update(sql.toString(), args.toArray());

        }

        @Override
        public ResistStatus getResistStatus(ResistStatus resistStatus) {
            StringBuilder sql = new StringBuilder("SELECT ");
            sql.append(" RESIST_SYS_RRN,STATUS_GROUP_ID,CURRENT_STATUS,LAST_EVENT_TRANS_RRN,CREATE_TIME FROM ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append(" WHERE RESIST_SYS_RRN = ?");
            List args = new ArrayList();
            args.add(resistStatus.getResistSysRrn());

            return jdbcTemplate.queryForObjectWithNull(sql.toString(), args.toArray(), new ResistStatusRowMapper());
        }

        @Override
        public String getPreResistStatus(ResistStatus resistStatus) {
            StringBuilder sql = new StringBuilder("SELECT ");
            sql.append("rh.status_before FROM ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append(" rs, ");
            sql.append(DataBaseNames.RESIST_STATUS_H);
            sql.append(" rh  WHERE ");
            sql.append(" rs.RESIST_SYS_RRN = rh.RESIST_SYS_RRN  AND rs.LAST_EVENT_TRANS_RRN = rh.TRANS_RRN ");
            sql.append(" AND rs.RESIST_SYS_RRN = ? ");
            return jdbcTemplate.queryForObject(sql.toString(), new Object[]{resistStatus.getResistSysRrn()}, String.class);
        }

        @Override
        public void deleteResistStatus(long resistRrn) {
            StringBuilder sql = new StringBuilder(" DELETE FROM ");
            sql.append(DataBaseNames.RESIST_STATUS);
            sql.append(" WHERE RESIST_SYS_RRN = ?");
            List args = new ArrayList();
            args.add(resistRrn);
            jdbcTemplate.update(sql.toString(), args.toArray());
        }

    }