package cn.reghao.bnt.web.devops.app.model.po;

import cn.reghao.bnt.web.devops.app.model.constant.BuildStatus;
import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
import cn.reghao.bnt.web.devops.app.model.po.log.BuildLog;
import cn.reghao.bnt.web.util.BaseEntity;
import cn.reghao.jutil.jdk.web.result.ResultStatus;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.Entity;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.LocalDateTime;

/**
 * 应用最新的一次构建
 *
 * @author reghao
 * @date 2021-05-21 09:57:35
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "devops_app_building")
public class AppBuilding extends BaseEntity {
    @OneToOne
    private AppConfig appConfig;
    @OneToOne
    private BuildLog buildLog;
    private String commitId;
    private LocalDateTime commitTime;
    private LocalDateTime buildTime;
    private int buildCode;
    private String buildResult;
    private String packagePath;
    private String buildBy;
    private boolean onBuilding;

    public AppBuilding(AppConfig appConfig) {
        this.appConfig = appConfig;
        this.buildCode = ResultStatus.SUCCESS.getCode();
        this.buildResult = BuildStatus.neverBuild.getDesc();
        this.onBuilding = false;
    }

    public AppBuilding(BuildLog buildLog) {
        this.appConfig = buildLog.getAppConfig();
        this.buildLog = buildLog;
        this.commitId = buildLog.getCommitId();
        this.commitTime = buildLog.getCommitTime();

        this.buildTime = buildLog.getBuildTime();
        this.buildCode = buildLog.getCode();
        this.buildResult = buildLog.getCode() == ResultStatus.SUCCESS.getCode() ?
                BuildStatus.buildSuccess.getDesc() : BuildStatus.buildFail.getDesc();
        this.packagePath = buildLog.getPackagePath();
        this.buildBy = buildLog.getBuildBy();
        this.onBuilding = false;
    }

    public void beforeBuild(String buildBy) {
        this.buildCode = ResultStatus.SUCCESS.getCode();
        this.buildResult = BuildStatus.onBuilding.getDesc();
        this.buildBy = buildBy;
        this.onBuilding = true;
    }

    public void afterBuild(BuildLog buildLog) {
        this.buildLog = buildLog;
        this.commitId = buildLog.getCommitId();
        this.commitTime = buildLog.getCommitTime();

        this.buildTime = buildLog.getBuildTime();
        this.buildCode = buildLog.getCode();
        this.buildResult = buildLog.getCode() == ResultStatus.SUCCESS.getCode() ?
                BuildStatus.buildSuccess.getDesc() : BuildStatus.buildFail.getDesc();
        this.packagePath = buildLog.getPackagePath();
        this.buildBy = buildLog.getBuildBy();
        this.onBuilding = false;
    }

    public void afterBuild(BuildLog buildLog, String errMsg) {
        this.buildLog = buildLog;
        this.commitId = buildLog.getCommitId();
        this.commitTime = buildLog.getCommitTime();

        this.buildTime = buildLog.getBuildTime();
        this.buildCode = 1;
        this.buildResult =errMsg;
        this.packagePath = buildLog.getPackagePath();
        this.buildBy = buildLog.getBuildBy();
        this.onBuilding = false;
    }
}
