package com.yc.education.controller.admin;/**
 * huangpan
 * 2019/11/11
 * hsa
 */

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.News;
import com.yc.education.model.Newsen;
import com.yc.education.service.NewsService;
import com.yc.education.service.NewsenService;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *@ClassName NewsController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/11/11 10:12
 *@Version 1.0
 */
@Controller
@RequestMapping("admin/")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private NewsenService newsenService;

    /*查询信息*/
    @RequestMapping("news_list.html")
    public ModelAndView getNews(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,
                                String titleName,String startTime,String endTime){
        ModelAndView modelAndView = new ModelAndView("admin/news_list");
        List<News> news = newsService.getNews(pageSize, pageNum, titleName, startTime, endTime);
        modelAndView.addObject("news", new PageInfo<>(news));
        modelAndView.addObject("titleName", titleName);
        modelAndView.addObject("startTime", startTime);
        modelAndView.addObject("endTime", endTime);
        return modelAndView;
    }

    /*查询信息*/
    @RequestMapping("news_list_en.html")
    public ModelAndView getNewsEn(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,
                                String titleName,String startTime,String endTime){
        ModelAndView modelAndView = new ModelAndView("adminEn/news_list");
        List<Newsen> newsen = newsenService.getNews(pageSize, pageNum, titleName, startTime, endTime);
        modelAndView.addObject("news", new PageInfo<>(newsen));
        modelAndView.addObject("titleName", titleName);
        modelAndView.addObject("startTime", startTime);
        modelAndView.addObject("endTime", endTime);
        return modelAndView;
    }

    /*查询单条记录*/
    @RequestMapping("news_ById.html")
    public ModelAndView newsById(Integer id){
        ModelAndView modelAndView = new ModelAndView("admin/news_update");
        News newsById = newsService.getNewsById(id);
        modelAndView.addObject("newsById", newsById);
        return modelAndView;
    }

    /*查询单条记录*/
    @RequestMapping("news_ById_en.html")
    public ModelAndView newsByIdEn(Integer id){
        ModelAndView modelAndView = new ModelAndView("adminEn/news_update");
        Newsen newsByIden = newsenService.getNewsById(id);
        modelAndView.addObject("newsById", newsByIden);
        return modelAndView;
    }

    /*执行修改*/
    @RequestMapping("news_update.html")
    public void newsUpdate(News news,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");
        news.setImg(url);
        String leng = news.getTimes().substring(0, 4) + "." + news.getTimes().substring(5, 7);
        news.setTimelength(Double.parseDouble(leng));
        newsService.updateNotNull(news);
        response.sendRedirect("news_list.html");
    }

    /*执行修改*/
    @RequestMapping("news_update_en.html")
    public void newsUpdateEn(Newsen newsen,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");
        String leng = newsen.getTimes().substring(0, 4) + "." + newsen.getTimes().substring(5, 7);
        newsen.setImg(url);
        newsen.setTimelength(Double.parseDouble(leng));
        newsenService.updateNotNull(newsen);
        response.sendRedirect("news_list_en.html");
    }

    /*新增信息*/
    @RequestMapping("news_save.html")
    public ModelAndView newsSave(){return  new ModelAndView("admin/news_add");}

    /*新增信息*/
    @RequestMapping("news_save_en.html")
    public ModelAndView newsSaveEn(){return  new ModelAndView("adminEn/news_add");}

    /*执行新增*/
    @RequestMapping("news_add.html")
    public void newsAdd(News news,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");
        news.setImg(url);
        if (news.getTimes().equals("")) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            news.setTimes(commentdatemin);
        }
        String leng = news.getTimes().substring(0, 4) + "." + news.getTimes().substring(5, 7);
        news.setTimelength(Double.parseDouble(leng));
        newsService.save(news);
        response.sendRedirect("news_list.html");
    }

    /*执行新增*/
    @RequestMapping("news_add_en.html")
    public void newsAddEn(Newsen newsen,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");
        if (newsen.getTimes().equals("")) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            newsen.setTimes(commentdatemin);
        }
        newsen.setImg(url);
        String leng = newsen.getTimes().substring(0, 4) + "." + newsen.getTimes().substring(5, 7);
        newsen.setTimelength(Double.parseDouble(leng));
        newsenService.save(newsen);
        response.sendRedirect("news_list_en.html");
    }

    /*执行删除*/
    @RequestMapping("delNews_ById.html")
    @ResponseBody
    public void delNewsById(Integer id){
        newsService.delNewsById(id);
    }

    /*执行删除*/
    @RequestMapping("delNews_ById_en.html")
    @ResponseBody
    public void delNewsByIdEn(Integer id){
        newsenService.delNewsById(id);
    }

    /*执行批量删除*/
    @RequestMapping("delNews")
    public void delNews(@RequestParam( value = "chkvalue") String chkvalue,HttpServletResponse response)throws  Exception{
        String[] skt = chkvalue.split(",");
        for (int i = 0; i < skt.length; i++) {
            newsService.delNewsById(Integer.parseInt(skt[i]));
        }
        response.sendRedirect("news_list.html");
    }

    /*执行批量删除*/
    @RequestMapping("delNews_en")
    public void delNewsEn(@RequestParam( value = "chkvalue") String chkvalue,HttpServletResponse response)throws  Exception{
        String[] skt = chkvalue.split(",");
        for (int i = 0; i < skt.length; i++) {
            newsenService.delNewsById(Integer.parseInt(skt[i]));
        }
        response.sendRedirect("news_list_en.html");
    }
}
