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

    private final String tablePrefix = "STAT_LOCAL_LOG_";

    public StatLocalLog(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.createStatLocalLogTable(crtMonth) < 0) {
            return false;
        }
        if (this.repo.addStatLocalLogPrimaryKey(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", "수집구분");
        this.repo.addColumnComment(tableName, "speed", "속도");
    }

}
