package com.qmth.qrzk.web.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
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.qmth.qrzk.repository.service.query.TaskQueryParams;
import com.qmth.qrzk.repository.task.Message;
import com.qmth.qrzk.repository.user.service.impl.MessageServiceImpl;
import com.qmth.qrzk.repository.utils.General;

@Controller
@RequestMapping("/message")
public class MessageController extends BaseAjaxController{
	
	private static final Logger log = Logger.getLogger(MessageController.class);
	
	@Autowired 
	private MessageServiceImpl messageService;
	
	@RequestMapping(value="/queryRelatedMessage")
	@ResponseBody
	public Map<String, Object> queryRelatedMessage(HttpServletRequest request, HttpSession session, TaskQueryParams params){
		if(params == null){
			params = new TaskQueryParams();
		}
		params.setRows(999);
		Map<String,Object> result = new HashMap<String, Object>();
		params.setCurDealer(this.getCurUser(session).getId());
		List<Message> rows = messageService.queryRelatedMessage(params);
		String path = General.getBasePath(request);
		result.put("rows", rows);
		result.put("total", params.getTotalNum());
		result.put("appPath", path);
		return result;
	}
	
	@RequestMapping("/saveMessage")
	@ResponseBody
	public Map<String,Object> saveMessage(int taskId, int nextUserId, HttpSession session){
		try{
			messageService.saveMessage(taskId, nextUserId, getCurUser(session));
			Map<String,Object> resultMap = successResult("保存消息成功");
			return resultMap;
		}catch(Exception e){
			log.error("保存消息[id=" + taskId + "]失败，原因：" + e.getMessage(), e);
			return failedResult(e);
		}
	}
}
