/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:tgaWebsite
 * File Name:NewsInfoController.java
 * Package Name:ccpit.business.newsManage
 * Date:2017年3月21日下午3:28:06
 * Copyright (c) 2017, dingwei@ccpit.org All Rights Reserved.
 *
*/

package ccpit.business.newsManage;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ccpit.base.controller.BaseController;
import ccpit.base.controller.Page;
import ccpit.base.controller.PageRequest;
import ccpit.base.logManage.LogInfoService;
import ccpit.base.user.User;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName:NewsInfoController <br/>
 * Function: TODO 新闻信息管理控制层. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年3月21日 下午3:28:06 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.8.0
 * @see 	 
 */
@RequestMapping(value="/business/newsManage")
@RestController
public class NewsInfoController extends BaseController {
	
	@Autowired
	private NewsInfoService newsInfoService;
	@Autowired
	private LogInfoService logInfoService;

	@RequestMapping(value ="/goinNewsInfoPage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/newsInfoPage");
		return mv;
	}
	
	/**
	 * 
	 * queryAllPerson:(这里用一句话描述这个方法的作用). <br/>
	 * @author dingwei
	 * @param request
	 * @param NewsInfo
	 * @return
	 * Date:2017年3月21日下午3:42:21
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/queryAllNewsInfo")
	public Object queryAllNewsInfo(HttpServletRequest request,NewsInfo NewsInfo) {
		PageRequest pageRequest = getPage(request);
		String title = request.getParameter("title");
		String newsType = request.getParameter("newsType");
		Page<NewsInfo> pageInformation = newsInfoService.queryNewsInfos(pageRequest, title,newsType);
		pageInformation.getRows().forEach(newsInfo -> {
			newsInfoService.createNewsInfosIndex(newsInfo);
		});
		return newsInfoService.convert(pageInformation);
	}
	/**
	 * 
	 * isExistNewsInfo:(根据标题查询是否存在). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * Date:2017年3月22日上午9:36:33
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/isExistNewsInfo")
	public Object isExistNewsInfo(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String title = request.getParameter("newsTitle");
		boolean isExist = newsInfoService.queryNewsInfoByTitle(title);
		result.put("flag", isExist);
		if(isExist){
			result.put("info", "新闻信息已存在，请不要重复录入...");
		}
		return result;
	}
	
	/**
	 * 
	 * addOrUpdateNewsInfo:(新增或修改). <br/>
	 * @author dingwei
	 * @param operate
	 * @param request
	 * @param response
	 * @param newsInfo
	 * @return
	 * Date:2017年3月21日下午3:47:51
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/addOrUpdateNewsInfo",produces = "application/json;charset=UTF-8")
	public Object addOrUpdateNewsInfo(HttpServletRequest request,HttpServletResponse response, NewsInfo newsInfo){
		String result = "";
		String imagePath = request.getParameter("imagePath");
		String operate = request.getParameter("operate");
		String newsInfoId = request.getParameter("newsInfoId");
		if(null != imagePath && !"".equals(imagePath)){
			newsInfo.setImagePath(imagePath);
		}
		boolean flag = newsInfoService.addOrUpdateNewsInfo(newsInfo, operate, newsInfoId, (User)getUserSession(request), request);
		if(flag){
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存成功！\" }";
		}else {
			result = "{ \"flag\": "+ flag  +", \"info\": \"数据保存失败！\" }";
		}
		return result;
	}
	
	/**
	 * 
	 * delPerson:(这里用一句话描述这个方法的作用). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * Date:2017年3月21日下午3:44:26
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/deleteNewsInfos")
	public Object deleteNewsInfos(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		boolean flag = newsInfoService.deleteNewsInfoById(request.getParameter("ids"));
		result.put("flag", flag);
		if(flag){
			result.put("info", "数据删除成功！");
		}else {
			result.put("info", "数据删除失败！");
		}
		return result;
	} 
	
	/**
	 * 
	 * queryPersonById:(通过id查询). <br/>
	 * @author dingwei
	 * @param id
	 * @return
	 * Date:2017年3月21日下午3:45:52
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/queryNewsInfoById/{id}")
	public NewsInfo queryNewsInfoById(@PathVariable long id){
		return newsInfoService.queryNewsInfoById(id);
	}
	
	
}

