22 changed files with 416 additions and 969 deletions
@ -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("删除失败"); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,225 +1,129 @@ |
|||||
package com.gxwebsoft.shop.controller; |
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.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.entity.ShopUserCoupon; |
||||
import com.gxwebsoft.shop.param.ShopUserCouponParam; |
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.Operation; |
||||
import io.swagger.v3.oas.annotations.tags.Tag; |
import io.swagger.v3.oas.annotations.tags.Tag; |
||||
import org.springframework.security.access.prepost.PreAuthorize; |
import org.springframework.security.access.prepost.PreAuthorize; |
||||
import org.springframework.web.bind.annotation.*; |
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
import javax.annotation.Resource; |
import javax.annotation.Resource; |
||||
import java.math.BigDecimal; |
|
||||
import java.util.List; |
import java.util.List; |
||||
import java.util.Map; |
|
||||
|
|
||||
/** |
/** |
||||
* 用户优惠券控制器 |
* 用户优惠券控制器 |
||||
* |
* |
||||
* @author 科技小王子 |
* @author 科技小王子 |
||||
* @since 2025-08-08 21:30:00 |
|
||||
|
* @since 2025-08-09 15:29:58 |
||||
*/ |
*/ |
||||
@Tag(name = "用户优惠券管理") |
@Tag(name = "用户优惠券管理") |
||||
@RestController |
@RestController |
||||
@RequestMapping("/api/shop/user-coupon") |
|
||||
|
@RequestMapping("/api/shop/shop-user-coupon") |
||||
public class ShopUserCouponController extends BaseController { |
public class ShopUserCouponController extends BaseController { |
||||
|
|
||||
@Resource |
@Resource |
||||
private ShopUserCouponService shopUserCouponService; |
private ShopUserCouponService shopUserCouponService; |
||||
|
|
||||
@PreAuthorize("hasAuthority('shop:userCoupon:list')") |
|
||||
|
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')") |
||||
@Operation(summary = "分页查询用户优惠券") |
@Operation(summary = "分页查询用户优惠券") |
||||
@GetMapping("/page") |
@GetMapping("/page") |
||||
public ApiResult<PageResult<ShopUserCoupon>> page(ShopUserCouponParam param) { |
public ApiResult<PageResult<ShopUserCoupon>> page(ShopUserCouponParam param) { |
||||
|
// 使用关联查询
|
||||
return success(shopUserCouponService.pageRel(param)); |
return success(shopUserCouponService.pageRel(param)); |
||||
} |
} |
||||
|
|
||||
@PreAuthorize("hasAuthority('shop:userCoupon:list')") |
|
||||
@Operation(summary = "查询用户优惠券列表") |
|
||||
|
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')") |
||||
|
@Operation(summary = "查询全部用户优惠券") |
||||
@GetMapping() |
@GetMapping() |
||||
public ApiResult<List<ShopUserCoupon>> list(ShopUserCouponParam param) { |
public ApiResult<List<ShopUserCoupon>> list(ShopUserCouponParam param) { |
||||
|
// 使用关联查询
|
||||
return success(shopUserCouponService.listRel(param)); |
return success(shopUserCouponService.listRel(param)); |
||||
} |
} |
||||
|
|
||||
@PreAuthorize("hasAuthority('shop:userCoupon:list')") |
|
||||
|
@PreAuthorize("hasAuthority('shop:shopUserCoupon:list')") |
||||
@Operation(summary = "根据id查询用户优惠券") |
@Operation(summary = "根据id查询用户优惠券") |
||||
@GetMapping("/{id}") |
@GetMapping("/{id}") |
||||
public ApiResult<ShopUserCoupon> get(@PathVariable("id") Long id) { |
|
||||
|
public ApiResult<ShopUserCoupon> get(@PathVariable("id") Integer id) { |
||||
|
// 使用关联查询
|
||||
return success(shopUserCouponService.getByIdRel(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 |
@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(); |
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 |
@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 |
@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 |
@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 |
@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 |
@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 success("删除成功"); |
||||
} |
} |
||||
return fail("删除失败"); |
return fail("删除失败"); |
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
@ -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; |
|
||||
|
|
||||
} |
|
@ -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); |
|
||||
|
|
||||
} |
|
@ -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; |
|
||||
|
|
||||
} |
|
@ -1,84 +1,96 @@ |
|||||
package com.gxwebsoft.shop.param; |
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.gxwebsoft.common.core.web.BaseParam; |
||||
|
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
import io.swagger.v3.oas.annotations.media.Schema; |
import io.swagger.v3.oas.annotations.media.Schema; |
||||
import lombok.Data; |
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
import lombok.EqualsAndHashCode; |
||||
|
|
||||
import java.time.LocalDateTime; |
|
||||
|
|
||||
/** |
/** |
||||
* 用户优惠券查询参数 |
* 用户优惠券查询参数 |
||||
* |
* |
||||
* @author 科技小王子 |
* @author 科技小王子 |
||||
* @since 2025-08-08 21:30:00 |
|
||||
|
* @since 2025-08-09 15:29:58 |
||||
*/ |
*/ |
||||
@Data |
@Data |
||||
@EqualsAndHashCode(callSuper = false) |
@EqualsAndHashCode(callSuper = false) |
||||
@Schema(description = "用户优惠券查询参数") |
|
||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
|
@Schema(name = "ShopUserCouponParam对象", description = "用户优惠券查询参数") |
||||
public class ShopUserCouponParam extends BaseParam { |
public class ShopUserCouponParam extends BaseParam { |
||||
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
||||
|
|
||||
@Schema(description = "id") |
@Schema(description = "id") |
||||
|
@QueryField(type = QueryType.EQ) |
||||
private Long id; |
private Long id; |
||||
|
|
||||
@Schema(description = "优惠券模板ID") |
@Schema(description = "优惠券模板ID") |
||||
|
@QueryField(type = QueryType.EQ) |
||||
private Integer couponId; |
private Integer couponId; |
||||
|
|
||||
@Schema(description = "用户ID") |
@Schema(description = "用户ID") |
||||
|
@QueryField(type = QueryType.EQ) |
||||
private Integer userId; |
private Integer userId; |
||||
|
|
||||
@Schema(description = "优惠券名称") |
@Schema(description = "优惠券名称") |
||||
private String name; |
private String name; |
||||
|
|
||||
|
@Schema(description = "优惠券描述") |
||||
|
private String description; |
||||
|
|
||||
@Schema(description = "优惠券类型(10满减券 20折扣券 30免费劵)") |
@Schema(description = "优惠券类型(10满减券 20折扣券 30免费劵)") |
||||
|
@QueryField(type = QueryType.EQ) |
||||
private Integer type; |
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; |
|
||||
} |
} |
||||
|
@ -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); |
|
||||
|
|
||||
} |
|
@ -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)); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -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)); |
||||
|
} |
||||
|
|
||||
|
} |
@ -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…
Reference in new issue