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.Honor;
import com.yc.education.service.HonorService;
import org.apache.ibatis.annotations.Param;
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.RequestMethod;
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 javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * huangpan
 * 2019/9/26
 * energy
 */
@Controller
public class HonorContoller {
    @Autowired
    HonorService honorService;
    /*查询所有*/
    @RequestMapping("admin/honor-list.html")
    public ModelAndView findHonor(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                  @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        ModelAndView modelAndView=new ModelAndView();
        List<Honor> list=honorService.getHonor(pageNum,pageSize);
        modelAndView.addObject("pageInfo", new PageInfo<>(list));
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getHonorId.html")
    public ModelAndView showHonor(@Param("id") int id){
        ModelAndView modelAndView=new ModelAndView("/admin/honor-update");
        Honor classi= honorService.getHonorId(id);
        modelAndView.addObject("classi", classi);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/honor-add.html")
    public ModelAndView reqHonor(){
        return  new ModelAndView();
    }
    /*执行修改*/
    @RequestMapping(value = "admin/honor-update.html",method = RequestMethod.POST)
    public void  updateHonor(HttpServletRequest request, HttpServletResponse response, Honor honor)
            throws Exception{
      String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        if(url!=null&&!"".equals(url)){
            // journalism.setPicture(url);
            honor.setRimg(url);
        }
        honorService.updateNotNull(honor);

        response.sendRedirect("honor-list.html");
    }
    /*执行新增*/
    @RequestMapping(value = "admin/honor-save.html",method = RequestMethod.POST)
    public void HonorSave(HttpServletRequest request, HttpServletResponse response, Honor honor)throws Exception{
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        System.err.println(url+"===========");
        honor.setRimg(url);
        honor.setRfpid(5);
        if (honor.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            honor.setReleasetime(commentdatemin);
        }
        honorService.save(honor);
        response.sendRedirect("honor-list.html");
    }
    /*删除*/
    @RequestMapping(value = "admin/deleteHonorById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@Param("id") int id){
        honorService.deleteHonorId(id);
    }
}
