package com.fs.app.controller.store;


import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.core.domain.R;
import com.fs.hisStore.domain.FsArticleScrm;
import com.fs.hisStore.param.*;
import com.fs.hisStore.service.IFsArticleCateScrmService;
import com.fs.hisStore.service.IFsArticleScrmService;
import com.fs.hisStore.service.IFsArticleViewsScrmService;
import com.fs.hisStore.vo.*;
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="/store/app/article")
public class ArticleScrmController extends AppBaseController {

    @Autowired
    private IFsArticleCateScrmService cateService;
    @Autowired
    private IFsArticleScrmService articleService;
    @Autowired
    private IFsArticleViewsScrmService 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){
        FsArticleScrm 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);
    }
}
