package com.yc.education.controller.admin;

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.service.NewsService;
import com.yc.education.util.AjaxMessage;
import com.yc.education.util.ViewUtil;
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 java.util.List;

/**
 * @author YaoShiHang
 * @Date 15:06 2018-04-09
 */
@Controller
@RequestMapping("/admin/")
public class NewsControllerAdmin {

    @Autowired
    private NewsService newsService;

    @ResponseBody
    @RequestMapping("newsbypage")
    public AjaxMessage<Object>  listAll(
                                        @RequestParam(required = false,defaultValue = "1")int page,
                                        @RequestParam(required = false,defaultValue = "10")int rows){
        List<News> newsList = newsService.listAll(page, rows);
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return new AjaxMessage<>(true, "", pageInfo);
    }

    @RequestMapping("addnews")
    public ModelAndView addnews(News news , HttpServletRequest request) {
        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.setImgsrc(url);
        int  rows =  newsService.save(news);
        return ViewUtil.returnview(rows,"newslist.html","新闻管理");
    }

    @ResponseBody
    @RequestMapping("getnewbyid")
    public News getByid(long id){
        return newsService.selectByKey(id);
    }

    @RequestMapping("updatanews")
    public ModelAndView updatanews(News news , HttpServletRequest request) {
        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)){
            news.setImgsrc(url);
        }
        int  rows =  newsService.updateNotNull(news);
        return ViewUtil.returnview(rows,"newslist.html","新闻管理");
    }
    /**
     * 删除 banner
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deletenews")
    public AjaxMessage<Object> delete(String id) {
        String[] idlist = id.split(",");
        int num = idlist.length;
        int rows = 0;
        for (String string : idlist) {
            rows += newsService.delete(Long.parseLong(string));
        }
        return ViewUtil.returnajax(rows, num);
    }


}
