...
|
...
|
@@ -16,12 +16,14 @@ import com.yohoufo.dal.promotion.model.UserCoupon; |
|
|
import com.yohoufo.promotion.convert.CouponConvert;
|
|
|
import com.yohoufo.promotion.service.cache.CollectiveCouponCacheService;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.function.Supplier;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -57,6 +59,13 @@ public class CollectiveCouponService { |
|
|
logger.warn("queryCouponListOfPrdDetail prdId illegal, {}",req);
|
|
|
return couponInfoListBuilder.build();
|
|
|
}
|
|
|
CouponListBo couponListBo = collectiveCouponCacheService.queryCouponListOfPrdDetail(req.getBusinessClient(),
|
|
|
prdId, pageNum, limit);
|
|
|
if (Objects.nonNull(couponListBo)){
|
|
|
resetDynamic(req, couponListBo.getCoupons());
|
|
|
return couponListBo;
|
|
|
}
|
|
|
|
|
|
final int currentDT = DateUtil.getCurrentTimeSecond();
|
|
|
final int positionType = PositionType.PRD_DETAIL.getCode();
|
|
|
|
...
|
...
|
@@ -69,26 +78,60 @@ public class CollectiveCouponService { |
|
|
final int offset = PageHelper.getOffsetOfMysql(pageNum, limit);
|
|
|
List<Coupon> datas= couponViewMapper.selectByAssociatedPrd(prdId, positionType,currentDT,offset,limit);
|
|
|
//when user login, show user coupon
|
|
|
final Map<Integer, UserCoupon> couponIdUserCouponMap = new HashMap<>(datas.size());
|
|
|
Integer uid;
|
|
|
if (req.isShowReceive() && Objects.nonNull(uid=req.getUid())){
|
|
|
|
|
|
final Integer uid = req.getUid();
|
|
|
boolean showUserReceive = req.isShowReceive() && Objects.nonNull(uid);
|
|
|
Supplier<Map<Integer, UserCoupon>> couponMapSupplier = ()-> {
|
|
|
Set<Integer> couponIds = datas.stream().map(Coupon::getId).collect(Collectors.toSet());
|
|
|
List<UserCoupon> userCoupons = userCouponMapper.selectUserCouponListByCouponIds(uid, couponIds);
|
|
|
Set<Integer> couponIdOfUser = null;
|
|
|
if (CollectionUtils.isNotEmpty(userCoupons)){
|
|
|
userCoupons.forEach(uc-> couponIdUserCouponMap.put(uc.getCouponId(), uc));
|
|
|
couponIdOfUser = couponIdUserCouponMap.keySet();
|
|
|
}
|
|
|
logger.info("queryCouponListOfPrdDetail selectUserCouponListByCouponIds find couponIdOfUser {} uid {} couponIds {}",
|
|
|
couponIdOfUser, uid,couponIds);
|
|
|
}
|
|
|
List<CouponSimpleBo> couponInfoListBo = datas.stream().map(coupon -> CouponConvert.do2CouponSimpleBo(coupon, couponIdUserCouponMap))
|
|
|
.collect(Collectors.toList());
|
|
|
return buildCouponIdUserCouponMap(uid, couponIds);
|
|
|
};
|
|
|
final Map<Integer, UserCoupon> couponIdUserCouponMap = showUserReceive ? couponMapSupplier.get()
|
|
|
: Collections.EMPTY_MAP;
|
|
|
|
|
|
return couponInfoListBuilder.total(total)
|
|
|
List<CouponSimpleBo> couponInfoListBo = datas.stream().map(coupon -> convert(coupon, couponIdUserCouponMap))
|
|
|
.collect(Collectors.toList());
|
|
|
couponListBo = couponInfoListBuilder.total(total)
|
|
|
.totalPage(totalPage).coupons(couponInfoListBo)
|
|
|
.build();
|
|
|
//set into cache
|
|
|
collectiveCouponCacheService.setCouponListOfPrdDetail(req.getBusinessClient(), prdId, pageNum, limit, couponListBo);
|
|
|
return couponListBo;
|
|
|
}
|
|
|
|
|
|
Map<Integer, UserCoupon> buildCouponIdUserCouponMap(Integer uid, Set<Integer> couponIds){
|
|
|
final Map<Integer, UserCoupon> couponIdUserCouponMap = new HashMap<>(couponIds.size());
|
|
|
List<UserCoupon> userCoupons = userCouponMapper.selectUserCouponListByCouponIds(uid, couponIds);
|
|
|
Set<Integer> couponIdOfUser = null;
|
|
|
if (CollectionUtils.isNotEmpty(userCoupons)){
|
|
|
userCoupons.forEach(uc-> couponIdUserCouponMap.put(uc.getCouponId(), uc));
|
|
|
couponIdOfUser = couponIdUserCouponMap.keySet();
|
|
|
}
|
|
|
logger.info("buildCouponIdUserCouponMap selectUserCouponListByCouponIds find couponIdOfUser {} uid {} couponIds {}",
|
|
|
couponIdOfUser, uid,couponIds);
|
|
|
return couponIdUserCouponMap;
|
|
|
}
|
|
|
|
|
|
void resetDynamic(CouponListReq req, List<CouponSimpleBo> couponInfoListBo){
|
|
|
final Integer uid = req.getUid();
|
|
|
boolean showUserReceive = req.isShowReceive() && Objects.nonNull(uid);
|
|
|
if(!showUserReceive){
|
|
|
return;
|
|
|
}
|
|
|
Set<Integer> couponIds = couponInfoListBo.stream().map(CouponSimpleBo::getCouponId).collect(Collectors.toSet());
|
|
|
Map<Integer, UserCoupon> couponIdUserCouponMap = buildCouponIdUserCouponMap(uid, couponIds);
|
|
|
if (MapUtils.isEmpty(couponIdUserCouponMap)){
|
|
|
return;
|
|
|
}
|
|
|
couponInfoListBo.forEach(bo->{
|
|
|
if (Objects.nonNull(couponIdUserCouponMap.get(bo.getCouponId()))){
|
|
|
bo.setReceive("Y");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
CouponSimpleBo convert(Coupon coupon,Map<Integer, UserCoupon> couponIdUserCouponMap){
|
|
|
UserCoupon userCoupon = couponIdUserCouponMap.get(coupon.getId());
|
|
|
return CouponConvert.do2CouponSimpleBo(coupon, userCoupon);
|
|
|
}
|
|
|
|
|
|
private boolean check(CouponListReq req){
|
...
|
...
|
@@ -101,7 +144,7 @@ public class CollectiveCouponService { |
|
|
return result;
|
|
|
}
|
|
|
/**
|
|
|
* TODO use cache
|
|
|
* use cache
|
|
|
* @param req
|
|
|
* @return
|
|
|
*/
|
...
|
...
|
@@ -110,7 +153,7 @@ public class CollectiveCouponService { |
|
|
if(!check(req)){
|
|
|
return null;
|
|
|
}
|
|
|
List<CouponSimpleBo> list = collectiveCouponCacheService.queryCouponTopListOfPrdDetail(req.getProductId());
|
|
|
List<CouponSimpleBo> list = collectiveCouponCacheService.queryCouponTopListOfPrdDetail(req.getBusinessClient(),req.getProductId());
|
|
|
if(Objects.nonNull(list)){
|
|
|
return list;
|
|
|
}
|
...
|
...
|
@@ -123,7 +166,7 @@ public class CollectiveCouponService { |
|
|
list = pcList.stream().limit(req.getLimit()).map(coupon -> CouponConvert.do2CouponSimpleBo(coupon))
|
|
|
.collect(Collectors.toList());
|
|
|
//
|
|
|
collectiveCouponCacheService.setCouponTopListOfPrdDetail(req.getProductId(), list);
|
|
|
collectiveCouponCacheService.setCouponTopListOfPrdDetail(req.getBusinessClient(), req.getProductId(), list);
|
|
|
return list;
|
|
|
}
|
|
|
|
...
|
...
|
|