package com.ruoyi.cms.controller.cms;

import com.ruoyi.cms.domain.SensitiveWordData;
import com.ruoyi.cms.service.SensitiveWordDataService;
import com.ruoyi.cms.util.EasyExcelUtils;
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 io.swagger.annotations.Api;
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 org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;


/**
 * 敏感词库
 *
 * @author 
 * @email 
 * @date 2025-10-10 10:42:16
 */
@RestController
@RequestMapping("/cms/sensitiveworddata")
@Api(tags = "后台：敏感词库")
public class SensitiveWordDataController extends BaseController {
    @Autowired
    private SensitiveWordDataService sensitiveWordDataService;

    /**
     * 列表
     */
    @ApiOperation("敏感词库详细信息")
    @PreAuthorize("@ss.hasPermi('cms:sensitiveworddata:list')")
    @GetMapping("/list")
    public TableDataInfo list(SensitiveWordData sensitiveWordData){
        startPage();
        List<SensitiveWordData> list = sensitiveWordDataService.selectSensitiveworddataList(sensitiveWordData);
        return getDataTable(list);
    }

    /**
     * 获取详细信息
     */
    @ApiOperation("获取敏感词库详细信息")
    @PreAuthorize("@ss.hasPermi('cms:sensitiveworddata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult list(@PathVariable("id") Long id){
        return success(sensitiveWordDataService.selectById(id));
    }


    /**
     * 保存
     */
    @ApiOperation("新增敏感词")
    @PreAuthorize("@ss.hasPermi('cms:sensitiveworddata:add')")
    @Log(title = "敏感词", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult save(@RequestBody SensitiveWordData sensitiveWordData){

        return toAjax(sensitiveWordDataService.insertSensitiveworddata(sensitiveWordData));
    }

    /**
     * 修改
     */
    @ApiOperation("修改敏感词")
    @PreAuthorize("@ss.hasPermi('cms:sensitiveworddata:edit')")
    @Log(title = "敏感词", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SensitiveWordData sensitiveWordData){
        return toAjax(sensitiveWordDataService.updateSensitiveworddata(sensitiveWordData));
    }

    /**
     * 删除敏感词
     */
    @ApiOperation("删除敏感词")
    @PreAuthorize("@ss.hasPermi('cms:sensitiveworddata:remove')")
    @Log(title = "敏感词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sensitiveWordDataService.deleteSensitiveworddataIds(ids));
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/exoprt")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception
    {
        try
        {
            InputStream inputStream = file.getInputStream();
            EasyExcelUtils.readExcelByBatch(inputStream, SensitiveWordData.class, 100, list -> {
                // 处理逻辑：如批量保存到数据库
                sensitiveWordDataService.batchInsert(list);
            });
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
