package com.zkqy.execution.produce.dispersed.controller;

import com.zkqy.common.annotation.Log;
import com.zkqy.common.core.controller.BaseController;
import com.zkqy.common.core.domain.AjaxResult;
import com.zkqy.common.enums.BusinessType;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeFormDateVo;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeFromVo;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeVo;
import com.zkqy.execution.produce.dispersed.entity.runbpm.TriggerExceptionVO;
import com.zkqy.execution.produce.dispersed.runbpm.PreExecutionToolClass;
import com.zkqy.execution.produce.dispersed.service.IBpmExecuteNodeService;
import com.zkqy.execution.produce.utils.EchoNodeFormData;
import com.zkqy.system.entity.CommonEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 流程执行共通Controller
 *
 * @author hzh
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/system/runbpm/process")
public class BpmRunController extends BaseController {

    @Autowired
    private PreExecutionToolClass ipreExecutionToolClass;

    @Autowired
    private IBpmExecuteNodeService bpmExecuteNodeService;

    @Autowired
    private EchoNodeFormData echoNodeFormData;

    /**
     * 共通投产流程接口
     */
    @Log(title = "投产执行流程", businessType = BusinessType.INSERT)
    @PostMapping("/productionScheduling")
    public AjaxResult productionScheduling(@RequestBody CommonEntity commonEntity) {
        return AjaxResult.success(ipreExecutionToolClass.ReadyToExecute(commonEntity));
    }

    /**
     * 共通执行节点接口
     */
    @Log(title = "执行流程节点", businessType = BusinessType.INSERT)
    @PostMapping("/processNodeExecution")
    public AjaxResult processNodeExecution(BpmRunNodeVo bpmRunNodeVo, @RequestParam("fileXML") MultipartFile fileXML) {
        if (!fileXML.isEmpty()) {
            try {
                // 解析得到当前文件中的内容
                byte[] bytes = fileXML.getBytes();
                String xmlContent = new String(bytes);
                bpmRunNodeVo.setTaskProcessXmlContent(xmlContent);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return AjaxResult.success(ipreExecutionToolClass.executionNode(bpmRunNodeVo));
    }


    /**
     * 共通执行节点表单回显数据接口
     */
    @GetMapping("/processNodeFormInfoData")
    public AjaxResult processNodeFormInfoData(BpmRunNodeFromVo bpmRunNodeFromVo) {
        return AjaxResult.success(ipreExecutionToolClass.processNodeFormInfoData(bpmRunNodeFromVo));
    }

    /**
     * handleSubmitForm
     *
     * @param bpmRunNodeFormDateVo
     * @return
     */
    @PostMapping("/submitNodeForm")
    public AjaxResult handleSubmitFormData(@RequestBody BpmRunNodeFormDateVo bpmRunNodeFormDateVo) {
        return AjaxResult.success(echoNodeFormData.handleSubmitForm(bpmRunNodeFormDateVo));
    }


    /**
     * 触发异常节点
     */
    @PostMapping("/triggerExceptionNode")
    public AjaxResult triggerExceptionNode(@RequestBody TriggerExceptionVO vo) throws Exception {
        return toAjax(bpmExecuteNodeService.triggerExceptionNode(vo));
    }


}