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

import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
import cn.reghao.bnt.web.devops.builder.chain.impl.BuildChainResult;
import cn.reghao.bnt.web.devops.builder.model.BuildConsumedDto;
import cn.reghao.bnt.web.devops.builder.tool.repo.CommitInfo;
import cn.reghao.bnt.web.util.BaseEntity;
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
 * 应用构建日志
 *
 * @author reghao
 * @date 2020-03-10 11:22:05
 */
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "devops_build_log")
public class BuildLog extends BaseEntity {
    @Column(unique = true)
    private String buildLogId;
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "app_id")
    private AppConfig appConfig;
    private String commitId;
    private LocalDateTime commitTime;
    private String packagePath;
    private BuildConfigSnapshot buildConfigSnapshot;
    private BuildConsumed buildConsumed;
    private LocalDateTime buildTime;
    private Integer code;
    @Column(columnDefinition="text")
    private String msg;
    private String buildBy;

    public BuildLog(BuildChainResult buildChainResult, AppConfig appConfig) {
        CommitInfo commitInfo = buildChainResult.getCommitInfo();
        BuildConsumedDto buildConsumedDto = buildChainResult.getBuildConsumedDto();

        this.buildLogId = buildChainResult.getBuildLogId();
        this.appConfig = appConfig;
        this.commitId = commitInfo != null ? commitInfo.getCommitId() : null;
        this.commitTime = commitInfo != null ? DateTimeConverter.localDateTime(commitInfo.getMsCommitTime()) : null;
        this.packagePath = buildChainResult.getPackagePath();
        this.buildConfigSnapshot = new BuildConfigSnapshot(appConfig);
        this.buildConsumed = new BuildConsumed(buildConsumedDto);
        this.buildTime = DateTimeConverter.localDateTime(buildChainResult.getResult().getTimestamp());
        this.code = buildChainResult.getResult().getCode();
        this.msg = buildChainResult.getResult().getMsg();
        this.buildBy = buildChainResult.getBuildBy();
    }
}
