fix(优惠券管理):新增优惠券管理功能模块
This commit is contained in:
+178
@@ -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<IPage<Coupon>> 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<Coupon> 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<Coupon> result = couponService.getCouponPage(page, coupon);
|
||||
return Result.succeed(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查询优惠券
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "根据ID查询优惠券")
|
||||
public Result<Coupon> getCouponById(@Parameter(description = "优惠券ID") @PathVariable Long id) {
|
||||
Coupon coupon = couponService.getById(id);
|
||||
return Result.succeed(coupon);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建优惠券
|
||||
*/
|
||||
@PostMapping
|
||||
@Operation(summary = "创建优惠券")
|
||||
public Result<Boolean> createCoupon(@RequestBody Coupon coupon) {
|
||||
boolean result = couponService.createCoupon(coupon);
|
||||
return Result.succeed(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新优惠券
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
@Operation(summary = "更新优惠券")
|
||||
public Result<Boolean> 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<Boolean> deleteCoupon(@Parameter(description = "优惠券ID") @PathVariable Long id) {
|
||||
boolean result = couponService.deleteCoupon(id);
|
||||
return Result.succeed(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据手机号查询用户优惠券
|
||||
*/
|
||||
@GetMapping("/user/{userPhone}")
|
||||
@Operation(summary = "根据手机号查询用户优惠券")
|
||||
public Result<List<Coupon>> getCouponsByUserPhone(@Parameter(description = "用户手机号") @PathVariable String userPhone) {
|
||||
List<Coupon> coupons = couponService.getCouponsByUserPhone(userPhone);
|
||||
return Result.succeed(coupons);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据手机号查询可用优惠券
|
||||
*/
|
||||
@GetMapping("/user/{userPhone}/available")
|
||||
@Operation(summary = "根据手机号查询可用优惠券")
|
||||
public Result<List<Coupon>> getAvailableCouponsByUserPhone(@Parameter(description = "用户手机号") @PathVariable String userPhone) {
|
||||
List<Coupon> coupons = couponService.getAvailableCouponsByUserPhone(userPhone);
|
||||
return Result.succeed(coupons);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用优惠券
|
||||
*/
|
||||
@PostMapping("/{id}/use")
|
||||
@Operation(summary = "使用优惠券")
|
||||
public Result<Boolean> 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<Boolean> 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<Boolean> 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<Void> updateExpiredCoupons() {
|
||||
couponService.updateExpiredCoupons();
|
||||
return Result.succeed("批量更新过期优惠券成功");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<Coupon> {
|
||||
|
||||
/**
|
||||
* 分页查询优惠券列表
|
||||
*/
|
||||
IPage<Coupon> selectCouponPage(Page<Coupon> page, @Param("coupon") Coupon coupon);
|
||||
|
||||
/**
|
||||
* 根据手机号查询用户优惠券
|
||||
*/
|
||||
List<Coupon> selectByUserPhone(@Param("userPhone") String userPhone);
|
||||
|
||||
/**
|
||||
* 根据手机号查询可用优惠券
|
||||
*/
|
||||
List<Coupon> 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();
|
||||
}
|
||||
@@ -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<OptionItem> tourismList;
|
||||
|
||||
@Schema(description = "二维码列表")
|
||||
private List<OptionItem> qrcodeList;
|
||||
|
||||
@Schema(description = "设备列表")
|
||||
private List<OptionItem> deviceList;
|
||||
|
||||
@Data
|
||||
@Schema(description = "选项项")
|
||||
public static class OptionItem {
|
||||
@Schema(description = "标签")
|
||||
private String label;
|
||||
|
||||
@Schema(description = "值")
|
||||
private Object value;
|
||||
}
|
||||
}
|
||||
@@ -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<Coupon> {
|
||||
|
||||
/**
|
||||
* 分页查询优惠券列表
|
||||
*/
|
||||
IPage<Coupon> getCouponPage(Page<Coupon> page, Coupon coupon);
|
||||
|
||||
/**
|
||||
* 创建优惠券
|
||||
*/
|
||||
boolean createCoupon(Coupon coupon);
|
||||
|
||||
/**
|
||||
* 更新优惠券
|
||||
*/
|
||||
boolean updateCoupon(Coupon coupon);
|
||||
|
||||
/**
|
||||
* 删除优惠券
|
||||
*/
|
||||
boolean deleteCoupon(Long id);
|
||||
|
||||
/**
|
||||
* 根据手机号查询用户优惠券
|
||||
*/
|
||||
List<Coupon> getCouponsByUserPhone(String userPhone);
|
||||
|
||||
/**
|
||||
* 根据手机号查询可用优惠券
|
||||
*/
|
||||
List<Coupon> 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);
|
||||
}
|
||||
+22
@@ -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);
|
||||
}
|
||||
+184
@@ -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<CouponMapper, Coupon> implements ICouponService {
|
||||
|
||||
@Autowired
|
||||
private ICtripApiService ctripApiService;
|
||||
|
||||
@Override
|
||||
public IPage<Coupon> getCouponPage(Page<Coupon> 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<Coupon> getCouponsByUserPhone(String userPhone) {
|
||||
return baseMapper.selectByUserPhone(userPhone);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Coupon> 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+51
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.order.mapper.CouponMapper">
|
||||
|
||||
<!-- 基础结果映射 -->
|
||||
<resultMap id="BaseResultMap" type="com.central.order.entity.Coupon">
|
||||
<id column="id" property="id" jdbcType="BIGINT"/>
|
||||
<result column="coupon_no" property="couponNo" jdbcType="VARCHAR"/>
|
||||
<result column="coupon_name" property="couponName" jdbcType="VARCHAR"/>
|
||||
<result column="coupon_type" property="couponType" jdbcType="INTEGER"/>
|
||||
<result column="amount" property="amount" jdbcType="DECIMAL"/>
|
||||
<result column="min_amount" property="minAmount" jdbcType="DECIMAL"/>
|
||||
<result column="discount_rate" property="discountRate" jdbcType="INTEGER"/>
|
||||
<result column="status" property="status" jdbcType="INTEGER"/>
|
||||
<result column="user_phone" property="userPhone" jdbcType="VARCHAR"/>
|
||||
<result column="user_id" property="userId" jdbcType="BIGINT"/>
|
||||
<result column="source" property="source" jdbcType="INTEGER"/>
|
||||
<result column="ctrip_coupon_id" property="ctripCouponId" jdbcType="VARCHAR"/>
|
||||
<result column="valid_start_time" property="validStartTime" jdbcType="TIMESTAMP"/>
|
||||
<result column="valid_end_time" property="validEndTime" jdbcType="TIMESTAMP"/>
|
||||
<result column="used_time" property="usedTime" jdbcType="TIMESTAMP"/>
|
||||
<result column="used_order_no" property="usedOrderNo" jdbcType="VARCHAR"/>
|
||||
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
|
||||
<result column="create_by" property="createBy" jdbcType="VARCHAR"/>
|
||||
<result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
|
||||
<result column="remark" property="remark" jdbcType="VARCHAR"/>
|
||||
<result column="tenant_id" property="tenantId" jdbcType="VARCHAR"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 基础字段 -->
|
||||
<sql id="Base_Column_List">
|
||||
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
|
||||
</sql>
|
||||
|
||||
<!-- 分页查询优惠券列表 -->
|
||||
<select id="selectCouponPage" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM t_coupon
|
||||
<where>
|
||||
<if test="coupon.couponName != null and coupon.couponName != ''">
|
||||
AND coupon_name LIKE CONCAT('%', #{coupon.couponName}, '%')
|
||||
</if>
|
||||
<if test="coupon.couponType != null">
|
||||
AND coupon_type = #{coupon.couponType}
|
||||
</if>
|
||||
<if test="coupon.status != null">
|
||||
AND status = #{coupon.status}
|
||||
</if>
|
||||
<if test="coupon.userPhone != null and coupon.userPhone != ''">
|
||||
AND user_phone = #{coupon.userPhone}
|
||||
</if>
|
||||
<if test="coupon.source != null">
|
||||
AND source = #{coupon.source}
|
||||
</if>
|
||||
<if test="coupon.ctripCouponId != null and coupon.ctripCouponId != ''">
|
||||
AND ctrip_coupon_id = #{coupon.ctripCouponId}
|
||||
</if>
|
||||
</where>
|
||||
ORDER BY create_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 根据手机号查询用户优惠券 -->
|
||||
<select id="selectByUserPhone" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM t_coupon
|
||||
WHERE user_phone = #{userPhone}
|
||||
ORDER BY create_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 根据手机号查询可用优惠券 -->
|
||||
<select id="selectAvailableByUserPhone" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM t_coupon
|
||||
WHERE user_phone = #{userPhone}
|
||||
AND status = 0
|
||||
AND valid_start_time <= NOW()
|
||||
AND valid_end_time >= NOW()
|
||||
ORDER BY create_time DESC
|
||||
</select>
|
||||
|
||||
<!-- 根据携程优惠券ID查询 -->
|
||||
<select id="selectByCtripCouponId" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM t_coupon
|
||||
WHERE ctrip_coupon_id = #{ctripCouponId}
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<!-- 根据订单号查询使用的优惠券 -->
|
||||
<select id="selectByUsedOrderNo" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
<include refid="Base_Column_List"/>
|
||||
FROM t_coupon
|
||||
WHERE used_order_no = #{usedOrderNo}
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<!-- 更新优惠券状态为已使用 -->
|
||||
<update id="updateCouponUsed">
|
||||
UPDATE t_coupon
|
||||
SET status = 1,
|
||||
used_time = NOW(),
|
||||
used_order_no = #{usedOrderNo},
|
||||
update_time = NOW()
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<!-- 批量更新过期优惠券状态 -->
|
||||
<update id="updateExpiredCoupons">
|
||||
UPDATE t_coupon
|
||||
SET status = 2,
|
||||
update_time = NOW()
|
||||
WHERE status = 0
|
||||
AND valid_end_time < NOW()
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user