package com.ruoyi.project.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.domain.TrsResults;
import com.ruoyi.project.domain.TrsResultsLog;
import com.ruoyi.project.domain.TrsWordResults;
import com.ruoyi.project.mapper.TrsResultsLogMapper;
import com.ruoyi.project.service.ITrsWordResultsService;
import com.ruoyi.project.utils.JiaoduiUtils;
import com.ruoyi.project.utils.TrsPUtils;
import com.ruoyi.project.utils.TrsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

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

/**
 * 配置管理Controller
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@Api("智能审校")
@Controller
@RequestMapping("/project/jiaodui")
public class TrsJiaoduiController extends BaseController
{
    private String prefix = "project/jiaodui";

    @Autowired
    private TrsUtils trsUtils;

    @Autowired
    private TrsPUtils trsPUtils;

    @Autowired
    private ITrsWordResultsService trsWordResultsService;
    /**
     * 在线校对
     */
    @RequiresPermissions("jiaodui:local")
    @GetMapping("/edit")
    public String edit(ModelMap mmap)
    {
        return prefix + "/edit";
    }

    /**
     * 请求参数对象
     */
    public static class JiaoduiRequest {
        private String content;
        private String title;
        private String type;

        // getter 和 setter
        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

    @ApiOperation("文本校对")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "校对内容", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "日志记录（1记录日志，0不记录）", required = true, dataType = "String", dataTypeClass = String.class)
    })
    @RequiresPermissions("jiaodui:local")
    @RequestMapping("/jiaodui")  // 改为 POST 映射
    @ResponseBody
    public AjaxResult jiaodui(@RequestBody JiaoduiRequest request, HttpServletRequest httpRequest)
    {
        // 参数校验
        if (request.getContent() == null || request.getContent().trim().isEmpty()) {
            return AjaxResult.error("校对内容不能为空");
        }

        TrsResultsLog trsResultsLog = trsUtils.getJiaodui(request.getContent(), request.getTitle(), request.getType(), httpRequest);
        trsPUtils.getJiaodui(request.getContent(), request.getTitle(), request.getType(), httpRequest, trsResultsLog);

        TrsWordResults trsWordResults = new TrsWordResults();
        trsWordResults.setLogId(trsResultsLog.getId());
        List<TrsWordResults> list = trsWordResultsService.selectTrsWordResultsList(trsWordResults);

        // 遍历列表，替换特定的correctWord值
        for (TrsWordResults result : list) {
            if ("敏感词(其他)！".equals(result.getCorrectWord())) {
                result.setCorrectWord("建议删除敏感内容");
            }
        }
        return AjaxResult.success("校对完成", list);
    }


/*    @ApiImplicitParams({
    @ApiImplicitParam(name = "content", value = "校对内容", required = true, dataType = "String", dataTypeClass = String.class),
    @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String", dataTypeClass = String.class),
    @ApiImplicitParam(name = "type", value = "日志记录（1记录日志，0不记录）", required = true, dataType = "String", dataTypeClass = String.class)
    })
    @RequiresPermissions("jiaodui:local")
    @RequestMapping ("/jiaodui")
    @ResponseBody
    public AjaxResult jiaodui(String content, String title, String type, HttpServletRequest request)
    {
        TrsResultsLog trsResultsLog = trsUtils.getJiaodui(content,title, type,  request);
        trsPUtils.getJiaodui(content,title, type,  request,trsResultsLog);

        TrsWordResults trsWordResults = new TrsWordResults();
        trsWordResults.setLogId(trsResultsLog.getId());
        List<TrsWordResults> list = trsWordResultsService.selectTrsWordResultsList(trsWordResults);
        // 遍历列表，替换特定的correctWord值
        for (TrsWordResults result : list) {
            if ("敏感词(其他)！".equals(result.getCorrectWord())) {
                result.setCorrectWord("建议删除敏感内容");
            }
        }
        return AjaxResult.success("校对完成", list);
    }*/


}
