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.param.home.HomeArticleCategoryPageParam;
import com.fs.hisStore.param.home.HomeArticlePageParam;
import com.fs.hisStore.param.home.HomeArticleViewListParam;
import com.fs.hisStore.service.IFsHomeArticleCategoryScrmService;
import com.fs.hisStore.service.IFsHomeArticleScrmService;
import com.fs.hisStore.service.IFsHomeArticleViewScrmService;
import com.fs.hisStore.vo.home.*;
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.GetMapping;
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("倍力之家相关接口")
@RestController
@RequestMapping(value = "/store/app/home/article")
public class HomeArticleScrmController extends AppBaseController {

    @Autowired
    private IFsHomeArticleCategoryScrmService homeCategoryService;

    @Autowired
    private IFsHomeArticleScrmService homeArticleService;

    @Autowired
    private IFsHomeArticleViewScrmService homeArticleViewService;

    @Login
    @ApiOperation("分页获取文章分类")
    @GetMapping("/getCategory")
    public R getCategory(HomeArticleCategoryPageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<HomeArticleCategoryListVO> list = homeCategoryService.selectHomeArticleCategoryPageList(param);
        PageInfo<HomeArticleCategoryListVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

    @Login
    @ApiOperation("分页获取文章列表")
    @GetMapping("/get")
    public R getArticle(HomeArticlePageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<HomeArticleListVO> list = homeArticleService.selectHomeArticlePageList(param);
        PageInfo<HomeArticleListVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

    @Login
    @ApiOperation("获取文章详情")
    @GetMapping("/details")
    public R getArticleDetails(@RequestParam Long articleId) {
        // 合并 1、获取详情
        HomeArticleDetailsVO details = homeArticleService.getHomeArticleDetailsById(articleId);

        // 合并 2、更新记录
        long userId = Long.parseLong(getUserId());
        int i = homeArticleViewService.addArticleRecord(articleId, userId);
        if(i > 0){
            return R.ok().put("data", details);
        } else {
            return R.error("获取详情失败");
        }

    }

//    @Login
//    @ApiOperation("记录文章阅读记录，更新阅读数")
//    @PostMapping("/addRecord")
//    public R addArticleRecord(@RequestParam Long articleId) {
//        long userId = Long.parseLong(getUserId());
//        int i = homeArticleViewService.addArticleRecord(articleId, userId);
//        if (i > 0) {
//            return R.ok();
//        } else {
//            return R.error();
//        }
//    }

    @ApiOperation("获取阅读人员列表")
    @GetMapping("/getViewsList")
    public R getArticleViewList(HomeArticleViewListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<HomeArticleViewsListVO> list = homeArticleViewService.selectHomeArticleViewsList(param);
        PageInfo<HomeArticleViewsListVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

}
