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.common.utils.TimeUtils;
import com.utic.center.utic.stat.server.dao.repository.UticStat04MinEtlpRepository;
import com.utic.center.utic.stat.server.dto.TrafficCenterDto;
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;

import java.time.LocalDateTime;

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

    private final UticStat04MinEtlpRepository etlpRepo;

    private String trafficMaxRegDate = "";
    private String past10Min;

    private void initialize() {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        String prcsTime = String.format("%04d%02d%02d%02d%02d00", year, month, day, hour, minute);
        this.past10Min = TimeUtils.addMinutes(prcsTime, -10);
    }

    @ProcessingElapsed(type="ETLP", name="지역 교통정보 이관 작업")
    @Override
    public boolean processing() {

        initialize();

        // RCV_LINK_TRAFFIC 교통정보 입력
        final String yyMm = this.past10Min.substring(2, 6); // 년(2)월(2)
        int result = this.etlpRepo.insertRcvLinkTraffic(yyMm, this.past10Min);
        if (result < 0) {
            ApplicationRepository.processState.setError("insertRcvLinkTraffic failed.");
        }

        ApplicationRepository.processState.setSndCnt(result);

        return true;
    }

    @ProcessingElapsed(type="ETLP", name="중앙 교통정보 이관 작업", starting = false)
    public boolean processingTrafficCenter() {

        TrafficCenterDto trafficCenterDto = this.etlpRepo.findTrafficCenter();
        if (trafficCenterDto == null) {
            return false;
        }

        if (trafficCenterDto.getDataCount() > 0 && !trafficCenterDto.getMaxRegDate().equals(this.trafficMaxRegDate)) {
            this.trafficMaxRegDate = trafficCenterDto.getMaxRegDate();

            final long start = System.currentTimeMillis();
            final String yyMm = this.trafficMaxRegDate.substring(2, 6);
            final int result = this.etlpRepo.insertTrafficCenterHist(yyMm);
            if (result < 0) {
                ApplicationRepository.processState.setError("insertTrafficCenterHist failed.");
            }
            ApplicationRepository.processState.setEtcCnt(result);
            log.info("[INF] {}: {}",
                    LogUtils.elapsedLog("TRAFFIC_CENTER_" + yyMm, result, System.currentTimeMillis()-start),
                    this.trafficMaxRegDate);
            return true;
        }
        return false;
    }
}
