package com.cm.common.article.controller.app.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.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.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: ArticleContentAppController
 * @Description: 文章内容
 * @Author: WenG
 * @Date: 2020-04-03 15:37
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "文章内容接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/articlecontent")
public class ArticleContentAppController extends AbstractController {

    @Autowired
    private IArticleContentService articleContentService;

    @ApiOperation(value = "新增文章内容", notes = "新增文章内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savearticlecontent")
    @CheckRequestBodyAnnotation
    public SuccessResult saveArticleContent(@RequestHeader("token") String token, @RequestBody ArticleContentVO articleContentVO) throws Exception {
        return articleContentService.saveArticleContentByToken(token, articleContentVO);
    }

    @ApiOperation(value = "删除文章内容(id列表)", notes = "删除文章内容(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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(@RequestHeader("token") String token, @PathVariable("ids") String ids) throws RemoveException {
        return articleContentService.removeArticleContentByToken(token, ids);
    }

    @ApiOperation(value = "修改文章内容", notes = "修改文章内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "articleContentId", value = "文章内容ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatearticlecontent/{articleContentId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateArticleContent(@RequestHeader("token") String token, @PathVariable("articleContentId") String articleContentId, @RequestBody ArticleContentVO articleContentVO) throws Exception {
        return articleContentService.updateArticleContentByToken(token, articleContentId, articleContentVO);
    }

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

    @ApiOperation(value = "文章内容列表", notes = "文章内容列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "articleCategoryId", value = "文章目录", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listarticlecontent")
    public List<ArticleContentDTO> listArticleContent(@RequestHeader("token") String token) throws SearchException {
        Map<String, Object> params = requestParams();
        params.put("isPublish", 1);
        return articleContentService.listArticleContent(params);
    }

    @ApiOperation(value = "文章内容分页列表", notes = "文章内容分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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(@RequestHeader("token") String token, ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        params.put("isPublish", 1);
        page.setParams(params);
        return articleContentService.listPageArticleContent(page);
    }

    @ApiOperation(value = "统计文章数量", notes = "统计文章数量接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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();
        params.put("isPublish", 1);
        return articleContentService.countArticleCount(params);
    }

}