package com.ruoyi.web.controller.sensitive;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sensitive.cache.LastUpdateTimeCache;
import com.ruoyi.sensitive.domain.Word;
import com.ruoyi.sensitive.dto.SensitiveConfig;
import com.ruoyi.sensitive.dto.WordTestResp;
import com.ruoyi.sensitive.service.SensitiveWordManager;
import com.ruoyi.sensitive.service.WordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 敏感词
 *
 * @author 77
 * @date 2024-02-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sensitive/word")
public class WordController extends BaseController {

    private final WordService wordService;
    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    @Autowired
    private LastUpdateTimeCache cache;
    @Autowired
    private SensitiveWordManager sensitiveWordManager;

    @PostMapping("/updateSensitiveConfig")
    public R<Void> updateSensitiveConfig(@RequestBody SensitiveConfig sensitiveConfig){
        sensitiveWordManager.updateSensitiveConfig(sensitiveConfig);
        return R.ok();
    }


    @GetMapping("/getSensitiveConfig")
    public R<SensitiveConfig> getSensitiveConfig(){
        return R.ok(sensitiveWordManager.getSensitiveConfig());
    }

    @GetMapping("/lastRefresh")
    public R<LocalDateTime> lastRefresh() {
        LocalDateTime time = cache.getRefreshTime();
        return R.ok(time);
    }

    /**
     * 查询敏感词列表
     */
    @SaCheckPermission("sensitive:word:list")
    @GetMapping("/list")
    public TableDataInfo<Word> list(Word bo, PageQuery pageQuery) {
        Page<Word> page = wordService.page(pageQuery.build(), Wrappers.lambdaQuery(bo));
        return TableDataInfo.build(page);
    }

    /**
     * 导出敏感词列表
     */
    @SaCheckPermission("sensitive:word:export")
    @Log(title = "敏感词", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Word bo, HttpServletResponse response) {
        List<Word> list = wordService.list(Wrappers.lambdaQuery(bo));
        ExcelUtil.exportExcel(list, "敏感词", Word.class, response);
    }

    /**
     * 获取敏感词详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sensitive:word:query")
    @GetMapping("/{id}")
    public R<Word> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(wordService.getById(id));
    }

    /**
     * 新增敏感词
     */
    @SaCheckPermission("sensitive:word:add")
    @Log(title = "敏感词", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Word bo) {
        return toAjax(wordService.saveWord(bo));
    }

    /**
     * 修改敏感词
     */
    @SaCheckPermission("sensitive:word:edit")
    @Log(title = "敏感词", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Word bo) {
        return toAjax(wordService.updateWordById(bo));
    }

    /**
     * 删除敏感词
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sensitive:word:remove")
    @Log(title = "敏感词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(wordService.removeWordBatchByIds(Arrays.asList(ids)));
    }


    @Log(title = "刷新敏感词", businessType = BusinessType.OTHER)
    @GetMapping("/refresh")
    public R<Void> refresh() {
        sensitiveWordBs.init();
        cache.setRefreshTime();
        return R.ok();
    }


    @PostMapping("/test")
    public R<WordTestResp> test(@RequestBody Word word) {
        return R.ok(sensitiveWordManager.test(word.getWord()));
    }
}
