每日使用限量

This commit is contained in:
wangw 2025-10-11 11:37:10 +08:00
parent f01ccf65ce
commit b1e5427561
2 changed files with 106 additions and 75 deletions

View File

@ -43,8 +43,6 @@ public class ACouponController {
private MkShopCouponRecordService couponRecordService;
@DubboReference
private ShopSyncService shopSyncService;
@Resource
private CouponTask couponTask;
/**
* 分页
@ -204,11 +202,4 @@ public class ACouponController {
public CzgResult<List<UserCouponVo>> findCoupon(@RequestParam Long shopUserId, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer isFood) {
return CzgResult.success(shopCouponService.findCoupon(StpKit.USER.getShopId(), shopUserId, type, isFood));
}
@GetMapping("/testTask")
public CzgResult<Void> getCouponGiftPage() {
couponTask.deliver();
return CzgResult.success();
}
}

View File

@ -46,6 +46,7 @@ import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@ -293,65 +294,18 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
//券id 券使用描述
Map<Long, JSONObject> coupons = new HashMap<>();
for (UserCouponVo tbUserCouponVo : tbUserCouponVos) {
int maxShow = 5;
if ("only".equals(tbUserCouponVo.getUseShopType())) {
ShopInfo shopInfo = shopInfoService.getById(tbUserCouponVo.getShopId());
tbUserCouponVo.setUseShops("仅本店:" + shopInfo.getShopName());
} else if ("all".equals(tbUserCouponVo.getUseShopType())) {
tbUserCouponVo.setUseShops("所有门店");
} else if ("custom".equals(tbUserCouponVo.getUseShopType())) {
if (StrUtil.isNotBlank(tbUserCouponVo.getUseShops())) {
QueryWrapper queryWrapper = QueryWrapper.create().select(ShopInfo::getShopName)
.in(ShopInfo::getId, Arrays.stream(tbUserCouponVo.getUseShops().split(",")).map(Long::parseLong).toList())
.eq(ShopInfo::getStatus, 1);
List<String> shopNames = shopInfoService.listAs(queryWrapper, String.class);
if (CollUtil.isNotEmpty(shopNames)) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < shopNames.size(); i++) {
if (i > 0) {
result.append(",");
}
result.append(shopNames.get(i));
if (i == maxShow - 1 && i != shopNames.size() - 1) {
result.append("...");
break;
}
}
tbUserCouponVo.setUseShops(result.toString());
}
}
}
//填充优惠券使用描述 每人每日使用限量校验
if (!coupons.containsKey(tbUserCouponVo.getCouponId())) {
setCouponInfo(coupons, tbUserCouponVo, null, week, formatter);
setCouponInfo(coupons, tbUserCouponVo, week, formatter, shopUserId);
}
//填充使用门店
setUseShop(tbUserCouponVo);
//填充使用/门槛商品
setFoods(tbUserCouponVo);
JSONObject couponJson = coupons.get(tbUserCouponVo.getCouponId());
tbUserCouponVo.setUseRestrictions(couponJson.getString("useRestrictions"));
tbUserCouponVo.setUse(couponJson.getBoolean("isUse"));
if (tbUserCouponVo.getType() == 1 || tbUserCouponVo.getType() == 2 || tbUserCouponVo.getType() == 3
|| tbUserCouponVo.getType() == 4 || tbUserCouponVo.getType() == 6) {
if (StrUtil.isNotBlank(tbUserCouponVo.getFoods()) && !",".equals(tbUserCouponVo.getFoods())) {
List<Long> couponFoodIds = Arrays.stream(tbUserCouponVo.getFoods().split(",")).map(Long::parseLong).toList();
if (CollUtil.isNotEmpty(couponFoodIds)) {
List<Product> list = productService.list(new QueryWrapper()
.in(Product::getId, couponFoodIds).eq(Product::getIsDel, 0));
// log.info("优惠券商品列表:{}", list);
if (CollUtil.isEmpty(list)) {
continue;
}
if (tbUserCouponVo.getType() == 1 || tbUserCouponVo.getType() == 3) {
tbUserCouponVo.setThresholdFoods(JSON.parseObject(JSON.toJSONString(list), new TypeReference<>() {
}));
} else {
tbUserCouponVo.setUseFoods(JSON.parseObject(JSON.toJSONString(list), new TypeReference<>() {
}));
}
String foods = list.stream().map(Product::getName).collect(Collectors.joining(","));
tbUserCouponVo.setFoods(foods);
}
} else {
tbUserCouponVo.setFoods("全部商品");
}
}
tbUserCouponVo.setUseRestrictions(couponJson.getString("useRestrictions"));
tbUserCouponVo.setNoUseRestrictions(couponJson.getString("noUseRestrictions"));
}
tbUserCouponVos.sort(Comparator.comparing(UserCouponVo::isUse).reversed());
return tbUserCouponVos;
@ -396,6 +350,7 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
return recordService.updateChain()
.set(MkShopCouponRecord::getStatus, 1)
.set(MkShopCouponRecord::getTargetId, orderId)
.set(MkShopCouponRecord::getUseTime, LocalDateTime.now())
.eq(MkShopCouponRecord::getShopUserId, shopUserId)
.in(MkShopCouponRecord::getId, ids).update();
}
@ -448,16 +403,87 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
return resultList;
}
private void setCouponInfo(Map<Long, JSONObject> coupons, UserCouponVo tbUserCouponVo, BigDecimal amount, String week, DateTimeFormatter formatter) {
JSONObject json = new JSONObject();
/**
* 设置优惠券使用门店
*/
private void setUseShop(UserCouponVo tbUserCouponVo) {
int maxShow = 5;
if ("only".equals(tbUserCouponVo.getUseShopType())) {
ShopInfo shopInfo = shopInfoService.getById(tbUserCouponVo.getShopId());
tbUserCouponVo.setUseShops("仅本店:" + shopInfo.getShopName());
} else if ("all".equals(tbUserCouponVo.getUseShopType())) {
tbUserCouponVo.setUseShops("所有门店");
} else if ("custom".equals(tbUserCouponVo.getUseShopType())) {
if (StrUtil.isNotBlank(tbUserCouponVo.getUseShops())) {
QueryWrapper queryWrapper = QueryWrapper.create().select(ShopInfo::getShopName)
.in(ShopInfo::getId, Arrays.stream(tbUserCouponVo.getUseShops().split(",")).map(Long::parseLong).toList())
.eq(ShopInfo::getStatus, 1);
List<String> shopNames = shopInfoService.listAs(queryWrapper, String.class);
if (CollUtil.isNotEmpty(shopNames)) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < shopNames.size(); i++) {
if (i > 0) {
result.append(",");
}
result.append(shopNames.get(i));
if (i == maxShow - 1 && i != shopNames.size() - 1) {
result.append("...");
break;
}
}
tbUserCouponVo.setUseShops(result.toString());
}
}
}
}
/**
* 填充使用/门槛商品
*/
private void setFoods(UserCouponVo tbUserCouponVo) {
if (tbUserCouponVo.getType() == 1 || tbUserCouponVo.getType() == 2 || tbUserCouponVo.getType() == 3
|| tbUserCouponVo.getType() == 4 || tbUserCouponVo.getType() == 6) {
if (StrUtil.isNotBlank(tbUserCouponVo.getFoods()) && !",".equals(tbUserCouponVo.getFoods())) {
List<Long> couponFoodIds = Arrays.stream(tbUserCouponVo.getFoods().split(",")).map(Long::parseLong).toList();
if (CollUtil.isNotEmpty(couponFoodIds)) {
List<Product> list = productService.list(new QueryWrapper()
.in(Product::getId, couponFoodIds).eq(Product::getIsDel, 0));
// log.info("优惠券商品列表:{}", list);
if (CollUtil.isEmpty(list)) {
tbUserCouponVo.setFoods("");
} else {
if (tbUserCouponVo.getType() == 1 || tbUserCouponVo.getType() == 3) {
tbUserCouponVo.setThresholdFoods(JSON.parseObject(JSON.toJSONString(list), new TypeReference<>() {
}));
} else {
tbUserCouponVo.setUseFoods(JSON.parseObject(JSON.toJSONString(list), new TypeReference<>() {
}));
}
String foods = list.stream().map(Product::getName).collect(Collectors.joining(","));
tbUserCouponVo.setFoods(foods);
}
}
} else {
tbUserCouponVo.setFoods("全部商品");
}
}
}
/**
* 过滤优惠券可用时间段 每人每日使用限量校验
* 填充优惠券Json信息
* {
* "isUse": true,
* "useRestrictions": "1000到1200可用",
* "noUseRestrictions": "不在可用时间范围内"
* }
*/
private void setCouponInfo(Map<Long, JSONObject> coupons, UserCouponVo tbUserCouponVo, String week, DateTimeFormatter formatter, Long shopUserId) {
boolean isUse = true;
ShopCoupon tbShopCoupon = getById(tbUserCouponVo.getCouponId());
StringBuilder useRestrictions = new StringBuilder("每天 ");
if (amount != null && tbShopCoupon.getCouponType().equals(1)) {
if (amount.compareTo(tbShopCoupon.getFullAmount()) < 0) {
isUse = false;
}
}
String noUseRestrictions = "";
if (StrUtil.isNotBlank(tbShopCoupon.getUseDays())) {
String[] split = tbShopCoupon.getUseDays().split(",");
if (split.length != 7) {
@ -465,7 +491,7 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
useRestrictions = new StringBuilder(tbShopCoupon.getUseDays() + " ");
}
if (!tbShopCoupon.getUseDays().contains(week)) {
tbUserCouponVo.setNoUseRestrictions("不在可用时间范围内");
noUseRestrictions = "不在可用时间范围内";
isUse = false;
}
}
@ -475,15 +501,15 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
LocalTime endTime = tbShopCoupon.getUseEndTime().toLocalTime();
if (currentTime.isBefore(startTime)) {
if (startTime.isBefore(endTime)) {
tbUserCouponVo.setNoUseRestrictions("不在可用时间范围内");
noUseRestrictions = "不在可用时间范围内";
isUse = false;
} else if (currentTime.isAfter(endTime)) {
tbUserCouponVo.setNoUseRestrictions("不在可用时间范围内");
noUseRestrictions = "不在可用时间范围内";
isUse = false;
}
} else if (startTime.isBefore(endTime)) {
if (currentTime.isAfter(endTime)) {
tbUserCouponVo.setNoUseRestrictions("不在可用时间范围内");
noUseRestrictions = "不在可用时间范围内";
isUse = false;
}
}
@ -493,8 +519,22 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
useRestrictions.append("全时段");
}
useRestrictions.append(" 可用");
if (tbUserCouponVo.getUseLimit() != -10086) {
long useCount = recordService.count(new QueryWrapper()
.eq(MkShopCouponRecord::getCouponId, tbUserCouponVo.getCouponId())
.eq(MkShopCouponRecord::getShopUserId, shopUserId)
.ge(MkShopCouponRecord::getUseTime, LocalDate.now().atTime(LocalTime.MIN))
.le(MkShopCouponRecord::getUseTime, LocalDate.now().atTime(LocalTime.MAX))
.eq(MkShopCouponRecord::getStatus, 1));
if (useCount >= tbUserCouponVo.getUseLimit()) {
isUse = false;
noUseRestrictions = "已达使用限量" + tbUserCouponVo.getUseLimit() + "";
}
}
JSONObject json = new JSONObject();
json.put("isUse", isUse);
json.put("useRestrictions", useRestrictions);
json.put("noUseRestrictions", noUseRestrictions);
coupons.put(tbUserCouponVo.getCouponId(), json);
}