package com.zhiwei.manage.servlet;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.bean.Message;
import com.zhiwei.manage.bean.PageEty;
import com.zhiwei.manage.service.MessageService;

/**
 * 信息处理器
 * 
 * @author admin
 *
 */
@Controller
public class MessageConteoller {

	@Autowired
	private MessageService msgService;

	/**
	 * 获取所有的信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/findAllMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllMessage(@RequestBody Map<String, Object> map) {
		String tempName = (String) map.get("tempName");
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		String pt = (String) map.get("pt");
		if (tempName == null || tempName.equals("")) {
			PageEty page = msgService.findAll(pageNo, pageSize, pt);
			JsonResult result = new JsonResult(true, 200, "请求成功", page);
			return JSON.toJSONString(result);
		} else {
			PageEty page = msgService.findByTmpName(tempName, pageNo, pageSize, pt);
			JsonResult result = new JsonResult(true, 200, "请求成功", page);
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 处理信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/handle", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String handle(@RequestBody Map<String, Object> map) {
		Message message = new Message();
		message.setHandle((boolean) map.get("handle"));
		message.setMessageId(String.valueOf(map.get("messageId")));
		message.setHandler(String.valueOf(map.get("handler")));
		message.setHandleMsg(String.valueOf(map.get("handleMsg")));
		message.setHandleDate(new Date());
		String pt = (String) map.get("pt");
		String str = msgService.update(message, pt);
		JsonResult result = new JsonResult(true, 200, str, "");
		return JSON.toJSONString(result);
	}

	/**
	 * 删除信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/deleteMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String delete(@RequestBody Map<String, Object> map) {
		List<String> messageId = (List<String>) map.get("messageId");
		String pt = (String) map.get("pt");
		msgService.delete(messageId, pt);
		JsonResult result = new JsonResult(true, 200, "删除成功", "");
		return JSON.toJSONString(result);
	}

	/**
	 * 获取处理过的信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/findHandleMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getNewMsg(@RequestBody Map<String, Object> map) {
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		String tempName = (String) map.get("tempName");
		String pt = (String) map.get("pt");
		if (tempName == null || tempName.equals("")) {
			PageEty page = msgService.findByHandle(pageNo, pageSize, true, tempName, pt);
			JsonResult result = new JsonResult(true, 200, "获取成功", page);
			return JSON.toJSONString(result);
		} else {
			PageEty page = msgService.findByHandle(pageNo, pageSize, true, tempName, pt);
			JsonResult result = new JsonResult(true, 200, "获取成功", page);
			return JSON.toJSONString(result);
		}
	}

}
