package cn.com.tenlion.configcontent.controller.api.contentantistop;

import cn.com.tenlion.configcontent.pojo.dtos.contentantistop.ContentAntistopDTO;
import cn.com.tenlion.configcontent.pojo.vos.contentantistop.ContentAntistopVO;
import cn.com.tenlion.configcontent.service.contentantistop.IContentAntistopService;
import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.common.component.SecurityComponent;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.CurrentUserIdInfoDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import io.swagger.annotations.*;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ContentAntistopController
 * @Description: 评论敏感词库
 * @Author: WenG
 * @Date: 2020-05-08 23:30
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "评论敏感词库接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/contentantistop")
public class ContentAntistopController extends DefaultBaseController {

    @Autowired
    private IContentAntistopService contentAntistopService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "敏感词导入", notes = "敏感词导入接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("saveuploadexcel")
    public SuccessResult saveUploadExcel(MultipartFile excel) throws Exception {
        String extName = ""; // 扩展名格式：
        String saveFilePath = "";
        try {
            if (excel.getOriginalFilename().lastIndexOf(".") >= 0){
                extName = excel.getOriginalFilename().substring(excel.getOriginalFilename().lastIndexOf("."));
            }
            String fileName = UUIDUtil.getUUID();
            String path = "D:\\News\\tempFile\\";
            File file = new File(path);
            if(!file.exists()) {
                file.mkdirs();
            }
            File saveFile = new File(path + fileName +  extName);
            FileUtils.copyInputStreamToFile(excel.getInputStream(), saveFile);
            saveFilePath = path + fileName + extName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentAntistopService.saveContentAntistopExcel(saveFilePath);
    }


    @ApiOperation(value = "评论敏感词库", notes = "评论敏感词库接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savecontentantistop")
    @CheckRequestBodyAnnotation
    public SuccessResult saveContentAntistop(@RequestBody ContentAntistopVO contentAntistopVO) throws Exception {
        return contentAntistopService.saveContentAntistop(contentAntistopVO);
    }

    @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("removecontentantistop/{ids}")
    public SuccessResult removeContentAntistop(@PathVariable("ids") String ids) throws RemoveException {
        return contentAntistopService.removeContentAntistop(ids);
    }

    @ApiOperation(value = "修改评论敏感词库", notes = "修改评论敏感词库接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentAntistopId", value = "评论敏感词库ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatecontentantistop/{contentAntistopId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateContentAntistop(@PathVariable("contentAntistopId") String contentAntistopId, @RequestBody ContentAntistopVO contentAntistopVO) throws Exception {
        return contentAntistopService.updateContentAntistop(contentAntistopId, contentAntistopVO);
    }

    @ApiOperation(value = "评论敏感词库详情(通过ID)", notes = "评论敏感词库详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentAntistopId", value = "评论敏感词库ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcontentantistopbyid/{contentAntistopId}")
    public ContentAntistopDTO getContentAntistopById(@PathVariable("contentAntistopId") String contentAntistopId) throws SearchException {
        return contentAntistopService.getContentAntistopById(contentAntistopId);
    }

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

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

    @ApiOperation(value = "当前用户id信息", notes = "当前用户id信息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcurrentuseridinfo")
    public CurrentUserIdInfoDTO getCurrentUserIdInfo() {
        return securityComponent.getCurrentUserIdInfo();
    }

}