package com.fjs.scenic.controller;

import java.util.List;

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.RestController;

import com.fjs.scenic.entity.news.News;
import com.fjs.scenic.entity.scenic.Pics;
import com.fjs.scenic.service.news.NewsService;
import com.fjs.scenic.service.scenic.PicsService;
import com.fjs.scenic.utils.ReturnResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/news")
public class NewsController {

	@Autowired
	private PicsService picsService;
	@Autowired
	private NewsService newsService;
	
	@ApiOperation(value="景区图片列表", notes="景区图片列表")
	@GetMapping("/getPics")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "currentPage", value = "页码", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "int", paramType = "query")
	})
	public ReturnResult getPics(int currentPage, int pageSize){
		currentPage = (currentPage == 0) ? 1 : currentPage;
		pageSize = (pageSize == 0) ? 1 : pageSize;
		PageHelper.startPage(currentPage, pageSize);
		List<Pics> picsList = picsService.selectParam("url", "");
		PageInfo<Pics> pageInfo = new PageInfo<>(picsList);
		return ReturnResult.ok(pageInfo);
	}
	
	@ApiOperation(value="新闻公告列表", notes="新闻公告列表")
	@GetMapping("/getNews")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "currentPage", value = "页码", required = true, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "int", paramType = "query")
	})
	public ReturnResult getNews(int currentPage, int pageSize){
		currentPage = (currentPage == 0) ? 1 : currentPage;
		pageSize = (pageSize == 0) ? 1 : pageSize;
		PageHelper.startPage(currentPage, pageSize);
		List<News> newsList = newsService.selectByParam("id,title,time,icon", "");
		PageInfo<News> pageInfo = new PageInfo<>(newsList);
		return ReturnResult.ok(pageInfo);
	}
	
	@ApiOperation(value="获取某个新闻公告", notes="获取某个新闻公告")
	@GetMapping("/getNewsForId")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "公告ID", required = true, dataType = "int", paramType = "query")
	})
	public ReturnResult getNewsForId(Integer id){
		return ReturnResult.ok(newsService.select(new News(id)).get(0));
	}
}
