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

import com.utic.center.utic.stat.server.dao.repository.dwdb.UticStatTableManagerRepository;
import com.utic.center.utic.stat.server.dto.TableExistDto;
import lombok.Getter;

@Getter
public class TrafficCenterHist extends MakeTablePartition {

    private final String tablePrefix= "TRAFFIC_CENTER_";

    public TrafficCenterHist(UticStatTableManagerRepository repo,
                             String crtMonth,
                             String commentCrtMonth) {
        super(repo, crtMonth, commentCrtMonth);
    }

    @Override
    public boolean createPartitionTable() {
        final String tableName = this.tablePrefix + crtMonth;
        return createTable(tableName, this.crtMonth, this.commentCrtMonth);
    }

    private boolean createTable(String tableName, String crtMonth, String commentCrtMonth) {
        TableExistDto existDto = this.repo.findTableExist(tableName);
        if (existDto == null) {
            return false;
        }
        if (existDto.getTableExist() > 0) {
            return true;
        }

        if (this.repo.createTrafficCenterHistTable(crtMonth) < 0) {
            return false;
        }
        if (this.repo.addTrafficCenterHistPrimaryKey(crtMonth) < 0) {
            return false;
        }
        addComments(tableName, commentCrtMonth);
        return true;
    }

    private void addComments(String tableName, String commentCrtMonth) {
        String tableComment = "제공이력정보_통합 " + commentCrtMonth;
        this.repo.addTableComment(tableName, tableComment);

        this.repo.addColumnComment(tableName, "linkid", "링크ID");
        this.repo.addColumnComment(tableName, "missvalueyn", "결측여부");
        this.repo.addColumnComment(tableName, "regdate", "등록일시");
        this.repo.addColumnComment(tableName, "linklevel", "링크레벨");
        this.repo.addColumnComment(tableName, "speed", "속도");
        this.repo.addColumnComment(tableName, "traveltime", "여행시간");
        this.repo.addColumnComment(tableName, "trafficgrade", "소통등급");
        this.repo.addColumnComment(tableName, "incidentid", "돌발ID");
        this.repo.addColumnComment(tableName, "datarestype", "데이터유형");
        this.repo.addColumnComment(tableName, "centerid", "센터ID");
        this.repo.addColumnComment(tableName, "psdmissvalueyn", "PSD결측보정여부");
        this.repo.addColumnComment(tableName, "psdspeed", "PSD속도");
        this.repo.addColumnComment(tableName, "knnspeed", "KNN속도");
        this.repo.addColumnComment(tableName, "knncnt", "KNN결측갯수");
    }

}
