package com.platform.modules.sensitiveword.controller;

import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.common.validator.ValidatorUtils;
import com.platform.modules.sensitiveword.entity.SensitiveWordEntity;
import com.platform.modules.sensitiveword.service.SensitiveWordService;
import com.platform.modules.sys.controller.AbstractController;

import io.swagger.annotations.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 敏感词
 *
 * @author Anine
 * @date 2019-03-11 15:49:30
 */
@RestController
@RequestMapping("/sensitive/word")
@Api(value = "敏感词", tags = "敏感词")
public class SensitiveWordController extends AbstractController {

	@Autowired
	private SensitiveWordService sensitiveWordService;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sensitive:word:list")
	@ApiOperation("列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "order", value = "排序顺序", allowableValues = "ASC,DESC", paramType = "query")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = SensitiveWordEntity.class, responseContainer = "List")})
	public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params) {
		PageUtils page = sensitiveWordService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@RequiresPermissions("sensitive:word:info")
	@ApiOperation("信息")
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = SensitiveWordEntity.class)})
	public R info(@PathVariable("id") Integer id) {
		SensitiveWordEntity sensitiveWord = sensitiveWordService.selectById(id);

		return R.ok().put("sensitiveWord", sensitiveWord);
	}

	/**
	 * 保存
	 */
	@SysLog("保存敏感词")
	@PostMapping("/save")
	@RequiresPermissions("sensitive:word:save")
	@ApiOperation("保存")
	public R save(@RequestBody SensitiveWordEntity sensitiveWord) {
		ValidatorUtils.validateEntity(sensitiveWord);
		sensitiveWordService.add(sensitiveWord, getUserId());

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改敏感词")
	@PostMapping("/update")
	@RequiresPermissions("sensitive:word:update")
	@ApiOperation("修改")
	public R update(@RequestBody SensitiveWordEntity sensitiveWord) {
		ValidatorUtils.validateEntity(sensitiveWord);
		sensitiveWordService.updateSensitive(sensitiveWord);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("删除敏感词")
	@PostMapping("/delete")
	@RequiresPermissions("sensitive:word:delete")
	@ApiOperation("删除")
	public R delete(@RequestBody Integer[] ids) {
		sensitiveWordService.deleteByIds(ids);

		return R.ok();
	}

	/**
	 * 替换检测
	 */
	@PostMapping("/replace")
	@RequiresPermissions("sensitive:word:replace")
	@ApiOperation("删除")
	public R delete(@RequestParam String txt) {

		return R.ok().put("result", sensitiveWordService.replace(txt));
	}

}
