InBoxRest.java 7.37 KB
package com.yoho.yhmessage.inbox.restapi;

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.common.restbean.ResponseBean;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.message.dal.model.InBox;
import com.yoho.service.model.inbox.request.BatchInboxReqBO;
import com.yoho.service.model.inbox.request.GetListReqBO;
import com.yoho.service.model.inbox.request.InboxReqBO;
import com.yoho.service.model.inbox.request.SetIsReadRequestBO;
import com.yoho.service.model.inbox.response.GetListRespBO;
import com.yoho.service.model.inbox.response.PageResponseBO;
import com.yoho.service.model.response.CommonRspBO;
import com.yoho.yhmessage.inbox.service.IInBoxService;
import com.yoho.yhmessage.inbox.service.model.CouponInBox;

/**
 * 类的描述
 *
 * @author CaoQi
 * @Time 2015/11/11
 */
//@Controller
//@RequestMapping("/InBoxRest")
public class InBoxRest {

	private static Logger logger = LoggerFactory.getLogger(InBoxRest.class);

	@Autowired
	private IInBoxService iInBoxService;

	@RequestMapping("/queryListById")
	@ResponseBody
	public Map<String, Object> queryListById(@RequestBody InboxReqBO inboxReqBO) {
		Integer count = iInBoxService.countById(inboxReqBO.getUid());
		List<InBox> inBoxList = iInBoxService.queryListById(inboxReqBO.getUid());
		Map<String, Object> result = new HashMap<>();
		result.put("total", count);
		result.put("list", inBoxList);
		return result;
	}

	@RequestMapping("/getInboxCount")
	@ResponseBody
	public Integer getInboxCount(@RequestBody InboxReqBO inboxReqBO) {
		logger.info("Begin Enter getInboxCount. inboxReqBO is {}", inboxReqBO);
		// (1)判断用户id是否存在
		if (null == inboxReqBO || inboxReqBO.getUid() < 1) {
			logger.warn("Uid is null or 0.");
			throw new ServiceException(ServiceError.SMS_INBOX_UID_NULL);
		}

		// (2)判断是否未读
		String isRead = (null == inboxReqBO.getIsRead()) ? "N" : inboxReqBO.getIsRead();

		// (2)调用服务获取inbox的数量
		int count = iInBoxService.countByParam(inboxReqBO.getUid(), isRead, "N");
		logger.info("End getInboxCount. inboxReqBO is {}", inboxReqBO);
		return count;
	}

	@RequestMapping("/saveInbox")
	@ResponseBody
	public ResponseBean saveInbox(@RequestBody InboxReqBO inboxReqBO) {
		InBox inBox = new InBox();
		inBox.setUid(inboxReqBO.getUid());
		inBox.setSendUid(inboxReqBO.getSendUid());
		inBox.setType(StringUtils.isEmpty(inboxReqBO.getType()) ? CouponInBox.TYPE : inboxReqBO.getType());
		inBox.setIsRead("N");
		inBox.setIsDel("N");
		inBox.setTitle(inboxReqBO.getTitle());
		inBox.setCreateTime(new Date().getTime() / 1000);
		inBox.setContent(inboxReqBO.getContent());
		inBox.setVerifyKey(inboxReqBO.getVerifyKey());
		iInBoxService.saveInbox(inBox);

		ResponseBean responseBean = new ResponseBean();
		responseBean.setCode("200");
		responseBean.setMessage("保存成功");
		return responseBean;
	}
	
	/**
	 * 批量发送站内信
	 * @param bo
	 * @return
	 */
	@RequestMapping("/insertBatch")
	@ResponseBody
	public CommonRspBO insertBatch(@RequestBody BatchInboxReqBO bo) throws ServiceException {
		logger.info("insertBatch with bo={}", bo);
		CommonRspBO response = iInBoxService.insertBatch(bo);
		logger.info("insertBatch success with bo={}", bo);
		return response;
	}

