package com.yc.education.controller;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.NewsCenter;
import com.yc.education.model.NewsType;
import com.yc.education.service.INewsCenterService;
import com.yc.education.service.INewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 新闻中心控制层
 * @author Hickey
 * @create 2019-07-23 10:48
 */
@Controller
public class NewsCenterController {

    @Autowired
    private INewsCenterService iNewsCenterService;//注入新闻信息service

    @Autowired
    private INewsTypeService iNewsTypeService;//注入新闻类别service

    /**
     * 前台  新闻中心
     * @return
     */
    @RequestMapping("news.html")
    public ModelAndView newsCenter(Integer id,
                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "3") Integer pageSize){
        ModelAndView mav  = new ModelAndView();

        //查询类别中权重最大的类别id,传给新闻中心界面 按权重决定新闻界面优先展示那个类别下的新闻
        NewsType  findId =  iNewsTypeService.listIdByWeight();

        mav.addObject("findId",findId);//根据权重查出的id传给前台

        List<NewsType> listAllType = iNewsTypeService.listAllType();//查询类别信息

        mav.addObject("listAllType",listAllType);//将类别信息传给前台

        List<NewsCenter> listAllNews =iNewsCenterService.listAllNews(id,pageNum,pageSize);//根据id查询所有新闻信息

        mav.addObject("pageinfo",new PageInfo<NewsCenter>(listAllNews));//查询所有新闻信息并分页的结果集

        mav.addObject("id",id);//传分类id,将根据分类id查出来的数据进行分页

        mav.addObject("pageNum",pageNum);//页码

        mav.addObject("pageSize",pageSize);//每页条数
        return mav;
    }
    /**
     * 前台 新闻中心 新闻详情
     * @return
     * @param id
     */
    @RequestMapping("newsdetail.html")
    public ModelAndView listNewsById(Integer id){

        ModelAndView mav = new ModelAndView();

        //查询类别中权重最大的类别id,传给新闻中心界面 按权重决定新闻界面优先展示那个类别下的新闻
        NewsType  findId =  iNewsTypeService.listIdByWeight();

        mav.addObject("findId",findId);//根据权重查出的id传给前台

        NewsCenter listNewsById = iNewsCenterService.selectByKey(id);//根据id查询新闻详情

        mav.addObject("listNewsById",listNewsById);//将详情信息传给前台


        return  mav;
    }
}
