
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 io.swagger.annotations.ApiModelProperty;
import org.jeecg.modules.workFlow.flowlong.engine.assist.Assert;
import org.jeecg.modules.workFlow.flowlong.engine.assist.DateUtils;
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;

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

 */
@Getter
@Setter
@ToString
public class FlwHisInstance extends FlwInstance {
    /**
     * 状态 0，活动 1，结束 更多查看 {@link InstanceState}
     */
    protected Integer instanceState;
    /**
     * 结束时间
     */
    @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 endTime;
    /**
     * 处理耗时
     */
    protected Long duration;

    public void setVariable(Integer instanceState) {
        this.instanceState = instanceState;
    }

    public FlwHisInstance instanceState(InstanceState instanceState) {
        this.instanceState = instanceState.getValue();
        return this;
    }

    public void setInstanceState(Integer instanceState) {
        Assert.isNull(InstanceState.get(instanceState), "插入的实例状态异常 [instanceState=" + instanceState + "]");
        this.instanceState = instanceState;
    }

    public static FlwHisInstance of(FlwInstance fi, InstanceState instanceState) {
        FlwHisInstance his = new FlwHisInstance();
        his.id = fi.getId();
        his.tenantId = fi.getTenantId();
        his.createId = fi.getCreateId();
        his.createBy = fi.getCreateBy();
        his.createTime = fi.getCreateTime();
        his.processId = fi.getProcessId();
        his.parentInstanceId = fi.getParentInstanceId();
        his.priority = fi.getPriority();
        his.instanceNo = fi.getInstanceNo();
        his.businessKey = fi.getBusinessKey();
        his.variable = fi.getVariable();
        his.currentNodeName = fi.getCurrentNodeName();
        his.currentNodeKey = fi.getCurrentNodeKey();
        his.expireTime = fi.getExpireTime();
        his.lastUpdateBy = fi.getLastUpdateBy();
        his.lastUpdateTime = fi.getLastUpdateTime();
        his.instanceState = instanceState.getValue();
        if (InstanceState.active != instanceState) {
            his.calculateDuration();
        }
        return his;
    }

    public FlwInstance toFlwInstance() {
        FlwInstance fi = new FlwInstance();
        fi.setId(this.id);
        fi.setTenantId(this.tenantId);
        fi.setCreateId(this.createId);
        fi.setCreateBy(this.createBy);
        fi.setCreateTime(this.createTime);
        fi.setProcessId(this.processId);
        fi.setParentInstanceId(this.parentInstanceId);
        fi.setPriority(this.priority);
        fi.setInstanceNo(this.instanceNo);
        fi.setBusinessKey(this.businessKey);
        fi.setVariable(this.variable);
        fi.setCurrentNodeName(this.currentNodeName);
        fi.setCurrentNodeKey(this.currentNodeKey);
        fi.setExpireTime(this.expireTime);
        fi.setLastUpdateBy(this.lastUpdateBy);
        fi.setLastUpdateTime(this.lastUpdateTime);
        return fi;
    }

    /**
     * 计算流程实例处理耗时
     */
    public void calculateDuration() {
        this.endTime = DateUtils.getCurrentDate();
        this.duration = DateUtils.calculateDateDifference(this.createTime, this.endTime);
    }
}
