package com.cm.common.article.controller.apis.articlecontent;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.article.pojo.dtos.articlecontent.ArticleContentDTO;
import com.cm.common.article.pojo.vos.articlecontent.ArticleContentVO;
import com.cm.common.article.service.articlecontent.IArticleContentService;
import com.cm.common.base.AbstractController;
import com.cm.common.component.SecurityComponent;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.CurrentUserIdInfoDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.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.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 + "/articlecontent")
public class ArticleContentController extends AbstractController {

    @Autowired
    private IArticleContentService articleContentService;
    @Autowired
    private SecurityComponent securityComponent;

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

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

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

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

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

    @ApiOperation(value = "文章内容列表", notes = "文章内容列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleCategoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @GetMapping("listarticlecontent")
    public List<ArticleContentDTO> listArticleContent() throws SearchException {
        Map<String, Object> params = requestParams();
        return articleContentService.listArticleContent(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 = "articleCategoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagearticlecontent")
    public SuccessResultList<List<ArticleContentDTO>> listPageArticleContent(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return articleContentService.listPageArticleContent(page);
    }

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

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

}