package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.store.domain.FsArticle;
import com.fs.store.param.FsArticleQueryParam;
import com.fs.store.param.FsArticleViewListQueryParam;
import com.fs.store.service.IFsArticleCateService;
import com.fs.store.service.IFsArticleService;
import com.fs.store.service.IFsArticleViewsService;
import com.fs.store.vo.FsArticleCateListQueryVO;
import com.fs.store.vo.FsArticleListQueryVO;
import com.fs.store.vo.FsArticleViewListQueryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api("健康百科接口")
@RestController
@RequestMapping(value="/app/article")
public class ArticleController extends  AppBaseController {

    @Autowired
    private IFsArticleCateService cateService;
    @Autowired
    private IFsArticleService articleService;
    @Autowired
    private IFsArticleViewsService viewsService;

    @ApiOperation("获取文章分类")
    @GetMapping("/getArticleCate")
        public R getArticleCate(HttpServletRequest request){
        List<FsArticleCateListQueryVO> cates=cateService.selectFsArticleCateListQuery();
        return R.ok().put("data",cates);
    }

    @ApiOperation("获取文章列表")
    @GetMapping("/getArticleList")
    public R getArticleList(FsArticleQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsArticleListQueryVO> list=articleService.selectFsArticleListQuery(param);
        PageInfo<FsArticleListQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @ApiOperation("获取文章详情")
    @GetMapping("/getArticleByArticleId")
    public R getArticleByArticleId(@RequestParam("articleId") Integer articleId){
        FsArticle article=articleService.selectFsArticleById(articleId);
        List<FsArticleViewListQueryVO> viewsList=viewsService.selectFsArticleViewsTopByArticleId(articleId);
        return R.ok().put("data",article).put("views", viewsList);
    }
    @Login
    @ApiOperation("更新阅读数")
    @PostMapping("/updateView")
    public R updateView(@RequestParam("articleId") Long articleId){
        viewsService.updateView(articleId,Long.parseLong(getUserId()));
        return R.ok();
    }
    @ApiOperation("获取阅读人列表")
    @GetMapping("/getArticleViewList")
    public R getArticleViewList(FsArticleViewListQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsArticleViewListQueryVO> viewsList=viewsService.selectFsArticleViewsListQueryVO(param);
        PageInfo<FsArticleViewListQueryVO> listPageInfo=new PageInfo<>(viewsList);
        return R.ok().put("data",listPageInfo);
    }
}
