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.yc.education.model.Banner;
import com.yc.education.service.BannerService;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *@ClassName BannerController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/16 16:40
 *@Version 1.0
 */
@Controller
public class BannerController {
    @Autowired
    BannerService bannerService;

    /*查*/
    /*后台查询*/
    @RequestMapping("admin/ banner-list.html")
    public ModelAndView selBanner(){
        List<Banner> banner = bannerService.selBanner();
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("banner", banner);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/banner-add.html")
    public ModelAndView addcoom(){
        ModelAndView modelAndView=new ModelAndView();
        return modelAndView;}
    /*执行新增*/
    @RequestMapping("admin/banner-save.html")
    public void savecoom(Banner banner, 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");
         banner.setSurfaceplot(url);
        bannerService.save(banner);
        response.sendRedirect("banner-list.html");

    }
    /*查看信息*/
    @RequestMapping("admin/bannerById.html")
    public ModelAndView coomById(Integer id){
        Banner bannerById = bannerService.getBannerById(id);
        ModelAndView modelAndView=new ModelAndView("admin/banner-update");
        modelAndView.addObject("bannerById", bannerById);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/banner-update.html")
    public void updatecoom(Banner banner, 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+"??????????");
        if(url!=null&&!url.equals("")){
            banner.setSurfaceplot(url);
        }
        bannerService.updateNotNull(banner);
        response.sendRedirect("banner-list.html");

    }
    /*执行删除*/
    @RequestMapping("admin/delBanner.html")
    @ResponseBody
    public void delBanner(Integer id){
        bannerService.delBannerById(id);
    }
}
