/**
 * 
 */
package org.ccpit.front.newsInfo;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.ccpit.base.controller.BaseController;
import org.ccpit.business.newsManage.NewsInfo;
import org.ccpit.business.newsManage.NewsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Company Name : 中贸促信息技术有限责任公司 Project Name:memberManageSys File
 * Name:DownloadFileController.java Package Name:ccpit.base.controller Date:
 * 2017年3月28日 上午9:08:15 Copyright (c) 2016, liyang@ccpit.org All Rights
 * Reserved.
 *
 */
@RestController
@RequestMapping(value = "/front/news")
public class NewsInfoFrontController extends BaseController {

	@Autowired
	private NewsInfoService newsInfoService;

	@Autowired
	private HttpServletRequest request;

	@RequestMapping("{type:[a-z]+}")
	public ModelAndView index(ModelAndView mv, HttpSession session,
			@PathVariable String type) {
		session.setAttribute("type", type);
		String typeName = "center".equals(session.getAttribute("type")) ? "中心要闻":"港澳新闻"; 
		mv.setViewName("/front/news/newsList");
		mv.addObject("typeName", typeName);
		return mv;
	}

	/**
	 * 新闻查询
	 * 
	 * @param newsType
	 *            /type 为center 或者 tga
	 * @param title
	 * @return
	 */
	@RequestMapping("/index")
	public Map<String, Object> newsInfos(HttpSession session,
			@RequestParam(value = "title", required = false) String title) {
		String type = "center".equals(session.getAttribute("type")) ? "1" : "2";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list",
				newsInfoService.queryNewsInfos(getPage(request), title, type)
						.getRows());
		map.put("total",
				newsInfoService.queryNewsInfos(getPage(request), title, type)
						.getTotal());
		return map;
	}

	@RequestMapping("/{id:^[0-9]*$}")
	public ModelAndView newsInfo(ModelAndView mv, @PathVariable String id) {
		NewsInfo newsInfo = newsInfoService.queryNewsInfoById(Long
				.parseLong(id));
		newsInfoService.addClickVolume(newsInfo);
		String contentType = 1 == (newsInfo.getNewsType()) ? "中心要闻" : "台港澳新闻";
		mv.addObject("contentType", contentType);
		mv.addObject("info", newsInfo);
		mv.setViewName("/front/news/content");
		return mv;
	}

}
