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

import com.utic.center.common.annotation.ProcessingElapsed;
import com.utic.center.common.service.AbstractProcessService;
import com.utic.center.utic.traf.server.dao.repository.dwdb.DwdbTrafficRepository;
import com.utic.center.utic.traf.server.dao.repository.utic.UticTrafficRepository;
import com.utic.center.utic.traf.server.dto.DataCountDto;
import com.utic.center.utic.traf.server.dto.ViewTextDto;
import com.utic.center.utic.traf.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 LinkTrafPrcsAfterService implements AbstractProcessService {

    private final UticTrafficRepository uticRepo;
    private final DwdbTrafficRepository dwdbRepo;

    @ProcessingElapsed(type="PRCS", name="교통정보 준비 작업", starting = false, endLine = true)
    @Override
    public boolean processing() {

        this.uticRepo.truncateTrafficLoc();         // 지역 교통정보 테이블 초기화
        this.uticRepo.truncateTrafficCenter(ApplicationRepository.nextTrafficCenter); // 작업 할 소통정보 테이블 초기화
        this.uticRepo.disableIndexes(ApplicationRepository.nextTrafficCenter, "1");
        this.uticRepo.disableIndexes(ApplicationRepository.nextTrafficCenter, "2");
        this.uticRepo.disablePrimaryKey(ApplicationRepository.nextTrafficCenter);

        // DWDB TRAFFIC_CENTER_MM(TRAFFIC_CENTER_HIST) 초기화
        // 다음달 테이블을 truncate 한다.
        ViewTextDto nextMonth = this.uticRepo.findNextMonth();
        if (nextMonth != null) {
            DataCountDto dataCheck = this.dwdbRepo.findTrafficCenterHistDataExists(nextMonth.getViewText());
            if (dataCheck != null) {
                if (dataCheck.getDataCount() > 0) {
                    this.dwdbRepo.truncateTrafficCenterHist(nextMonth.getViewText());
                }
            }
        }
        return true;
    }
}
