package cn.stylefeng.workflow.api.entity.instance;

import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.roses.kernel.rule.annotation.EnumFieldFormat;
import cn.stylefeng.roses.kernel.rule.annotation.SimpleFieldFormat;
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
import cn.stylefeng.roses.kernel.sys.api.format.UserAvatarFormatProcess;
import cn.stylefeng.workflow.api.enums.InstanceStatusEnum;
import cn.stylefeng.workflow.api.format.DefinitionFormatProcess;
import cn.stylefeng.workflow.api.pojo.InstanceVariable;
import cn.stylefeng.workflow.api.pojo.instance.SummaryFieldValue;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;
import java.util.List;
import java.util.Set;

@TableName(value = "flow_engine_runtime_instance", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
public class RuntimeInstance extends BaseEntity {

    @TableId(value = "instance_id", type = IdType.ASSIGN_ID)
    @ChineseDescription("主键")
    private Long instanceId;

    @TableField("model_id")
    @ChineseDescription("流程模型id")
    private Long modelId;

    @TableField("definition_id")
    @ChineseDescription("流程定义id")
    @SimpleFieldFormat(formatType = FormatTypeEnum.ADD_FIELD, processClass = DefinitionFormatProcess.class)
    private Long definitionId;

    @TableField("parent_instance_id")
    @ChineseDescription("上级流程实例id")
    private Long parentInstanceId;

    @TableField("param_send_parent_flag")
    @ChineseDescription("子流程变量是否传递到父级流程：true-是，false-否")
    private Boolean paramSendParentFlag;

    @TableField("parent_node_id")
    @ChineseDescription("当前流程对应的上级流程实例的节点id，非运行时节点")
    private Long parentNodeId;

    @TableField("start_type")
    @ChineseDescription("流程发起方式：10-页面发起，20-API接口发起")
    private Integer startType;

    @TableField("instance_title")
    @ChineseDescription("审批标题")
    private String instanceTitle;

    @TableField("applicant")
    @ChineseDescription("提交人id，发起人id")
    @SimpleFieldFormat(formatType = FormatTypeEnum.ADD_FIELD, processClass = UserAvatarFormatProcess.class)
    private Long applicant;

    @TableField("applicant_org_id")
    @ChineseDescription("发起人的组织机构id，以哪个身份发起")
    private Long applicantOrgId;

    @TableField("applicant_position_id")
    @ChineseDescription("发起人的职位id，以哪个身份发起")
    private Long applicantPositionId;

    @TableField("current_ru_node_id")
    @ChineseDescription("当前运行时节点id")
    private Long currentRuNodeId;

    @TableField("completion_time")
    @ChineseDescription("流程完成时间")
    private Date completionTime;

    @TableField("instance_status")
    @ChineseDescription("流程实例状态：10-草稿，20-处理中，30-已完成，40-拒绝，50-挂起，60-作废")
    @EnumFieldFormat(processEnum = InstanceStatusEnum.class)
    private Integer instanceStatus;

    @TableField("biz_primary_key")
    @ChineseDescription("绑定具体业务信息的唯一id，一般为业务表的主键id")
    private String bizPrimaryKey;

    @TableField(value = "instance_variable", typeHandler = JacksonTypeHandler.class)
    @ChineseDescription("流程变量（包含表单数据、一些运行时数据）")
    private InstanceVariable instanceVariable;

    @TableField(value = "version_flag", fill = FieldFill.INSERT)
    @ChineseDescription("乐观锁")
    @Version
    private Long versionFlag;

    @TableField(value = "del_flag", fill = FieldFill.INSERT)
    @TableLogic
    @ChineseDescription("逻辑删除:Y-已删除，N-未删除")
    private String delFlag;

    @TableField(value = "tenant_id", fill = FieldFill.INSERT)
    @ChineseDescription("租户号")
    private Long tenantId;

    @TableField(exist = false)
    @ChineseDescription("当前进行中的节点")
    private List<RuntimeNode> currentRuntimeNodeList;

    @TableField(exist = false)
    @ChineseDescription("当前进行中的审批人，存放用户id集合")
    @SimpleFieldFormat(formatType = FormatTypeEnum.ADD_FIELD, processClass = UserAvatarFormatProcess.class)
    private Set<Long> currentApproverList;

    @TableField(exist = false)
    @ChineseDescription("待办任务id")
    private Long taskId;

    @TableField(exist = false)
    @ChineseDescription("任务状态：10-发起，15-待开始，20-审批中，30-已审批，40-已拒绝，50-已退回，60-已撤回，70-已关闭，80-已转交")
    private Integer taskStatus;

    @TableField(exist = false)
    @ChineseDescription("摘要字段列表")
    List<SummaryFieldValue> summaryFieldValueList;

    @TableField(exist = false)
    @ChineseDescription("持续时间")
    private String durationFormat;

}
