package com.ruoyi.web.controller.ai;

import com.ruoyi.ai.domain.AiManager;
import com.ruoyi.ai.domain.AiTag;
import com.ruoyi.ai.service.IAiManagerService;
import com.ruoyi.ai.service.IAiTagService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * AI管理Controller
 * 
 * @author shi
 * @date 2025-11-13
 */
@RestController
@RequestMapping("/ai/manager")
public class AiManagerController extends BaseController
{
    @Autowired
    private IAiManagerService aiManagerService;

    @Autowired
    private IAiTagService aiTagService;

    /**
     * 查询AI管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiManager aiManager)
    {
        startPage();
        List<AiManager> list = aiManagerService.selectAiManagerList(aiManager);
        return getDataTable(list);
    }

    /**
     * 导出AI管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:export')")
    @Log(title = "AI管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiManager aiManager)
    {
        List<AiManager> list = aiManagerService.selectAiManagerList(aiManager);
        ExcelUtil<AiManager> util = new ExcelUtil<AiManager>(AiManager.class);
        util.exportExcel(response, list, "AI管理数据");
    }

    /**
     * 获取AI管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AiManager data = aiManagerService.selectAiManagerById(id);
        // 查询所有标签
        AiTag aiTag = new AiTag();
        aiTag.setAiId(data.getId());
        aiTag.setParentId(0l);
        aiTag.setStatus(0);
        List<AiTag> tagList = aiTagService.selectAiTagList(aiTag);
        AjaxResult ajaxResult = success();
        ajaxResult.put("data",data);
        ajaxResult.put("tagList",tagList);
        return ajaxResult;
    }

    /**
     * 新增AI管理
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:add')")
    @Log(title = "AI管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiManager aiManager)
    {
        return toAjax(aiManagerService.insertAiManager(aiManager));
    }

    /**
     * 修改AI管理
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:edit')")
    @Log(title = "AI管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiManager aiManager)
    {
        return toAjax(aiManagerService.updateAiManager(aiManager));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "修改AI状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody AiManager aiManager) {
        aiManager.setUpdateBy(getUsername());
        return toAjax(aiManagerService.updateAiStatus(aiManager));
    }

    /**
     * 删除AI管理
     */
    @PreAuthorize("@ss.hasPermi('ai:manager:remove')")
    @Log(title = "AI管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aiManagerService.deleteAiManagerByIds(ids));
    }
}
