package com.yc.education.controller.admin;/**
 * huangpan
 * 2019/10/16
 * energy
 */

import com.github.pagehelper.PageInfo;
import com.yc.education.model.Notice;
import com.yc.education.model.Personnel;
import com.yc.education.service.NoticeService;
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.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *@ClassName NoticeController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/16 9:53
 *@Version 1.0
 */
@Controller
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    /*查询公告信息*/
    @RequestMapping("notice.html")
    public ModelAndView getNotice(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                  @RequestParam(value = "pageSize",defaultValue = "7") Integer pageSize, HttpSession session){
        Personnel sesionPer =(Personnel) session.getAttribute("sesionPer");
        if(sesionPer==null||sesionPer.equals("")){
            return new ModelAndView("redirect:index.html");
        }else {
        ModelAndView modelAndView=new ModelAndView("xingtai/notice");
        List<Notice> notices = noticeService.getNotices(pageNum,pageSize);
        modelAndView.addObject("notices", new PageInfo<>(notices));
        modelAndView.addObject("pageNums", pageNum);
        return modelAndView;}
    }
    /*查看详情*/
    @RequestMapping("notice_detail.html")
    public ModelAndView getNoticeById(Integer id){
        ModelAndView modelAndView=new ModelAndView("xingtai/notice_detail");
        Notice noticeById = noticeService.getNoticeById(id);
        modelAndView.addObject("noticeById", noticeById);
        return modelAndView;
    }

    /*后台查询信息*/
    @RequestMapping("admin/notice-list.html")
    public ModelAndView selNotice(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                  @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        ModelAndView modelAndView=new ModelAndView();
        List<Notice> notices = noticeService.getNotices(pageNum,pageSize);
        modelAndView.addObject("notices", new PageInfo<>(notices));
        return modelAndView;
    }

    /*新增公告*/
    @RequestMapping("admin/notice-add.html")
    public ModelAndView addPersonnel(){return new ModelAndView();}
    /*执行新增*/
    @RequestMapping("admin/notice-save.html")
    public void savePersonnel(Notice notice, HttpServletResponse response)throws Exception{
        if (notice.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            notice.setReleasetime(commentdatemin);
        }
        noticeService.save(notice);
        response.sendRedirect("notice-list.html");

    }
    /*查看公告信息*/
    @RequestMapping("admin/noticeById.html")
    public ModelAndView noticeById(Integer id){
        Notice notice = noticeService.getNoticeById(id);
        ModelAndView modelAndView=new ModelAndView("admin/notice-update");
        modelAndView.addObject("notice", notice);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/notice-update.html")
    public void updatePersonnel(Notice notice,HttpServletResponse response)throws Exception{
        noticeService.updateNotNull(notice);
        response.sendRedirect("notice-list.html");

    }
    /*执行删除*/
    @RequestMapping("admin/delnotices.html")
    @ResponseBody
    public void delnotice(Integer id){
        System.err.println(id);
      noticeService.delNoticeById(id);
    }
}
