package cn.reghao.autodop.dmaster.app.service.bd;

import cn.reghao.autodop.dmaster.app.model.po.log.BuildLog;
import cn.reghao.autodop.dmaster.notification.service.notifier.ding.DingMsg;
import cn.reghao.jdkutil.converter.DateTimeConverter;
import lombok.Data;

/**
 * 应用构建通知消息
 *
 * @author reghao
 * @date 2020-06-24 10:32:34
 */
@Data
public class BuildNotifyMsg {
    private String buildId;
    private String appId;
    private String env;
    private String repoBranch;
    private String commitId;
    private String commitTime;
    private String buildResult;
    private String buildTime;
    private String buildBy;

    public BuildNotifyMsg(BuildLog buildLog) {
        this.buildId = buildLog.getId();
        this.appId = buildLog.getAppId();
        this.env = buildLog.getEnv();
        this.repoBranch = buildLog.getRepoBranch();
        this.commitId = buildLog.getCommitInfo().getCommitId();
        this.commitTime = DateTimeConverter.format(buildLog.getCommitInfo().getMsCommitTime());
        this.buildResult = buildLog.getResult().getMsg();
        this.buildTime = DateTimeConverter.format(buildLog.getBuildTime().getBuildTime());
        this.buildBy = buildLog.getBuildBy();
    }

    public DingMsg dingMsg() {
        String title = "应用构建消息";

        StringBuilder sb = new StringBuilder();
        sb.append("应用构建").append(System.lineSeparator());
        sb.append("# 构建 ID: ").append(buildId).append(System.lineSeparator());
        sb.append("# 应用: ").append(appId).append(System.lineSeparator());
        sb.append("# 环境: ").append(env).append(System.lineSeparator());
        sb.append("# 分支: ").append(repoBranch).append(System.lineSeparator());
        sb.append("# 版本: ").append(commitId).append(System.lineSeparator());
        sb.append("# 提交时间: ").append(commitTime).append(System.lineSeparator());
        sb.append("# 构建结果: ").append(buildResult).append(System.lineSeparator());
        sb.append("# 构建时间: ").append(buildTime).append(System.lineSeparator());
        sb.append("# 执行用户: ").append(buildBy);
        String text = sb.toString();

        return new DingMsg(title, text);
    }
}
