# EntityCounterTypeDaoImpl.java

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

    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.jdbc.mapper.RowMapper;
    import com.mycim.framework.utils.MiscUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.pms.dao.EntityCounterTypeDao;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.DataBaseNames;
    import com.mycim.valueobject.ems.EntityCounterType;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.support.rowset.SqlRowSet;
    import org.springframework.stereotype.Repository;

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

    /**
     * EntityCounterType
     *
     * @author pinyan.song
     * @version 6.0.0
     * @date 2019-11-15
     **/
    @Repository
    public class EntityCounterTypeDaoImpl implements EntityCounterTypeDao {

        @Autowired
        private JdbcTemplate jdbcTemplate;

        @Override
        public List<EntityCounterType> getEntityCounterTypesOfEntity(long entityRrn) {
            String sql = "SELECT ENTITY_RRN,EVENT_RRN,RECIPE_RRN," + "COUNTER_TYPE,QUANTITY,BASIS_CODE" + " FROM " +
                    DataBaseNames.ENTITY_COUNTER + " WHERE ENTITY_RRN = " + entityRrn +
                    " ORDER BY EVENT_RRN,RECIPE_RRN,COUNTER_TYPE";

            return jdbcTemplate.query(sql, new RowMapper<EntityCounterType>() {
                @Override
                public EntityCounterType mapRow(ResultSet rs, int rowNum) throws SQLException {
                    EntityCounterType entityCounterType = new EntityCounterType();
                    entityCounterType.setEventRrn(rs.getLong("EVENT_RRN"));
                    entityCounterType.setRecipeRrn(rs.getLong("RECIPE_RRN"));
                    entityCounterType.setCounterType(rs.getString("COUNTER_TYPE"));
                    entityCounterType.setQuantity(rs.getLong("QUANTITY"));
                    entityCounterType.setBasisCode(rs.getString("BASIS_CODE"));
                    entityCounterType.setSequenceNumber((long) (rowNum + 1));

                    return entityCounterType;
                }
            });

        }

        @Override
        public long insertEntityCounterType(EntityCounterType entityCounterType, TransactionLog transactionLog) {
            long instanceRrn = entityCounterType.getEntityRrn();

            String sql = "SELECT * FROM " + DataBaseNames.ENTITY_COUNTER + " WHERE ENTITY_RRN = " + instanceRrn + " AND " +
                    "EVENT_RRN = " + MiscUtils.parseSQL(entityCounterType.getEventRrn()) + " AND RECIPE_RRN = " +
                    MiscUtils.parseSQL(entityCounterType.getRecipeRrn()) + " AND COUNTER_TYPE = " +
                    MiscUtils.parseSQL(entityCounterType.getCounterType());

            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);

            if (rs.next()) {
                return 0;
            }
            Object[] obj = {entityCounterType.getEventRrn(), entityCounterType.getRecipeRrn(),
                    entityCounterType.getCounterType(), entityCounterType.getQuantity(), entityCounterType.getBasisCode()};

            // insert into entityCounter table
            sql = "INSERT INTO " + DataBaseNames.ENTITY_COUNTER + " (ENTITY_RRN,EVENT_RRN,RECIPE_RRN,COUNTER_TYPE," +
                    "QUANTITY,BASIS_CODE)" + " VALUES( " + instanceRrn + "," + MiscUtils.parseSQL(obj) + ")";

            jdbcTemplate.update(sql);

            insertEntityCounterTypeHistory(entityCounterType, transactionLog.getTransRrn());
            return instanceRrn;
        }

        @Override
        public void updateEntityCounterType(EntityCounterType entityCounterType, TransactionLog transactionLog) {
            insertEntityCounterTypeHistory(entityCounterType, transactionLog.getTransRrn());
            String sql = " UPDATE " + DataBaseNames.ENTITY_COUNTER + " SET BASIS_CODE =  " +
                    MiscUtils.parseSQL(entityCounterType.getBasisCode()) + "," + " QUANTITY = " +
                    MiscUtils.parseSQL(entityCounterType.getQuantity()) + " WHERE ENTITY_RRN = " +
                    entityCounterType.getEntityRrn() + " AND " + "EVENT_RRN = " + entityCounterType.getEventRrn() +
                    " AND RECIPE_RRN = " + entityCounterType.getRecipeRrn() + " AND COUNTER_TYPE = " +
                    MiscUtils.parseSQL(entityCounterType.getCounterType());
            jdbcTemplate.update(sql);
        }

        @Override
        public void deleteEntityCounterType(EntityCounterType entityCounterType, TransactionLog transactionLog) {
            insertEntityCounterTypeHistory(entityCounterType, transactionLog.getTransRrn());
            String sql = "DELETE FROM " + DataBaseNames.ENTITY_COUNTER + " WHERE ENTITY_RRN = " +
                    entityCounterType.getEntityRrn() + " AND EVENT_RRN  = " + entityCounterType.getEventRrn() +
                    " AND RECIPE_RRN = " + NumberUtils.toLong(entityCounterType.getRecipeRrn().toString()) +
                    " AND COUNTER_TYPE = " + MiscUtils.parseSQL(entityCounterType.getCounterType());
            jdbcTemplate.update(sql);
        }

        private void insertEntityCounterTypeHistory(EntityCounterType entityCounterType, long transRrn) {
            String sql = "INSERT INTO " + DataBaseNames.ENTITY_COUNTER_HISTORY + " (ENTITY_RRN,EVENT_RRN,RECIPE_RRN," +
                    "COUNTER_TYPE,QUANTITY,BASIS_CODE,TRANS_RRN) " + "SELECT ENTITY_RRN,EVENT_RRN,RECIPE_RRN," +
                    "COUNTER_TYPE,QUANTITY,BASIS_CODE,? FROM " + DataBaseNames.ENTITY_COUNTER + " WHERE ENTITY_RRN=? and " +
                    "EVENT_RRN=? and RECIPE_RRN=? and COUNTER_TYPE=?";

            jdbcTemplate.update(sql, transRrn, entityCounterType.getEntityRrn(), entityCounterType.getEventRrn(),
                                entityCounterType.getRecipeRrn(), entityCounterType.getCounterType());

        }

    }