package com.friday.operation.api.keepalive.dto.smartrpa;

import com.friday.operation.api.keepalive.constant.smartrpa.RobotState;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @Author YaoYao
 * @Date Created In 18:57 2022/11/29
 * @Description 心跳请求数据结构
 * @Modified By
 */
@Data
@Builder
@ApiModel(description = "心跳请求数据结构")
@AllArgsConstructor
@NoArgsConstructor
public class HeartbeatReq implements Serializable {
    @ApiModelProperty(value = "机器设备码", required = true)
    private String machineCode;
    @ApiModelProperty(value = "机器登录用户", required = true)
    private String machineUser;
    @ApiModelProperty(value = "机器状态", required = true)
    private String machineState;
    @ApiModelProperty(value = "当前运行任务ID")
    private String jobId;
    @ApiModelProperty(value = "当前任务运行状态")
    private String jobState;
    @ApiModelProperty(value = "动作确认")
    private ActionConfirmReq actionConfirm;

    public String getRobotKey(){
        return this.machineCode + this.machineUser;
    }

    public boolean reqValid(){
        if("Idle".equalsIgnoreCase(machineState)){
            this.machineState = RobotState.Available.getState();
        }
        return !StringUtils.isAnyEmpty(machineCode, machineUser, machineState);
    }

    public boolean jobStateReqValid(){
        return StringUtils.isEmpty(jobId) || (jobIdValid() && StringUtils.isNotEmpty(jobState));
    }

    boolean jobIdValid(){
        try {
            Integer.parseInt(jobId);
        } catch (Exception e){
            return false;
        }
        return true;
    }
}
