package com.dgtis.dorm.manager.wx.web;

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

import com.dgtis.dorm.manager.db.domain.AlumniUserMessage;
import com.dgtis.dorm.manager.db.service.UserMessageService;
import com.dgtis.dorm.manager.db.service.UserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/message")
public class WxMessageController {

	@Autowired
	private UserMessageService messageService;
    @Autowired
    private UserService userService;
	/**
	 * 发送私密消息
	 * @param fkUserId
	 * @param content
	 * @param fromUserId
	 * @return
	 */
	@RequestMapping("sendPrivateMessage")
	public Object sendPrivateMessage(String fkUserId, String content, String fromUserId) {
		if(fkUserId == null || content == null || fromUserId == null) {
			return ResponseUtil.fail402();
		}
		Map<Object, Object> result = new HashMap<Object, Object>();
		userService.sendMessage(fkUserId, "private", content, fromUserId);
		result.put("state", "success");
		result.put("massege", "发送成功");
		return ResponseUtil.ok(result);
	}
	
	/**
	 * 查询未读消息数量
	 * @return
	 */
	@RequestMapping("getMyPrivateMsgCount")
	public Object getMyPrivateMsgCount(String userId) {
		if(userId == null) {
			return ResponseUtil.fail402();
		}
		Map<String, Object> result = messageService.findMyPrivateMsgCount(userId);
		return ResponseUtil.ok(result);
	}

	/**
	 * 查询用户消息
	 * @param userId
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("getMyPrivateMessageList")
	public Object getMyPrivateMessageList(String userId,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		if(userId == null) {
			return ResponseUtil.fail402();
		}
		AlumniUserMessage message = new AlumniUserMessage();
		message.setMessageType("private");
		message.setFkUserId(userId);
		Map<String, Object> result = messageService.findUserMessage(message, page, size);
		return ResponseUtil.ok(result);
	}

	/**
	 * 用户消息聊天记录
	 */
	@RequestMapping("messageDetails")
	public Object messageDetails(String userId, String fromUserId) {
		if(userId == null || fromUserId == null) {
			return ResponseUtil.fail402();
		}
		Map<String, Object> result = messageService.findUserMessageDetails(userId, fromUserId);
		return ResponseUtil.ok(result);
	}

	/**
	 * 查询系统消息
	 * @param userId
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("getMySystemMessageList")
	public Object getMySystemMessageList(String userId,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		if(userId == null) {
			return ResponseUtil.fail402();
		}
		AlumniUserMessage message = new AlumniUserMessage();
		message.setFkUserId(userId);
		Map<String, Object> result = messageService.findSystemUserMessageByUserId(userId, page, size);
		return ResponseUtil.ok(result);
	}
	
	/**
	 * 消息详情
	 * @param messageId
	 * @return
	 */
	@RequestMapping("getMySystemMessageDetails")
	public Object getMySystemMessageDetails(String messageId) {
		if(messageId == null) {
			return ResponseUtil.fail402();
		}
		Map<String, Object> result = messageService.findMySystemMessageDetails(messageId);
		return ResponseUtil.ok(result);
	}

	@RequestMapping("updateMsg")
	public Object updateMsg(String msgId) {
		if (msgId == null) {
			return ResponseUtil.fail402();
		}
		messageService.updateMsg(msgId);
		return ResponseUtil.ok();
	}
}
