package com.ruoyi.interfaces.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.agentsflex.core.chain.Chain;
import com.agentsflex.core.chain.ChainEvent;
import com.agentsflex.core.chain.listener.ChainEventListener;
import dev.tinyflow.core.Tinyflow;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interfaces.domain.MdModelFlow;
import com.ruoyi.interfaces.service.IMdModelFlowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型流程Controller
 * 
 * @author 朱得糠
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/model/flow")
public class MdModelFlowController extends BaseController
{
    @Autowired
    private IMdModelFlowService mdModelFlowService;

    /**
     * 查询模型流程列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MdModelFlow mdModelFlow)
    {
        startPage();
        List<MdModelFlow> list = mdModelFlowService.selectMdModelFlowList(mdModelFlow);
        return getDataTable(list);
    }



    /**
     * 导出模型流程列表
     */
    @Log(title = "模型流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdModelFlow mdModelFlow)
    {
        List<MdModelFlow> list = mdModelFlowService.selectMdModelFlowList(mdModelFlow);
        ExcelUtil<MdModelFlow> util = new ExcelUtil<MdModelFlow>(MdModelFlow.class);
        util.exportExcel(response, list, "模型流程数据");
    }

    /**
     * 获取模型流程详细信息
     */
    @GetMapping(value = "/{flowId}")
    public AjaxResult getInfo(@PathVariable("flowId") Long flowId)
    {
        return success(mdModelFlowService.selectMdModelFlowByFlowId(flowId));
    }

    /**
     * 新增模型流程
     */
    @Log(title = "模型流程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdModelFlow mdModelFlow)
    {
        return toAjax(mdModelFlowService.insertMdModelFlow(mdModelFlow));
    }

    /**
     * 修改模型流程
     */
    @Log(title = "模型流程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdModelFlow mdModelFlow)
    {
        return toAjax(mdModelFlowService.updateMdModelFlow(mdModelFlow));
    }

    /**
     * 删除模型流程
     */
    @Log(title = "模型流程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{flowIds}")
    public AjaxResult remove(@PathVariable Long[] flowIds)
    {
        return toAjax(mdModelFlowService.deleteMdModelFlowByFlowIds(flowIds));
    }


    @GetMapping("/run")
    public AjaxResult run(String json){
        Tinyflow tinyflow = new Tinyflow(json);

        Chain chain = tinyflow.toChain();

        chain.addEventListener(new ChainEventListener() {
            @Override
            public void onEvent(ChainEvent event, Chain chain) {
                System.out.println("event: " + event.toString());
            }
        });
return success();
    }

}
