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

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

/**
 *@ClassName DownloadController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/16 9:52
 *@Version 1.0
 */
@Controller
public class DownloadController {
    @Autowired
    DownloadService downloadService;
    /*查询信息*/
    @RequestMapping("download.html")
    public ModelAndView getDownLoads(@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/download");
        List<Download> downloads = downloadService.getDownloads(pageNum,pageSize);
        modelAndView.addObject("downloads", new PageInfo<>(downloads));
        modelAndView.addObject("pageNums", pageNum);
        return modelAndView;}
    }

    /*后台查询*/
    @RequestMapping("admin/download-list.html")
    public ModelAndView seldownload(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                    @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        ModelAndView modelAndView=new ModelAndView();
        List<Download> downloads = downloadService.getDownloads(pageNum,pageSize);
        modelAndView.addObject("downloads", new PageInfo<>(downloads));
        return modelAndView;
    }

    /*新增员工*/
    @RequestMapping("admin/download-add.html")
    public ModelAndView addPersonnel(){return new ModelAndView();}
    /*执行新增*/
    @RequestMapping("admin/download-save.html")
    public void savePersonnel(Download download, HttpServletResponse response, HttpServletRequest request)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");
        download.setDpath(url);
        if (download.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            download.setReleasetime(commentdatemin);
        }
        downloadService.save(download);
        response.sendRedirect("download-list.html");

    }
    /*查看员工信息*/
    @RequestMapping("admin/downloadById.html")
    public ModelAndView downloadById(Integer id){
        Download download = downloadService.getDownloadById(id);
        ModelAndView modelAndView=new ModelAndView("admin/download-update");
        modelAndView.addObject("download", download);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/download-update.html")
    public void updatePersonnel(Download download, HttpServletResponse response, HttpServletRequest request)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+"url");
        if (url!=null&&!url.equals("")) {
            download.setDpath(url);
        }
        downloadService.updateNotNull(download);
        response.sendRedirect("download-list.html");

    }
    /*执行删除*/
    @RequestMapping("admin/deldownload.html")
    @ResponseBody
    public void deldownload(Integer id){
        downloadService.delDownload(id);
    }
}
