package cn.palmte.work.model;

import cn.palmte.work.bean.StatusEnum;
import cn.palmte.work.config.activiti.ActConstant;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;

/**
 * 项目审批任务记录
 */
@Data
@Entity
@Table(name = "project_task_record")
public class ProjectTaskRecord {

    public static final int STATUS_ROLLBACK = 4;//管理员退回
    public static final int STATUS_RE_ASSIGNEE = 5; //指定承接人

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    /**
     * 项目类型 0-四算项目  1-销售合同流程  2-业务采购流程
     */
    @Column(name = "project_type")
    private int projectType = 0;

    @Column(name = "project_id")
    private int projectId;

    @Column(name = "proc_def_id")
    private String procDefId;

    @Column(name = "proc_ins_id")
    private String procInsId;

    @Column(name = "task_def_key")
    private String taskDefKey;

    @Column(name = "task_name")
    private String taskName;

    /**
     * @see cn.palmte.work.bean.ApproveStatusEnum#approveStatus
     *
     * 状态 1-提交审批  2-审批通过 3-审批不通过 4-管理员退回  5-指定承接人
     */
    @Column(name = "task_status")
    private int taskStatus;

    /**
     * 审批人id
     */
    @Column(name = "assignee_id")
    private String assigneeId;

    /**
     * 审批人姓名
     */
    @Column(name = "assignee_name")
    private String assigneeName;

    /**
     * 审批意见
     */
    @Column(name = "task_comment")
    private String taskComment;

    @Column(name = "create_time")
    private Date createTime;

    @Transient
    private String roleName;

    /**
     * 任务节点位置  -1结束节点  0-未知或者其他节点  1-开始节点   2-发起审批节点
     */
    @Column(name = "task_index")
    private int taskIndex;

    /**
     * 会签单文件访问地址
     */
    @Column(name = "file_url")
    private String fileUrl;


    /**
     * 退回 project_task_record表id
     */
    @Column(name = "rollback_record_id")
    private int rollbackRecordId;

    /**
     * 退回到哪里 保存快照信息
     */
    @Column(name = "rollback_desc")
    private String rollbackDesc;


    /**
     * 是否运行管理员退回
     */
    @Transient
    private boolean canRollback = false;

    @Transient
    private String procDefName = "";

    @Transient
    private String procDefKey= "";

    public StatusEnum getStatusByProDefId() {
        if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_BUDGET)) {
            return StatusEnum.BUDGET_ACCOUNTS;
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_SETTLE)) {
            return StatusEnum.SETTLE_ACCOUNTS;
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_FINAL)) {
            return StatusEnum.FINAL_ACCOUNTS;
        }else{
            return StatusEnum.ESTIMATE_ACCOUNTS;
        }
    }

    public String getProcDefName() {
        if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_BUDGET)) {
            return "预算审批流程";
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_SETTLE)) {
            return "结算审批流程";
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_FINAL)) {
            return "决算审批流程";
        }else{
            return "概算审批流程";
        }
    }

    public void setProcDefName(String procDefName) {
        this.procDefName = procDefName;
    }

    public String getProcDefKey() {
        if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_BUDGET)) {
            return ActConstant.PROCESS_DEFKEY_BUDGET;
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_SETTLE)) {
            return ActConstant.PROCESS_DEFKEY_SETTLE;
        }else if (procDefId.startsWith(ActConstant.PROCESS_DEFKEY_FINAL)) {
            return ActConstant.PROCESS_DEFKEY_FINAL;
        }else{
            return ActConstant.PROCESS_DEFKEY_ESTIMATE;
        }
    }

    public void setProcDefKey(String procDefKey) {
        this.procDefKey = procDefKey;
    }
}
