package com.iamberry.wechat.handles.news;

import com.iamberry.app.tool.des.MD5;
import com.iamberry.wechat.autiXSS.XSSHandler;
import com.iamberry.wechat.core.entity.AdminUtils;
import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.banner.ActionBannerImage;
import com.iamberry.wechat.core.entity.news.News;
import com.iamberry.wechat.face.banner.AdminBannerService;
import com.iamberry.wechat.face.news.NewsService;
import com.iamberry.wechat.tools.ResponseJson;
import com.iamberry.wechat.tools.ValidatorUtil;
import com.iamberry.wechat.utils.StaticCacheMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何秀刚
 * @description ： 新闻内容的后台、前台
 * @createDate : 2016年6月13日
 */
@Controller
@RequestMapping("/news")
public class AdminNewsWebHandler {
	
	@Autowired
	private NewsService newsService;

	@Autowired
	private AdminUtils adminUtils;

	@Autowired
	private ValidatorUtil validatorUtil;

	@Autowired
	private AdminBannerService adminBannerService;
	
	private MD5 md5 = new MD5("newsmd55");

	@ResponseBody
	@RequestMapping("/list")
	public ResponseJson newsList(HttpServletRequest request,
		   @RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
		   @RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) {
		ResponseJson rj = new ResponseJson();

		News news = new News();
		news.getPage().setPageSize(pageSize);
		news.getPage().setPageNumber(pageNO);

		List<News> list = newsService.listNews(news);
		PageBean page = new PageBean();
		if(list != null && list.size() > 0){
			page = list.get(0).getPage();
		}
		Integer count = newsService.getCountnews(news);

		rj.addResponseKeyValue("newslist",list);
		rj.addResponseKeyValue("page",page);
		rj.addResponseKeyValue("count",count);
		rj.setReturnCode(200);
		rj.setResultCode(200);
		return rj;
	}

	@ResponseBody
	@RequestMapping("/get/{id}")
	public ResponseJson getNews(HttpServletRequest request,@PathVariable("id") Integer id) {
		ResponseJson rj = new ResponseJson();

		News news = newsService.getOne(id);

		rj.addResponseKeyValue("news",news);
		rj.setReturnCode(200);
		rj.setResultCode(200);
		return rj;
	}

}
