优惠券 券使用记录

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

View File

@ -1,11 +1,19 @@
package com.czg.controller.admin; package com.czg.controller.admin;
import com.czg.account.dto.ShopCouponDTO;
import com.czg.account.service.ShopCouponService; import com.czg.account.service.ShopCouponService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* 店铺优惠券
*
* @author ww * @author ww
* @description * @description
*/ */
@ -16,5 +24,37 @@ public class ShopCouponController {
@Resource @Resource
private ShopCouponService couponService; private ShopCouponService couponService;
/**
* 店铺优惠券列表
* 权限标识: coupon:list
* 状态 0 未使用 1已使用 2已过期
*/
@SaAdminCheckPermission("coupon:list")
@GetMapping
public CzgResult<List<ShopCouponDTO>> detail(@RequestParam(required = false) Integer type,
@RequestParam(required = false) Integer status) {
return CzgResult.success(couponService.getList(StpKit.USER.getShopId(), type, status));
}
/**
* 店铺优惠券新增
* 权限标识: coupon:add
*/
@SaAdminCheckPermission("coupon:add")
@PostMapping
public CzgResult<Boolean> add(@RequestBody @Validated ShopCouponDTO couponDTO) {
couponDTO.setShopId(StpKit.USER.getShopId());
return CzgResult.success(couponService.add(couponDTO));
}
/**
* 店铺优惠券修改
* 权限标识: coupon:edit
*/
@SaAdminCheckPermission("coupon:edit")
@PutMapping
public CzgResult<Boolean> edit(@RequestBody @Validated ShopCouponDTO couponDTO) {
couponDTO.setShopId(StpKit.USER.getShopId());
return CzgResult.success(couponService.edit(couponDTO));
}
} }

View File

@ -0,0 +1,34 @@
package com.czg.controller.user;
import com.czg.account.entity.ShopActivateCouponRecord;
import com.czg.account.service.ShopCouponService;
import com.czg.resp.CzgResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 店铺优惠券
*
* @author ww
* @description
*/
@RestController
@RequestMapping("/user/coupon")
public class UserShopCouponController {
@Resource
private ShopCouponService couponService;
/**
* 通过用户Id 查找优惠券
*
* @param status 0 未使用 1已使用 2已过期
*/
@GetMapping("/findByUserId")
public CzgResult<List<ShopActivateCouponRecord>> findByUserId(
@RequestParam Long shopUserId, @RequestParam(required = false) Integer status) {
return CzgResult.success(couponService.find(shopUserId, status));
}
}

View File

@ -37,6 +37,9 @@ public class UserOrderController {
return CzgResult.success(orderInfoService.getOrderByPage(queryDTO)); return CzgResult.success(orderInfoService.getOrderByPage(queryDTO));
} }
/**
* 生成订单
*/
@PostMapping("/createOrder") @PostMapping("/createOrder")
public CzgResult<OrderInfo> createOrder(@RequestBody OrderInfoAddDTO addDto) { public CzgResult<OrderInfo> createOrder(@RequestBody OrderInfoAddDTO addDto) {
addDto.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType")); addDto.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType"));

View File

@ -1,14 +0,0 @@
package com.czg.account.dto;
import lombok.Data;
/**
* @author ww
* @description
*/
@Data
public class ShopActivateCouponDTO {
private Long id;
private Integer num;
private String name;
}

View File

