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

import com.its.app.utils.Elapsed;
import com.its.vms.dao.mapper.BatchDaoService;
import com.its.vms.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 VmsCtlrDao extends BatchDaoService {

    public VmsCtlrDao(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
        this.serviceName = "VmsCtlrDao";//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());

            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;
    }

}
