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.Mien;
import com.yc.education.service.MienService;
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 MienController {
    @Autowired
    MienService mienService;
    /*查询所有*/
    @RequestMapping("admin/staff-list.html")
    public ModelAndView findMien(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                 @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        ModelAndView modelAndView=new ModelAndView("/admin/mien-list");
        List<Mien> list= mienService.getMienPage(pageNum, pageSize);
        modelAndView.addObject("mienList", new PageInfo<>(list));
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getMienId.html")
    public ModelAndView showMien(@Param("id") int id){
        ModelAndView modelAndView=new ModelAndView("/admin/mien-update");
        Mien classi= mienService.getMienId(id);
        modelAndView.addObject("classi", classi);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/mien-add.html")
    public ModelAndView reqMien(){
        return  new ModelAndView();
    }
    /*执行修改*/
    @RequestMapping(value = "admin/mien-update.html",method = RequestMethod.POST)
    public void  updateMien(HttpServletRequest request, HttpServletResponse response, Mien mien)
            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);
           mien.setMimg(url);
        }
        mienService.updateNotNull(mien);

        response.sendRedirect("staff-list.html");
    }
    /*执行新增*/
    @RequestMapping(value = "admin/mien-save.html",method = RequestMethod.POST)
    public void MienSave(HttpServletRequest request, HttpServletResponse response, Mien mien)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");
        mien.setMimg(url);
        mien.setMfpid(6);
        if (mien.getMdate().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            mien.setMdate(commentdatemin);
        }
        mienService.save(mien);

        response.sendRedirect("staff-list.html");
    }
    /*删除*/
    @RequestMapping(value = "admin/deleteMienById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@RequestParam( value = "id") int id){
        mienService.deleteMienId(id);
    }
}