@ -2,6 +2,7 @@
package com.czg.account.dto; package com.czg.account.dto;
import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.annotation.JSONField;
import com.czg.account.entity.ShopCoupon;
import com.czg.validator.group.InsertGroup; import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup; import com.czg.validator.group.UpdateGroup;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -59,11 +60,7 @@ public class ShopActivateDTO implements Serializable {
*/ */
private Integer isGiftCoupon; private Integer isGiftCoupon;
private List<ShopActivateCouponDTO> couponList; private List<ShopCoupon> couponList;
/**
* 优惠卷信息{卷id数量}
*/
private String coupons; private String coupons;
@JSONField(format = "yyyy-MM-dd HH:mm:ss") @JSONField(format = "yyyy-MM-dd HH:mm:ss")

View File

@ -36,9 +36,9 @@ public class ShopActivateCouponRecord implements Serializable {
private Long id; private Long id;
/** /**
* 会员id * 用户id shopUserId
*/ */
private Long vipUserId; private Long shopUserId;
/** /**
* 卷Id (校验是否可用) * 卷Id (校验是否可用)
@ -100,13 +100,18 @@ public class ShopActivateCouponRecord implements Serializable {
private String couponJson; private String couponJson;
/**
* invited 邀请
* activate 活动
*/
private String source; private String source;
/** /**
* 已使用 used * 未使用 0
* 未使用 not_used * 已使用 1
* 已过期 expired * 已过期 2
*/ */
private String status; private Integer status;
private Long targetId;
} }

View File

@ -2,6 +2,7 @@ package com.czg.account.service;
import com.czg.account.dto.ShopActivateDTO; import com.czg.account.dto.ShopActivateDTO;
import com.czg.account.entity.ShopActivate; import com.czg.account.entity.ShopActivate;
import com.czg.account.entity.ShopUser;
import com.mybatisflex.core.service.IService; import com.mybatisflex.core.service.IService;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -28,6 +29,6 @@ public interface ShopActivateService extends IService<ShopActivate> {
* 充值奖励 的关联id 是tb_shop_user_flow的充值 记录id * 充值奖励 的关联id 是tb_shop_user_flow的充值 记录id
* 支付/退款 tb_order_payment.id * 支付/退款 tb_order_payment.id
*/ */
void giveActivate(Long shopId, Long vipId, BigDecimal memAmount, Long relationId); void giveActivate(ShopUser shopUser, BigDecimal memAmount, Long relationId);
} }

View File

@ -1,6 +1,7 @@
package com.czg.account.service; package com.czg.account.service;
import com.czg.account.dto.ShopCouponDTO; import com.czg.account.dto.ShopCouponDTO;
import com.czg.account.entity.ShopActivateCouponRecord;
import com.czg.account.entity.ShopCoupon; import com.czg.account.entity.ShopCoupon;
import com.mybatisflex.core.service.IService; import com.mybatisflex.core.service.IService;
@ -18,21 +19,21 @@ public interface ShopCouponService extends IService<ShopCoupon> {
* 优惠券列表 * 优惠券列表
* *
* @param shopId 店铺id * @param shopId 店铺id
* @param type 1-满减 2-商品
* @param status 状态 0 未使用 1已使用 -1已过期 * @param status 状态 0 未使用 1已使用 -1已过期
*/ */
List<ShopCoupon> getList(Long shopId, Integer status); List<ShopCouponDTO> getList(Long shopId, Integer type, Integer status);
ShopCouponDTO getCouponById(ShopCouponDTO couponDTO);
Boolean add(ShopCouponDTO couponDTO); Boolean add(ShopCouponDTO couponDTO);
Boolean edit(ShopCouponDTO couponDTO); Boolean edit(ShopCouponDTO couponDTO);
Boolean delete(Long id);
Boolean find(Long id);
Boolean use(Integer shopId, Integer orderId, Integer vipUserId);
Boolean refund(); List<ShopActivateCouponRecord> find(Long shopUserId, Integer status);
Boolean use(List<Long> ids, Long shopUserId, Long orderId);
Boolean refund(Long orderId, Long shopUserId);
} }

View File

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

View File

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

View File

