package com.its.vds.service;

import com.its.app.AppUtils;
import com.its.app.utils.Elapsed;
import com.its.vds.dao.mapper.VdsStatMapper;
import com.its.vds.entity.VdsStatTime;
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();
        VdsStatTime statTime = new VdsStatTime(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_STAT_15M(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        VdsStatTime statTime = new VdsStatTime(statDt, fromDt, toDt);
        log.info("CRT_TB_VDS_DTCT_STAT_15M :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        int result = this.statMapper.CRT_TB_VDS_DTCT_STAT_15M(statTime);
        log.info("CRT_TB_VDS_DTCT_STAT_15M :: ..end. [{}], {} ms. {} EA.. {}", statTime.toString(), elapsed.milliSeconds(), result, Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

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

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

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

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

}
