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

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.yc.education.model.Idea;
import com.yc.education.service.IdeaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

/**
 *@ClassName IdeaController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/14 10:00
 *@Version 1.0
 */
@Controller
@RequestMapping("/admin/")
public class IdeaController {
    @Autowired
    IdeaService ideaService;

    /*根据id选择页面*/
    @RequestMapping("hr.html")
    public void getHr(Integer id,HttpServletResponse response)throws  Exception{
        if (id==1)
            response.sendRedirect("idea-list.html");
        if(id==2)
            response.sendRedirect("life-list.html");
        if(id==3)
            response.sendRedirect("job-list.html");
    }

    /*查询所有*/
    @RequestMapping("idea-list.html")
    public ModelAndView selAllIdea(){
        ModelAndView modelAndView =new ModelAndView();
        List<Idea> ideaList= ideaService.getIdeas();
        modelAndView.addObject("ideaList", ideaList);
        return modelAndView;
    }
    /*查看具体信息*/
    @RequestMapping("ideaById.html")
    public ModelAndView selIedaById(Integer id){
        ModelAndView modelAndView=new ModelAndView("/admin/idea-update");
        Idea ideaById = ideaService.getIdeaById(id);
        modelAndView.addObject("ideaById", ideaById);
        return  modelAndView;
    }
    /*执行修改*/
    @RequestMapping("idea-update.html")
    public void updateIdea(Idea idea , HttpServletRequest request, HttpServletResponse response)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&&!url.equals("")){
            idea.setSurfaceplot(url);
        }
        ideaService.updateNotNull(idea);
        response.sendRedirect("idea-list.html");
    }
}
