|
@@ -17,6 +17,7 @@ import com.github.zuihou.database.mybatis.auth.DataScope;
|
|
|
import com.github.zuihou.database.mybatis.conditions.Wraps;
|
|
|
import com.github.zuihou.database.mybatis.conditions.query.QueryWrap;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -213,7 +214,7 @@ public class CouponServiceImpl extends SuperServiceImpl<CouponMapper, Coupon> im
|
|
|
}
|
|
|
|
|
|
private List<MemberCoupon> selectCouponHisMemberCouponList(Long couponId) {
|
|
|
- // 查询当前用户的历史的优惠券领取记录
|
|
|
+ // 查询当前优惠券,所有用户的历史领取记录
|
|
|
MemberCoupon memberCouponSearchParam = new MemberCoupon();
|
|
|
memberCouponSearchParam.setCouponId(couponId);
|
|
|
QueryWrap queryWrap = Wraps.<MemberCoupon>q(memberCouponSearchParam);
|
|
@@ -243,7 +244,7 @@ public class CouponServiceImpl extends SuperServiceImpl<CouponMapper, Coupon> im
|
|
|
return "优惠券不存在";
|
|
|
}
|
|
|
|
|
|
- // 查询当前优惠券的已领取记录
|
|
|
+ // 查询当前优惠券的已领取记录,包含所有用户
|
|
|
List<MemberCoupon> memberCouponList = selectCouponHisMemberCouponList(couponId);
|
|
|
// 查询当前用户历史有效订单
|
|
|
List<Order> orderList = searchMemberHisValidOrderList(memberId);
|
|
@@ -264,6 +265,10 @@ public class CouponServiceImpl extends SuperServiceImpl<CouponMapper, Coupon> im
|
|
|
// 其他场景时,通过优惠方式动态计算当前客户的优惠券状态
|
|
|
// 逻辑1:按月累计消费 -> 当前客户、当前优惠券、当月消费是否满足指定金额金额领取条件
|
|
|
// 逻辑2:历史消费累计 ->当前客户、当前优惠券,在上一次消费后是否再次满足优惠券指定金额领取条件
|
|
|
+
|
|
|
+ // 从优惠券的领取记录中,过滤出当前用户领取的优惠券记录
|
|
|
+ memberCouponList = memberCouponList.stream().filter(memberCoupon -> memberCoupon.getMemberId().equals(memberId))
|
|
|
+ .collect(Collectors.toList());
|
|
|
if ("0".equals(coupon.getPreferentialMethod())) {
|
|
|
// 按月累计消费处理
|
|
|
boolean isExist = isExistInCurrentMonty(coupon, memberCouponList, monthBeginDate, monthEndDate);
|
|
@@ -279,25 +284,29 @@ public class CouponServiceImpl extends SuperServiceImpl<CouponMapper, Coupon> im
|
|
|
return "本月消费未满足优惠券限定金额";
|
|
|
}
|
|
|
} else {
|
|
|
+ // 历史消费累计处理
|
|
|
// 获取当前优惠券的,最后一个领取记录
|
|
|
- MemberCoupon memberCoupon = memberCouponList.get(0);
|
|
|
- if (memberCoupon != null) {
|
|
|
- Date lastMemberCouponCreateTime = memberCoupon.getCreateTime();
|
|
|
- // 统计上一次的领取时间到当前系统时间之间的订单消费总额
|
|
|
- BigDecimal totalAmount = orderList.stream().filter(order -> {
|
|
|
- Date createTime = Date.from(order.getCreateTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
- if (memberCoupon == null) {
|
|
|
- // 无当前优惠券的领取记录,返回当前时间之前的订单
|
|
|
- return currentDateTime.compareTo(createTime) < 1;
|
|
|
- } else {
|
|
|
- // 有当前优惠券的领取记录,返回上一次领取时间之后,并且当前时间之前的订单
|
|
|
- return lastMemberCouponCreateTime.compareTo(createTime) < 1
|
|
|
- && currentDateTime.compareTo(createTime) < 1;
|
|
|
- }
|
|
|
- }).map(Order::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ if (CollectionUtils.isNotEmpty(memberCouponList)) {
|
|
|
+ // 存在优惠券的领取记录时,验证优惠券
|
|
|
+ MemberCoupon memberCoupon = memberCouponList.get(0);
|
|
|
+ if (memberCoupon != null) {
|
|
|
+ Date lastMemberCouponCreateTime = memberCoupon.getCreateTime();
|
|
|
+ // 统计上一次的领取时间到当前系统时间之间的订单消费总额
|
|
|
+ BigDecimal totalAmount = orderList.stream().filter(order -> {
|
|
|
+ Date createTime = Date.from(order.getCreateTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ if (memberCoupon == null) {
|
|
|
+ // 无当前优惠券的领取记录,返回当前时间之前的订单
|
|
|
+ return currentDateTime.compareTo(createTime) < 1;
|
|
|
+ } else {
|
|
|
+ // 有当前优惠券的领取记录,返回上一次领取时间之后,并且当前时间之前的订单
|
|
|
+ return lastMemberCouponCreateTime.compareTo(createTime) < 1
|
|
|
+ && currentDateTime.compareTo(createTime) < 1;
|
|
|
+ }
|
|
|
+ }).map(Order::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
- if (totalAmount.compareTo(coupon.getAccumulatedAmount()) == -1) {
|
|
|
- return "已领取过该优惠券";
|
|
|
+ if (totalAmount.compareTo(coupon.getAccumulatedAmount()) == -1) {
|
|
|
+ return "已领取过该优惠券";
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|