|
|
package com.yohoufo.order.service.handler;
|
|
|
|
|
|
import com.google.common.eventbus.Subscribe;
|
|
|
import com.yoho.core.redis.cluster.annotation.Redis;
|
|
|
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
|
|
|
import com.yoho.core.redis.cluster.operations.nosync.YHSetOperations;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yohobuy.ufo.model.order.common.SkupStatus;
|
|
|
import com.yohoufo.common.alarm.IEventHandler;
|
|
|
import com.yohoufo.common.cache.CacheKeyEnum;
|
|
|
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderGoodsViewMapper;
|
|
|
import com.yohoufo.dal.order.model.OrderOverTime;
|
|
|
import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
import com.yohoufo.order.event.SellerOrderPriceChangeEvent;
|
|
|
import com.yohoufo.order.mq.DelayTime;
|
|
|
import com.yohoufo.order.mq.TopicConstants;
|
|
|
import com.yohoufo.order.mq.producer.TradeMqSender;
|
|
|
import com.yohoufo.order.service.impl.OrderOverTimeService;
|
|
|
import com.yohoufo.order.service.proxy.InBoxFacade;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by craig
|
|
|
*/
|
|
|
@Component
|
|
|
public class SellerOrderPriceChangeHandler implements IEventHandler<SellerOrderPriceChangeEvent> {
|
|
|
|
|
|
final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderGoodsViewMapper sellerOrderGoodsViewMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private InBoxFacade inBoxFacade;
|
|
|
|
|
|
@Redis("gwNoSyncRedis")
|
|
|
private YHSetOperations yhSetOperations;
|
|
|
|
|
|
@Redis("gwNoSyncRedis")
|
|
|
YHRedisTemplate yhRedisTemplate;
|
|
|
|
|
|
private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
@Override
|
|
|
@Subscribe
|
|
|
public void handle(SellerOrderPriceChangeEvent event) {
|
|
|
logger.info("Subscribe SellerOrderPriceChangeEvent msg, event {}", event);
|
|
|
SellerOrderGoods sog = sellerOrderGoodsMapper.selectByPrimaryKey(event.getSkup());
|
|
|
if(sog==null){
|
|
|
logger.warn("Subscribe SellerOrderPriceChangeEvent msg return SellerOrderGoods is null, event {}", event);
|
|
|
return ;
|
|
|
}
|
|
|
if(SkupStatus.CAN_SELL.getCode()!=sog.getStatus()){
|
|
|
logger.warn("Subscribe SellerOrderPriceChangeEvent msg return SellerOrderGoods status is not can sell , event {} ,sog {}", event,sog);
|
|
|
return ;
|
|
|
}
|
|
|
BigDecimal changedPrice = sog.getGoodsPrice();
|
|
|
//根据storageId获取所有在售的卖家的订单
|
|
|
List<SellerOrderGoods> list= sellerOrderGoodsViewMapper.selectCanSellByStorageId(sog.getStorageId());
|
|
|
//把自己过滤掉,只保留价格高于当前价格
|
|
|
list = list.stream().filter(r->r.getGoodsPrice().compareTo(changedPrice)>0).collect(Collectors.toList());
|
|
|
//从redis过滤uid+storgeid+date
|
|
|
Date currentTime = new Date();
|
|
|
String dateString = formatter.format(currentTime);//2019-03-20
|
|
|
|
|
|
//发送,并记录到redis
|
|
|
RedisKeyBuilder redisKey = getRedisKeyBuilder(dateString);
|
|
|
Set<String> members = yhSetOperations.members(redisKey);
|
|
|
if(CollectionUtils.isNotEmpty(members)){
|
|
|
list = list.stream().filter(r->!members.contains(getValueForSet(r))).collect(Collectors.toList());
|
|
|
}
|
|
|
Set<Integer> noticeSellerUidList = new HashSet<>();
|
|
|
Set<String> redisSetValue = new HashSet<>();
|
|
|
for(SellerOrderGoods r:list){
|
|
|
noticeSellerUidList.add(r.getUid());
|
|
|
redisSetValue.add(getValueForSet(r));
|
|
|
}
|
|
|
logger.info("Subscribe SellerOrderPriceChangeEvent begin send msg, event {},noticeSellerUidList {} ,redisSetValue {}", event,noticeSellerUidList,redisSetValue);
|
|
|
//发消息
|
|
|
inBoxFacade.noticeSellerWhenOtherChangePriceLower(sog,noticeSellerUidList);
|
|
|
// 记录到redis
|
|
|
if(redisSetValue.size()>0){
|
|
|
yhSetOperations.add(redisKey,redisSetValue.toArray(new String[redisSetValue.size()]));
|
|
|
yhRedisTemplate.longExpire(redisKey, 24, TimeUnit.HOURS); //24小时失效
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String getValueForSet(SellerOrderGoods r){
|
|
|
return r.getUid()+"-"+r.getStorageId();
|
|
|
}
|
|
|
|
|
|
public static RedisKeyBuilder getRedisKeyBuilder(String dateString){
|
|
|
return RedisKeyBuilder.newInstance().appendFixed("ufo:order:price:change:").appendVar(dateString);
|
|
|
}
|
|
|
} |
...
|
...
|
|