package com.yc.education.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.NewsType;
import com.yc.education.service.INewsCenterService;
import com.yc.education.service.INewsTypeService;
import com.yc.education.util.AjaxMessage;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 后台 新闻管理 新闻类别
 * @author Hickey
 * @create 2019-07-24 16:22
 */
@Controller
public class TypeManageController {

    @Autowired
    private INewsTypeService iNewsTypeService;

    @Autowired
    private INewsCenterService iNewsCenterService;

    /**
     * 后台
     * 查询全部新闻类别并分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/admin/typeManage.html")
    public ModelAndView listAllType(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "3") Integer pageSize){

        ModelAndView mav = new ModelAndView();

        List<NewsType> listAllType = iNewsTypeService.listAllType(pageNum,pageSize);

        mav.addObject("pageinfo",new PageInfo<NewsType>(listAllType));

        mav.addObject("pageNum",pageNum);
        mav.addObject("pageSize",pageSize);

        return mav;
    }

    /**
     * 后台
     * 跳转到类别添加页面
     * @return
     */
    @RequestMapping("/admin/addType.html")
    public  ModelAndView addType(){
        return  new ModelAndView();
    }

    /**
     * 后台
     * 添加类别
     * @param newsType
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/addTypeDetail.html")
    public AjaxMessage<Object>  addTypeDetail(NewsType newsType) {

        AjaxMessage am = new AjaxMessage();

        int rows = iNewsTypeService.save(newsType);

        if(rows > 0){
            am.setIs(true);
            am.setMsg("添加成功");
        }

        return  am;
    }

    /**
     * 后台  删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/deleteType.html")
    public AjaxMessage<Object> deleteType(Integer id){

        AjaxMessage  am = new AjaxMessage();

        //根据类别id查出所有该类别下的资讯信息总数
        int count = iNewsCenterService.findCountNews(id);
        if (count == 0){
            int rows= iNewsTypeService.delete(id);
            am.setIs(true);
        }else {
            am.setIs(false);
            am.setMsg("该类别下还有资讯,不能删除");
        }
        return am;
    }

    /**
     * 批量删除类别
     */
    @ResponseBody
    @RequestMapping("admin/deleteTypeArray.html")
    public AjaxMessage<Object>  deleteTypeArray (int [] typeArray){

        AjaxMessage<Object> am = new AjaxMessage<>();

        //根据批量删除的分类id查询所有资讯信息,来判断所选类别下是否还有资讯信息
        int counts = iNewsCenterService.listCountNews(typeArray);
        if(counts == 0){
            int rows =  iNewsTypeService.deleteTypeArray(typeArray);
            am.setIs(true);
        }else{
            am.setMsg("所删除类别中还有资讯信息,不能删除!");
            am.setIs(false);
        }

        return am;
    }
    /**
     * 修改前查询 并跳转到updateType.html
     * @param id
     */
    @RequestMapping("admin/updateType.html")
    public ModelAndView updateType(Integer id){
        ModelAndView mav = new ModelAndView();
        NewsType newsType = iNewsTypeService.selectByKey(id);
        mav.addObject("newsType",newsType);
        return  mav;
    }
    /**
     * 修改类别
     * @param newsType
     */
    @ResponseBody
    @RequestMapping("admin/updateDetailType.html")
    public AjaxMessage<Object> updateDetailType(NewsType newsType){
        AjaxMessage am = new AjaxMessage();
        int rows = iNewsTypeService.updateNotNull(newsType);
        if (rows > 0){
            am.setIs(true);
            am.setMsg("修改成功");
        }
        return am;
    }

}
