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

import java.util.List;

import com.zkqy.common.constant.BpmNodeTypeConstants;
import com.zkqy.common.utils.DateUtils;
import com.zkqy.common.utils.bpm.XmlDataParserUtils;
import com.zkqy.common.utils.uuid.IdUtils;
import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNode;
import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNodeForm;
import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNodeMiddle;
import com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess;
import com.zkqy.execution.produce.dispersed.entity.runbpm.SysBpmNodeScriptVO;
import com.zkqy.execution.produce.dispersed.entity.runbpm.TriggerExceptionVO;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeFormMapper;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeMiddleMapper;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeMapper;
import com.zkqy.execution.produce.dispersed.service.IBpmExecuteNodeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 节点执行（记录）Service业务层处理
 *
 * @author hzh
 * @date 2023-11-01
 */
@Service
public class BpmExecuteNodeServiceImpl implements IBpmExecuteNodeService {

    @Autowired
    private BpmExecuteNodeMapper bpmExecuteNodeMapper;

    @Resource
    private BpmExecuteProcessMapper bpmExecuteProcessMapper;

    @Resource
    private BpmExecuteNodeMiddleMapper bpmExecuteNodeMiddleMapper;

    @Autowired
    private BpmExecuteNodeFormMapper bpmExecuteNodeFormMapper;

    /**
     * 查询节点执行（记录）
     *
     * @param id 节点执行（记录）主键
     * @return 节点执行（记录）
     */
    @Override
    public BpmExecuteNode selectBpmExecuteNodeById(Long id) {
        return bpmExecuteNodeMapper.selectBpmExecuteNodeById(id);
    }

    /**
     * 查询节点执行（记录）列表
     *
     * @param bpmExecuteNode 节点执行（记录）
     * @return 节点执行（记录）
     */
    @Override
    public List<BpmExecuteNode> selectBpmExecuteNodeList(BpmExecuteNode bpmExecuteNode) {
        return bpmExecuteNodeMapper.selectBpmExecuteNodeList(bpmExecuteNode);
    }

    /**
     * 新增节点执行（记录）
     *
     * @param bpmExecuteNode 节点执行（记录）
     * @return 结果
     */
    @Override
    public int insertBpmExecuteNode(BpmExecuteNode bpmExecuteNode) {
        bpmExecuteNode.setCreateTime(DateUtils.getNowDate());
        return bpmExecuteNodeMapper.insertBpmExecuteNode(bpmExecuteNode);
    }

    /**
     * 修改节点执行（记录）
     *
     * @param bpmExecuteNode 节点执行（记录）
     * @return 结果
     */
    @Override
    public int updateBpmExecuteNode(BpmExecuteNode bpmExecuteNode) {
        return bpmExecuteNodeMapper.updateBpmExecuteNode(bpmExecuteNode);
    }

    /**
     * 批量删除节点执行（记录）
     *
     * @param ids 需要删除的节点执行（记录）主键
     * @return 结果
     */
    @Override
    public int deleteBpmExecuteNodeByIds(Long[] ids) {
        return bpmExecuteNodeMapper.deleteBpmExecuteNodeByIds(ids);
    }

    /**
     * 删除节点执行（记录）信息
     *
     * @param id 节点执行（记录）主键
     * @return 结果
     */
    @Override
    public int deleteBpmExecuteNodeById(Long id) {
        return bpmExecuteNodeMapper.deleteBpmExecuteNodeById(id);
    }

    @Override
    public int updateNodeState(BpmExecuteNode bpmExecuteNode) {
        return bpmExecuteNodeMapper.updateNodeState(bpmExecuteNode);
    }