	@RequestMapping("/updateInbox")
	@ResponseBody
	public ResponseBean updateInbox(@RequestBody InBox inBox) {
		iInBoxService.updateById(inBox);

		ResponseBean responseBean = new ResponseBean();
		responseBean.setCode("200");
		responseBean.setMessage("更新成功");
		return responseBean;
	}
	
	@RequestMapping("/delByIdsAndUid")
    @ResponseBody
    public ResponseBean delByIdsAndUid(@RequestBody InboxReqBO inboxReqBO) {
	    logger.info("Begin Enter delByIdsAndUid. InboxReqBO is {}", inboxReqBO);
	    if(inboxReqBO.getIds()==null){
	        ResponseBean responseBean = new ResponseBean();
	        responseBean.setCode("404");
	        responseBean.setMessage("message is null"); 
	        return responseBean;
	    }
	    if("".equals(inboxReqBO.getIds())){
	        ResponseBean responseBean = new ResponseBean();
            responseBean.setCode("405");
            responseBean.setMessage("id is null"); 
            return responseBean;
	    }
	    JSONArray js = new JSONArray();
	    for(String i:inboxReqBO.getIds().split(",")){
	        JSONObject obj = new JSONObject();
	        obj.put("id", i);
	        js.add(obj);
	    }
	    iInBoxService.delByIdsAndUid(inboxReqBO.getUid(),inboxReqBO.getIds());
	    ResponseBean responseBean = new ResponseBean();
        responseBean.setCode("200");
        responseBean.setData(js);
        responseBean.setMessage("success");
        return responseBean;
	}
	
	/**
	 * 老接口,获得用户未读取的消息列表
	 * @param page当前第几页,size分页长度,uid用户id
	 */
	@RequestMapping("/messageCount")
    @ResponseBody
	public ResponseBean messageCount(@RequestBody GetListReqBO reqBO){
	    logger.info("messageCount with param is {}", reqBO);
	    Integer uid = reqBO.getUid();
	    ResponseBean responseBean = new ResponseBean();
	    JSONObject obj = new JSONObject();
	    if(uid<1){
	        responseBean.setCode("400");
	        responseBean.setMessage("Message Count");
	        obj.put("count",0);
	        responseBean.setData(obj);
	        return responseBean;
	    }
	    PageResponseBO<GetListRespBO> pageList = iInBoxService.getInBoxList(reqBO, true);
	    obj.put("count",pageList.getTotal());
	    // pageList.getList()
        responseBean.setCode("200");
        responseBean.setMessage("success");
        responseBean.setData(pageList);
        logger.info("messageCount success with param is {}", reqBO);
        return responseBean;
	}
	
	/**
	 * 批量设置消息为已读
	 * @param req
	 * @return
	 */
	@RequestMapping("/setIsRead")
	@ResponseBody
	public CommonRspBO setIsRead(@RequestBody SetIsReadRequestBO req) {
		logger.info("setIsRead with param is {}", req);
		iInBoxService.setIsRead(req.getUid(), req.getIds());
		logger.info("setIsRead success with param is {}", req);
		return new CommonRspBO();
	}

	/**
	 * 获取消息列表(分页)
	 * 该接口用来兼容老版本app
	 * 
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping(value = "/getInBoxList")
	@ResponseBody
	public PageResponseBO<GetListRespBO> getInBoxList(@RequestBody GetListReqBO request) throws ServiceException {
		logger.info("enter getInBoxList with param is {}", request);
		PageResponseBO<GetListRespBO> pageList = iInBoxService.getInBoxList(request, true);
		return pageList;
	}

	/**
	 * 获取消息列表(分页)
	 * 新接口
	 * 
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping(value = "/getInBoxListNew")
	@ResponseBody
	public PageResponseBO<GetListRespBO> getInBoxListNew(@RequestBody GetListReqBO request) throws ServiceException {
		logger.info("enter getInBoxListForH5 with param is {}", request);
		return iInBoxService.getInBoxList(request, false);
	}
}