优惠券 券使用记录

This commit is contained in:
2025-02-22 11:47:54 +08:00
parent c29f508b4c
commit 97ae7983f1
12 changed files with 229 additions and 159 deletions

View File

@@ -1,18 +1,17 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.dto.ShopActivateCouponDTO;
import com.alibaba.fastjson2.TypeReference;
import com.czg.account.dto.ShopActivateDTO;
import com.czg.account.dto.shopuser.ShopUserMoneyEditDTO;
import com.czg.account.entity.ShopActivate;
import com.czg.account.entity.ShopActivateCouponRecord;
import com.czg.account.entity.ShopCoupon;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopActivateCouponRecordService;
import com.czg.account.service.ShopActivateService;
import com.czg.account.service.ShopCouponService;
@@ -57,7 +56,8 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
.listAs(ShopActivateDTO.class);
for (ShopActivateDTO activateDTO : activateDtoS) {
if (StrUtil.isNotBlank(activateDTO.getCoupons())) {
activateDTO.setCouponList(JSON.parseArray(activateDTO.getCoupons(), ShopActivateCouponDTO.class));
//组装优惠券
activateDTO.setCouponList(getCoupons(activateDTO.getCoupons()));
}
}
return activateDtoS;
@@ -67,9 +67,6 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
public Boolean add(ShopActivateDTO activateDTO) {
ShopActivate shopActivate = new ShopActivate();
BeanUtil.copyProperties(activateDTO, shopActivate);
if (CollUtil.isNotEmpty(activateDTO.getCouponList())) {
shopActivate.setCoupons(JSONObject.toJSONString(activateDTO.getCouponList()));
}
return save(shopActivate);
}
@@ -77,15 +74,12 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
public Boolean edit(ShopActivateDTO activateDTO) {
ShopActivate shopActivate = new ShopActivate();
BeanUtil.copyProperties(activateDTO, shopActivate);
if (CollUtil.isNotEmpty(activateDTO.getCouponList())) {
shopActivate.setCoupons(JSONObject.toJSONString(activateDTO.getCouponList()));
}
return updateById(shopActivate);
}
@Override
public void giveActivate(Long shopId, Long vipId, BigDecimal memAmount, Long relationId) {
ShopActivate activate = queryChain().select().eq(ShopActivate::getShopId, shopId)
public void giveActivate(ShopUser shopUser, BigDecimal memAmount, Long relationId) {
ShopActivate activate = queryChain().select().eq(ShopActivate::getShopId, shopUser.getShopId())
.le(ShopActivate::getAmount, memAmount)
.orderBy(ShopActivate::getGiftAmount, false)
.one();
@@ -94,17 +88,18 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
}
//赠送优惠券
if (activate.getIsGiftCoupon() == 1) {
List<ShopActivateCouponDTO> activateCoupons = JSON.parseArray(activate.getCoupons(), ShopActivateCouponDTO.class);
Map<Long, Integer> couponUseMap = JSONObject.parseObject(activate.getCoupons(), new TypeReference<>() {
});
Map<Long, ShopCoupon> couponMap = new HashMap<>();
activateCoupons.forEach(coupon -> {
ShopCoupon shopCoupon = new ShopCoupon();
if (couponMap.containsKey(coupon.getId())) {
shopCoupon = couponMap.get(coupon.getId());
couponUseMap.forEach((couponId, giftNumber) -> {
ShopCoupon shopCoupon;
if (couponMap.containsKey(couponId)) {
shopCoupon = couponMap.get(couponId);
} else {
shopCoupon = couponService.queryChain().select().eq(ShopCoupon::getId, coupon.getId()).one();
couponMap.put(coupon.getId(), shopCoupon);
shopCoupon = couponService.queryChain().select().eq(ShopCoupon::getId, couponId).one();
couponMap.put(couponId, shopCoupon);
}
if (shopCoupon != null && shopCoupon.getStatus().equals(1) && shopCoupon.getLeftNumber() > coupon.getNum()) {
if (shopCoupon != null && shopCoupon.getStatus().equals(1) && shopCoupon.getLeftNumber() > giftNumber) {
LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
LocalDateTime end = null;
if ("fixed".equals(shopCoupon.getValidityType())) {
@@ -117,31 +112,30 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
}
List<ShopActivateCouponRecord> actGiveRecords = new ArrayList<>();
ShopActivateCouponRecord record = new ShopActivateCouponRecord();
record.setVipUserId(vipId);
record.setShopUserId(shopUser.getId());
record.setCouponId(shopCoupon.getId());
record.setShopId(shopId);
record.setShopId(shopUser.getShopId());
record.setSourceActId(activate.getId());
record.setSourceFlowId(relationId);
record.setUseStartTime(start);
record.setUseEndTime(end);
record.setSource("activate");
record.setName(shopCoupon.getTitle());
record.setCouponJson(getCouponJson(activate, shopCoupon));
if (shopCoupon.getType() == 1) {
record.setType(1);
record.setName("" + shopCoupon.getFullAmount() + "" + shopCoupon.getDiscountAmount());
record.setFullAmount(shopCoupon.getFullAmount());
record.setDiscountAmount(shopCoupon.getDiscountAmount());
} else if (shopCoupon.getType() == 2) {
record.setType(2);
record.setName("商品券");
record.setProId(shopCoupon.getProId());
}
for (int i = 0; i < coupon.getNum(); i++) {
for (int i = 0; i < giftNumber; i++) {
actGiveRecords.add(record);
}
inRecordService.saveBatch(actGiveRecords);
couponService.updateChain()
.set(ShopCoupon::getLeftNumber, shopCoupon.getLeftNumber() - coupon.getNum())
.set(ShopCoupon::getLeftNumber, shopCoupon.getLeftNumber() - giftNumber)
.eq(ShopCoupon::getId, shopCoupon.getId())
.update();
}
@@ -150,7 +144,7 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
//赠送金额
if (activate.getGiftAmount() != null && activate.getGiftAmount().compareTo(BigDecimal.ZERO) > 0) {
ShopUserMoneyEditDTO shopUserMoneyEditDTO = ShopUserMoneyEditDTO.builder()
.id(vipId)
.id(shopUser.getId())
.money(activate.getGiftAmount())
.type(1)
.remark("充值活动赠送")
@@ -158,10 +152,25 @@ public class ShopActivateServiceImpl extends ServiceImpl<ShopActivateMapper, Sho
.bizEnum(ShopUserFlowBizEnum.AWARD_IN)
.build();
//更新会员余额 并生成流水
shopUserService.updateMoney(shopId, shopUserMoneyEditDTO);
shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO);
}
}
/**
* 获取优惠券详细信息 目前仅返回 id 名称 剩余数量 赠送数量
*/
private List<ShopCoupon> getCoupons(String couponJson) {
Map<Long, Integer> couponMap = JSONObject.parseObject(couponJson, new TypeReference<>() {
});
List<ShopCoupon> list = couponService.queryChain()
.select(ShopCoupon::getId, ShopCoupon::getTitle, ShopCoupon::getLeftNumber)
.eq(ShopCoupon::getId, couponMap.keySet())
.list();
list.forEach(coupon -> {
coupon.setNumber(couponMap.get(coupon.getId()));
});
return list;
}
private String getCouponJson(ShopActivate activate, ShopCoupon tbShopCoupon) {
JSONObject result = new JSONObject();

View File

@@ -1,14 +1,14 @@
package com.czg.service.account.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.ShopCouponDTO;
import com.czg.account.entity.ShopActivateCouponRecord;
import com.czg.account.service.ShopActivateCouponRecordService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopCoupon;
import com.czg.account.service.ShopActivateCouponRecordService;
import com.czg.account.service.ShopCouponService;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.ShopCouponMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@@ -24,81 +24,60 @@ import java.util.List;
public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCoupon> implements ShopCouponService {
@Resource
private ShopActivateCouponRecordService inService;
private ShopActivateCouponRecordService couponRecordService;
@Override
public List<ShopCoupon> getList(Long shopId, Integer status) {
QueryWrapper queryWrapper = new QueryWrapper();
if (shopId != null) {
if (status == 1) {
queryWrapper.eq(ShopActivateCouponRecord::getShopId, shopId);
}else {
queryWrapper.eq(ShopActivateCouponRecord::getShopId, shopId);
}
}
return switch (status) {
case -1 -> {
queryWrapper.ge(ShopActivateCouponRecord::getUseEndTime, DateUtil.date());
yield list(queryWrapper);
}
case 0 -> {
// yield outService.list(queryWrapper);
yield null;
}
case 1 -> {
yield null;
}
default -> throw new IllegalStateException("Unexpected value: " + status);
};
}
@Override
public ShopCouponDTO getCouponById(ShopCouponDTO couponDTO) {
return null;
public List<ShopCouponDTO> getList(Long shopId, Integer type, Integer status) {
return queryChain().select().eq(ShopCoupon::getShopId, shopId)
.eq(ShopCoupon::getType, type)
.eq(ShopCoupon::getStatus, status)
.orderBy(ShopCoupon::getCreateTime).desc().listAs(ShopCouponDTO.class);
}
@Override
public Boolean add(ShopCouponDTO couponDTO) {
return null;
ShopCoupon shopCoupon = new ShopCoupon();
BeanUtil.copyProperties(couponDTO, shopCoupon);
return save(shopCoupon);
}
@Override
public Boolean edit(ShopCouponDTO couponDTO) {
return null;
ShopCoupon shopCoupon = new ShopCoupon();
BeanUtil.copyProperties(couponDTO, shopCoupon);
return updateById(shopCoupon);
}
@Override
public List<ShopActivateCouponRecord> find(Long shopUserId, Integer status) {
return couponRecordService.queryChain()
.eq(ShopActivateCouponRecord::getShopId, StpKit.USER.getShopId())
.eq(ShopActivateCouponRecord::getShopUserId, shopUserId)
.eq(ShopActivateCouponRecord::getStatus, status)
.orderBy(ShopActivateCouponRecord::getStatus).asc()
.orderBy(ShopActivateCouponRecord::getCreateTime).desc()
.select().list();
}
@Override
public Boolean delete(Long id) {
return null;
}
@Override
public Boolean find(Long id) {
return null;
}
@Override
public Boolean use(Integer shopId, Integer orderId, Integer vipUserId) {
return null;
public Boolean use(List<Long> ids, Long shopUserId, Long orderId) {
return couponRecordService.updateChain()
.set(ShopActivateCouponRecord::getStatus, 1)
.set(ShopActivateCouponRecord::getTargetId, orderId)
.eq(ShopActivateCouponRecord::getShopUserId, shopUserId)
.in(ShopActivateCouponRecord::getId, ids).update();
}
/**
* 退还券
*
*/
@Override
public Boolean refund() {
// for (ShopActivateOutRecord outRecord : param) {
// outService.updateChain()
// .set(ShopActivateOutRecord::getRefNum, outRecord.getRefNum())
// .eq(ShopActivateOutRecord::getId, outRecord.getId())
// .update();
// ShopActivateInRecord inRecord = inService.getById(outRecord.getGiveId());
// inRecord.setOverNum(inRecord.getOverNum() + outRecord.getRefNum());
// inService.updateOverNum(inRecord.getId(), inRecord.getOverNum());
// }
return true;
public Boolean refund(Long orderId, Long shopUserId) {
return couponRecordService.updateChain()
.set(ShopActivateCouponRecord::getStatus, 0)
.eq(ShopActivateCouponRecord::getShopUserId, shopUserId)
.eq(ShopActivateCouponRecord::getTargetId, orderId)
.update();
}
}