
package org.jeecg.modules.workFlow.flowlong.engine.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.jeecg.modules.workFlow.flowlong.engine.core.FlowLongContext;
import org.jeecg.modules.workFlow.flowlong.engine.core.enums.InstancePriority;
import org.jeecg.modules.workFlow.flowlong.engine.core.enums.InstanceState;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 流程实例实体类
 *

 */
@Getter
@Setter
@ToString
public class FlwInstance extends FlowEntity {
    /**
     * 流程定义ID
     */
    protected Long processId;
    /**
     * 父流程实例ID
     */
    protected Long parentInstanceId;
    /**
     * 流程实例优先级 0，正常 1，异步
     */
    protected Integer priority;
    /**
     * 流程实例编号
     */
    protected String instanceNo;
    /**
     * 业务KEY（用于关联业务逻辑实现预留）
     *
     * <p>
     * 子流程情况，该字段用于存放父流程所在节点KEY
     * </p>
     */
    protected String businessKey;
    /**
     * 变量json
     */
    protected String variable;
    /**
     * 当前所在节点名称
     */
    protected String currentNodeName;
    /**
     * 当前所在节点key
     */
    protected String currentNodeKey;
    /**
     * 流程实例期望完成时间
     */
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Date expireTime;
    /**
     * 流程实例上一次更新人
     */
    protected String lastUpdateBy;
    /**
     * 流程实例上一次更新时间
     */
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Date lastUpdateTime;

    public FlwInstance() {
        // 默认优先级 0
        this.priority = 0;
    }

    public static FlwInstance of(String businessKey) {
        FlwInstance flwInstance = new FlwInstance();
        flwInstance.setBusinessKey(businessKey);
        return flwInstance;
    }

    public void priority(InstancePriority instancePriority) {
        this.priority = instancePriority.getValue();
    }

    @SuppressWarnings({"all"})
    public Map<String, Object> variableToMap() {
        Map<String, Object> map = FlowLongContext.fromJson(this.variable, Map.class);
        return null != map ? map : new HashMap<>();
    }

    public void setMapVariable(Map<String, Object> args) {
        this.variable = FlowLongContext.toJson(args);
    }
}
