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

import com.its.app.utils.Elapsed;
import com.its.pis.dao.mapper.BatchDaoService;
import com.its.pis.entity.TbVdsCtlrStts;
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 VdsCtlrDao extends BatchDaoService {

    public VdsCtlrDao(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
        this.serviceName = "VdsCtlrDao";
        this.mapperName  = "";//com.its.vds.dao.mapper.VdsCtlrMapper.";
    }

    public List<HashMap<String, Object>> getSttsList(List<TbVdsCtlrStts> req) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        req.forEach(obj -> {
            HashMap<String, Object> param = new HashMap<>();
            param.put("VDS_CTLR_NMBR",     obj.getVDS_CTLR_NMBR());
            param.put("UPDT_DT",           obj.getUPDT_DT());
            param.put("CMNC_STTS_CD",      obj.getCMNC_STTS_CD());
            param.put("PRNT_PWER_STTS_CD", obj.getPRNT_PWER_STTS_CD());
            param.put("CBOX_DOOR_STTS_CD", obj.getCBOX_DOOR_STTS_CD());
            param.put("FAN_STTS_CD",       obj.getFAN_STTS_CD());
            param.put("HETR_STTS_CD",      obj.getHETR_STTS_CD());
            param.put("SUB_CTLR_VAL",      obj.getSUB_CTLR_VAL());
            param.put("CBOX_TMPR",         obj.getCBOX_TMPR());
            param.put("INPT_VOLT",         obj.getINPT_VOLT());
            param.put("PRNT_VOLT",         obj.getPRNT_VOLT());
            lists.add(param);
        });
        return lists;
    }

    public int updateStts(List<TbVdsCtlrStts> 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 + "batchUpdateVdsCtlrStts";
            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<TbVdsCtlrStts> 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 + "batchInsertVdsCtlrSttsHs";
            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;
    }

}
