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.TbVdsDtctClct;
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 VdsDtctDao extends BatchDaoService {

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

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

            param.put("vdsDtctNmbr",  obj.getVdsDtctNmbr());
            param.put("clctDt",       obj.getClctDt());
            param.put("trafClctCycl", obj.getTrafClctCycl());
            param.put("tfvl",         obj.getTfvl());
            param.put("sTfvl",        obj.getSTfvl());
            param.put("mTfvl",        obj.getMTfvl());
            param.put("lTfvl",        obj.getLTfvl());
            param.put("sped",         obj.getSped());
            param.put("avrgOcpyRate", obj.getAvrgOcpyRate());
            param.put("avrgLngt",     obj.getAvrgLngt());
            param.put("hdwy",         obj.getHdwy());
            param.put("spceOcpyRate", obj.getSpceOcpyRate());
            param.put("spceAvrgSped", obj.getSpceAvrgSped());

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

    public int updateVdsDtctClctPnst(List<TbVdsDtctClct> req) {
        log.info("{}.updateVdsDtctClctPnst: START. {} EA.", this.serviceName, req.size());
        Elapsed elapsed = new Elapsed();
        int total = updateBatch("batchUpdateVdsDtctClctPnst", getVdsDtctClct(req));
        log.info("{}.updateVdsDtctClctPnst: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

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

}