    @Override
    @Transactional
    public int triggerExceptionNode(TriggerExceptionVO vo) throws Exception {
        //新增节点标签ID
        String newUserTaskTagId = "Activity_" + IdUtils.fastUUID();
        //select bpm_execute_process
        BpmExecuteProcess bpmExecuteProcess = bpmExecuteProcessMapper.selectBpmExecuteProcessByTaskKey(vo.getTaskProcessKey());
        //sys_bpm_node_script info
        SysBpmNodeScriptVO sysBpmNodeScriptVO = vo.getSysBpmNodeScriptVO();

        //select bpm_execute_node_form
        BpmExecuteNodeForm bpmExecuteNodeForm = new BpmExecuteNodeForm();
        bpmExecuteNodeForm.setTaskProcessKey(vo.getTaskProcessKey());
        bpmExecuteNodeForm.setTaskNodeKey(vo.getTaskNodeKey());
        bpmExecuteNodeForm.setTaskScriptKey(vo.getSysBpmNodeScriptVO().getScriptKey());
        BpmExecuteNodeForm bpmExecuteNodeForm1 = bpmExecuteNodeFormMapper.selectBpmExecuteNodeFormInfo(bpmExecuteNodeForm);
        //insert bpm_execute_node_form
        bpmExecuteNodeForm1.setTaskNodeKey(newUserTaskTagId);
        bpmExecuteNodeFormMapper.insertBpmExecuteNodeForm(bpmExecuteNodeForm1);

        //修改当前执行流程xml内容，当前节点下一节点信息
        String afterAnalysis = XmlDataParserUtils.addUserTaskTag(bpmExecuteProcess.getTaskProcessXmlContent(), newUserTaskTagId, sysBpmNodeScriptVO.getScriptName(), vo.getTaskNodeKey());
        bpmExecuteProcess.setTaskProcessXmlContent(afterAnalysis);
        bpmExecuteProcess.setTaskNodeKey(newUserTaskTagId);
        bpmExecuteProcess.setTaskNodeNextKey(bpmExecuteProcess.getTaskNodeKey());
        bpmExecuteProcessMapper.updateBpmExecuteProcess(bpmExecuteProcess);
        //生成虚拟角色编码
        String taskNodeRolePermission = IdUtils.fastSimpleUUID();

        //得到当前节点信息，添加异常节点信息
        BpmExecuteNode bpmExecuteNode = bpmExecuteNodeMapper.queryBpmExecuteNodeBytaskNodeKey(vo.getTaskProcessKey(), vo.getTaskNodeKey());
        bpmExecuteNode.setId(null);
        bpmExecuteNode.setTaskNodeKey(newUserTaskTagId);
        bpmExecuteNode.setTaskNodeName(sysBpmNodeScriptVO.getScriptName());
        bpmExecuteNode.setTaskNodeType(BpmNodeTypeConstants.EXCEPTION_TASK);
        bpmExecuteNode.setTaskRemark(sysBpmNodeScriptVO.getScriptDescription());
        bpmExecuteNode.setTaskNodeState("0");
        bpmExecuteNode.setTaskNodeNumber("");
        bpmExecuteNode.setTaskNodeWeight("");
        bpmExecuteNode.setTaskNodeFormKey(bpmExecuteNodeForm1.getTaskNodeFormKey());
        bpmExecuteNode.setTaskNodeFormType(bpmExecuteNodeForm1.getTaskNodeFromType());
        bpmExecuteNode.setTaskNodeRolePermission(taskNodeRolePermission);
        bpmExecuteNodeMapper.insertBpmExecuteNode(bpmExecuteNode);


        //得到当前节点用户关联表信息，添加异常节点用户关联表信息
        BpmExecuteNodeMiddle bpmExecuteNodeMiddle = bpmExecuteNodeMiddleMapper.queryBpmExecuteNodeMiddleByTaskNodeKey(vo.getTaskProcessKey(), vo.getTaskNodeKey());
        bpmExecuteNodeMiddle.setId(null);
        bpmExecuteNodeMiddle.setTaskNodeKey(newUserTaskTagId);
        bpmExecuteNodeMiddle.setTaskAutomaticScriptTriggerType(sysBpmNodeScriptVO.getScriptKey());
        bpmExecuteNodeMiddle.setTaskVirtuallyRole(taskNodeRolePermission);
        return bpmExecuteNodeMiddleMapper.insertBpmExecuteNodeMiddle(bpmExecuteNodeMiddle);
    }
}
