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.model.Life;
import com.yc.education.model.Mien;
import com.yc.education.service.IdeaService;
import com.yc.education.service.LifeService;
import com.yc.education.service.MienService;
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 javax.servlet.http.HttpSession;
import java.util.List;

/**
 *@ClassName LifeController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/14 10:01
 *@Version 1.0
 */
@Controller
@RequestMapping("/admin/")
public class LifeController {
    @Autowired
    LifeService lifeService;
    @Autowired
    IdeaService ideaService;
    @Autowired
    MienService mienService;

    /*查询所有*/
    @RequestMapping("life-list.html")
    public ModelAndView getLifes(){
        ModelAndView modelAndView=new ModelAndView();
        List<Life> lifes = lifeService.getLifes();
        modelAndView.addObject("lifes", lifes);
        List<Idea> ideas = ideaService.getIdeas();
        modelAndView.addObject("ideas", ideas);
        return  modelAndView;
    }
    /*查询单条信息*/
    @RequestMapping("lifeById.html")
    public ModelAndView selLifeById(Integer id){
        ModelAndView modelAndView=new ModelAndView("/admin/life-update");
        Life lifeById = lifeService.getLifeById(id);
        modelAndView.addObject("lifeById", lifeById);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("life-update.html")
    public void updateLife(Life life , 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("")) {
        life.setSurfaceplot(url);
        }
        lifeService.updateNotNull(life);
        response.sendRedirect("life-list.html");
    }

    /*星泰生活*/
        /*查询所有*/
    @RequestMapping("shengh-list.html")
    public ModelAndView shengh(){
        ModelAndView modelAndView=new ModelAndView();
        Mien shengh = mienService.getShengh();
        modelAndView.addObject("shengh", shengh);
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("shenghById.html")
    public ModelAndView selShenhById(Integer id, HttpSession session){
        ModelAndView modelAndView=new ModelAndView("/admin/shengh-update");
        Mien mienId = mienService.getMienId(id);
        modelAndView.addObject("classi", mienId);
        session.setAttribute("mienId", mienId);
        return modelAndView;
    }
    @RequestMapping(value = "shengh-update.html")
    public void  updateMien(HttpServletRequest request, HttpServletResponse response, Mien mien,HttpSession session)throws Exception{
        Mien mien1=(Mien) session.getAttribute("mienId");
        mien.setMhomepage(mien1.getMhomepage());
        mien.setSort(mien1.getSort());
        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("shengh-list.html");
    }
}
