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

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

@Getter
public class LinkFilterLog extends MakeTablePartition {

    private final String tablePrefix = "LINK_FILTER_LOG_";

    public LinkFilterLog(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.createLinkFilterLogTable(crtMonth) < 0) {
            return false;
        }
        if (this.repo.addLinkFilterLogPrimaryKey(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, "local_date", "저장시각");
        this.repo.addColumnComment(tableName, "center_id", "센터코드");
        this.repo.addColumnComment(tableName, "linkid", "링크ID");
        this.repo.addColumnComment(tableName, "rev_type", "수집구분 U:수도권지역센터, S:통합배포연계, L:12개지자체연계");
        this.repo.addColumnComment(tableName, "link_filter_yn", "링크필터링여부");
        this.repo.addColumnComment(tableName, "linklevel_filter_yn", "링크레벨필터링여부");
        this.repo.addColumnComment(tableName, "zero_yn", "속도0값 필터링여부");
        this.repo.addColumnComment(tableName, "std_yn", "표준편차 필터링여부");
        this.repo.addColumnComment(tableName, "sta_yn", "패턴정보 필터링여부");
        this.repo.addColumnComment(tableName, "rgf_yn", "도로등급 필터링여부");
    }

}
