package com.fs.app.controller;


import com.fs.app.utils.JwtUtils;
import com.fs.common.core.domain.R;
import com.fs.his.domain.FsArticle;
import com.fs.his.domain.FsArticleCate;
import com.fs.his.domain.FsDepartment;
import com.fs.his.param.BaseParam;
import com.fs.his.param.FsArticleListUParam;
import com.fs.his.service.IFsArticleCateService;
import com.fs.his.service.IFsArticleService;
import com.fs.his.service.IFsDepartmentService;
import com.fs.his.service.IFsDoctorArticleService;
import com.fs.his.vo.FsArticleListUVO;
import com.fs.his.vo.FsDoctorListUVO;
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.cache.annotation.Cacheable;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("文章接口")
@RestController
@RequestMapping(value="/app/article")
public class ArticleController {
	@Autowired
    JwtUtils jwtUtils;
	@Autowired
	private IFsArticleService articleService;

	@Autowired
	private IFsArticleCateService articleCateService;

	@Cacheable(value="getArticleCateList", key = "#status != null ? #status : 'getArticleCateList'")
	@ApiOperation("获取文章分类列表")
	@GetMapping("/getArticleCateList")
	public R getArticleCateList(@RequestParam(required = false) Integer status){
		Map<String,Object> params = new HashMap<>();
		params.put("status", status);
		List<FsArticleCate> list=articleCateService.selectFsArticleCateAllListVO(params);
		return R.ok().put("data",list);
	}
	@ApiOperation("获取文章列表")
	@Cacheable(value="getArticleList", key="#param")
	@GetMapping("/getArticleList")
	public R getArticleList(FsArticleListUParam param){
		PageHelper.startPage(param.getPageNum(), param.getPageSize());
		try {
			List<FsArticleListUVO> list=articleService.selectFsArticleListUVO(param);
			PageInfo<FsArticleListUVO> listPageInfo=new PageInfo<>(list);
			return R.ok().put("data",listPageInfo);
		} catch (Exception e){
			return R.error("操作异常");
		}
	}

	@ApiOperation("获取文章详情")
	@GetMapping("/getArticleById")
//	@Cacheable(value="getArticleById", key="#articleId")
	public R getArticleById(@RequestParam("articleId")Long articleId, HttpServletRequest request){
		FsArticle article=articleService.selectFsArticleByArticleId(articleId);
		//更新VIEW
		articleService.updateFsArticleViews(article.getArticleId());
		return R.ok().put("data",article);
	}

}
