/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:sfzxWebsite
 * File Name:NoticeController.java
 * Package Name:ccpit.business.noticeManage
 * Date:2017年4月12日上午11:26:54
 * Copyright (c) 2017, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.business.noticeManage;

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

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

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.logManage.LogInfoService;
import org.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:NoticeController <br/>
 * Function: TODO 通知公告信息管理控制层. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年4月12日 上午11:26:54 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.8.0
 * @see 	 
 */
@RestController
@RequestMapping("/business/noticeManage")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;
	@Autowired
	private LogInfoService logInfoService;

	@RequestMapping(value ="/goinNoticePage")
	public ModelAndView getPage() {
		ModelAndView mv = new ModelAndView("business/noticePage");
		return mv;
	}
	
	/**
	 * 
	 * queryAllPerson:(这里用一句话描述这个方法的作用). <br/>
	 * @author dingwei
	 * @param request
	 * @param Notice
	 * @return
	 * Date:2017年3月21日下午3:42:21
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/queryAllNotice")
	public Object queryAllNotice(HttpServletRequest request,Notice Notice) {
		PageRequest pageRequest = getPage(request);
		String title = request.getParameter("noticeTitle");
		String noticeType = request.getParameter("noticeType");
		Page<Notice> pageInformation = noticeService.queryNotices(pageRequest, title,noticeType);
//		pageInformation.getRows().forEach(Notice -> {
//			noticeService.createNoticesIndex(Notice);
//		});
		return noticeService.convert(pageInformation);
	}
	/**
	 * 
	 * isExistNotice:(根据标题查询是否存在). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * Date:2017年3月22日上午9:36:33
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/isExistNotice")
	public Object isExistNotice(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String title = request.getParameter("noticeTitle");
		boolean isExist = noticeService.queryNoticeByTitle(title);
		result.put("flag", isExist);
		if(isExist){
			result.put("info", "新闻信息已存在，请不要重复录入...");
		}
		System.out.println(result.get("flag"));
		return result;
	}
	
	/**
	 * 
	 * addOrUpdateNotice:(新增或修改). <br/>
	 * @author dingwei
	 * @param operate
	 * @param request
	 * @param response
	 * @param Notice
	 * @return
	 * Date:2017年3月21日下午3:47:51
	 * @since JDK 1.8.0
	 */
	@RequestMapping(value="/addOrUpdateNotice",produces = "application/json;charset=UTF-8")
	public Object addOrUpdateNotice(HttpServletRequest request,HttpServletResponse response, Notice notice){
		String result = "";
		String imagePath = request.getParameter("imagePath");
		String operate = request.getParameter("operate");
		String noticeId = request.getParameter("noticeId");
		
		boolean flag = noticeService.addOrUpdateNotice(notice, operate, noticeId, (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="/deleteNotices")
	public Object deleteNotices(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		boolean flag = noticeService.deleteNoticeById(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="/queryNoticeById/{id}")
	public Notice queryNoticeById(@PathVariable long id){
		return noticeService.queryNoticeById(id);
	}
	
}

