package com.zhiwei.manage.servlet;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;

@Controller
public class MessageConteoller {

	@Autowired
	private MessageService msgService;

	@RequestMapping(value = "/findAllMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllMessage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, String tempName,String 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);
		}
	}

	@RequestMapping(value = "/handle", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String handle(Message message) {
		String str = msgService.update(message);
		JsonResult result = new JsonResult(true, 200, str, "");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/deleteMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String delete(@RequestParam(value = "messageId[]") List<String> messageId) {
		msgService.delete(messageId);
		JsonResult result = new JsonResult(true, 200, "删除成功", "");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/findHandleMsg", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getNewMsg(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, String tempName,String 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);
		}
	}

}
