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

import com.its.app.utils.Elapsed;
import com.its.bis.dao.mapper.BatchDaoService;
import com.its.bis.entity.TbVmsCtlrStts;
import lombok.extern.slf4j.Slf4j;
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 NodeLinkDao extends BatchDaoService {

    public NodeLinkDao(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
        this.serviceName = "NodeLinkDao";//this.getClass().getName();
    }

    public List<HashMap<String, Object>> getSttsList(List<TbVmsCtlrStts> req) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        req.forEach(obj -> {
            HashMap<String, Object> param = new HashMap<>();

            param.put("vmsCtlrNmbr",    obj.getVmsCtlrNmbr());
            param.put("updtDt",         obj.getUpdtDt());
            param.put("cmncSttsCd",     obj.getCmncSttsCd());
            param.put("pwerSttsCd",     obj.getPwerSttsCd());
            param.put("modlSttsCd",     obj.getModlSttsCd());
            param.put("cboxDoorSttsCd", obj.getCboxDoorSttsCd());
            param.put("fanSttsCd",      obj.getFanSttsCd());
            param.put("hetrSttsCd",     obj.getHetrSttsCd());
            param.put("cboxTmpr",       obj.getCboxTmpr());
            param.put("brghVal",        obj.getBrghVal());
            param.put("commSttsCd",     obj.getCommSttsCd());
            param.put("modlStts",       obj.getModlStts());
            param.put("pwerStts",       obj.getPwerStts());
            param.put("cboxHum",        obj.getCboxHum());
            param.put("cboxFanSttsCd",  obj.getCboxFanSttsCd());
            param.put("cboxHetrSttsCd", obj.getCboxHetrSttsCd());
            param.put("tmpr",           obj.getTmpr());
            param.put("hum",            obj.getHum());
            param.put("doorSttsCd",     obj.getDoorSttsCd());
            param.put("brghMode",       obj.getBrghMode());
            param.put("brghCurr",       obj.getBrghCurr());
            param.put("brghWeek",       obj.getBrghWeek());
            param.put("brghNght",       obj.getBrghNght());
            param.put("pwerOnOffCd",    obj.getPwerOnOffCd());

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

    public int updateStts(List<TbVmsCtlrStts> req, boolean isHistory) {
        log.info("{}.updateStts: START. {} EA. History {}", this.serviceName, req.size(), isHistory);
        Elapsed elapsed = new Elapsed();
        int total = updateBatch("batchUpdateCtlrStts", getSttsList(req));
        log.info("{}.updateStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

    public int insertStts(List<TbVmsCtlrStts> req) {
        log.info("{}.insertStts: START. {} EA.", this.serviceName, req.size());
        Elapsed elapsed = new Elapsed();
        int total = insertBatch("batchInsertCtlrSttsHs", getSttsList(req));
        log.info("{}.insertStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

}
