Browse Source

修复代码生成器

main
科技小王子 2 weeks ago
parent
commit
a036e65247
  1. 2
      src/main/java/com/gxwebsoft/shop/controller/ShopCouponController.java
  2. 129
      src/main/java/com/gxwebsoft/shop/controller/ShopGoodsCouponController.java
  3. 220
      src/main/java/com/gxwebsoft/shop/controller/ShopUserCouponController.java
  4. 2
      src/main/java/com/gxwebsoft/shop/entity/ShopCoupon.java
  5. 55
      src/main/java/com/gxwebsoft/shop/entity/ShopGoodsCoupon.java
  6. 92
      src/main/java/com/gxwebsoft/shop/entity/ShopUserCoupon.java
  7. 2
      src/main/java/com/gxwebsoft/shop/mapper/ShopCouponMapper.java
  8. 37
      src/main/java/com/gxwebsoft/shop/mapper/ShopGoodsCouponMapper.java
  9. 49
      src/main/java/com/gxwebsoft/shop/mapper/ShopUserCouponMapper.java
  10. 232
      src/main/java/com/gxwebsoft/shop/mapper/xml/ShopUserCouponMapper.xml
  11. 2
      src/main/java/com/gxwebsoft/shop/param/ShopCouponParam.java
  12. 52
      src/main/java/com/gxwebsoft/shop/param/ShopGoodsCouponParam.java
  13. 86
      src/main/java/com/gxwebsoft/shop/param/ShopUserCouponParam.java
  14. 2
      src/main/java/com/gxwebsoft/shop/service/ShopCouponService.java
  15. 42
      src/main/java/com/gxwebsoft/shop/service/ShopGoodsCouponService.java
  16. 121
      src/main/java/com/gxwebsoft/shop/service/ShopUserCouponService.java
  17. 2
      src/main/java/com/gxwebsoft/shop/service/impl/ShopCouponServiceImpl.java
  18. 47
      src/main/java/com/gxwebsoft/shop/service/impl/ShopGoodsCouponServiceImpl.java
  19. 47
      src/main/java/com/gxwebsoft/shop/service/impl/ShopUserCouponServiceImpl.java
  20. 2
      src/test/java/com/gxwebsoft/generator/ShopGenerator.java
  21. 6
      src/test/java/com/gxwebsoft/generator/templates/index.vue.btl
  22. 156
      src/test/java/com/gxwebsoft/shop/utils/CouponUtilsTest.java

2
src/main/java/com/gxwebsoft/shop/controller/ShopCouponController.java

@ -22,7 +22,7 @@ import java.util.List;
* 优惠券控制器
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
* @since 2025-08-09 15:29:58
*/
@Tag(name = "优惠券管理")
@RestController

129
src/main/java/com/gxwebsoft/shop/controller/ShopGoodsCouponController.java

