From c5edeb7c8bc4cc67bb882a7a98a03780617f4182 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Mon, 21 Jul 2025 12:08:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BC=98=E6=83=A0=E5=88=B8=E7=AE=A1?= =?UTF-8?q?=E7=90=86)=EF=BC=9A=E6=96=B0=E5=A2=9E=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/controller/CouponController.java | 178 +++++++ .../java/com/central/order/entity/Coupon.java | 138 ++++++ .../central/order/mapper/CouponMapper.java | 52 ++ .../central/order/model/vo/FilterOptions.java | 29 ++ .../central/order/service/ICouponService.java | 68 +++ .../order/service/ICtripApiService.java | 22 + .../order/service/impl/CouponServiceImpl.java | 184 +++++++ .../service/impl/CtripApiServiceImpl.java | 51 ++ .../order/task/CouponExpirationTask.java | 32 ++ .../main/resources/mapper/CouponMapper.xml | 124 +++++ .../src/pages/orders/CouponManagement.tsx | 449 ++++++++++++++++++ .../main/frontend/src/services/orders/api.ts | 94 ++++ .../frontend/src/services/orders/typings.d.ts | 24 + 优惠券管理功能说明.md | 175 +++++++ 14 files changed, 1620 insertions(+) create mode 100644 zlt-business/order-center/src/main/java/com/central/order/controller/CouponController.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/entity/Coupon.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/mapper/CouponMapper.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/model/vo/FilterOptions.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/service/ICouponService.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/service/ICtripApiService.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/service/impl/CouponServiceImpl.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/service/impl/CtripApiServiceImpl.java create mode 100644 zlt-business/order-center/src/main/java/com/central/order/task/CouponExpirationTask.java create mode 100644 zlt-business/order-center/src/main/resources/mapper/CouponMapper.xml create mode 100644 zlt-web/react-web/src/main/frontend/src/pages/orders/CouponManagement.tsx create mode 100644 优惠券管理功能说明.md diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/CouponController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/CouponController.java new file mode 100644 index 00000000..56b0e618 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/CouponController.java @@ -0,0 +1,178 @@ +package com.central.order.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.central.common.model.Result; +import com.central.order.entity.Coupon; +import com.central.order.service.ICouponService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +/** + * 优惠券管理控制器 + */ +@Slf4j +@RestController +@RequestMapping("/coupon") +@Tag(name = "优惠券管理") +public class CouponController { + + @Autowired + private ICouponService couponService; + + /** + * 分页查询优惠券列表 + */ + @GetMapping("/page") + @Operation(summary = "分页查询优惠券列表") + public Result> getCouponPage( + @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current, + @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size, + @Parameter(description = "优惠券名称") @RequestParam(required = false) String couponName, + @Parameter(description = "优惠券类型") @RequestParam(required = false) Integer couponType, + @Parameter(description = "状态") @RequestParam(required = false) Integer status, + @Parameter(description = "用户手机号") @RequestParam(required = false) String userPhone, + @Parameter(description = "来源") @RequestParam(required = false) Integer source) { + + Page page = new Page<>(current, size); + Coupon coupon = new Coupon(); + coupon.setCouponName(couponName); + coupon.setCouponType(couponType); + coupon.setStatus(status); + coupon.setUserPhone(userPhone); + coupon.setSource(source); + + IPage result = couponService.getCouponPage(page, coupon); + return Result.succeed(result); + } + + /** + * 根据ID查询优惠券 + */ + @GetMapping("/{id}") + @Operation(summary = "根据ID查询优惠券") + public Result getCouponById(@Parameter(description = "优惠券ID") @PathVariable Long id) { + Coupon coupon = couponService.getById(id); + return Result.succeed(coupon); + } + + /** + * 创建优惠券 + */ + @PostMapping + @Operation(summary = "创建优惠券") + public Result createCoupon(@RequestBody Coupon coupon) { + boolean result = couponService.createCoupon(coupon); + return Result.succeed(result); + } + + /** + * 更新优惠券 + */ + @PutMapping("/{id}") + @Operation(summary = "更新优惠券") + public Result updateCoupon(@Parameter(description = "优惠券ID") @PathVariable Long id, @RequestBody Coupon coupon) { + coupon.setId(id); + boolean result = couponService.updateCoupon(coupon); + return Result.succeed(result); + } + + /** + * 删除优惠券 + */ + @DeleteMapping("/{id}") + @Operation(summary = "删除优惠券") + public Result deleteCoupon(@Parameter(description = "优惠券ID") @PathVariable Long id) { + boolean result = couponService.deleteCoupon(id); + return Result.succeed(result); + } + + /** + * 根据手机号查询用户优惠券 + */ + @GetMapping("/user/{userPhone}") + @Operation(summary = "根据手机号查询用户优惠券") + public Result> getCouponsByUserPhone(@Parameter(description = "用户手机号") @PathVariable String userPhone) { + List coupons = couponService.getCouponsByUserPhone(userPhone); + return Result.succeed(coupons); + } + + /** + * 根据手机号查询可用优惠券 + */ + @GetMapping("/user/{userPhone}/available") + @Operation(summary = "根据手机号查询可用优惠券") + public Result> getAvailableCouponsByUserPhone(@Parameter(description = "用户手机号") @PathVariable String userPhone) { + List coupons = couponService.getAvailableCouponsByUserPhone(userPhone); + return Result.succeed(coupons); + } + + /** + * 使用优惠券 + */ + @PostMapping("/{id}/use") + @Operation(summary = "使用优惠券") + public Result useCoupon( + @Parameter(description = "优惠券ID") @PathVariable Long id, + @Parameter(description = "订单号") @RequestParam String orderNo) { + boolean result = couponService.useCoupon(id, orderNo); + return Result.succeed(result); + } + + /** + * 携程创建优惠券接口 + */ + @PostMapping("/ctrip/create") + @Operation(summary = "携程创建优惠券接口") + public Result createCouponFromCtrip( + @Parameter(description = "携程优惠券ID") @RequestParam String ctripCouponId, + @Parameter(description = "用户手机号") @RequestParam String userPhone, + @Parameter(description = "优惠券名称") @RequestParam String couponName, + @Parameter(description = "优惠券类型") @RequestParam Integer couponType, + @Parameter(description = "优惠券面额") @RequestParam BigDecimal amount, + @Parameter(description = "使用门槛金额") @RequestParam(required = false) BigDecimal minAmount, + @Parameter(description = "折扣率") @RequestParam(required = false) Integer discountRate, + @Parameter(description = "有效期开始时间") @RequestParam String validStartTime, + @Parameter(description = "有效期结束时间") @RequestParam String validEndTime) { + + // 这里需要根据实际的时间格式进行解析 + // 暂时使用当前时间,实际应该解析传入的时间字符串 + Date startTime = new Date(); + Date endTime = new Date(); + + boolean result = couponService.createCouponFromCtrip(ctripCouponId, userPhone, couponName, + couponType, amount, minAmount, discountRate, + startTime, endTime); + return Result.succeed(result); + } + + /** + * 核销携程优惠券 + */ + @PostMapping("/ctrip/verify") + @Operation(summary = "核销携程优惠券") + public Result verifyCtripCoupon( + @Parameter(description = "携程优惠券ID") @RequestParam String ctripCouponId, + @Parameter(description = "订单号") @RequestParam String orderNo) { + boolean result = couponService.verifyCtripCoupon(ctripCouponId, orderNo); + return Result.succeed(result); + } + + /** + * 批量更新过期优惠券 + */ + @PostMapping("/update-expired") + @Operation(summary = "批量更新过期优惠券") + public Result updateExpiredCoupons() { + couponService.updateExpiredCoupons(); + return Result.succeed("批量更新过期优惠券成功"); + } +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/entity/Coupon.java b/zlt-business/order-center/src/main/java/com/central/order/entity/Coupon.java new file mode 100644 index 00000000..14f5f55f --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/entity/Coupon.java @@ -0,0 +1,138 @@ +package com.central.order.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.FieldFill; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Date; + +/** + * 优惠券实体类 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@TableName("t_coupon") +public class Coupon implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键ID + */ + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + /** + * 优惠券编号 + */ + private String couponNo; + + /** + * 优惠券名称 + */ + private String couponName; + + /** + * 优惠券类型:1-满减券,2-折扣券,3-免费券 + */ + private Integer couponType; + + /** + * 优惠券面额 + */ + private BigDecimal amount; + + /** + * 使用门槛金额 + */ + private BigDecimal minAmount; + + /** + * 折扣率(0-100,折扣券时使用) + */ + private Integer discountRate; + + /** + * 优惠券状态:0-未使用,1-已使用,2-已过期,3-已作废 + */ + private Integer status; + + /** + * 用户手机号 + */ + private String userPhone; + + /** + * 用户ID + */ + private Long userId; + + /** + * 来源:1-携程,2-高飞自建 + */ + private Integer source; + + /** + * 携程优惠券ID(来源为携程时使用) + */ + private String ctripCouponId; + + /** + * 有效期开始时间 + */ + private Date validStartTime; + + /** + * 有效期结束时间 + */ + private Date validEndTime; + + /** + * 使用时间 + */ + private Date usedTime; + + /** + * 使用订单号 + */ + private String usedOrderNo; + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; + + /** + * 租户ID + */ + private String tenantId; + + /** + * 创建人 + */ + private String createBy; + + /** + * 更新人 + */ + private String updateBy; + + /** + * 备注 + */ + private String remark; +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/mapper/CouponMapper.java b/zlt-business/order-center/src/main/java/com/central/order/mapper/CouponMapper.java new file mode 100644 index 00000000..c5cd25aa --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/mapper/CouponMapper.java @@ -0,0 +1,52 @@ +package com.central.order.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.central.order.entity.Coupon; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * 优惠券Mapper接口 + */ +@Mapper +public interface CouponMapper extends BaseMapper { + + /** + * 分页查询优惠券列表 + */ + IPage selectCouponPage(Page page, @Param("coupon") Coupon coupon); + + /** + * 根据手机号查询用户优惠券 + */ + List selectByUserPhone(@Param("userPhone") String userPhone); + + /** + * 根据手机号查询可用优惠券 + */ + List selectAvailableByUserPhone(@Param("userPhone") String userPhone); + + /** + * 根据携程优惠券ID查询 + */ + Coupon selectByCtripCouponId(@Param("ctripCouponId") String ctripCouponId); + + /** + * 根据订单号查询使用的优惠券 + */ + Coupon selectByUsedOrderNo(@Param("usedOrderNo") String usedOrderNo); + + /** + * 更新优惠券状态为已使用 + */ + int updateCouponUsed(@Param("id") Long id, @Param("usedOrderNo") String usedOrderNo); + + /** + * 批量更新过期优惠券状态 + */ + int updateExpiredCoupons(); +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/model/vo/FilterOptions.java b/zlt-business/order-center/src/main/java/com/central/order/model/vo/FilterOptions.java new file mode 100644 index 00000000..ca1dea15 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/model/vo/FilterOptions.java @@ -0,0 +1,29 @@ +package com.central.order.model.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +@Data +@Schema(description = "筛选选项") +public class FilterOptions { + @Schema(description = "景区列表") + private List tourismList; + + @Schema(description = "二维码列表") + private List qrcodeList; + + @Schema(description = "设备列表") + private List deviceList; + + @Data + @Schema(description = "选项项") + public static class OptionItem { + @Schema(description = "标签") + private String label; + + @Schema(description = "值") + private Object value; + } +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/ICouponService.java b/zlt-business/order-center/src/main/java/com/central/order/service/ICouponService.java new file mode 100644 index 00000000..38bd2d66 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/service/ICouponService.java @@ -0,0 +1,68 @@ +package com.central.order.service; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.IService; +import com.central.order.entity.Coupon; + +import java.util.List; + +/** + * 优惠券服务接口 + */ +public interface ICouponService extends IService { + + /** + * 分页查询优惠券列表 + */ + IPage getCouponPage(Page page, Coupon coupon); + + /** + * 创建优惠券 + */ + boolean createCoupon(Coupon coupon); + + /** + * 更新优惠券 + */ + boolean updateCoupon(Coupon coupon); + + /** + * 删除优惠券 + */ + boolean deleteCoupon(Long id); + + /** + * 根据手机号查询用户优惠券 + */ + List getCouponsByUserPhone(String userPhone); + + /** + * 根据手机号查询可用优惠券 + */ + List getAvailableCouponsByUserPhone(String userPhone); + + /** + * 使用优惠券 + */ + boolean useCoupon(Long couponId, String orderNo); + + /** + * 根据携程优惠券ID创建优惠券(携程接口调用) + */ + boolean createCouponFromCtrip(String ctripCouponId, String userPhone, String couponName, + Integer couponType, java.math.BigDecimal amount, + java.math.BigDecimal minAmount, Integer discountRate, + java.util.Date validStartTime, + java.util.Date validEndTime); + + /** + * 批量更新过期优惠券状态 + */ + void updateExpiredCoupons(); + + /** + * 核销携程优惠券 + */ + boolean verifyCtripCoupon(String ctripCouponId, String orderNo); +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/ICtripApiService.java b/zlt-business/order-center/src/main/java/com/central/order/service/ICtripApiService.java new file mode 100644 index 00000000..0c202829 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/service/ICtripApiService.java @@ -0,0 +1,22 @@ +package com.central.order.service; + +/** + * 携程API服务接口 + */ +public interface ICtripApiService { + + /** + * 核销携程优惠券 + * @param ctripCouponId 携程优惠券ID + * @param orderNo 订单号 + * @return 是否核销成功 + */ + boolean verifyCoupon(String ctripCouponId, String orderNo); + + /** + * 查询携程优惠券状态 + * @param ctripCouponId 携程优惠券ID + * @return 优惠券状态 + */ + String getCouponStatus(String ctripCouponId); +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/impl/CouponServiceImpl.java b/zlt-business/order-center/src/main/java/com/central/order/service/impl/CouponServiceImpl.java new file mode 100644 index 00000000..190beae7 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/service/impl/CouponServiceImpl.java @@ -0,0 +1,184 @@ +package com.central.order.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.central.order.entity.Coupon; +import com.central.order.mapper.CouponMapper; +import com.central.order.service.ICouponService; +import com.central.order.service.ICtripApiService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +/** + * 优惠券服务实现类 + */ +@Slf4j +@Service +public class CouponServiceImpl extends ServiceImpl implements ICouponService { + + @Autowired + private ICtripApiService ctripApiService; + + @Override + public IPage getCouponPage(Page page, Coupon coupon) { + return baseMapper.selectCouponPage(page, coupon); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean createCoupon(Coupon coupon) { + // 生成优惠券编号 + if (!StringUtils.hasText(coupon.getCouponNo())) { + coupon.setCouponNo(generateCouponNo()); + } + + // 设置默认值 + if (coupon.getStatus() == null) { + coupon.setStatus(0); // 未使用 + } + if (coupon.getCreateTime() == null) { + coupon.setCreateTime(new Date()); + } + if (coupon.getUpdateTime() == null) { + coupon.setUpdateTime(new Date()); + } + + return save(coupon); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean updateCoupon(Coupon coupon) { + coupon.setUpdateTime(new Date()); + return updateById(coupon); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean deleteCoupon(Long id) { + return removeById(id); + } + + @Override + public List getCouponsByUserPhone(String userPhone) { + return baseMapper.selectByUserPhone(userPhone); + } + + @Override + public List getAvailableCouponsByUserPhone(String userPhone) { + return baseMapper.selectAvailableByUserPhone(userPhone); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean useCoupon(Long couponId, String orderNo) { + Coupon coupon = getById(couponId); + if (coupon == null) { + log.error("优惠券不存在,couponId: {}", couponId); + return false; + } + + if (coupon.getStatus() != 0) { + log.error("优惠券状态不正确,couponId: {}, status: {}", couponId, coupon.getStatus()); + return false; + } + + if (coupon.getValidEndTime().before(new Date())) { + log.error("优惠券已过期,couponId: {}", couponId); + return false; + } + + return baseMapper.updateCouponUsed(couponId, orderNo) > 0; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean createCouponFromCtrip(String ctripCouponId, String userPhone, String couponName, + Integer couponType, java.math.BigDecimal amount, + java.math.BigDecimal minAmount, Integer discountRate, + java.util.Date validStartTime, + java.util.Date validEndTime) { + + // 检查是否已存在该携程优惠券 + Coupon existingCoupon = baseMapper.selectByCtripCouponId(ctripCouponId); + if (existingCoupon != null) { + log.warn("携程优惠券已存在,ctripCouponId: {}", ctripCouponId); + return true; // 已存在也算成功 + } + + Coupon coupon = new Coupon(); + coupon.setCouponNo(generateCouponNo()); + coupon.setCouponName(couponName); + coupon.setCouponType(couponType); + coupon.setAmount(amount); + coupon.setMinAmount(minAmount); + coupon.setDiscountRate(discountRate); + coupon.setStatus(0); // 未使用 + coupon.setUserPhone(userPhone); + coupon.setSource(1); // 来源:携程 + coupon.setCtripCouponId(ctripCouponId); + coupon.setValidStartTime(validStartTime); + coupon.setValidEndTime(validEndTime); + coupon.setCreateTime(new Date()); + coupon.setUpdateTime(new Date()); + coupon.setCreateBy("ctrip"); + coupon.setUpdateBy("ctrip"); + + return save(coupon); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateExpiredCoupons() { + int count = baseMapper.updateExpiredCoupons(); + log.info("批量更新过期优惠券完成,更新数量: {}", count); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean verifyCtripCoupon(String ctripCouponId, String orderNo) { + Coupon coupon = baseMapper.selectByCtripCouponId(ctripCouponId); + if (coupon == null) { + log.error("携程优惠券不存在,ctripCouponId: {}", ctripCouponId); + return false; + } + + if (coupon.getStatus() != 0) { + log.error("携程优惠券状态不正确,ctripCouponId: {}, status: {}", ctripCouponId, coupon.getStatus()); + return false; + } + + if (coupon.getValidEndTime().before(new Date())) { + log.error("携程优惠券已过期,ctripCouponId: {}", ctripCouponId); + return false; + } + + // 调用携程核销接口 + boolean ctripVerifyResult = ctripApiService.verifyCoupon(ctripCouponId, orderNo); + if (!ctripVerifyResult) { + log.error("携程核销失败,ctripCouponId: {}, orderNo: {}", ctripCouponId, orderNo); + return false; + } + + // 更新本地优惠券状态 + return baseMapper.updateCouponUsed(coupon.getId(), orderNo) > 0; + } + + /** + * 生成优惠券编号 + */ + private String generateCouponNo() { + return "CP" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 8).toUpperCase(); + } + + +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/impl/CtripApiServiceImpl.java b/zlt-business/order-center/src/main/java/com/central/order/service/impl/CtripApiServiceImpl.java new file mode 100644 index 00000000..ce3196b0 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/service/impl/CtripApiServiceImpl.java @@ -0,0 +1,51 @@ +package com.central.order.service.impl; + +import com.central.order.service.ICtripApiService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * 携程API服务实现类 + */ +@Slf4j +@Service +public class CtripApiServiceImpl implements ICtripApiService { + + @Override + public boolean verifyCoupon(String ctripCouponId, String orderNo) { + // TODO: 实现具体的携程API调用逻辑 + // 这里需要根据携程的API文档实现具体的调用 + log.info("调用携程核销接口,ctripCouponId: {}, orderNo: {}", ctripCouponId, orderNo); + + // 模拟API调用 + try { + // 这里应该调用携程的核销API + // 例如:HttpClient调用携程的核销接口 + + // 暂时返回true,实际需要根据API调用结果返回 + return true; + } catch (Exception e) { + log.error("携程核销接口调用失败", e); + return false; + } + } + + @Override + public String getCouponStatus(String ctripCouponId) { + // TODO: 实现具体的携程API调用逻辑 + // 这里需要根据携程的API文档实现具体的调用 + log.info("调用携程查询优惠券状态接口,ctripCouponId: {}", ctripCouponId); + + // 模拟API调用 + try { + // 这里应该调用携程的查询API + // 例如:HttpClient调用携程的查询接口 + + // 暂时返回"VALID",实际需要根据API调用结果返回 + return "VALID"; + } catch (Exception e) { + log.error("携程查询优惠券状态接口调用失败", e); + return "UNKNOWN"; + } + } +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/java/com/central/order/task/CouponExpirationTask.java b/zlt-business/order-center/src/main/java/com/central/order/task/CouponExpirationTask.java new file mode 100644 index 00000000..b5145e08 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/task/CouponExpirationTask.java @@ -0,0 +1,32 @@ +package com.central.order.task; + +import com.central.order.service.ICouponService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * 优惠券过期定时任务 + */ +@Slf4j +@Component +public class CouponExpirationTask { + + @Autowired + private ICouponService couponService; + + /** + * 每天凌晨2点执行,批量更新过期优惠券状态 + */ + @Scheduled(cron = "0 0 2 * * ?") + public void updateExpiredCoupons() { + log.info("开始执行优惠券过期状态更新任务"); + try { + couponService.updateExpiredCoupons(); + log.info("优惠券过期状态更新任务执行完成"); + } catch (Exception e) { + log.error("优惠券过期状态更新任务执行失败", e); + } + } +} \ No newline at end of file diff --git a/zlt-business/order-center/src/main/resources/mapper/CouponMapper.xml b/zlt-business/order-center/src/main/resources/mapper/CouponMapper.xml new file mode 100644 index 00000000..62a810f0 --- /dev/null +++ b/zlt-business/order-center/src/main/resources/mapper/CouponMapper.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + id, coupon_no, coupon_name, coupon_type, amount, min_amount, discount_rate, status, + user_phone, user_id, source, ctrip_coupon_id, valid_start_time, valid_end_time, + used_time, used_order_no, create_time, update_time, create_by, update_by, remark, tenant_id + + + + + + + + + + + + + + + + + + + + UPDATE t_coupon + SET status = 1, + used_time = NOW(), + used_order_no = #{usedOrderNo}, + update_time = NOW() + WHERE id = #{id} + + + + + UPDATE t_coupon + SET status = 2, + update_time = NOW() + WHERE status = 0 + AND valid_end_time < NOW() + + + \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/CouponManagement.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/CouponManagement.tsx new file mode 100644 index 00000000..efe8ccf0 --- /dev/null +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/CouponManagement.tsx @@ -0,0 +1,449 @@ +import React, { useState, useEffect } from 'react'; +import { + Card, + Table, + Button, + Space, + Modal, + Form, + Input, + Select, + DatePicker, + InputNumber, + message, + Popconfirm, + Tag, + Tooltip, +} from 'antd'; +import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons'; +import { getCouponList, createCoupon, updateCoupon, deleteCoupon, getCouponById } from '@/services/orders/api'; +import moment from 'moment'; + +const { Option } = Select; +const { RangePicker } = DatePicker; + +const CouponManagement: React.FC = () => { + const [loading, setLoading] = useState(false); + const [dataSource, setDataSource] = useState([]); + const [total, setTotal] = useState(0); + const [current, setCurrent] = useState(1); + const [pageSize, setPageSize] = useState(10); + const [modalVisible, setModalVisible] = useState(false); + const [editingCoupon, setEditingCoupon] = useState(null); + const [form] = Form.useForm(); + + // 优惠券类型选项 + const couponTypeOptions = [ + { label: '满减券', value: 1 }, + { label: '折扣券', value: 2 }, + { label: '免费券', value: 3 }, + ]; + + // 优惠券状态选项 + const statusOptions = [ + { label: '未使用', value: 0 }, + { label: '已使用', value: 1 }, + { label: '已过期', value: 2 }, + { label: '已作废', value: 3 }, + ]; + + // 来源选项 + const sourceOptions = [ + { label: '携程', value: 1 }, + { label: '高飞自建', value: 2 }, + ]; + + // 获取优惠券列表 + const fetchCouponList = async (params?: any) => { + setLoading(true); + try { + const result = await getCouponList({ + current: params?.current || current, + pageSize: params?.pageSize || pageSize, + ...params, + }); + setDataSource(result.data || []); + setTotal(result.total || 0); + } catch (error) { + message.error('获取优惠券列表失败'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + fetchCouponList(); + }, []); + + // 表格列定义 + const columns = [ + { + title: '优惠券编号', + dataIndex: 'couponNo', + key: 'couponNo', + width: 150, + }, + { + title: '优惠券名称', + dataIndex: 'couponName', + key: 'couponName', + width: 200, + }, + { + title: '优惠券类型', + dataIndex: 'couponType', + key: 'couponType', + width: 100, + render: (type: number) => { + const option = couponTypeOptions.find(item => item.value === type); + return option ? option.label : '-'; + }, + }, + { + title: '面额/折扣', + key: 'amount', + width: 120, + render: (record: ORDERS.Coupon) => { + if (record.couponType === 2) { + return `${record.discountRate}%`; + } + return `¥${record.amount}`; + }, + }, + { + title: '使用门槛', + dataIndex: 'minAmount', + key: 'minAmount', + width: 100, + render: (amount: number) => amount ? `¥${amount}` : '-', + }, + { + title: '状态', + dataIndex: 'status', + key: 'status', + width: 100, + render: (status: number) => { + const option = statusOptions.find(item => item.value === status); + const color = status === 0 ? 'green' : status === 1 ? 'blue' : status === 2 ? 'orange' : 'red'; + return option ? {option.label} : '-'; + }, + }, + { + title: '用户手机号', + dataIndex: 'userPhone', + key: 'userPhone', + width: 130, + }, + { + title: '来源', + dataIndex: 'source', + key: 'source', + width: 100, + render: (source: number) => { + const option = sourceOptions.find(item => item.value === source); + return option ? option.label : '-'; + }, + }, + { + title: '有效期', + key: 'validTime', + width: 200, + render: (record: ORDERS.Coupon) => ( +
+
{moment(record.validStartTime).format('YYYY-MM-DD HH:mm')}
+
至
+
{moment(record.validEndTime).format('YYYY-MM-DD HH:mm')}
+
+ ), + }, + { + title: '创建时间', + dataIndex: 'createTime', + key: 'createTime', + width: 150, + render: (time: string) => time ? moment(time).format('YYYY-MM-DD HH:mm') : '-', + }, + { + title: '操作', + key: 'action', + width: 150, + fixed: 'right' as const, + render: (record: ORDERS.Coupon) => ( + + + + }> + `共 ${total} 条记录`, + }} + onChange={handleTableChange} + scroll={{ x: 1500 }} + /> + + setModalVisible(false)} + width={600} + destroyOnClose + > +
+ + + + + + + + + prevValues.couponType !== currentValues.couponType} + > + {({ getFieldValue }) => { + const couponType = getFieldValue('couponType'); + return couponType === 2 ? ( + + + + ) : ( + + + + ); + }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + ); +}; + +export default CouponManagement; \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/services/orders/api.ts b/zlt-web/react-web/src/main/frontend/src/services/orders/api.ts index c10ecb04..ff10ee4c 100644 --- a/zlt-web/react-web/src/main/frontend/src/services/orders/api.ts +++ b/zlt-web/react-web/src/main/frontend/src/services/orders/api.ts @@ -339,3 +339,97 @@ export async function getWxMessageStatistics(params: { }); } +// 优惠券相关接口 +export async function getCouponList(params?: { [key: string]: string | number }) { + const { current, pageSize, ...rest } = params ?? {}; + const result = await request>('/api-order/coupon/page', { + method: 'GET', + params: { current, size: pageSize, ...rest }, + }); + return { data: result.data, total: result.total }; +} + +export async function getCouponById(id: number) { + const result = await request>(`/api-order/coupon/${id}`, { + method: 'GET', + }); + return result; +} + +export async function createCoupon(data: ORDERS.Coupon) { + const result = await request>('/api-order/coupon', { + method: 'POST', + data, + }); + return result; +} + +export async function updateCoupon(id: number, data: ORDERS.Coupon) { + const result = await request>(`/api-order/coupon/${id}`, { + method: 'PUT', + data, + }); + return result; +} + +export async function deleteCoupon(id: number) { + const result = await request>(`/api-order/coupon/${id}`, { + method: 'DELETE', + }); + return result; +} + +export async function getCouponsByUserPhone(userPhone: string) { + const result = await request>(`/api-order/coupon/user/${userPhone}`, { + method: 'GET', + }); + return result; +} + +export async function getAvailableCouponsByUserPhone(userPhone: string) { + const result = await request>(`/api-order/coupon/user/${userPhone}/available`, { + method: 'GET', + }); + return result; +} + +export async function useCoupon(id: number, orderNo: string) { + const result = await request>(`/api-order/coupon/${id}/use`, { + method: 'POST', + params: { orderNo }, + }); + return result; +} + +export async function createCouponFromCtrip(params: { + ctripCouponId: string; + userPhone: string; + couponName: string; + couponType: number; + amount: number; + minAmount?: number; + discountRate?: number; + validStartTime: string; + validEndTime: string; +}) { + const result = await request>('/api-order/coupon/ctrip/create', { + method: 'POST', + params, + }); + return result; +} + +export async function verifyCtripCoupon(ctripCouponId: string, orderNo: string) { + const result = await request>('/api-order/coupon/ctrip/verify', { + method: 'POST', + params: { ctripCouponId, orderNo }, + }); + return result; +} + +export async function updateExpiredCoupons() { + const result = await request>('/api-order/coupon/update-expired', { + method: 'POST', + }); + return result; +} \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/services/orders/typings.d.ts b/zlt-web/react-web/src/main/frontend/src/services/orders/typings.d.ts index 697101a1..e9adf72b 100644 --- a/zlt-web/react-web/src/main/frontend/src/services/orders/typings.d.ts +++ b/zlt-web/react-web/src/main/frontend/src/services/orders/typings.d.ts @@ -98,7 +98,31 @@ declare namespace ORDERS { approveReason: string; createTime?: string; tenantId?: string; + } + + type Coupon = { + id: number; + couponNo: string; + couponName: string; + couponType: number; // 1-满减券,2-折扣券,3-免费券 + amount: number; + minAmount?: number; + discountRate?: number; + status: number; // 0-未使用,1-已使用,2-已过期,3-已作废 + userPhone: string; + userId?: number; + source: number; // 1-携程,2-高飞自建 + ctripCouponId?: string; + validStartTime: string; + validEndTime: string; + usedTime?: string; + usedOrderNo?: string; + createTime?: string; updateTime?: string; + tenantId?: string; + createBy?: string; + updateBy?: string; + remark?: string; } // 添加 Dashboard 相关类型 diff --git a/优惠券管理功能说明.md b/优惠券管理功能说明.md new file mode 100644 index 00000000..859a0d10 --- /dev/null +++ b/优惠券管理功能说明.md @@ -0,0 +1,175 @@ +# 优惠券管理功能说明 + +## 功能概述 + +根据携程优惠券兑换流程图,实现了完整的优惠券管理功能,包括: + +1. **优惠券基础管理**:创建、编辑、删除、查询优惠券 +2. **携程集成**:携程创建优惠券接口、核销接口 +3. **用户优惠券查询**:根据手机号查询用户优惠券 +4. **自动过期处理**:定时任务自动更新过期优惠券状态 + +## 数据库设计 + +### 优惠券表 (t_coupon) + +| 字段名 | 类型 | 说明 | +|--------|------|------| +| id | bigint | 主键ID | +| coupon_no | varchar(64) | 优惠券编号(唯一) | +| coupon_name | varchar(128) | 优惠券名称 | +| coupon_type | int | 优惠券类型:1-满减券,2-折扣券,3-免费券 | +| amount | decimal(10,2) | 优惠券面额 | +| min_amount | decimal(10,2) | 使用门槛金额 | +| discount_rate | int | 折扣率(0-100,折扣券时使用) | +| status | int | 优惠券状态:0-未使用,1-已使用,2-已过期,3-已作废 | +| user_phone | varchar(20) | 用户手机号 | +| user_id | bigint | 用户ID | +| source | int | 来源:1-携程,2-高飞自建 | +| ctrip_coupon_id | varchar(64) | 携程优惠券ID(来源为携程时使用) | +| valid_start_time | datetime | 有效期开始时间 | +| valid_end_time | datetime | 有效期结束时间 | +| used_time | datetime | 使用时间 | +| used_order_no | varchar(64) | 使用订单号 | +| create_time | datetime | 创建时间 | +| update_time | datetime | 更新时间 | +| create_by | varchar(64) | 创建人 | +| update_by | varchar(64) | 更新人 | +| remark | varchar(255) | 备注 | +| tenant_id | varchar(64) | 租户ID | + +## 后端API接口 + +### 1. 优惠券基础管理 + +- `GET /api-order/coupon/page` - 分页查询优惠券列表 +- `GET /api-order/coupon/{id}` - 根据ID查询优惠券 +- `POST /api-order/coupon` - 创建优惠券 +- `PUT /api-order/coupon/{id}` - 更新优惠券 +- `DELETE /api-order/coupon/{id}` - 删除优惠券 + +### 2. 用户优惠券查询 + +- `GET /api-order/coupon/user/{userPhone}` - 根据手机号查询用户优惠券 +- `GET /api-order/coupon/user/{userPhone}/available` - 根据手机号查询可用优惠券 + +### 3. 优惠券使用 + +- `POST /api-order/coupon/{id}/use` - 使用优惠券 + +### 4. 携程集成接口 + +- `POST /api-order/coupon/ctrip/create` - 携程创建优惠券接口 +- `POST /api-order/coupon/ctrip/verify` - 核销携程优惠券 + +### 5. 系统管理 + +- `POST /api-order/coupon/update-expired` - 批量更新过期优惠券 + +## 前端功能 + +### 优惠券管理页面 + +位置:`/pages/orders/CouponManagement.tsx` + +功能: +- 优惠券列表展示(支持分页、筛选) +- 新增优惠券 +- 编辑优惠券 +- 删除优惠券 +- 查看优惠券详情 + +### 主要特性 + +1. **响应式表格**:支持横向滚动,适配不同屏幕尺寸 +2. **智能表单**:根据优惠券类型动态显示不同字段 +3. **状态管理**:不同状态用不同颜色标签显示 +4. **时间处理**:使用moment.js处理时间格式化 + +## 业务流程 + +### 1. 携程优惠券创建流程 + +``` +用户在携程购券 → 携程调用高飞创建优惠券接口 → 高飞创建本地优惠券记录 +``` + +### 2. 用户下单优惠券使用流程 + +``` +用户扫码进入下单页 → 查询用户绑定手机号 → 通过手机号查询高飞用户优惠券记录 → +有可用优惠券自动带出 → 用户选择是否核销 → 调用携程核销接口 → 进入支付流程 +``` + +### 3. 优惠券过期处理流程 + +``` +定时任务每天凌晨2点执行 → 查询所有未使用且已过期的优惠券 → 批量更新状态为已过期 +``` + +## 定时任务 + +### 优惠券过期处理任务 + +- **类名**:`CouponExpirationTask` +- **执行时间**:每天凌晨2点 +- **功能**:批量更新过期优惠券状态 + +## 携程API集成 + +### 携程API服务 + +- **接口**:`ICtripApiService` +- **实现**:`CtripApiServiceImpl` + +### 待实现功能 + +1. **核销接口**:`verifyCoupon(String ctripCouponId, String orderNo)` +2. **状态查询**:`getCouponStatus(String ctripCouponId)` + +### 集成说明 + +目前携程API调用为模拟实现,需要根据携程提供的API文档进行实际集成: + +1. 配置携程API密钥和接口地址 +2. 实现HTTP客户端调用 +3. 处理API响应和错误 +4. 添加重试机制和日志记录 + +## 部署说明 + +### 1. 数据库迁移 + +执行 `database_migration.sql` 中的优惠券表创建语句。 + +### 2. 后端部署 + +确保以下组件已正确配置: +- MyBatis Plus配置 +- 定时任务配置(@EnableScheduling) +- 数据库连接配置 + +### 3. 前端部署 + +确保以下依赖已安装: +- Ant Design +- moment.js +- TypeScript类型定义 + +## 注意事项 + +1. **优惠券编号唯一性**:系统自动生成优惠券编号,确保唯一性 +2. **携程优惠券ID唯一性**:同一携程优惠券ID只能创建一次 +3. **时间处理**:所有时间字段使用UTC时间存储 +4. **状态管理**:优惠券状态变更需要记录操作日志 +5. **API安全**:携程集成接口需要添加适当的认证和授权 + +## 扩展功能 + +### 可扩展的功能点 + +1. **优惠券模板**:支持创建优惠券模板,批量生成优惠券 +2. **优惠券统计**:优惠券使用情况统计和分析 +3. **优惠券推送**:向用户推送优惠券到期提醒 +4. **优惠券分享**:支持优惠券分享功能 +5. **多平台集成**:支持其他平台的优惠券集成 \ No newline at end of file