package com.its.dsrc.service;

import com.its.app.AppUtils;
import com.its.app.utils.Elapsed;
import com.its.dsrc.dao.mapper.RseStatMapper;
import com.its.dsrc.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 RseStatMapper statMapper;
    private final String logKey = "statistics";

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

    @PreDestroy
    public void destroyService() {
    }

    @Async("statisticsExecutor")
    public void CRT_TB_RSE_OBU_CLCT_STAT_HH(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_RSE_OBU_CLCT_STAT_HH :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        this.statMapper.CRT_TB_RSE_OBU_CLCT_STAT_HH(statTime);
        log.info("CRT_TB_RSE_OBU_CLCT_STAT_HH :: ..end. [{}], {} ms. {}", statTime.toString(), elapsed.milliSeconds(), Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    @Async("statisticsExecutor")
    public void CRT_TB_RSE_OBU_CLCT_STAT_DD(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_RSE_OBU_CLCT_STAT_DD :: start. [{}], {}", statTime.toString(), Thread.currentThread().getName());
        this.statMapper.CRT_TB_RSE_OBU_CLCT_STAT_DD(statTime);
        log.info("CRT_TB_RSE_OBU_CLCT_STAT_DD :: ..end. [{}], {} ms. {}", statTime.toString(), elapsed.milliSeconds(), Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

}
