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

import cn.reghao.autodop.common.msg.rpc.dto.app.AppStatus;
import cn.reghao.autodop.common.msg.rpc.dto.app.DeployResult;
import cn.reghao.jdkutil.result.Result;
import cn.reghao.autodop.dmaster.util.db.BaseDocument;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;

/**
 * 应用部署日志
 *
 * @author reghao
 * @date 2020-03-10 11:22:05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Document("DeployLog")
public class DeployLog extends BaseDocument {
    private String buildLogId;
    private String machineId;
    private String machineIpv4;
    private LocalDateTime deployTime;
    private Result result;
    private String deployBy;

    public static DeployLog from(DeployResult deployResult) {
        DeployLog deployLog = new DeployLog();
        deployLog.setBuildLogId(deployResult.getBuildLogId());
        deployLog.setResult(deployResult.getResult());
        AppStatus appStatus = deployResult.getAppStatus();
        if (appStatus != null) {
            deployLog.setMachineId(appStatus.getMachineId());
            deployLog.setMachineIpv4(appStatus.getMachineIpv4());
            deployLog.setDeployTime(appStatus.getStartTime());
        }
        deployLog.setDeployBy("某人");
        return deployLog;
    }
}
