package com.utic.center.utic.stat.server.service;

import com.utic.center.common.annotation.ProcessingElapsed;
import com.utic.center.common.service.AbstractProcessService;
import com.utic.center.common.utils.LogUtils;
import com.utic.center.utic.stat.server.dao.repository.UticStat05MinStatRepository;
import com.utic.center.utic.stat.server.dto.CodeValueDto;
import com.utic.center.utic.stat.server.repository.ApplicationRepository;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Getter
@RequiredArgsConstructor
@Service
public class UticStat05MinStatService implements AbstractProcessService {

    private final UticStatParamService paramService;
    private final UticStat05MinStatRepository statRepo;
    private final UticStatPatternService patternService;

    @ProcessingElapsed(type="05MIN", name="정주기 5분 통계생성", starting = false)
    @Override
    public boolean processing() {

        String regDate = ApplicationRepository.regDate;

        log.info("[INF] {}: regDate[{}]", LogUtils.elapsedLog("05분 통계시각"), regDate);

        CodeValueDto codeValue0 = this.paramService.getCodeValue(UticStatParamService.CodeValueKey.CODE_00);
        CodeValueDto codeValue1 = this.paramService.getCodeValue(UticStatParamService.CodeValueKey.CODE_01);
        CodeValueDto codeValue2 = this.paramService.getCodeValue(UticStatParamService.CodeValueKey.CODE_02);
        CodeValueDto codeValue3 = this.paramService.getCodeValue(UticStatParamService.CodeValueKey.CODE_03);
        CodeValueDto codeValue4 = this.paramService.getCodeValue(UticStatParamService.CodeValueKey.CODE_04);

        // CENTER_LOG 센터 수집이력 저장
        this.statRepo.insertCenterLog(regDate);

        // STAT_TRAFFIC_LOG 센터 수집이력 저장
        this.statRepo.insertStatTrafficLog(regDate);

        // FILTER_LOG 센터 수집이력 저장
        this.statRepo.insertFilterLog(regDate,
                codeValue0.getCodeValue(),
                codeValue1.getCodeValue(),
                codeValue2.getCodeValue(),
                codeValue3.getCodeValue(),
                codeValue4.getCodeValue());

        // LINK_FILTER_LOG 센터 수집이력 저장
        this.statRepo.insertLinkFilterLog(codeValue4.getCodeValue());

        // STAT_5MIN_CENTER 통계정보
        // STAT_5MIN_CENTER 1레벨 5분통계 저장
        this.statRepo.insertStat5MinCenter(regDate, codeValue4.getCodeValue());

        // STAT_FILTER_LOG 필터가공정보 저장
        this.statRepo.insertStatFilterLog(regDate);

        // STAT_5MIN_CENTER 2,3,4레벨 5분통계 저장
        for (int level = 2; level <= 4; level++) {
            this.statRepo.insertStat5MinCenterLevel(String.valueOf(level), regDate);
        }

        // STAT_5MIN_CENTER 통계정보 여행시간 UPDATE
        this.statRepo.updateStat5MinCenterTravelTime(regDate);

        // 05분 패턴 업데이트
        this.patternService.patternUpdate(regDate, true);

        // STAT_5MIN_EXW 통계정보
//        this.statRepo.insertStat5MinExw(regDate, codeValue4.getCodeValue());
//        for (int level = 2; level <= 4; level++) {
//            this.statRepo.insertStat5MinExwLevel(String.valueOf(level), regDate);
//        }
        // STAT_5MIN_EXW 통계정보 여행시간 UPDATE
//        this.statRepo.updateStat5MinExwTravelTime(regDate);

        // STAT_5MIN_LOC 통계정보
        this.statRepo.insertStat5MinLoc(regDate, codeValue4.getCodeValue());
        for (int level = 2; level <= 4; level++) {
            this.statRepo.insertStat5MinLocLevel(String.valueOf(level), regDate);
        }
        // STAT_5MIN_LOC 통계정보 여행시간 UPDATE
        this.statRepo.updateStat5MinLocTravelTime(regDate);

        return true;
    }

}
