package cn.reghao.bnt.web.devops.app.model.vo.msg;

import cn.reghao.bnt.web.admin.service.notifier.ding.DingMsg;
import cn.reghao.bnt.web.devops.app.model.po.AppBuilding;
import cn.reghao.jutil.jdk.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(AppBuilding appBuilding) {
        this.buildId = appBuilding.getBuildLog().getBuildLogId();
        this.appId = appBuilding.getAppConfig().getAppId();
        this.env = appBuilding.getAppConfig().getEnv();
        this.repoBranch = appBuilding.getAppConfig().getRepoBranch();
        this.commitId = appBuilding.getCommitId();
        this.commitTime = DateTimeConverter.format(appBuilding.getCommitTime());
        this.buildResult = appBuilding.getBuildResult();
        this.buildTime = DateTimeConverter.format(appBuilding.getBuildTime());
        this.buildBy = appBuilding.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);
    }
}
