| | |
| | | import com.aliyun.openservices.ons.api.Message;
|
| | | import com.aliyun.openservices.ons.api.MessageListener;
|
| | | import com.aliyun.openservices.ons.api.Producer;
|
| | | import com.aliyun.openservices.ons.api.SendResult;
|
| | | import com.google.gson.Gson;
|
| | | import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
|
| | | import com.yeshi.fanli.dto.mq.order.body.OrderMQMsg;
|
| | | import com.yeshi.fanli.dto.mq.order.body.OrderMoneyRecievedMQMsg;
|
| | | import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
|
| | | import com.yeshi.fanli.dto.mq.user.body.UserSystemCouponUseMQMsg;
|
| | | import com.yeshi.fanli.entity.bus.user.UserSystemCouponRecord;
|
| | | import com.yeshi.fanli.entity.mq.MQUnSendInfo;
|
| | | import com.yeshi.fanli.entity.system.SystemCoupon;
|
| | | import com.yeshi.fanli.dto.mq.order.body.TeamRewardPreRecieveMQMsg;
|
| | | import com.yeshi.fanli.exception.ParamsException;
|
| | | import com.yeshi.fanli.exception.money.OrderMoneySettleException;
|
| | | import com.yeshi.fanli.exception.mq.MQUnSendInfoException;
|
| | | import com.yeshi.fanli.exception.money.TeamEincomeRecordException;
|
| | | import com.yeshi.fanli.exception.order.CommonOrderException;
|
| | | import com.yeshi.fanli.exception.order.InviteOrderSubsidyException;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | import com.yeshi.fanli.service.inter.mq.MQUnSendInfoService;
|
| | | import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService;
|
| | | import com.yeshi.fanli.service.inter.order.CommonOrderService;
|
| | | import com.yeshi.fanli.service.inter.order.OrderMoneySettleService;
|
| | | import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
|
| | | import com.yeshi.fanli.service.manger.order.TeamSubsidyManager;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.util.rocketmq.MQTopicName;
|
| | |
|
| | | /**
|
| | |
| | | @Component
|
| | | public class InviteOrderSubsidyMessageListener implements MessageListener {
|
| | | @Resource
|
| | | private InviteOrderSubsidyService inviteOrderSubsidyService;
|
| | | private TeamSubsidyManager teamSubsidyManager;
|
| | |
|
| | | @Resource
|
| | | private UserSystemCouponRecordService userSystemCouponRecordService;
|
| | |
| | |
|
| | | @Resource
|
| | | private OrderMoneySettleService orderMoneySettleService;
|
| | |
|
| | | @Resource
|
| | | private CommonOrderService commonOrderService;
|
| | |
|
| | | // 团队奖励开始时间
|
| | | private static final long TEAM_REWARD_START_TIME = TimeUtil.convertToTimeTemp("2019-04-15", "yyyy-MM-dd");
|
| | |
|
| | | public InviteOrderSubsidyMessageListener() {
|
| | |
|
| | |
| | | // 查询上级红包
|
| | | OrderMQMsg dto = new Gson().fromJson(new String(message.getBody()), OrderMQMsg.class);
|
| | | if (dto != null) {
|
| | | try {
|
| | | inviteOrderSubsidyService.addOrUpdateByOrder(dto.getOrderId(), dto.getType());
|
| | | } catch (InviteOrderSubsidyException e) {
|
| | | return Action.ReconsumeLater;
|
| | | }
|
| | | }
|
| | | } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderFanLiActual.name())) {
|
| | | OrderMoneyRecievedMQMsg dto = new Gson().fromJson(new String(message.getBody()),
|
| | | OrderMoneyRecievedMQMsg.class);
|
| | | if (dto != null) {
|
| | | if (dto.getType() == OrderMoneyRecievedMQMsg.TYPE_ZIGOU) {// 自购到账
|
| | | // 判断订单号是否在补贴中
|
| | | long count = inviteOrderSubsidyService.countByOrderIdAndSourceType(dto.getOrderId(),
|
| | | dto.getSourceType());
|
| | | if (count > 0) {// 延时消息发送
|
| | | sendOrderSubsidyUpdateLater(dto, 5);
|
| | | }
|
| | | } else if (dto.getType() == OrderMoneyRecievedMQMsg.TYPE_INVITE) {// 邀请到账
|
| | | // 补贴到账
|
| | | // 查询出订单创建时间
|
| | | Date orderTime = commonOrderService.getThirdCreateTime(dto.getOrderId(), dto.getType());
|
| | | if (orderTime.getTime() >= TEAM_REWARD_START_TIME) {
|
| | | try {
|
| | | orderMoneySettleService.inviteSubsidySettle(dto.getUid(), message.getKey());
|
| | | teamSubsidyManager.addOrUpdateOrder(dto.getOrderId(), dto.getType());
|
| | | return Action.CommitMessage;
|
| | | } catch (OrderMoneySettleException e) {
|
| | | } catch (InviteOrderSubsidyException e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e, new Gson().toJson(dto), "");
|
| | | } catch (CommonOrderException e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e, new Gson().toJson(dto), "");
|
| | | }
|
| | | return Action.ReconsumeLater;
|
| | | } else {
|
| | | try {
|
| | | teamSubsidyManager.addOrUpdateOrder(dto.getOrderId(), dto.getType());
|
| | | } catch (Exception e) {
|
| | | LogHelper.errorDetailInfo(e, new Gson().toJson(dto), "");
|
| | | return Action.ReconsumeLater;
|
| | | }
|
| | | }
|
| | | }
|
| | | } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.orderFanLiDelay.name())) {
|
| | | OrderMoneyRecievedMQMsg dto = new Gson().fromJson(new String(message.getBody()),
|
| | | OrderMoneyRecievedMQMsg.class);
|
| | | if (dto != null) {
|
| | | if (dto.getType() == OrderMoneyRecievedMQMsg.TYPE_ZIGOU) {// 延时时间已到
|
| | | // 判断订单号是否在补贴中
|
| | | long count = inviteOrderSubsidyService.countByOrderIdAndSourceType(dto.getOrderId(),
|
| | | dto.getSourceType());
|
| | | if (count > 0) {
|
| | | // 判断是否要使用奖励券
|
| | | UserSystemCouponRecord record = userSystemCouponRecordService
|
| | | .getRecordByOrderNo(dto.getOrderId(), UserSystemCouponRecord.STATE_SUCCESS);
|
| | | if (record != null
|
| | | && record.getCouponType()
|
| | | .equalsIgnoreCase(SystemCoupon.CouponTypeEnum.rebatePercentCoupon.name())
|
| | | && record.getGoodSource() == dto.getSourceType()) {// 成功使用了奖励券
|
| | | inviteOrderSubsidyService.invalidByOrderIdAndSourceType(dto.getOrderId(),
|
| | | dto.getSourceType());
|
| | | } else {
|
| | | // 使数据有效
|
| | | try {
|
| | | inviteOrderSubsidyService.validByOrderIdAndSourceType(dto.getOrderId(),
|
| | | dto.getSourceType());
|
| | | } catch (InviteOrderSubsidyException e) {
|
| | | e.printStackTrace();
|
| | | return Action.ReconsumeLater;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | } else if (MQTopicName.TOPIC_USER.name().equalsIgnoreCase(message.getTopic())) {
|
| | | if (tag.equalsIgnoreCase(UserTopicTagEnum.useSystemCoupon.name())) {
|
| | | UserSystemCouponUseMQMsg dto = new Gson().fromJson(new String(message.getBody()),
|
| | | UserSystemCouponUseMQMsg.class);
|
| | | if (dto != null && dto.getCouponType()
|
| | | .equalsIgnoreCase(SystemCoupon.CouponTypeEnum.rebatePercentCoupon.name())) {// 返利奖励券的使用
|
| | | long count = inviteOrderSubsidyService.countByOrderIdAndSourceType(dto.getOrderId(),
|
| | | dto.getSourceType());
|
| | | if (count > 0) {
|
| | | // 使相关订单失效
|
| | | inviteOrderSubsidyService.invalidByOrderIdAndSourceType(dto.getOrderId(), dto.getSourceType());
|
| | | }
|
| | | } else if (tag.equalsIgnoreCase(OrderTopicTagEnum.teamRewardPreRecieved.name())) {// 团队奖金预到账
|
| | | TeamRewardPreRecieveMQMsg dto = new Gson().fromJson(new String(message.getBody()),
|
| | | TeamRewardPreRecieveMQMsg.class);
|
| | | try {
|
| | | teamSubsidyManager.addToTeamEincome(dto.getUid(), message.getKey(), dto.getPreGetTime());
|
| | | return Action.CommitMessage;
|
| | | } catch (OrderMoneySettleException e) {
|
| | | e.printStackTrace();
|
| | | } catch (TeamEincomeRecordException e) {
|
| | | e.printStackTrace();
|
| | | } catch (ParamsException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return Action.ReconsumeLater;
|
| | | }
|
| | | //
|
| | | }
|
| | | return Action.CommitMessage;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 发送订单延时消息
|
| | | * |
| | | * @param day
|
| | | */
|
| | | private boolean sendOrderSubsidyUpdateLater(OrderMoneyRecievedMQMsg dto, int day) {
|
| | |
|
| | | Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiDelay, dto);
|
| | | msg.setKey(dto.getSourceType() + "_" + dto.getOrderId());
|
| | | long delayTime = System.currentTimeMillis() + (Constant.IS_TEST ? 1000 * 30L : 1000 * 60 * 60 * 24L * day);
|
| | | msg.setStartDeliverTime(delayTime);
|
| | | SendResult sendResult = producer.send(msg);
|
| | | if (sendResult != null) {
|
| | | return true;
|
| | | } else {
|
| | | MQUnSendInfo info = new MQUnSendInfo();
|
| | | info.setBody(new String(msg.getBody()));
|
| | | if (msg.getStartDeliverTime() > 0)
|
| | | info.setDeliverTime(new Date(msg.getStartDeliverTime()));
|
| | | info.setKey(msg.getKey());
|
| | | info.setTag(msg.getTag());
|
| | | info.setTopic(msg.getTopic());
|
| | | try {
|
| | | mqUnSendInfoService.addMQUnSendInfo(info);
|
| | | } catch (MQUnSendInfoException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | }
|