package com.its.dsrc.dao.mapper.batch;

import com.its.app.utils.Elapsed;
import com.its.dsrc.dao.mapper.BatchDaoService;
import com.its.dsrc.entity.TbRseCtlrStts;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

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

@Slf4j
@Repository
public class RseCtlrDao extends BatchDaoService {

    public RseCtlrDao(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
        this.serviceName = "RseCtlrDao";
        this.mapperName  = "";
    }

    public List<HashMap<String, Object>> getSttsList(List<TbRseCtlrStts> req) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        req.forEach(obj -> {
            HashMap<String, Object> param = new HashMap<>();
            param.put("RSE_CTLR_NMBR",  obj.getRSE_CTLR_NMBR());
            param.put("UPDT_DT",        obj.getUPDT_DT());
            param.put("CMNC_STTS_CD",   obj.getCMNC_STTS_CD());
            param.put("CNTL_DEVC_STTS", obj.getCNTL_DEVC_STTS());
            param.put("ATN_1_STTS",     obj.getATN_1_STTS());
            param.put("ATN_2_STTS",     obj.getATN_2_STTS());
            param.put("ATN_3_STTS",     obj.getATN_3_STTS());
            param.put("ATN_4_STTS",     obj.getATN_4_STTS());
            param.put("ATN_1_MTNS",     obj.getATN_1_MTNS());
            param.put("ATN_2_MTNS",     obj.getATN_2_MTNS());
            param.put("ATN_3_MTNS",     obj.getATN_3_MTNS());
            param.put("ATN_4_MTNS",     obj.getATN_4_MTNS());
            param.put("CBOX_DOOR_STTS_CD",  obj.getCBOX_DOOR_STTS_CD());
            param.put("FRONT_DOOR_STTS_CD", obj.getFRONT_DOOR_STTS_CD());
            param.put("BACK_DOOR_STTS_CD",  obj.getBACK_DOOR_STTS_CD());
            param.put("FAN_STTS_CD",        obj.getFAN_STTS_CD());
            param.put("HETR_STTS_CD",       obj.getHETR_STTS_CD());
            param.put("RTU_STTS_CD",        obj.getRTU_STTS_CD());
            param.put("CBOX_TMPR",          obj.getCBOX_TMPR());
            param.put("CBOX_HMDT",          obj.getCBOX_HMDT());

            lists.add(param);
        });
        return lists;
    }

    public int updateStts(List<TbRseCtlrStts> req, boolean isHistory) {
        log.info("{}.updateStts: START. {} EA. History {}", this.serviceName, req.size(), isHistory);
        Elapsed elapsed = new Elapsed();
        int total = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);

            this.mapper = this.mapperName + "batchUpdateRseCtlrStts";
            total += updateBatch(this.mapper, getSttsList(req));

            sqlSession.commit();
        } catch(Exception e) {
            log.error("updateStts: Exception, {}, {}", req, e.getMessage());
        }
        finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        log.info("{}.updateStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

    public int insertStts(List<TbRseCtlrStts> req) {
        log.info("{}.insertStts: START. {} EA.", this.serviceName, req.size());
        Elapsed elapsed = new Elapsed();
        int total = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);

            this.mapper = this.mapperName + "batchInsertRseCtlrSttsHs";
            total += insertBatch(this.mapper, getSttsList(req));

            sqlSession.commit();
        } catch(Exception e) {
            log.error("insertStts: Exception, {}, {}", req, e.getMessage());
        }
        finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        log.info("{}.insertStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

}
