package com.zkqy.execution.produce.dispersed.service.impl.script.common;


import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNode;
import com.zkqy.execution.produce.dispersed.entity.runbpm.IScriptEntity;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeMapper;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteProcessMapper;
import com.zkqy.execution.produce.dispersed.service.IScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;

/**
 * 共通结束节点
 *
 * @author hanzihang
 * @date 2023/11/24
 */
@Service("commonEndEvent")
@Scope("prototype")
public class CommonNodeEndServiceImpl implements IScriptService {

    @Autowired // 执行流程
    private BpmExecuteProcessMapper runBpmExecuteProcessMapper;

    @Autowired  // 执行流程节点表
    private BpmExecuteNodeMapper runBpmExecuteNodeMapper;

    @Override
    public Object preExecutionScript(Object parameter) {
        IScriptEntity iScriptEntity = (IScriptEntity) parameter;
        if (iScriptEntity.getScriptFlowKey() == null) return false;
        if (iScriptEntity.getScriptNodeKey() == null) return false;
        return true;
    }

    @Override
    public Object executionScript(Object parameter) {
        IScriptEntity iScriptEntity = (IScriptEntity) parameter;
        // 执行结束脚本逻辑。。。
        // 更改任务流程状态
        runBpmExecuteProcessMapper.endProcess(iScriptEntity.getScriptFlowKey());
        // 更改节点任务状态
        BpmExecuteNode bpmExecuteNode = new BpmExecuteNode();
        bpmExecuteNode.setTaskProcessKey(iScriptEntity.getScriptFlowKey());
        bpmExecuteNode.setTaskNodeType(iScriptEntity.getScriptNodeKey());
        runBpmExecuteNodeMapper.updateNodeState(bpmExecuteNode);
        return "流程执行成功！";
    }

    @Override
    public Object afterExecutionScript(Object parameter) {
        return null;
    }

    @Override
    public boolean isVerificationMethod() {
        return true;
    }

    @Override
    @PreDestroy
    public boolean preDestroy() {
        // 执行清理操作
        System.err.print("清理成功！");
        System.err.print("流程结束成功！");
        return true;
    }
}
