package com.its.bis.service;

import com.its.app.utils.Elapsed;
import com.its.app.utils.TimeUtils;
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;
import java.util.Calendar;
import java.util.Date;

@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_RSE_OBU_CLCT_STAT_HH: start. [{}], {}", statTime, Thread.currentThread().getName());
//        this.statMapper.CRT_TB_RSE_OBU_CLCT_STAT_HH(statTime);
//        log.info("CRT_RSE_OBU_CLCT_STAT_HH: ..end. [{}], {} ms. {}", statTime, 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_RSE_OBU_CLCT_STAT_DD: start. [{}], {}", statTime, Thread.currentThread().getName());
//        this.statMapper.CRT_TB_RSE_OBU_CLCT_STAT_DD(statTime);
//        log.info("CRT_RSE_OBU_CLCT_STAT_DD: ..end. [{}], {} ms. {}", statTime, elapsed.milliSeconds(), Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

    public static int getDayWeek(Date paramDt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(paramDt);
        return cal.get(Calendar.DAY_OF_WEEK);		/* DAY_OF_WEEK 리턴값이 일요일(1), 월요일(2), 화요일(3) ~~ 토요일(7)을 반환합니다. */
    }

    public String getDayWeekCd(int week) {
        String sWeek = "DTW0";
        switch(week) {
            case 1: sWeek = "DTW7";	break;
            case 2: sWeek = "DTW1";	break;
            case 3: sWeek = "DTW2";	break;
            case 4: sWeek = "DTW3";	break;
            case 5: sWeek = "DTW4";	break;
            case 6: sWeek = "DTW5";	break;
            case 7: sWeek = "DTW6"; break;
        }
        return sWeek;
    }

    @Async("statisticsExecutor")
    public void CRT_TB_RSE_OD_STAT_HH(String statDt, String fromDt, String toDt) {
        MDC.put("id", logKey);
        Elapsed elapsed = new Elapsed();
        Date dtStat = TimeUtils.stringToDate(statDt);
        String weekCd = getDayWeekCd(getDayWeek(dtStat));
//        voStatisticsTime statTime = new voStatisticsTime(statDt, fromDt, toDt);
//        statTime.setDAY_TYPE_CD(weekCd);
//        statTime.setB_FROM_DT(TimeUtils.getTime(dtStat, TimeUtils.TYPE_PRCS_30MIN));
//        statTime.setB_TO_DT(TimeUtils.getToTime(statTime.getB_FROM_DT(), TimeUtils.TYPE_PRCS_30MIN));
//        //log.info("{}", statTime);
//        log.info("------CRT_RSE_OD_STAT_HH: start. [{}], {}", statTime, Thread.currentThread().getName());
//        this.statMapper.CRT_TB_RSE_OD_STAT_HH(statTime);
//        log.info("------CRT_RSE_OD_STAT_HH: ..end. [{}], {} ms. {}", statTime, elapsed.milliSeconds(), Thread.currentThread().getName());
        MDC.remove(logKey);
        MDC.clear();
    }

}
