package com.fs.his.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsAiWorkflow;
import com.fs.his.domain.FsAiWorkflowNodeType;
import com.fs.his.param.FsAiWorkflowSaveParam;
import com.fs.his.param.FsAiWorkflowUpdateBindWCParam;
import com.fs.his.service.IFsAiWorkflowService;
import com.fs.his.vo.FsAiWorkflowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * AI工作流Controller
 *
 * @author fs
 * @date 2026-01-06
 */
@RestController
@RequestMapping("/his/aiWorkflow")
public class FsAiWorkflowController extends BaseController {

    @Autowired
    private IFsAiWorkflowService fsAiWorkflowService;

    /**
     * 查询AI工作流列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsAiWorkflow fsAiWorkflow) {
        startPage();
        List<FsAiWorkflow> list = fsAiWorkflowService.selectFsAiWorkflowList(fsAiWorkflow);
        return getDataTable(list);
    }

    /**
     * 导出AI工作流列表
     */
    @Log(title = "AI工作流", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsAiWorkflow fsAiWorkflow) {
        List<FsAiWorkflow> list = fsAiWorkflowService.selectFsAiWorkflowList(fsAiWorkflow);
        ExcelUtil<FsAiWorkflow> util = new ExcelUtil<FsAiWorkflow>(FsAiWorkflow.class);
        return util.exportExcel(list, "AI工作流数据");
    }

    /**
     * 获取AI工作流详细信息(包含节点和连线)
     */
    @GetMapping(value = "/{workflowId}")
    public AjaxResult getInfo(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(fsAiWorkflowService.selectFsAiWorkflowById(workflowId));
    }

    /**
     * 保存AI工作流(新增或更新)
     */
    @Log(title = "AI工作流", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody FsAiWorkflowSaveParam param) {
        Long workflowId = fsAiWorkflowService.saveFsAiWorkflow(param);
        return AjaxResult.success(workflowId);
    }

    /**
     * 修改AI工作流状态
     */
    @Log(title = "AI工作流", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{workflowId}/{status}")
    public AjaxResult updateStatus(@PathVariable("workflowId") Long workflowId,
                                   @PathVariable("status") Integer status) {
        return toAjax(fsAiWorkflowService.updateFsAiWorkflowStatus(workflowId, status));
    }

    /**
     * 删除AI工作流
     */
    @Log(title = "AI工作流", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workflowIds}")
    public AjaxResult remove(@PathVariable Long[] workflowIds) {
        return toAjax(fsAiWorkflowService.deleteFsAiWorkflowByIds(workflowIds));
    }

    /**
     * 复制AI工作流
     */
    @Log(title = "AI工作流", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{workflowId}")
    public AjaxResult copy(@PathVariable("workflowId") Long workflowId) {
        Long newWorkflowId = fsAiWorkflowService.copyFsAiWorkflow(workflowId);
        if (newWorkflowId != null) {
            return AjaxResult.success(newWorkflowId);
        }
        return AjaxResult.error("复制失败，工作流不存在");
    }

    /**
     * 获取所有启用的节点类型
     */
    @GetMapping("/nodeTypes")
    public AjaxResult getNodeTypes() {
        List<FsAiWorkflowNodeType> list = fsAiWorkflowService.selectAllEnabledNodeTypes();
        return AjaxResult.success(list);
    }
    /**
     * 导出工作流流程图JSON
     * 包含节点信息、连接顺序、节点类型
     */
//    @Log(title = "AI工作流", businessType = BusinessType.EXPORT)
    @GetMapping("/exportJson/{workflowId}")
    public AjaxResult exportJson(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(fsAiWorkflowService.exportWorkflowJson(workflowId));
    }
    /**
     * 分页销售
     */
    @GetMapping("/listCompanyUser")
    public AjaxResult listCompanyUser() {
        return AjaxResult.success(fsAiWorkflowService.listCompanyUser());
    }

    /**
     * 查销售
     */
    @GetMapping("/getCompanyUserById/{companyUserId}")
    public AjaxResult getCompanyUserById(@PathVariable("companyUserId") Long companyUserId) {
        return AjaxResult.success(fsAiWorkflowService.getCompanyUserById(companyUserId));
    }

//    /**
//     * 查销售是否已经被绑定
//     */
//    @GetMapping("/checkCompanyUserBeUsed/{companyUserId}")
//    public AjaxResult checkCompanyUserBeUsed(@PathVariable("companyUserId") Long companyUserId) {
//        return AjaxResult.success(fsAiWorkflowService.checkCompanyUserBeUsed(companyUserId));
//    }

    /**
     * 查工作流已绑定的销售
     */
    @GetMapping("/getBindCompanyUserByWorkflowId/{workflowId}")
    public AjaxResult getBindCompanyUserByWorkflowId(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(fsAiWorkflowService.getBindCompanyUserByWorkflowId(workflowId));
    }

    /**
     * 修改工作流绑定的销售
     */
    @PostMapping("/updateWorkflowBindCompanyUser")
    public AjaxResult updateWorkflowBindCompanyUser(@RequestBody FsAiWorkflowUpdateBindWCParam param) {
        return fsAiWorkflowService.updateWorkflowBindCompanyUser(param);
    }

}
