package com.yc.education.controller.admin;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
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 com.yc.education.util.AjaxMessage;
import com.yc.education.util.ViewUtil;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台管理  新闻中心  新闻信息
 * @author Hickey
 * @create 2019-07-24 8:48
 */
@Controller
public class NewsManageController {

    @Autowired
    private INewsCenterService iNewsCenterService;

    @Autowired
    private INewsTypeService iNewsTypeService;

    /**
     * 后台  新闻管理
     * 查询所有新闻信息 并展示
     * @param id
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("admin/newsManage.html")
    public ModelAndView listAllNews(Integer id,
                                    String title,
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "6") Integer pageSize){
        ModelAndView mav = new ModelAndView();

        List<NewsCenter> manageAllNews  =  iNewsCenterService.manageAllNews(id,title,pageNum,pageSize);//查询所有新闻信息并分页

        mav.addObject("pageinfo",new PageInfo<NewsCenter>(manageAllNews));//将通用接口中的结果集(list)传给前台,里面包含新闻信息和分页信息

        mav.addObject("id",id);//根据新闻类别id搜索

        mav.addObject("title",title);//根据输入新闻名称 模糊查询
        mav.addObject("pageNum",pageNum);//页码

        mav.addObject("pageSize",pageSize);//每页展示多少条

        List<NewsType> listAllType = iNewsTypeService.listAllType();//查询所有新闻类别信息,后台新闻信息列表展示

        mav.addObject("listAllType",listAllType);

        return  mav;
    }

    /**
     * 后台  跳转到添加新闻界面  并查询新闻类别
     * @return
     */
    @RequestMapping("admin/addNews.html")
    public ModelAndView addNews(){

        ModelAndView mav = new ModelAndView();

        List<NewsType> type  =  iNewsTypeService.listAllType();//新闻信息添加中,新闻类别是下拉框

        mav.addObject("type",type);

        return  mav;
    }

    /**
     * 后台 添加新闻信息
     * @param newsCenter
     * @param request
     * @return
     */
    @RequestMapping("admin/addNewsDetail.html")
    public ModelAndView addNewsDetail(NewsCenter newsCenter, HttpServletRequest request){


        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "admin")
                .invoke(ActionMap.UPLOAD_FILE);
        newsCenter.setNewsimage(state.getInfo("url"));

        request.getSession();
        System.out.println("================="+request.getSession()+"1111111111111111111111");

        int rows  = iNewsCenterService.save(newsCenter);//添加新闻信息 返回受影响的行数
        return ViewUtil.returnview(rows,"newsManage.html","新闻列表");
    }

    /**
     * 后台 跳转到新闻修改界面  并根据id查出该条新闻信息
     * @param id
     * @return
     */
    @RequestMapping("admin/updateNews.html")
    public ModelAndView updateNews(Integer id){
        ModelAndView  mav = new ModelAndView();

        NewsCenter findNewsById = iNewsCenterService.selectByKey(id);//根据id查出要查看的该条新闻信息

        mav.addObject("findNewsById",findNewsById);

        return  mav;
    }

    /**
     * 后台 修改新闻信息
     * @param newsCenter
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/updateNewsDetail.html")
    public AjaxMessage<Object> updateNewsDetail(NewsCenter newsCenter){

        AjaxMessage am = new AjaxMessage();

        int rows  = iNewsCenterService.updateNotNull(newsCenter);//修改新闻信息

        if(rows > 0 ){

            am.setIs(true);
            am.setMsg("修改成功");
        }else {
            am.setIs(false);
            am.setMsg("修改失败");
        }
        return  am;
    }

    /**
     * 后台 根据id删除单条新闻
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/deleteNews.html")
    public AjaxMessage<Object> deleteNews(Integer id){
        AjaxMessage  am = new AjaxMessage();

        int rows = iNewsCenterService.delete(id);//根据id删除选中的新闻信息

        if(rows > 0){
            am.setIs(true);
            am.setMsg("删除成功");
        }else {
            am.setIs(false);
            am.setMsg("删除失败");
        }
        return  am;

    }


    /**
     * 后台
     * 批量删除
     * @param newsArray
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/deleteNewsArray.html")
    public  AjaxMessage<Object> deleteNewsArray(int [] newsArray){

        AjaxMessage  am   =  new AjaxMessage();

        int rows = iNewsCenterService.deleteNewsArray(newsArray);

        if (rows > 1){
            am.setIs(true);
            am.setMsg("删除成功");
        }else {
            am.setIs(false);
            am.setMsg("删除失败");
        }
        return am;
    }

}
