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 RcvLinkTraffic extends MakeTablePartition {

    private final String tablePrefix = "RCV_LINK_TRAFFIC_";

    public RcvLinkTraffic(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.createRcvLinkTrafficTable(crtMonth) < 0) {
            return false;
        }
        if (this.repo.addRcvLinkTrafficCenterPrimaryKey(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, "regdate", "등록시각");
        this.repo.addColumnComment(tableName, "tocenterid", "목적지센터ID");
        this.repo.addColumnComment(tableName, "linknumber", "링크ID");
        this.repo.addColumnComment(tableName, "logdate", "저장시각");
        this.repo.addColumnComment(tableName, "fromcenterid", "소스센터ID");
        this.repo.addColumnComment(tableName, "speedrate", "Km/h");
        this.repo.addColumnComment(tableName, "volumerate", "대/h");
        this.repo.addColumnComment(tableName, "densityrate", "대/h");
        this.repo.addColumnComment(tableName, "traveltimequantity", "초/기준");
        this.repo.addColumnComment(tableName, "delayquanity", "초/기준");
        this.repo.addColumnComment(tableName, "vehiclequeuelengthquantity", "m/기준");
        this.repo.addColumnComment(tableName, "occupancypercent", "점유율");
    }

}