@ -81,8 +81,9 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
@DubboReference @DubboReference
private PointsBasicSettingService pointsBasicService; private PointsBasicSettingService pointsBasicService;
@DubboReference @DubboReference
private ShopCouponService couponService;
@DubboReference
private ShopActivateCouponRecordService couponRecordService; private ShopActivateCouponRecordService couponRecordService;
@Resource @Resource
private RedisService redisService; private RedisService redisService;
@Resource @Resource
@ -152,38 +153,14 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) { if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
throw new ValidateException("生成订单失败,折扣比例不正确"); throw new ValidateException("生成订单失败,折扣比例不正确");
} }
//商品券 //商品券 <商品id数量>
Map<Long, Integer> prodCouponMap = new HashMap<>(); Map<Long, Integer> prodCouponMap = new HashMap<>();
//满减券 满fullCouponAmount 减disCouponAmount //满减券 满fullCouponAmount 减disCouponAmount
BigDecimal fullAmount = BigDecimal.ZERO; BigDecimal fullAmount = BigDecimal.ZERO;
BigDecimal discountAmount = BigDecimal.ZERO; BigDecimal discountAmount = BigDecimal.ZERO;
if (CollUtil.isNotEmpty(param.getCouponList())) { //校验优惠券
//校验优惠券 checkCoupon(prodCouponMap, fullAmount, discountAmount, param);
List<ShopActivateCouponRecord> records = couponRecordService.list(QueryWrapper.create()
.where(ShopActivateCouponRecord::getId).in(param.getCouponList())
.and(ShopActivateCouponRecord::getStatus).eq("not_used"));
if (CollUtil.isEmpty(records)) {
throw new ValidateException("生成订单失败,优惠券信息不存在");
} else if (records.size() != param.getCouponList().size()) {
throw new ValidateException("生成订单失败,优惠券信息不正确");
}
boolean isFullMinus = false;
for (ShopActivateCouponRecord record : records) {
if (record.getType().equals(1)) {
if (isFullMinus) {
throw new ValidateException("生成订单失败,满减券仅可使用一张");
}
fullAmount = record.getFullAmount();
discountAmount = record.getDiscountAmount();
isFullMinus = true;
} else if (record.getType().equals(2)) {
prodCouponMap.compute(record.getProId(), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
}
}
}
if (discountAmount.compareTo(param.getFullCouponDiscountAmount()) != 0) {
throw new ValidateException("生成订单失败,满减券减免金额不正确");
}
//获取商品信息 计算金额 需要传入优惠券 减去优惠券 //获取商品信息 计算金额 需要传入优惠券 减去优惠券
List<OrderDetail> orderDetails = cartService.getCartByTableCode(table.getTableCode(), param.isVipPrice() ? 1 : 0, param.getPlaceNum()); List<OrderDetail> orderDetails = cartService.getCartByTableCode(table.getTableCode(), param.isVipPrice() ? 1 : 0, param.getPlaceNum());
//总打包费 //总打包费
@ -237,6 +214,8 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
//生成订单 //discount_info 所有折扣 几折 折扣金额 满减金额 优惠券金额 积分抵扣金额 抹零 //生成订单 //discount_info 所有折扣 几折 折扣金额 满减金额 优惠券金额 积分抵扣金额 抹零
OrderInfo orderInfo = initOrderInfo(param, shopInfo, table); OrderInfo orderInfo = initOrderInfo(param, shopInfo, table);
orderDetailService.createOrderDetails(orderInfo.getId(), orderDetails); orderDetailService.createOrderDetails(orderInfo.getId(), orderDetails);
//券消耗
couponService.use(param.getCouponList(), shopUser.getId(), orderInfo.getId());
return orderInfo; return orderInfo;
} }
@ -296,6 +275,39 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
orderDetails = resultList; orderDetails = resultList;
} }
/**
* 校验优惠券可用性 回填优惠券信息
*/
private void checkCoupon(Map<Long, Integer> prodCouponMap, BigDecimal fullAmount, BigDecimal discountAmount, OrderInfoAddDTO param) {
if (CollUtil.isNotEmpty(param.getCouponList())) {
//校验优惠券
List<ShopActivateCouponRecord> records = couponRecordService.list(QueryWrapper.create()
.where(ShopActivateCouponRecord::getId).in(param.getCouponList())
.and(ShopActivateCouponRecord::getStatus).eq(0));
if (CollUtil.isEmpty(records)) {
throw new ValidateException("生成订单失败,优惠券信息不存在");
} else if (records.size() != param.getCouponList().size()) {
throw new ValidateException("生成订单失败,优惠券信息不正确");
}
boolean isFullMinus = false;
for (ShopActivateCouponRecord record : records) {
if (record.getType().equals(1)) {
if (isFullMinus) {
throw new ValidateException("生成订单失败,满减券仅可使用一张");
}
fullAmount = record.getFullAmount();
discountAmount = record.getDiscountAmount();
isFullMinus = true;
} else if (record.getType().equals(2)) {
prodCouponMap.compute(record.getProId(), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
}
}
}
if (discountAmount.compareTo(param.getFullCouponDiscountAmount()) != 0) {
throw new ValidateException("生成订单失败,满减券减免金额不正确");
}
}
@Override @Override
@Transactional @Transactional
public void payCallBackOrder(@NotBlank String orderNo, @NotNull JSONObject resultJson) { public void payCallBackOrder(@NotBlank String orderNo, @NotNull JSONObject resultJson) {
@ -338,7 +350,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
//更新会员余额 并生成流水 //更新会员余额 并生成流水
Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO); Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO);
//会员活动 //会员活动
activateService.giveActivate(shopUser.getShopId(), shopUser.getId(), activateService.giveActivate(shopUser,
new BigDecimal(czgCallBackDto.getAmount() / 100L), flowId); new BigDecimal(czgCallBackDto.getAmount() / 100L), flowId);
} }

