package com.cm.common.article.controller.app.apis.articlecategory;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
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.SuccessResultList;
import com.cm.common.article.pojo.dtos.articlecategory.ArticleCategoryDTO;
import com.cm.common.article.pojo.vos.articlecategory.ArticleCategoryVO;
import com.cm.common.article.service.articlecategory.IArticleCategoryService;
import io.swagger.annotations.*;
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: ArticleCategoryAppController
 * @Description: 文章类别
 * @Author: WenG
 * @Date: 2020-04-03 15:20
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "文章类别接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/articlecategory")
public class ArticleCategoryAppController extends AbstractController {

    @Autowired
    private IArticleCategoryService articleCategoryService;

    @ApiOperation(value = "新增文章类别", notes = "新增文章类别接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savearticlecategory")
    @CheckRequestBodyAnnotation
    public SuccessResult saveArticleCategory(@RequestHeader("token") String token, @RequestBody ArticleCategoryVO articleCategoryVO) throws Exception {
        return articleCategoryService.saveArticleCategoryByToken(token, articleCategoryVO);
    }

    @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("removearticlecategory/{ids}")
    public SuccessResult removeArticleCategory(@RequestHeader("token") String token, @PathVariable("ids") String ids) throws RemoveException {
        return articleCategoryService.removeArticleCategoryByToken(token, ids);
    }

    @ApiOperation(value = "修改文章类别", notes = "修改文章类别接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "articleCategoryId", value = "文章类别ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatearticlecategory/{articleCategoryId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateArticleCategory(@RequestHeader("token") String token, @PathVariable("articleCategoryId") String articleCategoryId, @RequestBody ArticleCategoryVO articleCategoryVO) throws Exception {
        return articleCategoryService.updateArticleCategoryByToken(token, articleCategoryId, articleCategoryVO);
    }

    @ApiOperation(value = "文章类别详情(通过ID)", notes = "文章类别详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "articleCategoryId", value = "文章类别ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getarticlecategorybyid/{articleCategoryId}")
    public ArticleCategoryDTO getArticleCategoryById(@RequestHeader("token") String token, @PathVariable("articleCategoryId") String articleCategoryId) throws SearchException {
        return articleCategoryService.getArticleCategoryById(articleCategoryId);
    }

    @ApiOperation(value = "文章类别列表", notes = "文章类别列表接口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listarticlecategory"+ ISystemConstant.APP_RELEASE_SUFFIX )
    public List<ArticleCategoryDTO> listArticleCategoryRelease() throws SearchException {
        Map<String, Object> params = requestParams();
        return articleCategoryService.listArticleCategory(params);
    }

    @ApiOperation(value = "文章类别列表", notes = "文章类别列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listarticlecategory")
    public List<ArticleCategoryDTO> listArticleCategory(@RequestHeader("token") String token) throws SearchException {
        Map<String, Object> params = requestParams();
        return articleCategoryService.listArticleCategory(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")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagearticlecategory")
    public SuccessResultList<List<ArticleCategoryDTO>> listPageArticleCategory(@RequestHeader("token") String token, ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return articleCategoryService.listPageArticleCategory(page);
    }

}