Authored by qinchao

Merge branch 'dev_689_更低价出售提醒' into test6.8.9

@@ -35,4 +35,6 @@ public interface SellerOrderGoodsViewMapper { @@ -35,4 +35,6 @@ public interface SellerOrderGoodsViewMapper {
35 @Param("limit")Integer limit); 35 @Param("limit")Integer limit);
36 36
37 int selectEntryCntByUidStatusGBSku(SellerOrderGoods condition); 37 int selectEntryCntByUidStatusGBSku(SellerOrderGoods condition);
  38 +
  39 + List<SellerOrderGoods> selectCanSellByStorageId(@Param("storageId")int storageId);
38 } 40 }
@@ -123,4 +123,11 @@ @@ -123,4 +123,11 @@
123 ) t 123 ) t
124 </select> 124 </select>
125 125
  126 +
  127 + <select id="selectEntryListByUidStatusGBSkc" resultMap="BaseResultMap">
  128 + SELECT <include refid="Base_Column_List" />
  129 + FROM seller_order_goods
  130 + WHERE storage_id = #{storageId,jdbcType=INTEGER} and status = 1
  131 + </select>
  132 +
126 </mapper> 133 </mapper>
  1 +package com.yohoufo.order.event;
  2 +
  3 +import com.yohoufo.common.alarm.Event;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.NoArgsConstructor;
  7 +import lombok.experimental.Builder;
  8 +
  9 +/**
  10 + * Created by craig.qin on 2019/3/20.
  11 + * 价格变动(调价、新的上架)发消息
  12 + */
  13 +@Data
  14 +@Builder
  15 +@NoArgsConstructor
  16 +@AllArgsConstructor
  17 +public class SellerOrderPriceChangeEvent extends Event{
  18 +
  19 + private int sellerUid;
  20 +
  21 + private Integer skup;
  22 +}
  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 +}
@@ -21,6 +21,7 @@ import java.math.BigDecimal; @@ -21,6 +21,7 @@ import java.math.BigDecimal;
21 import java.util.Arrays; 21 import java.util.Arrays;
22 import java.util.List; 22 import java.util.List;
23 import java.util.Optional; 23 import java.util.Optional;
  24 +import java.util.Set;
24 import java.util.concurrent.ArrayBlockingQueue; 25 import java.util.concurrent.ArrayBlockingQueue;
25 import java.util.concurrent.ExecutorService; 26 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.ThreadPoolExecutor; 27 import java.util.concurrent.ThreadPoolExecutor;
@@ -945,6 +946,36 @@ public class InBoxFacade { @@ -945,6 +946,36 @@ public class InBoxFacade {
945 } 946 }
946 } 947 }
947 948
  949 + /**
  950 + * 更低的出价提醒
  951 + * @param sog
  952 + */
  953 + public void noticeSellerWhenOtherChangePriceLower(SellerOrderGoods sog,Set<Integer> noticeSellerUidList){
  954 + String prdName = sog.getProductName();
  955 + String sizeName = sog.getSizeName();
  956 + try {
  957 + executorService.execute(() -> {
  958 + // 发站内信
  959 + logger.info("record noticeSellerWhenOtherChangePriceLower for seller,noticeSellerUidList {}, sog {}", noticeSellerUidList, sog);
  960 +
  961 + Product product = productMapper.selectByPrimaryKey(sog.getProductId());
  962 + String productCode = product.getProductCode();
  963 + String params = buildParams(prdName, sizeName,productCode,sog.getGoodsPrice().toPlainString());
  964 +
  965 + noticeSellerUidList.stream().forEach(
  966 + sellerUid ->{
  967 + InboxReqVO req = buildInboxReqVO(sellerUid, params, InboxBusinessTypeEnum.NOTICE_SELLER_WHEN_OTHER_LOWER_PRICE);
  968 + InBoxResponse resp = inBoxSDK.addInbox(req);
  969 + logger.info("record seller sham send out inbox msg,sellerUid {}, prdName {},sizeName {} resp {}",
  970 + sellerUid, prdName, sizeName, resp);
  971 + }
  972 + );
  973 + });
  974 + } catch (Exception e) {
  975 + logger.warn("InBoxFacade noticeSellerWhenOtherChangePriceLower error inbox msg, noticeSellerUidList {}, sog {} ", noticeSellerUidList, sog, e);
  976 + }
  977 + }
  978 +
948 /********************************************************************* 979 /*********************************************************************
949 * 其他---短信 980 * 其他---短信
950 ********************************************************************/ 981 ********************************************************************/