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

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

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

    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("vdsCtlrNmbr",     obj.getVdsCtlrNmbr());
            param.put("updtDt",          obj.getUpdtDt());
            param.put("cmncSttsCd",      obj.getCmncSttsCd());
            param.put("cboxDoorSttsCd",  obj.getCboxDoorSttsCd());
            param.put("frontDoorSttsCd", obj.getFrontDoorSttsCd());
            param.put("backDoorSttsCd",  obj.getBackDoorSttsCd());
            param.put("fanSttsCd",       obj.getFanSttsCd());
            param.put("hetrSttsCd",      obj.getHetrSttsCd());
            param.put("cboxTmpr",        obj.getCboxTmpr());
            param.put("prntPwerSttsCd",  obj.getPrntPwerSttsCd());
            param.put("inptVolt",        obj.getInptVolt());
            param.put("prntVolt",        obj.getPrntVolt());

            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 = updateBatch("batchUpdateCtlrStts", getSttsList(req));
        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 = insertBatch("batchInsertCtlrSttsHs", getSttsList(req));
        log.info("{}.insertStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

}
