|
|
1
|
+package com.yohoufo.order.service.handler;
|
|
|
2
|
+
|
|
|
3
|
+import com.google.common.eventbus.Subscribe;
|
|
|
4
|
+import com.yoho.core.redis.cluster.annotation.Redis;
|
|
|
5
|
+import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
|
|
|
6
|
+import com.yoho.core.redis.cluster.operations.nosync.YHSetOperations;
|
|
|
7
|
+import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
8
|
+import com.yohobuy.ufo.model.order.common.SkupStatus;
|
|
|
9
|
+import com.yohoufo.common.alarm.IEventHandler;
|
|
|
10
|
+import com.yohoufo.common.cache.CacheKeyEnum;
|
|
|
11
|
+import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
12
|
+import com.yohoufo.dal.order.SellerOrderGoodsViewMapper;
|
|
|
13
|
+import com.yohoufo.dal.order.model.OrderOverTime;
|
|
|
14
|
+import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
15
|
+import com.yohoufo.order.event.SellerOrderPriceChangeEvent;
|
|
|
16
|
+import com.yohoufo.order.mq.DelayTime;
|
|
|
17
|
+import com.yohoufo.order.mq.TopicConstants;
|
|
|
18
|
+import com.yohoufo.order.mq.producer.TradeMqSender;
|
|
|
19
|
+import com.yohoufo.order.service.impl.OrderOverTimeService;
|
|
|
20
|
+import com.yohoufo.order.service.proxy.InBoxFacade;
|
|
|
21
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
22
|
+import org.slf4j.Logger;
|
|
|
23
|
+import org.slf4j.LoggerFactory;
|
|
|
24
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
25
|
+import org.springframework.stereotype.Component;
|
|
|
26
|
+
|
|
|
27
|
+import java.math.BigDecimal;
|
|
|
28
|
+import java.text.SimpleDateFormat;
|
|
|
29
|
+import java.util.*;
|
|
|
30
|
+import java.util.concurrent.TimeUnit;
|
|
|
31
|
+import java.util.stream.Collectors;
|
|
|
32
|
+
|
|
|
33
|
+/**
|
|
|
34
|
+ * Created by craig
|
|
|
35
|
+ */
|
|
|
36
|
+@Component
|
|
|
37
|
+public class SellerOrderPriceChangeHandler implements IEventHandler<SellerOrderPriceChangeEvent> {
|
|
|
38
|
+
|
|
|
39
|
+ final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
40
|
+
|
|
|
41
|
+ @Autowired
|
|
|
42
|
+ private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
43
|
+
|
|
|
44
|
+ @Autowired
|
|
|
45
|
+ private SellerOrderGoodsViewMapper sellerOrderGoodsViewMapper;
|
|
|
46
|
+
|
|
|
47
|
+ @Autowired
|
|
|
48
|
+ private InBoxFacade inBoxFacade;
|
|
|
49
|
+
|
|
|
50
|
+ @Redis("gwNoSyncRedis")
|
|
|
51
|
+ private YHSetOperations yhSetOperations;
|
|
|
52
|
+
|
|
|
53
|
+ @Redis("gwNoSyncRedis")
|
|
|
54
|
+ YHRedisTemplate yhRedisTemplate;
|
|
|
55
|
+
|
|
|
56
|
+ private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
57
|
+
|
|
|
58
|
+ @Override
|
|
|
59
|
+ @Subscribe
|
|
|
60
|
+ public void handle(SellerOrderPriceChangeEvent event) {
|
|
|
61
|
+ logger.info("Subscribe SellerOrderPriceChangeEvent msg, event {}", event);
|
|
|
62
|
+ SellerOrderGoods sog = sellerOrderGoodsMapper.selectByPrimaryKey(event.getSkup());
|
|
|
63
|
+ if(sog==null){
|
|
|
64
|
+ logger.warn("Subscribe SellerOrderPriceChangeEvent msg return SellerOrderGoods is null, event {}", event);
|
|
|
65
|
+ return ;
|
|
|
66
|
+ }
|
|
|
67
|
+ if(SkupStatus.CAN_SELL.getCode()!=sog.getStatus()){
|
|
|
68
|
+ logger.warn("Subscribe SellerOrderPriceChangeEvent msg return SellerOrderGoods status is not can sell , event {} ,sog {}", event,sog);
|
|
|
69
|
+ return ;
|
|
|
70
|
+ }
|
|
|
71
|
+ BigDecimal changedPrice = sog.getGoodsPrice();
|
|
|
72
|
+ //根据storageId获取所有在售的卖家的订单
|
|
|
73
|
+ List<SellerOrderGoods> list= sellerOrderGoodsViewMapper.selectCanSellByStorageId(sog.getStorageId());
|
|
|
74
|
+ //把自己过滤掉,只保留价格高于当前价格
|
|
|
75
|
+ list = list.stream().filter(r->r.getGoodsPrice().compareTo(changedPrice)>0).collect(Collectors.toList());
|
|
|
76
|
+ //从redis过滤uid+storgeid+date
|
|
|
77
|
+ Date currentTime = new Date();
|
|
|
78
|
+ String dateString = formatter.format(currentTime);//2019-03-20
|
|
|
79
|
+
|
|
|
80
|
+ //发送,并记录到redis
|
|
|
81
|
+ RedisKeyBuilder redisKey = getRedisKeyBuilder(dateString);
|
|
|
82
|
+ Set<String> members = yhSetOperations.members(redisKey);
|
|
|
83
|
+ if(CollectionUtils.isNotEmpty(members)){
|
|
|
84
|
+ list = list.stream().filter(r->!members.contains(getValueForSet(r))).collect(Collectors.toList());
|
|
|
85
|
+ }
|
|
|
86
|
+ Set<Integer> noticeSellerUidList = new HashSet<>();
|
|
|
87
|
+ Set<String> redisSetValue = new HashSet<>();
|
|
|
88
|
+ for(SellerOrderGoods r:list){
|
|
|
89
|
+ noticeSellerUidList.add(r.getUid());
|
|
|
90
|
+ redisSetValue.add(getValueForSet(r));
|
|
|
91
|
+ }
|
|
|
92
|
+ logger.info("Subscribe SellerOrderPriceChangeEvent begin send msg, event {},noticeSellerUidList {} ,redisSetValue {}", event,noticeSellerUidList,redisSetValue);
|
|
|
93
|
+ //发消息
|
|
|
94
|
+ inBoxFacade.noticeSellerWhenOtherChangePriceLower(sog,noticeSellerUidList);
|
|
|
95
|
+ // 记录到redis
|
|
|
96
|
+ if(redisSetValue.size()>0){
|
|
|
97
|
+ yhSetOperations.add(redisKey,redisSetValue.toArray(new String[redisSetValue.size()]));
|
|
|
98
|
+ yhRedisTemplate.longExpire(redisKey, 24, TimeUnit.HOURS); //24小时失效
|
|
|
99
|
+ }
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ public String getValueForSet(SellerOrderGoods r){
|
|
|
103
|
+ return r.getUid()+"-"+r.getStorageId();
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ public static RedisKeyBuilder getRedisKeyBuilder(String dateString){
|
|
|
107
|
+ return RedisKeyBuilder.newInstance().appendFixed("ufo:order:price:change:").appendVar(dateString);
|
|
|
108
|
+ }
|
|
|
109
|
+} |