package com.ruoyi.web.controller.ai;

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

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.ai.domain.AiTag;
import com.ruoyi.ai.service.IAiTagService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * ai-标签管理Controller
 * 
 * @author shi
 * @date 2025-11-24
 */
@RestController
@RequestMapping("/ai/tag")
public class AiTagController extends BaseController
{
    @Autowired
    private IAiTagService aiTagService;

    /**
     * 查询ai-标签管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:list')")
    @GetMapping("/list")
    public AjaxResult list(AiTag aiTag)
    {
        List<AiTag> list = aiTagService.selectAiTagList(aiTag);
        return success(list);
    }


    /**
     * 导出ai-标签管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:export')")
    @Log(title = "ai-标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiTag aiTag)
    {
        List<AiTag> list = aiTagService.selectAiTagList(aiTag);
        ExcelUtil<AiTag> util = new ExcelUtil<AiTag>(AiTag.class);
        util.exportExcel(response, list, "ai-标签管理数据");
    }

    /**
     * 获取ai-标签管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aiTagService.selectAiTagById(id));
    }

    /**
     * 新增ai-标签管理
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:add')")
    @Log(title = "ai-标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiTag aiTag)
    {
        return toAjax(aiTagService.insertAiTag(aiTag));
    }

    /**
     * 修改ai-标签管理
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:edit')")
    @Log(title = "ai-标签管理", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@RequestBody AiTag aiTag)
    {
        int rows = aiTagService.updateAiTag(aiTag);
        if (aiTag.getStatus() == 1 && aiTag.getParentId() != 0l) {
            // 查询是否父级是否开启并且还有其他子级开启数量
            boolean b = aiTagService.checkStatus(aiTag.getParentId());
            if (!b) {
                throw new ServiceException("必须开启一个子级标签！");
            }
        }
        return toAjax(rows);
    }

    /**
     * 删除ai-标签管理
     */
    @PreAuthorize("@ss.hasPermi('ai:tag:remove')")
    @Log(title = "ai-标签管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aiTagService.deleteAiTagByIds(ids));
    }
}
