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.TbPisInfrStts;
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 PisInfrDao extends BatchDaoService {

    public PisInfrDao(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
        this.serviceName = "PisInfrDao";
        this.mapperName  = "";//"com.its.pis.dao.mapper.PisInfrMapper.";
    }

    public List<HashMap<String, Object>> getSttsList(List<TbPisInfrStts> req) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        req.forEach(obj -> {
            HashMap<String, Object> param = new HashMap<>();
            param.put("PIS_NMBR",     obj.getPIS_NMBR());
            param.put("UPDT_DT",      obj.getUPDT_DT());
            param.put("CMNC_STTS_CD", obj.getCMNC_STTS_CD());
            param.put("CONN_DT",      obj.getCONN_DT());
            param.put("DIS_CONN_DT",  obj.getDIS_CONN_DT());
            lists.add(param);
        });
        return lists;
    }

    public int updateStts(List<TbPisInfrStts> 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 + "batchUpdatePisInfrStts";
            total += updateBatch(this.mapper, getSttsList(req));

            sqlSession.commit();
        } catch(Exception e) {
            log.error("updateStts: 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 insertStts(List<TbPisInfrStts> 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 + "batchInsertPisInfrSttsHs";
            total += insertBatch(this.mapper, getSttsList(req));

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

}
