mitao
2024-09-13 b3cef9d27013afee054bdd467defd0b6be218526
ruoyi-modules/ruoyi-article/src/main/java/com/ruoyi/article/controller/management/MgtSensitiveWordsController.java
@@ -1,11 +1,31 @@
package com.ruoyi.article.controller.management;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.article.controller.management.dto.MgtSensitiveWordsDTO;
import com.ruoyi.article.controller.management.dto.MgtSensitiveWordsQuery;
import com.ruoyi.article.controller.management.vo.MgtSensitiveWordsVO;
import com.ruoyi.article.service.ISensitiveWordsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.page.CollUtils;
import com.ruoyi.common.core.utils.page.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
 * <p>
@@ -16,12 +36,90 @@
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/sensitive-words")
@RequestMapping("/mgt/sensitive-words")
@RequiredArgsConstructor
@Api(value = "管理后台-敏感词管理相关接口", tags = "管理后台-敏感词管理相关接口")
public class MgtSensitiveWordsController {
    private final ISensitiveWordsService sensitiveWordsService;
    /**
     * 获取敏感词列表的分页数据
     *
     * @param query 管理后台-敏感词查询对象
     * @return PageDTO<MgtSensitiveWordsVO>
     */
    @ApiOperation("获取敏感词列表的分页数据")
    @PostMapping("/page")
    public R<PageDTO<MgtSensitiveWordsVO>> getSensitiveWordsPage(
            @Validated @RequestBody MgtSensitiveWordsQuery query) {
        return R.ok(sensitiveWordsService.getSensitiveWordsPage(query));
    }
    /**
     * 添加敏感词
     *
     * @param dto 敏感词数据传输对象
     */
    @ApiOperation("添加敏感词")
    @PostMapping("/save")
    public R<?> saveSensitiveWords(@Validated @RequestBody MgtSensitiveWordsDTO dto) {
        sensitiveWordsService.saveSensitiveWords(dto);
        return R.ok();
    }
    /**
     * 上传敏感词
     *
     * @param id 敏感词id
     */
    @ApiOperation("删除敏感词")
    @DeleteMapping("/{id}")
    public R<?> delSensitiveWords(
            @ApiParam(name = "id", value = "敏感词id", required = true) @PathVariable("id") Long id) {
        sensitiveWordsService.removeById(id);
        return R.ok();
    }
    /**
     * 下载导入模板
     *
     * @param response
     */
    @ApiOperation("下载导入模板")
    @GetMapping("/exportTemplate")
    public void exportSensitiveWordsTemplate(HttpServletResponse response) {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType(
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        try {
            String fileName = URLEncoder.encode("敏感词导入模板", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition",
                    "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), MgtSensitiveWordsDTO.class)
                    .sheet("敏感词导入模板")
                    .doWrite(CollUtils.emptyList());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 敏感词导入
     *
     * @param file 导入文件
     * @return
     */
    @ApiOperation("敏感词导入")
    @PostMapping("/importSensitiveWords")
    public R<?> importSensitiveWords(@RequestPart("file") MultipartFile file) {
        try {
            sensitiveWordsService.importSensitiveWords(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }
}