package org.cs.mgr.user.ctl;

import java.util.Map;

import org.apache.log4j.Logger;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.user.service.IMgmMessageService;
import org.cs.mgr.user.service.ISmsService;
import org.cs.util.Pager;
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.ResponseBody;

import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/rest/mgmMessage")
public class MgmMessageAPI extends BaseCtl{

	private Logger log = Logger.getLogger(MgmMessageAPI.class);
	@Autowired
	public ISmsService smsService;
	
	@Autowired
	public IMgmMessageService mgmMessageService;
	
	@ResponseBody
	@RequestMapping("/getMgmMessageList")
	public JSONObject getMgmMessageList(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			Pager page=mgmMessageService.getMgmMessageList(map);
			json=getJson(1, page, "获取消息列表成功");
		} catch (Exception e) {
			log.error("获取消息列表失败："+e.getMessage());
			json = getJson(0, "", "获取消息列表失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/countMgmMessage")
	public JSONObject countMgmMessage(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			json=getJson(1, mgmMessageService.countMgmMessage(map.get("userId")), "获取成功");
		} catch (Exception e) {
			log.error("获取失败："+e.getMessage());
			json = getJson(0, "", "获取失败");
		}
		return json;
	}
	
	/**
	 * 
	 * <p>修改消息状态为已读</p>
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/updateMgmMessageState")
	public JSONObject updateMgmMessageState(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			json = mgmMessageService.updateMgmMessageState(map.get("id"));
		} catch (Exception e) {
			json = getJson(0, "","修改失败");
		}
		return json;
	}
}
