# StatusDAOImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.jdbc.JdbcTemplate;
    import com.mycim.framework.utils.MiscUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.status.dao.StatusDAO;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.Constants;
    import com.mycim.valueobject.consts.DataBaseNames;
    import com.mycim.valueobject.ems.EntityStatus;
    import com.mycim.valueobject.ems.Event;
    import com.mycim.valueobject.table.SubEntityTable;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;

    @Repository
    public class StatusDAOImpl implements StatusDAO {

        public static final String DATE_FORMAT = "YYYY/MM/DD HH24:MI:SS";

        private static final String NUMBER_2 = "2";

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public void updateStatusEndTime(long transRrn, String statusEndTime) {
            String sql = "update transaction_log set trans_start_timestamp=to_timestamp('" + statusEndTime + "', '" +
                    DATE_FORMAT + "'),trans_end_timestamp=to_timestamp('" + statusEndTime + "', '" + DATE_FORMAT + "') " +
                    " where trans_rrn=" + transRrn;
            jdbcTemplate.update(sql);
        }

        @Override
        public void updateAllEntityStatus(Collection entityStatuss, Event event, long entityRrn, long transRrn) {
            String sql = "Update  " + DataBaseNames.ENTITY + " SET AVAILABILITY=" +
                    StringUtils.wrapWithApostropheIfMissing(event.getTargetAvailability()) + " WHERE ENTITY_RRN=" +
                    entityRrn;

            String sqlEqpt = "Update  " + DataBaseNames.EQUIPMENT + " SET AVAILABILITY=" +
                    StringUtils.wrapWithApostropheIfMissing(event.getTargetAvailability()) + " WHERE EQPT_RRN=" + entityRrn;

            String sqlH = "";
           /* if (event.getTargetAvailability() != null && NUMBER_2.equalsIgnoreCase(
                    event.getTargetAvailability())) {

                // not to update
            } else {*/
            jdbcTemplate.update(sql);
            jdbcTemplate.update(sqlEqpt);
            //  }

            Iterator it = entityStatuss.iterator();

            while (it.hasNext()) {
                EntityStatus entityStatus = (EntityStatus) it.next();

                sql = "UPDATE " + DataBaseNames.ENTITY_STATUS + " SET CURRENT_STATUS = " +
                        StringUtils.wrapWithApostropheIfMissing(entityStatus.getCurrentStatus()) +
                        ",LAST_EVENT_TRANS_RRN =" + (transRrn) + " WHERE ENTITY_RRN = " + entityStatus.getEntityRrn() +
                        " AND STATUS_GROUP_ID = " +
                        StringUtils.wrapWithApostropheIfMissing(entityStatus.getStatusGroupId());
                if (entityStatus.getCurrentStatus() != null && !"SAME".equalsIgnoreCase(entityStatus.getCurrentStatus())) {

                    Object[] obj = {entityStatus.getStatusGroupId(), entityStatus.getStatusBefore(),
                            entityStatus.getCurrentStatus()};

                    sqlH = " INSERT INTO " + DataBaseNames.ENTITY_STATUS_HISTORY + "(TRANS_RRN,STATUS_GROUP_ID," +
                            "STATUS_BEFORE,STATUS_AFTER)" + " VALUES(" + transRrn + "," + MiscUtils.parseSQL(obj) + ")";

                } else {
                    Object[] obj = {entityStatus.getStatusGroupId(), entityStatus.getStatusBefore(),
                            entityStatus.getStatusBefore()};

                    sqlH = " INSERT INTO " + DataBaseNames.ENTITY_STATUS_HISTORY + "(TRANS_RRN,STATUS_GROUP_ID," +
                            "STATUS_BEFORE,STATUS_AFTER)" + " VALUES(" + transRrn + "," + MiscUtils.parseSQL(obj) + ")";

                }

                jdbcTemplate.batchUpdate(sql, sqlH);
            }
        }

        @Override
        public void updatePodStatus(Long podRrn, Long transRrn, String status, String comments) {
            String sql = " UPDATE POD SET STATUS = ? WHERE POD_RRN=?";
            jdbcTemplate.update(sql, new Object[]{status, podRrn});
        }

        @Override
        public void updateCarrierStatus(Long carrierRrn, Long transRrn, String status, String comments) {
            String sql1 = " UPDATE CARRIER SET STATUS = ? WHERE CARRIER_RRN=?";
            jdbcTemplate.update(sql1, new Object[]{status, carrierRrn});
        }

        @Override
        public void updateDoorStatus(Long doorRrn, Long transRrn, String status, String comments) {
            String sql = " UPDATE DOOR SET STATUS = ? WHERE DOOR_RRN=?";
            jdbcTemplate.update(sql, new Object[]{status, doorRrn});
        }

        @Override
        public void updateEntityStatus(EntityStatus entityStatus, SubEntityTable set, TransactionLog tl) {
            String mainTable = set.getSubEntityTableName();
            String differentPk = set.getTableDifferentPrimaryKey();

            String sql = "UPDATE " + mainTable + " SET CURRENT_STATUS = ?, LAST_EVENT_TRANS_RRN =? ,UPDATE_USER_RRN=?, UPDATE_TIME= ? "
                    + " WHERE " + differentPk + " = ? AND STATUS_GROUP_ID = ? " ;
            jdbcTemplate.update(sql, entityStatus.getCurrentStatus(), tl.getTransRrn(), LocalContext.getUserRrn(),
                                tl.getTransStartTimestamp(), entityStatus.getEntityRrn(), entityStatus.getStatusGroupId());

        }

        @Override
        public void insertEntityStatusHistory(EntityStatus entityStatus, SubEntityTable set, TransactionLog tl) {
            String hisTable = set.getSubEntityHistoryTableName();
            String differentPk = set.getTableDifferentPrimaryKey();

            String sqlH = " INSERT INTO " + hisTable + "(TRANS_RRN,STATUS_GROUP_ID,STATUS_BEFORE,STATUS_AFTER,"
                    + "TRANS_ID,TRANS_USER_RRN,TRANS_TIME," + differentPk + ") VALUES(?,?,?,?,?,?,?,?)";
            Object[] obj;
            if (!StringUtils.equalsIgnoreCase("SAME", entityStatus.getCurrentStatus())) {
                obj = new Object[]{tl.getTransRrn(), entityStatus.getStatusGroupId(), entityStatus.getStatusBefore(), entityStatus.getCurrentStatus(),
                                tl.getTransId(), LocalContext.getUserRrn(), tl.getTransStartTimestamp(), entityStatus.getEntityRrn()};
            } else {
                obj = new Object[]{tl.getTransRrn(), entityStatus.getStatusGroupId(), entityStatus.getStatusBefore(), entityStatus.getStatusBefore(),
                                tl.getTransId(), LocalContext.getUserRrn(), tl.getTransStartTimestamp(), entityStatus.getEntityRrn()};
            }
            jdbcTemplate.update(sqlH, obj);

        }

        @Override
        public boolean checkChamberAllInTheSameState(long parentRrn, List<Long> excludeChamberRrns,String state) {
            String sql = " SELECT COUNT(ET.ENTITY_RRN) FROM ENTITY ET LEFT JOIN ENTITY_STATUS ES ON ET.ENTITY_RRN = ES.ENTITY_RRN";
            sql += " WHERE ET.PARENT_ENTITY_RRN =?  AND ES.CURRENT_STATUS !=?   ";
            if(CollectionUtils.isNotEmpty(excludeChamberRrns)){
                //基本数据类型 不牵扯 双引号直接取
                String inParam=StringUtils.join(excludeChamberRrns,",");
                sql+= String.format(" AND ES.ENTITY_RRN NOT IN (%s) ",inParam);
            }
            return jdbcTemplate.queryForObjectWithNull(sql, new Object[]{parentRrn, state}, long.class)==0;
        }

    }