package com.fzm.mall.server.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.system.entity.dto.SysSensitiveWordsDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysSensitiveWordsSaveDTO;
import com.fzm.mall.server.admin.system.service.ISysSensitiveWordsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * <p>
 * 系统中心-敏感词 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-29
 */
@Api(tags = "系统中心")
@RestController
@RequestMapping("/sys-sensitive-words")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysSensitiveWordsController {

    private final ISysSensitiveWordsService sensitiveWordsService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "系统中心-敏感词-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "系统中心-敏感词-新增", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid SysSensitiveWordsSaveDTO sensitiveWordsSaveDTO) {

        sensitiveWordsService.save(sensitiveWordsSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-敏感词-删除")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "系统中心-敏感词-删除", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{sensitiveWordsId}")
    public ResponseVO delete(
            @ApiParam(value = "sensitiveWordsId", required = true) @PathVariable(value = "sensitiveWordsId") String sensitiveWordsId) {
        sensitiveWordsService.delete(sensitiveWordsId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-敏感词-主键查询")
    @ApiOperationSupport(order = 3)
    @GetMapping("/{sensitiveWordsId}")
    public ResponseVO<SysSensitiveWordsDTO> read(
            @ApiParam(value = "sensitiveWordsId", required = true) @PathVariable(value = "sensitiveWordsId") String sensitiveWordsId) {
        SysSensitiveWordsDTO result = sensitiveWordsService.read(sensitiveWordsId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "系统中心-敏感词-列表查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "敏感词") @RequestParam(required = false) String sensitiveWords,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<SysSensitiveWordsDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        IPage<SysSensitiveWordsDTO> result = sensitiveWordsService.page(page, sensitiveWords);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "系统中心-敏感词-测试")
    @ApiOperationSupport(order = 5)
    @GetMapping("/test")
    public ResponseVO test(@ApiParam(value = "测试敏感词文本") @RequestParam String text) {
        ResponseVO responseVO;
        Set<String> result = sensitiveWordsService.test(text);
        if (CollectionUtils.isEmpty(result)) {
            responseVO = resFac.getSimpleSuccessResponse();
        } else {
            responseVO = resFac.getFailResponseWithData(result);
        }
        return responseVO;
    }
}