@ -1,129 +0,0 @@
package com.gxwebsoft.shop.controller;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopGoodsCouponService;
import com.gxwebsoft.shop.entity.ShopGoodsCoupon;
import com.gxwebsoft.shop.param.ShopGoodsCouponParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 商品优惠券表控制器
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
@Tag(name = "商品优惠券表管理")
@RestController
@RequestMapping("/api/shop/shop-goods-coupon")
public class ShopGoodsCouponController extends BaseController {
@Resource
private ShopGoodsCouponService shopGoodsCouponService;
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:list')")
@Operation(summary = "分页查询商品优惠券表")
@GetMapping("/page")
public ApiResult<PageResult<ShopGoodsCoupon>> page(ShopGoodsCouponParam param) {
// 使用关联查询
return success(shopGoodsCouponService.pageRel(param));
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:list')")
@Operation(summary = "查询全部商品优惠券表")
@GetMapping()
public ApiResult<List<ShopGoodsCoupon>> list(ShopGoodsCouponParam param) {
// 使用关联查询
return success(shopGoodsCouponService.listRel(param));
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:list')")
@Operation(summary = "根据id查询商品优惠券表")
@GetMapping("/{id}")
public ApiResult<ShopGoodsCoupon> get(@PathVariable("id") Integer id) {
// 使用关联查询
return success(shopGoodsCouponService.getByIdRel(id));
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:save')")
@OperationLog
@Operation(summary = "添加商品优惠券表")
@PostMapping()
public ApiResult<?> save(@RequestBody ShopGoodsCoupon shopGoodsCoupon) {
// 记录当前登录用户id
User loginUser = getLoginUser();
if (loginUser != null) {
shopGoodsCoupon.setUserId(loginUser.getUserId());
}
if (shopGoodsCouponService.save(shopGoodsCoupon)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:update')")
@OperationLog
@Operation(summary = "修改商品优惠券表")
@PutMapping()
public ApiResult<?> update(@RequestBody ShopGoodsCoupon shopGoodsCoupon) {
if (shopGoodsCouponService.updateById(shopGoodsCoupon)) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:remove')")
@OperationLog
@Operation(summary = "删除商品优惠券表")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
if (shopGoodsCouponService.removeById(id)) {
return success("删除成功");
}
return fail("删除失败");
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:save')")
@OperationLog
@Operation(summary = "批量添加商品优惠券表")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<ShopGoodsCoupon> list) {
if (shopGoodsCouponService.saveBatch(list)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:update')")
@OperationLog
@Operation(summary = "批量修改商品优惠券表")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopGoodsCoupon> batchParam) {
if (batchParam.update(shopGoodsCouponService, "id")) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('shop:shopGoodsCoupon:remove')")
@OperationLog
@Operation(summary = "批量删除商品优惠券表")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (shopGoodsCouponService.removeByIds(ids)) {
return success("删除成功");
}
return fail("删除失败");
}
}

220
src/main/java/com/gxwebsoft/shop/controller/ShopUserCouponController.java

@ -1,225 +1,129 @@
package com.gxwebsoft.shop.controller;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.ShopUserCouponService;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import com.gxwebsoft.shop.param.ShopUserCouponParam;
import com.gxwebsoft.shop.service.ShopUserCouponService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* 用户优惠券控制器
*
* @author 科技小王子
* @since 2025-08-08 21:30:00
* @since 2025-08-09 15:29:58
*/
@Tag(name = "用户优惠券管理")
@RestController
@RequestMapping("/api/shop/user-coupon")
@RequestMapping("/api/shop/shop-user-coupon")
public class ShopUserCouponController extends BaseController {
@Resource
private ShopUserCouponService shopUserCouponService;
@PreAuthorize("hasAuthority('shop:userCoupon:list')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')")
@Operation(summary = "分页查询用户优惠券")
@GetMapping("/page")
public ApiResult<PageResult<ShopUserCoupon>> page(ShopUserCouponParam param) {
// 使用关联查询
return success(shopUserCouponService.pageRel(param));
}
@PreAuthorize("hasAuthority('shop:userCoupon:list')")
@Operation(summary = "查询用户优惠券列表")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')")
@Operation(summary = "查询全部用户优惠券")
@GetMapping()
public ApiResult<List<ShopUserCoupon>> list(ShopUserCouponParam param) {
// 使用关联查询
return success(shopUserCouponService.listRel(param));
}
@PreAuthorize("hasAuthority('shop:userCoupon:list')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')")
@Operation(summary = "根据id查询用户优惠券")
@GetMapping("/{id}")
public ApiResult<ShopUserCoupon> get(@PathVariable("id") Long id) {
public ApiResult<ShopUserCoupon> get(@PathVariable("id") Integer id) {
// 使用关联查询
return success(shopUserCouponService.getByIdRel(id));
}
@Operation(summary = "获取当前用户的优惠券列表")
@GetMapping("/my")
public ApiResult<List<ShopUserCoupon>> getMyCoupons(ShopUserCouponParam param) {
User loginUser = getLoginUser();
if (loginUser == null) {
return fail("请先登录",null);
}
param.setUserId(loginUser.getUserId());
return success(shopUserCouponService.listRel(param));
}
@Operation(summary = "获取当前用户可用的优惠券")
@GetMapping("/my/available")
public ApiResult<List<ShopUserCoupon>> getMyAvailableCoupons(
@RequestParam(required = false) Integer goodsId,
@RequestParam(required = false) Integer categoryId,
@RequestParam(required = false) BigDecimal orderAmount) {
User loginUser = getLoginUser();
if (loginUser == null) {
return fail("请先登录",null);
}
List<ShopUserCoupon> coupons = shopUserCouponService.getAvailableCoupons(
loginUser.getUserId(), goodsId, categoryId, orderAmount);
return success(coupons);
}
@Operation(summary = "统计当前用户优惠券数量")
@GetMapping("/my/count")
public ApiResult<Map<String, Object>> getMyCount() {
User loginUser = getLoginUser();
if (loginUser == null) {
return fail("请先登录",null);
}
Map<String, Object> count = shopUserCouponService.countUserCoupons(loginUser.getUserId());
return success(count);
}
@PreAuthorize("hasAuthority('shop:shopUserCoupon:save')")
@OperationLog
@Operation(summary = "领取优惠券")
@PostMapping("/receive/{couponId}")
public ApiResult<?> receiveCoupon(@PathVariable("couponId") Integer couponId) {
@Operation(summary = "添加用户优惠券")
@PostMapping()
public ApiResult<?> save(@RequestBody ShopUserCoupon shopUserCoupon) {
// 记录当前登录用户id
User loginUser = getLoginUser();
if (loginUser == null) {
return fail("请先登录",null);
if (loginUser != null) {
shopUserCoupon.setUserId(loginUser.getUserId());
}
// 检查是否可以领取
Map<String, Object> checkResult = shopUserCouponService.checkCanReceiveCoupon(
loginUser.getUserId(), couponId);
if (!(Boolean) checkResult.get("canReceive")) {
return fail(checkResult.get("reason").toString());
}
boolean success = shopUserCouponService.receiveCoupon(loginUser.getUserId(), couponId);
if (success) {
return success("领取成功");
if (shopUserCouponService.save(shopUserCoupon)) {
return success("添加成功");
}
return fail("领取失败");
return fail("添加失败");
}
@PreAuthorize("hasAuthority('shop:userCoupon:save')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:update')")
@OperationLog
@Operation(summary = "系统发放优惠券给用户")
@PostMapping("/issue")
public ApiResult<?> issueCoupon(@RequestParam Integer userId,
@RequestParam Integer couponId,
@RequestParam(required = false) String source) {
boolean success = shopUserCouponService.issueCouponToUser(userId, couponId, source);
if (success) {
return success("发放成功");
@Operation(summary = "修改用户优惠券")
@PutMapping()
public ApiResult<?> update(@RequestBody ShopUserCoupon shopUserCoupon) {
if (shopUserCouponService.updateById(shopUserCoupon)) {
return success("修改成功");
}
return fail("发放失败");
return fail("修改失败");
}
@PreAuthorize("hasAuthority('shop:userCoupon:save')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:remove')")
@OperationLog
@Operation(summary = "批量发放优惠券")
@PostMapping("/batch-issue")
public ApiResult<?> batchIssueCoupons(@RequestParam List<Integer> userIds,
@RequestParam Integer couponId,
@RequestParam(required = false) String source) {
int successCount = shopUserCouponService.batchIssueCoupons(userIds, couponId, source);
return success("成功发放" + successCount + "张优惠券");
}
@PreAuthorize("hasAuthority('shop:userCoupon:update')")
@OperationLog
@Operation(summary = "使用优惠券")
@PutMapping("/use")
public ApiResult<?> useCoupon(@RequestParam Long userCouponId,
@RequestParam Long orderId,
@RequestParam String orderNo) {
boolean success = shopUserCouponService.useCoupon(userCouponId, orderId, orderNo);
if (success) {
return success("使用成功");
@Operation(summary = "删除用户优惠券")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
if (shopUserCouponService.removeById(id)) {
return success("删除成功");
}
return fail("使用失败");
return fail("删除失败");
}
@PreAuthorize("hasAuthority('shop:userCoupon:update')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:save')")
@OperationLog
@Operation(summary = "退还优惠券")
@PutMapping("/return/{orderId}")
public ApiResult<?> returnCoupon(@PathVariable("orderId") Long orderId) {
boolean success = shopUserCouponService.returnCoupon(orderId);
if (success) {
return success("退还成功");
}
return fail("退还失败");
}
@Operation(summary = "计算优惠券优惠金额")
@PostMapping("/calculate-discount")
public ApiResult<BigDecimal> calculateDiscount(@RequestParam Long userCouponId,
@RequestParam BigDecimal orderAmount) {
ShopUserCoupon userCoupon = shopUserCouponService.getById(userCouponId);
if (userCoupon == null) {
return fail("优惠券不存在",null);
@Operation(summary = "批量添加用户优惠券")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<ShopUserCoupon> list) {
if (shopUserCouponService.saveBatch(list)) {
return success("添加成功");
}
BigDecimal discountAmount = shopUserCouponService.calculateDiscountAmount(userCoupon, orderAmount);
return success(discountAmount);
return fail("添加失败");
}
@Operation(summary = "验证优惠券是否可用于指定商品")
@GetMapping("/validate")
public ApiResult<Boolean> validateCoupon(@RequestParam Long userCouponId,
@RequestParam(required = false) Integer goodsId,
@RequestParam(required = false) Integer categoryId) {
ShopUserCoupon userCoupon = shopUserCouponService.getById(userCouponId);
if (userCoupon == null) {
return fail("优惠券不存在",null);
}
boolean valid = shopUserCouponService.validateCouponForGoods(userCoupon, goodsId, categoryId);
return success(valid);
}
@PreAuthorize("hasAuthority('shop:userCoupon:update')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:update')")
@OperationLog
@Operation(summary = "更新过期优惠券状态")
@PutMapping("/update-expired")
public ApiResult<?> updateExpiredCoupons() {
int count = shopUserCouponService.updateExpiredCoupons();
return success("更新了" + count + "张过期优惠券");
}
@PreAuthorize("hasAuthority('shop:userCoupon:list')")
@Operation(summary = "获取即将过期的优惠券")
@GetMapping("/expiring-soon")
public ApiResult<List<ShopUserCoupon>> getExpiringSoonCoupons(
@RequestParam(defaultValue = "3") Integer days) {
List<ShopUserCoupon> coupons = shopUserCouponService.getExpiringSoonCoupons(days);
return success(coupons);
@Operation(summary = "批量修改用户优惠券")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopUserCoupon> batchParam) {
if (batchParam.update(shopUserCouponService, "id")) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('shop:userCoupon:remove')")
@PreAuthorize("hasAuthority('shop:shopUserCoupon:remove')")
@OperationLog
@Operation(summary = "删除用户优惠券")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Long id) {
if (shopUserCouponService.removeById(id)) {
@Operation(summary = "批量删除用户优惠券")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (shopUserCouponService.removeByIds(ids)) {
return success("删除成功");
}
return fail("删除失败");
}
}

2
src/main/java/com/gxwebsoft/shop/entity/ShopCoupon.java

@ -15,7 +15,7 @@ import lombok.EqualsAndHashCode;
* 优惠券
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
* @since 2025-08-09 15:29:58
*/
@Data
@EqualsAndHashCode(callSuper = false)

55
src/main/java/com/gxwebsoft/shop/entity/ShopGoodsCoupon.java

@ -1,55 +0,0 @@
package com.gxwebsoft.shop.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableLogic;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 商品优惠券表
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Schema(name = "ShopGoodsCoupon对象", description = "商品优惠券表")
public class ShopGoodsCoupon implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@Schema(description = "商品id")
private Integer goodsId;
@Schema(description = "优惠劵id")
private Integer issueCouponId;
@Schema(description = "排序(数字越小越靠前)")
private Integer sortNumber;
@Schema(description = "状态, 0正常, 1冻结")
private Integer status;
@Schema(description = "是否删除, 0否, 1是")
@TableLogic
private Integer deleted;
@Schema(description = "用户ID")
private Integer userId;
@Schema(description = "租户id")
private Integer tenantId;
@Schema(description = "注册时间")
private LocalDateTime createTime;
@Schema(description = "修改时间")
private LocalDateTime updateTime;
}

92
src/main/java/com/gxwebsoft/shop/entity/ShopUserCoupon.java

@ -1,31 +1,59 @@
package com.gxwebsoft.shop.entity;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 用户优惠券
*
* @author 科技小王子
* @since 2025-08-08 21:30:00
* @since 2025-08-09 15:29:58
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("shop_user_coupon")
@Schema(name = "ShopUserCoupon对象", description = "用户优惠券")
public class ShopUserCoupon implements Serializable {
private static final long serialVersionUID = 1L;
// 优惠券类型常量
/** 满减券 */
public static final Integer TYPE_REDUCE = 10;
/** 折扣券 */
public static final Integer TYPE_DISCOUNT = 20;
/** 免费券 */
public static final Integer TYPE_FREE = 30;
// 适用范围常量
/** 全部商品 */
public static final Integer APPLY_ALL = 10;
/** 指定商品 */
public static final Integer APPLY_GOODS = 20;
/** 指定分类 */
public static final Integer APPLY_CATEGORY = 30;
// 使用状态常量
/** 未使用 */
public static final Integer STATUS_UNUSED = 0;
/** 已使用 */
public static final Integer STATUS_USED = 1;
/** 已过期 */
public static final Integer STATUS_EXPIRED = 2;
// 获取方式常量
/** 主动领取 */
public static final Integer OBTAIN_ACTIVE = 10;
/** 系统发放 */
public static final Integer OBTAIN_SYSTEM = 20;
/** 活动赠送 */
public static final Integer OBTAIN_ACTIVITY = 30;
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ -86,57 +114,15 @@ public class ShopUserCoupon implements Serializable {
@Schema(description = "是否删除, 0否, 1是")
@TableLogic
private Integer deleted;
private Boolean deleted;
@Schema(description = "租户id")
private Integer tenantId;
@Schema(description = "创建时间")
private Date createTime;
private LocalDateTime createTime;
@Schema(description = "修改时间")
private Date updateTime;
private LocalDateTime updateTime;
// 优惠券状态常量
public static final int STATUS_UNUSED = 0; // 未使用
public static final int STATUS_USED = 1; // 已使用
public static final int STATUS_EXPIRED = 2; // 已过期
// 优惠券类型常量
public static final int TYPE_REDUCE = 10; // 满减券
public static final int TYPE_DISCOUNT = 20; // 折扣券
public static final int TYPE_FREE = 30; // 免费券
// 适用范围常量
public static final int APPLY_ALL = 10; // 全部商品
public static final int APPLY_GOODS = 20; // 指定商品
public static final int APPLY_CATEGORY = 30; // 指定分类
// 获取方式常量
public static final int OBTAIN_RECEIVE = 10; // 主动领取
public static final int OBTAIN_SYSTEM = 20; // 系统发放
public static final int OBTAIN_ACTIVITY = 30; // 活动赠送
/**
* 检查优惠券是否已过期
*/
public boolean isExpired() {
return this.endTime != null && this.endTime.isBefore(LocalDateTime.now());
}
/**
* 检查优惠券是否可用
*/
public boolean isAvailable() {
return this.status == STATUS_UNUSED && !isExpired();
}
/**
* 检查优惠券是否在有效期内
*/
public boolean isInValidPeriod() {
LocalDateTime now = LocalDateTime.now();
return (this.startTime == null || !this.startTime.isAfter(now)) &&
(this.endTime == null || !this.endTime.isBefore(now));
}
}

2
src/main/java/com/gxwebsoft/shop/mapper/ShopCouponMapper.java

@ -12,7 +12,7 @@ import java.util.List;
* 优惠券Mapper
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
* @since 2025-08-09 15:29:58
*/
public interface ShopCouponMapper extends BaseMapper<ShopCoupon> {

37
src/main/java/com/gxwebsoft/shop/mapper/ShopGoodsCouponMapper.java

@ -1,37 +0,0 @@
package com.gxwebsoft.shop.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.shop.entity.ShopGoodsCoupon;
import com.gxwebsoft.shop.param.ShopGoodsCouponParam;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 商品优惠券表Mapper
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
public interface ShopGoodsCouponMapper extends BaseMapper<ShopGoodsCoupon> {
/**
* 分页查询
*
* @param page 分页对象
* @param param 查询参数
* @return List<ShopGoodsCoupon>
*/
List<ShopGoodsCoupon> selectPageRel(@Param("page") IPage<ShopGoodsCoupon> page,
@Param("param") ShopGoodsCouponParam param);
/**
* 查询全部
*
* @param param 查询参数
* @return List<User>
*/
List<ShopGoodsCoupon> selectListRel(@Param("param") ShopGoodsCouponParam param);
}

49
src/main/java/com/gxwebsoft/shop/mapper/ShopUserCouponMapper.java

@ -12,65 +12,26 @@ import java.util.List;
* 用户优惠券Mapper
*
* @author 科技小王子
* @since 2025-08-08 21:30:00
* @since 2025-08-09 15:29:58
*/
public interface ShopUserCouponMapper extends BaseMapper<ShopUserCoupon> {
/**
* 分页查询用户优惠券
* 分页查询
*
* @param page 分页对象
* @param param 查询参数
* @return List<ShopUserCoupon>
*/
List<ShopUserCoupon> selectPageRel(@Param("page") IPage<ShopUserCoupon> page,
@Param("param") ShopUserCouponParam param);
@Param("param") ShopUserCouponParam param);
/**
* 查询用户优惠券列表
* 查询全部
*
* @param param 查询参数
* @return List<ShopUserCoupon>
* @return List<User>
*/
List<ShopUserCoupon> selectListRel(@Param("param") ShopUserCouponParam param);
/**
* 查询用户可用的优惠券
*
* @param param 查询参数
* @return List<ShopUserCoupon>
*/
List<ShopUserCoupon> selectAvailableCoupons(@Param("param") ShopUserCouponParam param);
/**
* 统计用户优惠券数量
*
* @param userId 用户ID
* @return 统计结果
*/
java.util.Map<String, Object> countUserCoupons(@Param("userId") Integer userId);
/**
* 查询即将过期的优惠券
*
* @param days 天数
* @return List<ShopUserCoupon>
*/
List<ShopUserCoupon> selectExpiringSoon(@Param("days") Integer days);
/**
* 批量更新过期状态
*
* @return 更新数量
*/
int updateExpiredStatus();
/**
* 检查用户是否已领取指定优惠券
*
* @param userId 用户ID
* @param couponId 优惠券模板ID
* @return 已领取数量
*/
int countUserReceivedCoupon(@Param("userId") Integer userId, @Param("couponId") Integer couponId);
}

232
src/main/java/com/gxwebsoft/shop/mapper/xml/ShopUserCouponMapper.xml

@ -2,41 +2,11 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxwebsoft.shop.mapper.ShopUserCouponMapper">
<!-- 基础查询SQL -->
<!-- 关联查询sql -->
<sql id="selectSql">
SELECT
a.id,
a.coupon_id,
a.user_id,
a.name,
a.description,
a.type,
a.reduce_price,
a.discount,
a.min_price,
a.apply_range,
a.apply_range_config,
a.start_time,
a.end_time,
a.status,
a.use_time,
a.order_id,
a.order_no,
a.obtain_type,
a.obtain_source,
a.deleted,
a.tenant_id,
a.create_time,
a.update_time,
u.nickname as user_nickname,
u.phone as user_phone
SELECT a.*
FROM shop_user_coupon a
LEFT JOIN user u ON a.user_id = u.user_id
<where>
a.deleted = 0
<if test="param.tenantId != null">
AND a.tenant_id = #{param.tenantId}
</if>
<if test="param.id != null">
AND a.id = #{param.id}
</if>
@ -46,44 +16,59 @@
<if test="param.userId != null">
AND a.user_id = #{param.userId}
</if>
<if test="param.name != null and param.name != ''">
<if test="param.name != null">
AND a.name LIKE CONCAT('%', #{param.name}, '%')
</if>
<if test="param.description != null">
AND a.description LIKE CONCAT('%', #{param.description}, '%')
</if>
<if test="param.type != null">
AND a.type = #{param.type}
</if>
<if test="param.status != null">
AND a.status = #{param.status}
<if test="param.reducePrice != null">
AND a.reduce_price = #{param.reducePrice}
</if>
<if test="param.discount != null">
AND a.discount = #{param.discount}
</if>
<if test="param.minPrice != null">
AND a.min_price = #{param.minPrice}
</if>
<if test="param.applyRange != null">
AND a.apply_range = #{param.applyRange}
</if>
<if test="param.obtainType != null">
AND a.obtain_type = #{param.obtainType}
<if test="param.applyRangeConfig != null">
AND a.apply_range_config LIKE CONCAT('%', #{param.applyRangeConfig}, '%')
</if>
<if test="param.orderId != null">
AND a.order_id = #{param.orderId}
<if test="param.startTime != null">
AND a.start_time LIKE CONCAT('%', #{param.startTime}, '%')
</if>
<if test="param.orderNo != null and param.orderNo != ''">
AND a.order_no = #{param.orderNo}
<if test="param.endTime != null">
AND a.end_time LIKE CONCAT('%', #{param.endTime}, '%')
</if>
<if test="param.startTimeBegin != null">
AND a.start_time &gt;= #{param.startTimeBegin}
<if test="param.status != null">
AND a.status = #{param.status}
</if>
<if test="param.startTimeEnd != null">
AND a.start_time &lt;= #{param.startTimeEnd}
<if test="param.useTime != null">
AND a.use_time LIKE CONCAT('%', #{param.useTime}, '%')
</if>
<if test="param.endTimeBegin != null">
AND a.end_time &gt;= #{param.endTimeBegin}
<if test="param.orderId != null">
AND a.order_id LIKE CONCAT('%', #{param.orderId}, '%')
</if>
<if test="param.orderNo != null">
AND a.order_no LIKE CONCAT('%', #{param.orderNo}, '%')
</if>
<if test="param.obtainType != null">
AND a.obtain_type = #{param.obtainType}
</if>
<if test="param.endTimeEnd != null">
AND a.end_time &lt;= #{param.endTimeEnd}
<if test="param.obtainSource != null">
AND a.obtain_source LIKE CONCAT('%', #{param.obtainSource}, '%')
</if>
<if test="param.useTimeBegin != null">
AND a.use_time &gt;= #{param.useTimeBegin}
<if test="param.deleted != null">
AND a.deleted = #{param.deleted}
</if>
<if test="param.useTimeEnd != null">
AND a.use_time &lt;= #{param.useTimeEnd}
<if test="param.deleted == null">
AND a.deleted = 0
</if>
<if test="param.createTimeStart != null">
AND a.create_time &gt;= #{param.createTimeStart}
@ -91,150 +76,21 @@
<if test="param.createTimeEnd != null">
AND a.create_time &lt;= #{param.createTimeEnd}
</if>
<if test="param.includeExpired != null and !param.includeExpired">
AND (a.end_time IS NULL OR a.end_time &gt; NOW())
</if>
<if test="param.onlyAvailable != null and param.onlyAvailable">
AND a.status = 0
AND (a.start_time IS NULL OR a.start_time &lt;= NOW())
AND (a.end_time IS NULL OR a.end_time &gt; NOW())
</if>
<if test="param.keywords != null and param.keywords != ''">
AND (a.name LIKE CONCAT('%', #{param.keywords}, '%')
OR a.description LIKE CONCAT('%', #{param.keywords}, '%')
OR u.nickname LIKE CONCAT('%', #{param.keywords}, '%')
OR u.phone LIKE CONCAT('%', #{param.keywords}, '%'))
</if>
<if test="param.keywords != null">
AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
)
</if>
</where>
</sql>
<!-- 分页查询 -->
<select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.ShopUserCoupon">
<include refid="selectSql"/>
ORDER BY a.create_time DESC
<include refid="selectSql"></include>
</select>
<!-- 查询全部 -->
<select id="selectListRel" resultType="com.gxwebsoft.shop.entity.ShopUserCoupon">
<include refid="selectSql"/>
ORDER BY a.create_time DESC
</select>
<!-- 查询用户可用的优惠券 -->
<select id="selectAvailableCoupons" resultType="com.gxwebsoft.shop.entity.ShopUserCoupon">
SELECT
a.id,
a.coupon_id,
a.user_id,
a.name,
a.description,
a.type,
a.reduce_price,
a.discount,
a.min_price,
a.apply_range,
a.apply_range_config,
a.start_time,
a.end_time,
a.status,
a.use_time,
a.order_id,
a.order_no,
a.obtain_type,
a.obtain_source,
a.deleted,
a.tenant_id,
a.create_time,
a.update_time
FROM shop_user_coupon a
WHERE a.deleted = 0
AND a.user_id = #{param.userId}
AND a.status = 0
AND (a.start_time IS NULL OR a.start_time &lt;= NOW())
AND (a.end_time IS NULL OR a.end_time &gt; NOW())
<if test="param.orderAmount != null">
AND (a.min_price IS NULL OR a.min_price &lt;= #{param.orderAmount})
</if>
<if test="param.goodsId != null">
AND (a.apply_range = 10
OR (a.apply_range = 20 AND FIND_IN_SET(#{param.goodsId}, a.apply_range_config) > 0))
</if>
<if test="param.categoryId != null">
AND (a.apply_range = 10
OR (a.apply_range = 30 AND FIND_IN_SET(#{param.categoryId}, a.apply_range_config) > 0))
</if>
ORDER BY
CASE a.type
WHEN 10 THEN a.reduce_price
WHEN 20 THEN #{param.orderAmount} * (100 - a.discount) / 100
ELSE #{param.orderAmount}
END DESC,
a.end_time ASC
</select>
<!-- 统计用户优惠券数量 -->
<select id="countUserCoupons" resultType="java.util.Map">
SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 0 AND (end_time IS NULL OR end_time > NOW()) THEN 1 ELSE 0 END) as unused,
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as used,
SUM(CASE WHEN status = 2 OR (status = 0 AND end_time IS NOT NULL AND end_time &lt;= NOW()) THEN 1 ELSE 0 END) as expired
FROM shop_user_coupon
WHERE deleted = 0 AND user_id = #{userId}
</select>
<!-- 查询即将过期的优惠券 -->
<select id="selectExpiringSoon" resultType="com.gxwebsoft.shop.entity.ShopUserCoupon">
SELECT
a.id,
a.coupon_id,
a.user_id,
a.name,
a.description,
a.type,
a.reduce_price,
a.discount,
a.min_price,
a.apply_range,
a.apply_range_config,
a.start_time,
a.end_time,
a.status,
a.use_time,
a.order_id,
a.order_no,
a.obtain_type,
a.obtain_source,
a.deleted,
a.tenant_id,
a.create_time,
a.update_time
FROM shop_user_coupon a
WHERE a.deleted = 0
AND a.status = 0
AND a.end_time IS NOT NULL
AND a.end_time > NOW()
AND a.end_time &lt;= DATE_ADD(NOW(), INTERVAL #{days} DAY)
ORDER BY a.end_time ASC
</select>
<!-- 批量更新过期状态 -->
<update id="updateExpiredStatus">
UPDATE shop_user_coupon
SET status = 2, update_time = NOW()
WHERE deleted = 0
AND status = 0
AND end_time IS NOT NULL
AND end_time &lt;= NOW()
</update>
<!-- 检查用户是否已领取指定优惠券 -->
<select id="countUserReceivedCoupon" resultType="int">
SELECT COUNT(*)
FROM shop_user_coupon
WHERE deleted = 0
AND user_id = #{userId}
AND coupon_id = #{couponId}
<include refid="selectSql"></include>
</select>
</mapper>

2
src/main/java/com/gxwebsoft/shop/param/ShopCouponParam.java

@ -13,7 +13,7 @@ import lombok.EqualsAndHashCode;
* 优惠券查询参数
*
* @author 科技小王子
* @since 2025-08-09 15:26:01
* @since 2025-08-09 15:29:57
*/
@Data
@EqualsAndHashCode(callSuper = false)

52
src/main/java/com/gxwebsoft/shop/param/ShopGoodsCouponParam.java

@ -1,52 +0,0 @@
package com.gxwebsoft.shop.param;
import java.math.BigDecimal;
import com.gxwebsoft.common.core.annotation.QueryField;
import com.gxwebsoft.common.core.annotation.QueryType;
import com.gxwebsoft.common.core.web.BaseParam;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 商品优惠券表查询参数
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(name = "ShopGoodsCouponParam对象", description = "商品优惠券表查询参数")
public class ShopGoodsCouponParam extends BaseParam {
private static final long serialVersionUID = 1L;
@QueryField(type = QueryType.EQ)
private Integer id;
@Schema(description = "商品id")
@QueryField(type = QueryType.EQ)
private Integer goodsId;
@Schema(description = "优惠劵id")
@QueryField(type = QueryType.EQ)
private Integer issueCouponId;
@Schema(description = "排序(数字越小越靠前)")
@QueryField(type = QueryType.EQ)
private Integer sortNumber;
@Schema(description = "状态, 0正常, 1冻结")
@QueryField(type = QueryType.EQ)
private Integer status;
@Schema(description = "是否删除, 0否, 1是")
@QueryField(type = QueryType.EQ)
private Integer deleted;
@Schema(description = "用户ID")
@QueryField(type = QueryType.EQ)
private Integer userId;
}

86
src/main/java/com/gxwebsoft/shop/param/ShopUserCouponParam.java

@ -1,84 +1,96 @@
package com.gxwebsoft.shop.param;
import java.math.BigDecimal;
import com.gxwebsoft.common.core.annotation.QueryField;
import com.gxwebsoft.common.core.annotation.QueryType;
import com.gxwebsoft.common.core.web.BaseParam;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 用户优惠券查询参数
*
* @author 科技小王子
* @since 2025-08-08 21:30:00
* @since 2025-08-09 15:29:58
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Schema(description = "用户优惠券查询参数")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(name = "ShopUserCouponParam对象", description = "用户优惠券查询参数")
public class ShopUserCouponParam extends BaseParam {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
@QueryField(type = QueryType.EQ)
private Long id;
@Schema(description = "优惠券模板ID")
@QueryField(type = QueryType.EQ)
private Integer couponId;
@Schema(description = "用户ID")
@QueryField(type = QueryType.EQ)
private Integer userId;
@Schema(description = "优惠券名称")
private String name;
@Schema(description = "优惠券描述")
private String description;
@Schema(description = "优惠券类型(10满减券 20折扣券 30免费劵)")
@QueryField(type = QueryType.EQ)
private Integer type;
@Schema(description = "使用状态(0未使用 1已使用 2已过期)")
private Integer status;
@Schema(description = "适用范围(10全部商品 20指定商品 30指定分类)")
private Integer applyRange;
@Schema(description = "满减券-减免金额")
@QueryField(type = QueryType.EQ)
private BigDecimal reducePrice;
@Schema(description = "获取方式(10主动领取 20系统发放 30活动赠送)")
private Integer obtainType;
@Schema(description = "折扣券-折扣率(0-100)")
@QueryField(type = QueryType.EQ)
private Integer discount;
@Schema(description = "使用订单ID")
private Long orderId;
@Schema(description = "最低消费金额")
@QueryField(type = QueryType.EQ)
private BigDecimal minPrice;
@Schema(description = "使用订单号")
private String orderNo;
@Schema(description = "适用范围(10全部商品 20指定商品 30指定分类)")
@QueryField(type = QueryType.EQ)
private Integer applyRange;
@Schema(description = "有效期开始时间-查询开始")
private LocalDateTime startTimeBegin;
@Schema(description = "适用范围配置(json格式)")
private String applyRangeConfig;
@Schema(description = "有效期开始时间-查询结束")
private LocalDateTime startTimeEnd;
@Schema(description = "有效期开始时间")
private String startTime;
@Schema(description = "有效期结束时间-查询开始")
private LocalDateTime endTimeBegin;
@Schema(description = "有效期结束时间")
private String endTime;
@Schema(description = "有效期结束时间-查询结束")
private LocalDateTime endTimeEnd;
@Schema(description = "使用状态(0未使用 1已使用 2已过期)")
@QueryField(type = QueryType.EQ)
private Integer status;
@Schema(description = "使用时间-查询开始")
private LocalDateTime useTimeBegin;
@Schema(description = "使用时间")
private String useTime;
@Schema(description = "使用时间-查询结束")
private LocalDateTime useTimeEnd;
@Schema(description = "使用订单ID")
private Long orderId;
@Schema(description = "是否包含已过期的券(默认false)")
private Boolean includeExpired = false;
@Schema(description = "使用订单号")
private String orderNo;
@Schema(description = "是否只查询可用的券(默认false)")
private Boolean onlyAvailable = false;
@Schema(description = "获取方式(10主动领取 20系统发放 30活动赠送)")
@QueryField(type = QueryType.EQ)
private Integer obtainType;
@Schema(description = "商品ID(用于筛选可用优惠券)")
private Integer goodsId;
@Schema(description = "获取来源描述")
private String obtainSource;
@Schema(description = "商品分类ID(用于筛选可用优惠券)")
private Integer categoryId;
@Schema(description = "是否删除, 0否, 1是")
@QueryField(type = QueryType.EQ)
private Boolean deleted;
@Schema(description = "订单金额(用于筛选可用优惠券)")
private java.math.BigDecimal orderAmount;
}

2
src/main/java/com/gxwebsoft/shop/service/ShopCouponService.java

@ -11,7 +11,7 @@ import java.util.List;
* 优惠券Service
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
* @since 2025-08-09 15:29:58
*/
public interface ShopCouponService extends IService<ShopCoupon> {

42
src/main/java/com/gxwebsoft/shop/service/ShopGoodsCouponService.java

@ -1,42 +0,0 @@
package com.gxwebsoft.shop.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopGoodsCoupon;
import com.gxwebsoft.shop.param.ShopGoodsCouponParam;
import java.util.List;
/**
* 商品优惠券表Service
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
public interface ShopGoodsCouponService extends IService<ShopGoodsCoupon> {
/**
* 分页关联查询
*
* @param param 查询参数
* @return PageResult<ShopGoodsCoupon>
*/
PageResult<ShopGoodsCoupon> pageRel(ShopGoodsCouponParam param);
/**
* 关联查询全部
*
* @param param 查询参数
* @return List<ShopGoodsCoupon>
*/
List<ShopGoodsCoupon> listRel(ShopGoodsCouponParam param);
/**
* 根据id查询
*
* @param id
* @return ShopGoodsCoupon
*/
ShopGoodsCoupon getByIdRel(Integer id);
}

121
src/main/java/com/gxwebsoft/shop/service/ShopUserCouponService.java

@ -5,20 +5,18 @@ import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import com.gxwebsoft.shop.param.ShopUserCouponParam;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* 用户优惠券Service
*
* @author 科技小王子
* @since 2025-08-08 21:30:00
* @since 2025-08-09 15:29:58
*/
public interface ShopUserCouponService extends IService<ShopUserCoupon> {
/**
* 分页关联查询用户优惠券
* 分页关联查询
*
* @param param 查询参数
* @return PageResult<ShopUserCoupon>
@ -26,7 +24,7 @@ public interface ShopUserCouponService extends IService<ShopUserCoupon> {
PageResult<ShopUserCoupon> pageRel(ShopUserCouponParam param);
/**
* 关联查询用户优惠券列表
* 关联查询全部
*
* @param param 查询参数
* @return List<ShopUserCoupon>
@ -34,120 +32,11 @@ public interface ShopUserCouponService extends IService<ShopUserCoupon> {
List<ShopUserCoupon> listRel(ShopUserCouponParam param);
/**
* 根据id查询用户优惠券
* 根据id查询
*
* @param id id
* @return ShopUserCoupon
*/
ShopUserCoupon getByIdRel(Long id);
ShopUserCoupon getByIdRel(Integer id);
/**
* 用户领取优惠券
*
* @param userId 用户ID
* @param couponId 优惠券模板ID
* @return 是否成功
*/
boolean receiveCoupon(Integer userId, Integer couponId);
/**
* 系统发放优惠券给用户
*
* @param userId 用户ID
* @param couponId 优惠券模板ID
* @param source 发放来源描述
* @return 是否成功
*/
boolean issueCouponToUser(Integer userId, Integer couponId, String source);
/**
* 批量发放优惠券给多个用户
*
* @param userIds 用户ID列表
* @param couponId 优惠券模板ID
* @param source 发放来源描述
* @return 成功发放的数量
*/
int batchIssueCoupons(List<Integer> userIds, Integer couponId, String source);
/**
* 使用优惠券
*
* @param userCouponId 用户优惠券ID
* @param orderId 订单ID
* @param orderNo 订单号
* @return 是否成功
*/
boolean useCoupon(Long userCouponId, Long orderId, String orderNo);
/**
* 退还优惠券(订单取消时)
*
* @param orderId 订单ID
* @return 是否成功
*/
boolean returnCoupon(Long orderId);
/**
* 获取用户可用的优惠券列表
*
* @param userId 用户ID
* @param goodsId 商品ID(可选)
* @param categoryId 商品分类ID(可选)
* @param orderAmount 订单金额(可选)
* @return List<ShopUserCoupon>
*/
List<ShopUserCoupon> getAvailableCoupons(Integer userId, Integer goodsId,
Integer categoryId, BigDecimal orderAmount);
/**
* 计算优惠券优惠金额
*
* @param userCoupon 用户优惠券
* @param orderAmount 订单金额
* @return 优惠金额
*/
BigDecimal calculateDiscountAmount(ShopUserCoupon userCoupon, BigDecimal orderAmount);
/**
* 验证优惠券是否可用于指定商品
*
* @param userCoupon 用户优惠券
* @param goodsId 商品ID
* @param categoryId 商品分类ID
* @return 是否可用
*/
boolean validateCouponForGoods(ShopUserCoupon userCoupon, Integer goodsId, Integer categoryId);
/**
* 统计用户优惠券数量
*
* @param userId 用户ID
* @return 统计结果 {total: 总数, unused: 未使用, used: 已使用, expired: 已过期}
*/
Map<String, Object> countUserCoupons(Integer userId);
/**
* 更新过期优惠券状态
*
* @return 更新数量
*/
int updateExpiredCoupons();
/**
* 获取即将过期的优惠券
*
* @param days 天数
* @return List<ShopUserCoupon>
*/
List<ShopUserCoupon> getExpiringSoonCoupons(Integer days);
/**
* 检查用户是否可以领取指定优惠券
*
* @param userId 用户ID
* @param couponId 优惠券模板ID
* @return 检查结果 {canReceive: boolean, reason: String}
*/
Map<String, Object> checkCanReceiveCoupon(Integer userId, Integer couponId);
}

2
src/main/java/com/gxwebsoft/shop/service/impl/ShopCouponServiceImpl.java

@ -15,7 +15,7 @@ import java.util.List;
* 优惠券Service实现
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
* @since 2025-08-09 15:29:58
*/
@Service
public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCoupon> implements ShopCouponService {

47
src/main/java/com/gxwebsoft/shop/service/impl/ShopGoodsCouponServiceImpl.java

@ -1,47 +0,0 @@
package com.gxwebsoft.shop.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.ShopGoodsCouponMapper;
import com.gxwebsoft.shop.service.ShopGoodsCouponService;
import com.gxwebsoft.shop.entity.ShopGoodsCoupon;
import com.gxwebsoft.shop.param.ShopGoodsCouponParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 商品优惠券表Service实现
*
* @author 科技小王子
* @since 2025-08-09 15:26:02
*/
@Service
public class ShopGoodsCouponServiceImpl extends ServiceImpl<ShopGoodsCouponMapper, ShopGoodsCoupon> implements ShopGoodsCouponService {
@Override
public PageResult<ShopGoodsCoupon> pageRel(ShopGoodsCouponParam param) {
PageParam<ShopGoodsCoupon, ShopGoodsCouponParam> page = new PageParam<>(param);
page.setDefaultOrder("sort_number asc, create_time desc");
List<ShopGoodsCoupon> list = baseMapper.selectPageRel(page, param);
return new PageResult<>(list, page.getTotal());
}
@Override
public List<ShopGoodsCoupon> listRel(ShopGoodsCouponParam param) {
List<ShopGoodsCoupon> list = baseMapper.selectListRel(param);
// 排序
PageParam<ShopGoodsCoupon, ShopGoodsCouponParam> page = new PageParam<>();
page.setDefaultOrder("sort_number asc, create_time desc");
return page.sortRecords(list);
}
@Override
public ShopGoodsCoupon getByIdRel(Integer id) {
ShopGoodsCouponParam param = new ShopGoodsCouponParam();
param.setId(id);
return param.getOne(baseMapper.selectListRel(param));
}
}

47
src/main/java/com/gxwebsoft/shop/service/impl/ShopUserCouponServiceImpl.java

@ -0,0 +1,47 @@
package com.gxwebsoft.shop.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.ShopUserCouponMapper;
import com.gxwebsoft.shop.service.ShopUserCouponService;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import com.gxwebsoft.shop.param.ShopUserCouponParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用户优惠券Service实现
*
* @author 科技小王子
* @since 2025-08-09 15:29:58
*/
@Service
public class ShopUserCouponServiceImpl extends ServiceImpl<ShopUserCouponMapper, ShopUserCoupon> implements ShopUserCouponService {
@Override
public PageResult<ShopUserCoupon> pageRel(ShopUserCouponParam param) {
PageParam<ShopUserCoupon, ShopUserCouponParam> page = new PageParam<>(param);
page.setDefaultOrder("sort_number asc, create_time desc");
List<ShopUserCoupon> list = baseMapper.selectPageRel(page, param);
return new PageResult<>(list, page.getTotal());
}
@Override
public List<ShopUserCoupon> listRel(ShopUserCouponParam param) {
List<ShopUserCoupon> list = baseMapper.selectListRel(param);
// 排序
PageParam<ShopUserCoupon, ShopUserCouponParam> page = new PageParam<>();
page.setDefaultOrder("sort_number asc, create_time desc");
return page.sortRecords(list);
}
@Override
public ShopUserCoupon getByIdRel(Integer id) {
ShopUserCouponParam param = new ShopUserCouponParam();
param.setId(id);
return param.getOne(baseMapper.selectListRel(param));
}
}

2
src/test/java/com/gxwebsoft/generator/ShopGenerator.java

@ -55,7 +55,6 @@ public class ShopGenerator {
// "shop_goods_sku",
// "shop_goods_category",
"shop_coupon",
"shop_goods_coupon",
// "shop_goods_description",
// "shop_goods_log",
// "shop_goods_relation",
@ -89,6 +88,7 @@ public class ShopGenerator {
// "shop_dealer_user",
// "shop_user_referee",
// "shop_dealer_withdraw",
"shop_user_coupon",
// "shop_cart",
// "shop_count",
// "shop_express",

6
src/test/java/com/gxwebsoft/generator/templates/index.vue.btl

@ -1,6 +1,5 @@
<template>
<div class="page">
<div class="ele-body">
<a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
<a-card :bordered="false" :body-style="{ padding: '16px' }">
<ele-pro-table
ref="tableRef"
@ -46,8 +45,7 @@
<!-- 编辑弹窗 -->
<${entity}Edit v-model:visible="showEdit" :data="current" @done="reload" />
</div>
</div>
</a-page-header>
</template>
<script lang="ts" setup>

156
src/test/java/com/gxwebsoft/shop/utils/CouponUtilsTest.java

@ -0,0 +1,156 @@
package com.gxwebsoft.shop.utils;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* CouponUtils 测试类
*/
public class CouponUtilsTest {
@Test
public void testGetTypeName() {
assertEquals("满减券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_REDUCE));
assertEquals("折扣券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_DISCOUNT));
assertEquals("免费券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_FREE));
assertEquals("未知", CouponUtils.getTypeName(null));
assertEquals("未知", CouponUtils.getTypeName(99));
}
@Test
public void testGetStatusName() {
assertEquals("未使用", CouponUtils.getStatusName(ShopUserCoupon.STATUS_UNUSED));
assertEquals("已使用", CouponUtils.getStatusName(ShopUserCoupon.STATUS_USED));
assertEquals("已过期", CouponUtils.getStatusName(ShopUserCoupon.STATUS_EXPIRED));
assertEquals("未知", CouponUtils.getStatusName(null));
assertEquals("未知", CouponUtils.getStatusName(99));
}
@Test
public void testGetApplyRangeName() {
assertEquals("全部商品", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_ALL));
assertEquals("指定商品", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_GOODS));
assertEquals("指定分类", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_CATEGORY));
assertEquals("未知", CouponUtils.getApplyRangeName(null));
assertEquals("未知", CouponUtils.getApplyRangeName(99));
}
@Test
public void testCalculateDiscountAmount() {
// 测试满减券
ShopUserCoupon reduceCoupon = new ShopUserCoupon();
reduceCoupon.setType(ShopUserCoupon.TYPE_REDUCE);
reduceCoupon.setReducePrice(new BigDecimal("10.00"));
reduceCoupon.setMinPrice(new BigDecimal("50.00"));
BigDecimal discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("10.00"), discount);
// 测试不满足最低消费
discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("30.00"));
assertEquals(BigDecimal.ZERO, discount);
// 测试折扣券
ShopUserCoupon discountCoupon = new ShopUserCoupon();
discountCoupon.setType(ShopUserCoupon.TYPE_DISCOUNT);
discountCoupon.setDiscount(80); // 8折
discountCoupon.setMinPrice(new BigDecimal("50.00"));
discount = CouponUtils.calculateDiscountAmount(discountCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("20.0000"), discount);
// 测试免费券
ShopUserCoupon freeCoupon = new ShopUserCoupon();
freeCoupon.setType(ShopUserCoupon.TYPE_FREE);
discount = CouponUtils.calculateDiscountAmount(freeCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("100.00"), discount);
}
@Test
public void testIsApplicableToGoods() {
// 测试全部商品可用
ShopUserCoupon allCoupon = new ShopUserCoupon();
allCoupon.setApplyRange(ShopUserCoupon.APPLY_ALL);
assertTrue(CouponUtils.isApplicableToGoods(allCoupon, 123, 456));
// 测试指定商品可用
ShopUserCoupon goodsCoupon = new ShopUserCoupon();
goodsCoupon.setApplyRange(ShopUserCoupon.APPLY_GOODS);
goodsCoupon.setApplyRangeConfig("123,456,789");
assertTrue(CouponUtils.isApplicableToGoods(goodsCoupon, 123, 999));
assertFalse(CouponUtils.isApplicableToGoods(goodsCoupon, 999, 999));
// 测试指定分类可用
ShopUserCoupon categoryCoupon = new ShopUserCoupon();
categoryCoupon.setApplyRange(ShopUserCoupon.APPLY_CATEGORY);
categoryCoupon.setApplyRangeConfig("10,20,30");
assertTrue(CouponUtils.isApplicableToGoods(categoryCoupon, 999, 20));
assertFalse(CouponUtils.isApplicableToGoods(categoryCoupon, 999, 99));
}
@Test
public void testIsExpired() {
ShopUserCoupon coupon = new ShopUserCoupon();
// 测试未过期
coupon.setEndTime(LocalDateTime.now().plusDays(1));
assertFalse(CouponUtils.isExpired(coupon));
// 测试已过期
coupon.setEndTime(LocalDateTime.now().minusDays(1));
assertTrue(CouponUtils.isExpired(coupon));
// 测试无结束时间
coupon.setEndTime(null);
assertFalse(CouponUtils.isExpired(coupon));
}
@Test
public void testIsAvailable() {
ShopUserCoupon coupon = new ShopUserCoupon();
coupon.setStatus(ShopUserCoupon.STATUS_UNUSED);
coupon.setStartTime(LocalDateTime.now().minusDays(1));
coupon.setEndTime(LocalDateTime.now().plusDays(1));
assertTrue(CouponUtils.isAvailable(coupon));
// 测试已使用
coupon.setStatus(ShopUserCoupon.STATUS_USED);
assertFalse(CouponUtils.isAvailable(coupon));
// 测试已过期
coupon.setStatus(ShopUserCoupon.STATUS_UNUSED);
coupon.setEndTime(LocalDateTime.now().minusDays(1));
assertFalse(CouponUtils.isAvailable(coupon));
// 测试还未开始
coupon.setEndTime(LocalDateTime.now().plusDays(1));
coupon.setStartTime(LocalDateTime.now().plusDays(1));
assertFalse(CouponUtils.isAvailable(coupon));
}
@Test
public void testIsValidCouponCode() {
assertTrue(CouponUtils.isValidCouponCode("CPN00000001000001123456"));
assertFalse(CouponUtils.isValidCouponCode("CPN123"));
assertFalse(CouponUtils.isValidCouponCode("ABC00000001000001123456"));
assertFalse(CouponUtils.isValidCouponCode(null));
}
@Test
public void testGenerateCouponCode() {
String code = CouponUtils.generateCouponCode(1, 1);
assertNotNull(code);
assertTrue(code.startsWith("CPN"));
assertEquals(23, code.length());
assertTrue(CouponUtils.isValidCouponCode(code));
}
}
Loading…
Cancel
Save