package com.yc.education.controller;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.CaseType;
import com.yc.education.model.NewsType;
import com.yc.education.model.SuccessCase;
import com.yc.education.service.ICaseTypeService;
import com.yc.education.service.INewsCenterService;
import com.yc.education.service.INewsTypeService;
import com.yc.education.service.ISuccessCaseService;
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-25 9:06
 */
@Controller
public class SuccessCaseController {

    @Autowired
    private ISuccessCaseService iSuccessCaseService;//注入成功案例服务层

    @Autowired
    private ICaseTypeService iCaseTypeService;//注入案例类别服务层

    @Autowired
    private INewsTypeService iNewsTypeService;//注入新闻类别服务层  将id 传给案例界面的新闻中心 按权重展示新闻类别

    /**
     * 前台 查询案例信息和案例类别信息 并分页
     * 参数id在此方法中无用,只是为了下面listCaseById根据类别id查询案例并分页可以调用同一个方法listAllCase
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("case.html")
    public ModelAndView  showCaseInfo(Integer id,
                                      @RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "6") Integer pageSize){

        ModelAndView  mav  = new ModelAndView();


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

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

        List<CaseType>  listCaseTyp  = iCaseTypeService.listAllCaseType();//查询案列类别

        mav.addObject("listAllCaseType",listCaseTyp);

        List<SuccessCase> list  = iSuccessCaseService.listAllCase(id,pageNum,pageSize);//查询案例信息并分页

        mav.addObject("pageinfo",new PageInfo<SuccessCase>(list));

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

        mav.addObject("pageSize",pageSize);//条数

        return mav;
    }

    /**
     * 前台
     * 根据案例id查询该案例详情
     * @param id
     * @return
     */
    @RequestMapping("casedetail.html")
    public  ModelAndView findCaseById(Integer id){

        ModelAndView  mav = new ModelAndView();

        SuccessCase successCase = iSuccessCaseService.selectByKey(id);//调用通用接口根据id查案列详情

        mav.addObject("findCaseById",successCase);

        return  mav;
    }

    /**
     * 前台
     * 根据案例类别id查询该类别的所有案例并分页
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("caselist.html")
    public ModelAndView listCaseById (Integer id,
                                      @RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "3") Integer pageSize){

       ModelAndView mav = new ModelAndView();

       CaseType caseType  = iCaseTypeService.selectByKey(id);//根据类别id查出该类别信息  传给前台

       mav.addObject("caseType",caseType);

       List<SuccessCase> listCaseById  =  iSuccessCaseService.listAllCase(id,pageNum,pageSize);//根据类别id查所有该类别下的所有案例 分页

       mav.addObject("listCaseById",new PageInfo<SuccessCase>(listCaseById));//分页封装类中的list存放listCaseById的内容

       mav.addObject("typeid",id);//类别id

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

       mav.addObject("pageSize",pageSize);//显示条数

       return  mav;
    }
}
