package com.its.pis.service;

import com.its.app.AppUtils;
import com.its.app.utils.Elapsed;
import com.its.pis.dao.mapper.VdsStatMapper;
import com.its.pis.vo.voStatisticsTime;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Slf4j
@Service
@Transactional(rollbackFor = {Exception.class})
public class StatisticsServices {

    private VdsStatMapper statMapper;
    private final String logKey = "statistics";

    @PostConstruct
    private void init() {
        this.statMapper = (VdsStatMapper) AppUtils.getBean(VdsStatMapper.class);
    }

    @PreDestroy
    public void destroyService() {
    }

    @Async("statisticsExecutor")
    public void CRT_TB_VDS_DTCT_HS(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_HS :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_HS(statTime);
        log.info("CRT_TB_VDS_DTCT_HS :: ..end. [{}], {} ms. {} EA. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    @Async("statisticsExecutor")
    public void CRT_TB_VDS_DTCT_15M_STAT(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_15M_STAT :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_15M_STAT(statTime);
        log.info("CRT_TB_VDS_DTCT_15M_STAT :: ..end. [{}], {} ms. {} EA.. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    @Async("statisticsExecutor")
    public void CRT_TB_VDS_DTCT_HH_STAT(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_HH_STAT :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_HH_STAT(statTime);
        log.info("CRT_TB_VDS_DTCT_HH_STAT :: ..end. [{}], {} ms. {} EA. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    @Async("statisticsExecutor")
    public void CRT_TB_VDS_DTCT_DD_STAT(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_DD_STAT :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_DD_STAT(statTime);
        log.info("CRT_TB_VDS_DTCT_DD_STAT :: ..end. [{}], {} ms. {} EA. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    @Async("statisticsExecutor")
    public void CRT_TB_VDS_DTCT_MN_STAT(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_MN_STAT :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_MN_STAT(statTime);
        log.info("CRT_TB_VDS_DTCT_MN_STAT :: ..end. [{}], {} ms. {} EA. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

//    @Async("statisticsExecutor")
//    public void CRT_TB_VDS_DTCT_YY_STAT(String statDt, String fromDt, String toDt) {
//        MDC.put("id", logKey);
//        Elapsed elapsed = new Elapsed();
//        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
//        log.info("CRT_TB_VDS_DTCT_YY_STAT :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
//        int result = this.statMapper.CRT_TB_VDS_DTCT_YY_STAT(statTime);
//        log.info("CRT_TB_VDS_DTCT_YY_STAT :: ..end. [{}], {} ms. {} EA. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
//        MDC.remove(logKey);
//        MDC.clear();
//    }

}
