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.TbVmsDsplPrst;
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 VmsManageDao extends BatchDaoService {

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

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

            param.put("vmsCtlrNmbr",       obj.getVmsCtlrNmbr());
            param.put("phase",             obj.getPhase());
            param.put("dsplDt",            obj.getDsplDt());
            param.put("vmsSchType",        obj.getVmsSchType());
            param.put("vmsSchFormType",    obj.getVmsSchFormType());
            param.put("vmsFormId",         obj.getVmsFormId());
            param.put("dsplHh",            obj.getDsplHh());
            param.put("vmsDsplMsgData",    obj.getVmsDsplMsgData());
            param.put("vmsDsplMsgImag",    obj.getVmsDsplMsgImag());
            param.put("vmsDsplMsgTxt",     obj.getVmsDsplMsgTxt());
            param.put("vmsFormDsplDrctCd", obj.getVmsFormDsplDrctCd());
            param.put("vmsFormDsplMthdCd", obj.getVmsFormDsplMthdCd());
            param.put("dnldYn",            obj.getDnldYn());

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

    public int updateVmsDsplPrst(List<TbVmsDsplPrst> req) {
        String mapperFunc = "batchUpdateVmsDsplPrst";
        log.info("{}.{}: START. {} EA.", this.serviceName, mapperFunc, req.size());
        Elapsed elapsed = new Elapsed();
        int total = updateBatch(mapperFunc, getVmsDsplList(req));
        log.info("{}.{}: ..END. {} EA. {} ms.", this.serviceName, mapperFunc, total, elapsed.milliSeconds());
        return total;
    }

    public int insertVmsDsplPrst(List<TbVmsDsplPrst> req) {
        String mapperFunc = "batchInsertVmsDsplPrst";
        log.info("{}.{}: START. {} EA.", this.serviceName, mapperFunc, req.size());
        Elapsed elapsed = new Elapsed();
        int total = insertBatch("batchInsertVmsDsplPrst", getVmsDsplList(req));
        log.info("{}.{}: ..END. {} EA. {} ms.", this.serviceName, mapperFunc, total, elapsed.milliSeconds());
        return total;
    }

}
