package com.webchat.controller.client;


import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.ClickEvent;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.SafeClick;
import com.webchat.config.annotation.ValidateLogin;
import com.webchat.config.annotation.ValidatePermission;
import com.webchat.domain.vo.request.publicaccount.SaveArticleRequestVO;
import com.webchat.domain.vo.response.publicaccount.ArticleBaseResponseVO;
import com.webchat.service.publicaccount.ArticleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "【客户端】公众号文章管理")
@RestController
@RequestMapping("/api/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;


    /**
     * 文章发布
     *
     * @param saveArticleRequest
     * @return
     */
    @ValidateLogin
    @ValidatePermission(role = RoleCodeEnum.ADMIN)
    @SafeClick(event = ClickEvent.SAVE_ARTICLE, time = 2000, message = "2s内只能提交一次")
    @PostMapping("/save")
    public APIResponseBean<Long> save(@RequestBody SaveArticleRequestVO saveArticleRequest) {
        // 基础参数校验
        saveArticleRequest.validaRequestParam();
        // 设置文章作者
        String userId = SessionHelper.getCurrentUserId();
        saveArticleRequest.setAuthor(userId);
        Long articleId = articleService.save(saveArticleRequest);
        return APIResponseBeanUtil.success(articleId);
    }

    /**
     * 文章详情接口
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public APIResponseBean<ArticleBaseResponseVO> detail(@PathVariable Long id) {

        return APIResponseBeanUtil.success( articleService.viewArticle(id));
    }

    /**
     * 文章列表查询接口
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidatePermission(role = RoleCodeEnum.ADMIN)
    @GetMapping("/page")
    public APIPageResponseBean<List<ArticleBaseResponseVO>> page(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return articleService.page(pageNo, pageSize);
    }
}