View File

@ -28,6 +28,7 @@ import com.czg.system.enums.SysParamCodeEnum;
import com.czg.system.service.SysParamsService; import com.czg.system.service.SysParamsService;
import com.czg.utils.AssertUtil; import com.czg.utils.AssertUtil;
import com.czg.utils.MD5Util; import com.czg.utils.MD5Util;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import lombok.NonNull; import lombok.NonNull;
@ -64,6 +65,8 @@ public class PayServiceImpl implements PayService {
private ShopUserFlowService userFlowService; private ShopUserFlowService userFlowService;
@DubboReference @DubboReference
private ShopActivateCouponRecordService inRecordService; private ShopActivateCouponRecordService inRecordService;
@DubboReference
private ShopCouponService couponService;
@Resource @Resource
private RabbitPublisher rabbitPublisher; private RabbitPublisher rabbitPublisher;
@Resource @Resource
@ -243,7 +246,7 @@ public class PayServiceImpl implements PayService {
//更新会员余额 并生成流水 //更新会员余额 并生成流水
Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO); Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO);
//会员活动 //会员活动
shopActivateService.giveActivate(shopUser.getShopId(), shopUser.getId(), payParam.getAmount(), flowId); shopActivateService.giveActivate(shopUser, payParam.getAmount(), flowId);
return CzgResult.success(); return CzgResult.success();
} }
@ -342,7 +345,7 @@ public class PayServiceImpl implements PayService {
} }
Long refPaymentId = null; Long refPaymentId = null;
if (!refPayParam.isCashRefund()) { if (!refPayParam.isCashRefund()) {
OrderPayment payment = null; OrderPayment payment;
if (inFlow.getRelationId() != null) { if (inFlow.getRelationId() != null) {
payment = paymentService.getById(inFlow.getRelationId()); payment = paymentService.getById(inFlow.getRelationId());
} else { } else {
@ -379,11 +382,11 @@ public class PayServiceImpl implements PayService {
.eq(ShopUserFlow::getId, giftFlow.getId()) .eq(ShopUserFlow::getId, giftFlow.getId())
.update(); .update();
} }
inRecordService.updateChain() //移除优惠券
.set(ShopActivateCouponRecord::getStatus, "not_used") inRecordService.remove(QueryWrapper.create()
.eq(ShopActivateCouponRecord::getSourceFlowId, inFlow.getId()) .eq(ShopActivateCouponRecord::getSourceFlowId, inFlow.getId())
.update(); .eq(ShopActivateCouponRecord::getSource, "activate")
.eq(ShopActivateCouponRecord::getStatus, 0));
return CzgResult.success(); return CzgResult.success();
} }