package cn.reghao.autodop.dmaster.app.model.po;

import cn.reghao.jdkutil.result.ResultStatus;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.model.po.config.RunningConfig;
import cn.reghao.autodop.dmaster.app.model.po.log.BuildLog;
import cn.reghao.autodop.dmaster.app.service.bd.tools.repo.CommitInfo;
import cn.reghao.autodop.dmaster.util.db.BaseEntity;
import cn.reghao.jdkutil.converter.DateTimeConverter;
import lombok.*;

import javax.persistence.Entity;
import java.time.LocalDateTime;

/**
 * 需要构建的应用
 *
 * @author reghao
 * @date 2021-05-21 09:57:35
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
public class AppBuilding extends BaseEntity<Integer> {
    private String appId;
    private String appName;
    private String appType;
    private String env;
    private String repoBranch;
    private Integer httpPort;

    private String buildLogId;
    private String commitId;
    private LocalDateTime commitTime;
    private String buildResult;
    private LocalDateTime buildTime;
    private String packagePath;
    private String buildBy;

    public AppBuilding(AppConfig app) {
        this.appId = app.getAppId();
        this.appName = app.getAppName();
        this.env = app.getEnv();
        this.appType = app.getAppType();
        this.repoBranch = app.getRepoBranch();
        RunningConfig runningConfig = app.getRunningConfig();
        if (runningConfig != null) {
            this.httpPort = runningConfig.getHttpPort();
        } else {
            this.httpPort = 0;
        }
    }

    public AppBuilding refresh(AppConfig app) {
        this.appName = app.getAppName();
        this.env = app.getEnv();
        this.appType = app.getAppType();
        this.repoBranch = app.getRepoBranch();
        RunningConfig runningConfig = app.getRunningConfig();
        if (runningConfig != null) {
            this.httpPort = runningConfig.getHttpPort();
        } else {
            this.httpPort = 0;
        }

        return this;
    }

    public void update(BuildLog buildLog) {
        this.setBuildLogId(buildLog.getId());
        this.setAppId(buildLog.getAppId());
        this.setEnv(buildLog.getEnv());
        this.setAppType(buildLog.getAppType());
        this.setRepoBranch(buildLog.getRepoBranch());

        CommitInfo commitInfo = buildLog.getCommitInfo();
        if (commitInfo != null) {
            this.setCommitId(commitInfo.getCommitId());
            this.setCommitTime(DateTimeConverter.localDateTime(commitInfo.getMsCommitTime()));
        }

        int code = buildLog.getResult().getCode();
        if (code == ResultStatus.SUCCESS.getCode()) {
            this.setBuildResult(ResultStatus.SUCCESS.getMsg());
        } else {
            this.setBuildResult(ResultStatus.FAIL.getMsg());
        }
        this.setBuildTime(buildLog.getBuildTime().getBuildTime());
        this.setPackagePath(buildLog.getPackagePath());
        this.setBuildBy(buildLog.getBuildBy());
        this.setUpdateTime(LocalDateTime.now());
    }
}
