package com.yunzhupaas.flowable.util;

import cn.hutool.core.util.ObjectUtil;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.flowable.entity.OperatorEntity;
import com.yunzhupaas.flowable.entity.TaskEntity;
import com.yunzhupaas.flowable.entity.TemplateEntity;
import com.yunzhupaas.flowable.enums.OperatorStateEnum;
import com.yunzhupaas.flowable.model.message.FlowMsgModel;
import com.yunzhupaas.flowable.model.time.FlowTimeModel;
import com.yunzhupaas.flowable.service.OperatorService;
import com.yunzhupaas.flowable.service.TaskService;
import com.yunzhupaas.flowable.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：云筑产品开发平台组
 * @version: V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date ：2024/5/23 上午10:33
 */
@Component
public class OverTimeUtil {
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private TaskUtil taskUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private OperatorService operatorService;
    @Autowired
    private TemplateService templateService;

    public void overMsg(FlowTimeModel flowTimeModel) throws WorkFlowException {
        TaskEntity task = taskService.getInfo(flowTimeModel.getTaskId());
        //暂停，是跳过
        if (task == null) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        boolean isSuccess = false;
        try {
            taskUtil.isSuspend(task);
        } catch (Exception e) {
            isSuccess = true;
        }
        if (!isSuccess) {
            // 判断是否下架
            TemplateEntity template = templateService.getById(task.getTemplateId());
            if (null != template && !ObjectUtil.equals(template.getStatus(), 1)) {
                isSuccess = true;
            }
        }
        if (isSuccess) {
            if (flowTimeModel.getOverTime()) {
                flowTimeModel.setNum(flowTimeModel.getNum() + 1);
                flowTimeModel.setTransferNum(flowTimeModel.getTransferNum() + 1);
            }
            flowTimeModel.setIsPause(true);
            return;
        }
        taskUtil.isCancel(task);
        OperatorEntity operator = operatorService.getById(flowTimeModel.getOperatorId());
        if (operator == null || ObjectUtil.equals(operator.getStatus(), OperatorStateEnum.Futility.getCode())) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        if (operator.getHandleStatus() != null) {
            throw new WorkFlowException(MsgCode.WF031.get());
        }
        // complete为1的经办，不执行（加签、指派、转办、驳回、撤回、变更、复活流程，该节点的限时提醒规则重新生效）
        if (operator.getCompletion().equals(1)) {
            throw new WorkFlowException(MsgCode.FA001.get());
        }
        if (flowTimeModel.getOverTime()) {
            if (operator.getDuedate() == null) {
                operator.setDuedate(new Date());
                operatorService.updateById(operator);
            }
        }
        List<OperatorEntity> list = new ArrayList<>();
        list.add(operator);
        FlowMsgModel msgModel = new FlowMsgModel();
        msgModel.setFlowModel(flowTimeModel.getFlowModel());
        msgModel.setTaskEntity(task);
        msgModel.setNodeList(flowTimeModel.getFlowModel().getNodeEntityList());
        msgModel.setNodeCode(operator.getNodeCode());
        msgModel.setOperatorList(list);
        msgModel.setNotice(!flowTimeModel.getOverTime());
        msgModel.setOvertime(flowTimeModel.getOverTime());
        msgModel.setWait(false);
        msgUtil.message(msgModel);
        if (flowTimeModel.getOverTime()) {
            flowTimeModel.setNum(flowTimeModel.getNum() + 1);
            flowTimeModel.setTransferNum(flowTimeModel.getTransferNum() + 1);
        }
    }

}

