package com.cm.plugin.controller.apis;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.component.SecurityComponent;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.plugin.pojo.dtos.SensitiveWordsDTO;
import com.cm.plugin.pojo.vos.SensitiveWordsVO;
import com.cm.plugin.service.ISensitiveWordsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SensitiveWordsController
 * @Description: 敏感词
 * @Author: WenG
 * @Date: 2020-03-13 23:16
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "敏感词接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/sensitivewords")
public class SensitiveWordsController extends AbstractController {

    @Autowired
    private ISensitiveWordsService sensitiveWordsService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "新增敏感词", notes = "新增敏感词接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savesensitivewords")
    @CheckRequestBodyAnnotation
    public SuccessResult saveSensitiveWords(@RequestBody SensitiveWordsVO sensitiveWordsVO) throws Exception {
        return sensitiveWordsService.saveSensitiveWords(sensitiveWordsVO);
    }

    @ApiOperation(value = "删除敏感词(id列表)", notes = "删除敏感词(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removesensitivewords/{ids}")
    public SuccessResult removeSensitiveWords(@PathVariable("ids") String ids) throws RemoveException {
        return sensitiveWordsService.removeSensitiveWords(ids);
    }

    @ApiOperation(value = "修改敏感词", notes = "修改敏感词接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sensitiveWordsId", value = "敏感词ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatesensitivewords/{sensitiveWordsId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateSensitiveWords(@PathVariable("sensitiveWordsId") String sensitiveWordsId, @RequestBody SensitiveWordsVO sensitiveWordsVO) throws Exception {
        return sensitiveWordsService.updateSensitiveWords(sensitiveWordsId, sensitiveWordsVO);
    }

    @ApiOperation(value = "敏感词详情(通过ID)", notes = "敏感词详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sensitiveWordsId", value = "敏感词ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getsensitivewordsbyid/{sensitiveWordsId}")
    public SensitiveWordsDTO getSensitiveWordsById(@PathVariable("sensitiveWordsId") String sensitiveWordsId) throws SearchException {
        return sensitiveWordsService.getSensitiveWordsById(sensitiveWordsId);
    }

    @ApiOperation(value = "敏感词列表", notes = "敏感词列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listsensitivewords")
    public List<SensitiveWordsDTO> listSensitiveWords() throws SearchException {
        Map<String, Object> params = requestParams();
        return sensitiveWordsService.listSensitiveWords(params);
    }

    @ApiOperation(value = "敏感词分页列表", notes = "敏感词分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "Integer", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagesensitivewords")
    public SuccessResultList<List<SensitiveWordsDTO>> listPageSensitiveWords(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return sensitiveWordsService.listPageSensitiveWords(page);
    }

    @ApiOperation(value = "导入敏感词", notes = "导入敏感词接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savesensitivewordsexcel")
    public SuccessResult saveSensitiveWordsExcel(MultipartFile excel) throws IOException {
        return sensitiveWordsService.saveSensitiveWordsExcel(excel);
    }

}