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

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

    public List<HashMap<String, Object>> getClctList(List<voVdsDtctClct> req) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        req.forEach(obj -> {
            HashMap<String, Object> param = new HashMap<>();
            param.put("VDS_DTCT_NMBR",  obj.getVDS_DTCT_NMBR());
            param.put("CLCT_DT",        obj.getCLCT_DT());
            param.put("SYST_KIND_DVSN", obj.getSYST_KIND_DVSN());
            param.put("TFVL",           obj.getTFVL());
            param.put("SPED",           obj.getSPED());
            param.put("AVRG_OCPY_RATE", obj.getAVRG_OCPY_RATE());
            param.put("AVRG_LNGT",      obj.getAVRG_LNGT());
            param.put("HDWY",           obj.getHDWY());
            param.put("SPCE_OCPY_RATE", obj.getSPCE_OCPY_RATE());
            param.put("SPCE_AVRG_SPED", obj.getSPCE_AVRG_SPED());

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

    public int updateClct(List<voVdsDtctClct> 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 + "batchUpdateVdsDtctClctPnst";
            total += updateBatch(this.mapper, getClctList(req));

            sqlSession.commit();
        } catch(Exception e) {
            log.error("updateClct: 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 insertClct(List<voVdsDtctClct> 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 + "batchInsertVdsDtctClct";
            total += insertBatch(this.mapper, getClctList(req));

            sqlSession.commit();
        } catch(Exception e) {
            log.error("insertClct: Exception, {}, {}", req, e.getMessage());
        }
        finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        log.info("{}.insertStts: ..END. {} EA. {} ms.", this.serviceName, total, elapsed.milliSeconds());
        return total;
    }

}
