package ink.wgink.module.article.controller.api.content;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.article.pojo.dtos.content.ContentDTO;
import ink.wgink.module.article.pojo.vos.content.ContentVO;
import ink.wgink.module.article.service.content.IContentService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.RegexUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ArticleContentController
 * @Description: 文章内容
 * @Author: WenG
 * @Date: 2020-04-03 15:37
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文章内容接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/content")
public class ContentController extends DefaultBaseController {

    @Autowired
    private IContentService contentService;

    @ApiOperation(value = "新增文章内容", notes = "新增文章内容接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult saveArticleContent(@RequestBody ContentVO contentVO) {
        if (!StringUtils.isBlank(contentVO.getLink()) && !RegexUtil.isUrl(contentVO.getLink())) {
            throw new ParamsException("外链格式不正确");
        }
        contentService.save(contentVO);
        return new SuccessResult();
    }

    @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("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        contentService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改文章内容", notes = "修改文章内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "文章内容ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{contentId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("contentId") String contentId, @RequestBody ContentVO contentVO) {
        if (!StringUtils.isBlank(contentVO.getLink()) && !RegexUtil.isUrl(contentVO.getLink())) {
            throw new ParamsException("外链格式不正确");
        }
        contentService.update(contentId, contentVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "修改文章发布状态", notes = "修改文章发布状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "文章内容ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update-publish-status/{contentId}/{isPublish}")
    public SuccessResult updatePublishStatus(@PathVariable("contentId") String contentId, @PathVariable("isPublish") Integer isPublish) {
        if (isPublish != 0 && isPublish != 1) {
            throw new ParamsException("发布状态类型错误");
        }
        contentService.updatePublishStatus(contentId, isPublish);
        return new SuccessResult();
    }

    @ApiOperation(value = "文章内容详情(通过ID)", notes = "文章内容详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "文章内容ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{contentId}")
    public ContentDTO get(@PathVariable("contentId") String contentId) {
        return contentService.get(contentId);
    }

    @ApiOperation(value = "文章内容列表", notes = "文章内容列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @GetMapping("list")
    public List<ContentDTO> list() {
        Map<String, Object> params = requestParams();
        return contentService.list(params);
    }

    @ApiOperation(value = "文章内容分页列表", notes = "文章内容分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", 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"),
            @ApiImplicitParam(name = "categoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<ContentDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return contentService.listPage(page);
    }

    @ApiOperation(value = "统计文章数量", notes = "统计文章数量接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    public SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        return new SuccessResultData<>(contentService.count(params));
    }

}