Authored by chaogeng

添加手动发站内信

package com.yoho.activity.queue.restapi;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.activity.queue.ApiResponse;
import com.yoho.core.common.restbean.ResponseBean;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.queue.dal.IDrawlineLuckyUserDAO;
import com.yoho.queue.dal.IDrawlinePrizeSettingDAO;
import com.yoho.queue.dal.model.DrawlineLuckyUser;
import com.yoho.queue.dal.model.DrawlinePrizeSetting;
import com.yoho.service.model.activity.drawline.request.InboxReqBO;
@Controller
@RequestMapping("/LuckyUserNoticeRest")
public class LuckyUserNoticeRest {
private static Logger logger = LoggerFactory.getLogger(LuckyUserNoticeRest.class);
private static final String SEND_INBOX_SERVICE_URL = "message.saveInbox";
private static final String LIMITCODE_MSG_TITLE = "【限购码】您获得了1个限购码";
private static final String LIMITCODE_MSG_CONTENT = "尊敬的客户:恭喜您成功获得1个限购码!您可以在个人中心查看详情和使用。";
@Autowired
private IDrawlineLuckyUserDAO luckyUserDAO;
@Autowired
private IDrawlinePrizeSettingDAO prizeSettingDAO;
@Autowired
ServiceCaller serviceCaller;
@RequestMapping("/noticeByActId")
@ResponseBody
public ApiResponse noticeByActId(int activityId) {
logger.info("Enter noticeByActId.");
ApiResponse res = new ApiResponse();
if(activityId < 1){
// (1) 查询未发送站内信中奖用户参与活动的ID
List<Integer> actIdList = luckyUserDAO.selectUnNoticeActivityIds();
logger.debug("luckyUserDAO.selectUnNoticeActivityIds: result is {}", actIdList);
if (null == actIdList || 0 == actIdList.size()) {
return res;
}
for (Integer actId : actIdList) {
luckyUserNotice(actId);
}
}else{
luckyUserNotice(activityId);
}
return res;
}
private JSONArray luckyUserNotice(int activityId){
JSONArray res = new JSONArray();
DrawlinePrizeSetting prizeSetting = prizeSettingDAO.selectByActivityId(activityId);
logger.debug("prizeSettingDAO.selectByActivityId: articleId is {}", activityId);
if (null == prizeSetting) {
logger.warn("prize is not setted artivityId is {}", activityId);
return res;
}
// (1) 查询中奖用户列表
List<DrawlineLuckyUser> luckyUserList = luckyUserDAO.selectNotcieUserByActId(activityId);
logger.debug("luckyUserDAO.selectNotcieUserByActId: articleId is {}", activityId);
if (null == luckyUserList || 0 == luckyUserList.size()) {
return res;
}
// (2) 遍历中奖用户列表发送站内信
for (DrawlineLuckyUser luckyUser : luckyUserList) {
if (null == luckyUser || null == luckyUser.getUid() || 0 == luckyUser.getUid()) {
continue;
}
int luckyUserId = luckyUser.getUid();
InboxReqBO inboxReqBO = new InboxReqBO();
inboxReqBO.setUid(luckyUserId);
inboxReqBO.setType("1");
inboxReqBO.setTitle(LIMITCODE_MSG_TITLE);
inboxReqBO.setContent(LIMITCODE_MSG_CONTENT);
try {
// (2.1) 调用发送站内信服务
ResponseBean resBean = serviceCaller.call(SEND_INBOX_SERVICE_URL, inboxReqBO, ResponseBean.class);
if (null != resBean && resBean.getCode().equals("200")) {
// (2.2) 更新中奖表站内信发送状态
int num = luckyUserDAO.updateByActIdAndUid(activityId, luckyUserId);
logger.debug("luckyUserDAO.updateByActIdAndUids, activityId is {}, uid is {}", activityId, luckyUserId);
if (num > 0) {
JSONObject userObj = new JSONObject();
userObj.put("luckyUserId", luckyUserId);
res.add(userObj);
logger.info("send lucky user inbox success, uid is {}", luckyUserId);
}
}
} catch (Exception e) {
logger.error("send luckyUser inbox failed", e);
}
}
return res;
}
}
... ...