完善了积分兑换优惠券

This commit is contained in:
chen-xin-zhi 2024-11-01 16:53:34 +08:00
parent 11ee6f6c15
commit abe291342c
23 changed files with 430 additions and 7 deletions

View File

@ -0,0 +1,24 @@
package com.cultural.heritage.controller.address;
import com.cultural.heritage.service.address.ContactsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/contacts")
@Slf4j
@Tag(name = "联系人接口")
public class ContactsController {
@Resource
private ContactsService contactsService;
}

View File

@ -12,13 +12,20 @@ import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.coupon.CouponAddRequest;
import com.cultural.heritage.model.dto.coupon.CouponQueryRequest;
import com.cultural.heritage.model.dto.coupon.CouponUpdateRequest;
import com.cultural.heritage.model.dto.exchange.ExchangeAddRequest;
import com.cultural.heritage.model.entity.Coupon;
import com.cultural.heritage.model.entity.Exchange;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.entity.UserCoupon;
import com.cultural.heritage.service.good.CouponService;
import com.cultural.heritage.service.good.ExchangeService;
import com.cultural.heritage.service.good.UserCouponService;
import com.cultural.heritage.service.user.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.xml.transform.Result;
@ -39,6 +46,14 @@ public class CouponController {
private UserService userService;
@Resource
private UserCouponService userCouponService;
@Resource
private ExchangeService exchangeService;
/**
* 添加优惠券
* @param couponAddRequest 优惠券添加请求体
@ -113,8 +128,63 @@ public class CouponController {
}
/**
* 积分兑换优惠券
* @param exchangeAddRequest 兑换记录添加请求体
* @return 是否兑换成功
*/
@PostMapping("/exchange")
@Transactional
public BaseResponse<Boolean> pointsExchangeCoupon(@RequestBody ExchangeAddRequest exchangeAddRequest) {
if (exchangeAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 更新用户积分
Long userId = exchangeAddRequest.getUserId();
Integer requirePoints = exchangeAddRequest.getRequirePoints();
Integer quantity = exchangeAddRequest.getQuantity();
if (userId <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
User user = userService.getById(userId);
ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
int residual = user.getPoints() - requirePoints * quantity;
if (residual < 0) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "积分不足");
}
user.setPoints(residual);
boolean save = userService.updateById(user);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
// 插入一条兑换记录
Exchange exchange = new Exchange();
BeanUtils.copyProperties(exchangeAddRequest, exchange);
boolean result = exchangeService.save(exchange);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
//给当前用户添加优惠券
UserCoupon userCoupon = new UserCoupon();
BeanUtils.copyProperties(exchangeAddRequest, userCoupon);
boolean couponResult = userCouponService.save(userCoupon);
ThrowUtils.throwIf(!couponResult, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "兑换成功");
}
/**
* 用户删除兑换记录
* @param commonRequest 兑换记录删除请求体
* @return 是否兑换成功
*/
@PostMapping("/delRecord")
public BaseResponse<Boolean> deleteExchangeRecord(@RequestBody CommonRequest commonRequest) {
if (commonRequest == null || commonRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = commonRequest.getId();
boolean result = exchangeService.removeById(id);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "删除兑换记录成功");
}
}

View File

@ -0,0 +1,7 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.Contacts;
public interface ContactsMapper extends BaseMapper<Contacts> {
}

View File

@ -0,0 +1,7 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.Exchange;
public interface ExchangeMapper extends BaseMapper<Exchange> {
}

View File

@ -0,0 +1,8 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.UserCoupon;
public interface UserCouponMapper extends BaseMapper<UserCoupon> {
}

View File

@ -0,0 +1,42 @@
package com.cultural.heritage.model.dto.exchange;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 积分兑换优惠券请求
*/
@Data
public class ExchangeAddRequest implements Serializable {
/**
* 用户id
*/
private Long userId;
/**
* 优惠券id
*/
private Long couponId;
/**
* 数量
*/
private Integer quantity;
/**
* 优惠券积分价格
*/
private Integer requirePoints;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -10,8 +10,8 @@ import java.io.Serializable;
import java.util.Date;
/**
* 地址
* @TableName address
* 地址
* @TableName address
*/
@Data
@TableName("address")

View File

@ -11,8 +11,8 @@ import java.io.Serializable;
import java.util.Date;
/**
* 商品类别
* @TableName category
* 商品类别
* @TableName category
*/
@Data
@TableName("category")

View File

@ -0,0 +1,67 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 联系人
* @TableName contacts
*/
@Data
@TableName("contacts")
public class Contacts implements Serializable {
/**
* 联系人id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 联系人姓名
*/
private String name;
/**
* 联系人手机号
*/
private String phone;
/**
* 用户id
*/
private Long userId;
/**
* 是否默认
*/
private Integer isDefault;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 是否删除
*/
private Integer isDelete;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -12,7 +12,7 @@ import java.io.Serializable;
import java.util.Date;
/**
* 优惠券列表
* 优惠券
* @TableName coupon
*/
@Data

View File

@ -0,0 +1,67 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 积分兑换记录
* @TableName exchange_record
*/
@Data
@TableName("exchange_record")
public class Exchange implements Serializable {
/**
* 兑换记录id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 优惠券id
*/
private Long couponId;
/**
* 数量
*/
private Integer quantity;
/**
* 优惠券积分价格
*/
private Integer requirePoints;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 是否删除
*/
private Integer isDelete;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -11,7 +11,7 @@ import java.io.Serial;
import java.util.Date;
/**
* 商品
* 商品
* @TableName good
*/
@TableName(value = "good")

View File

@ -11,7 +11,7 @@ import java.io.Serializable;
import java.util.Date;
/**
* 用户
* 用户
* @TableName user
*/
@TableName(value = "user")

View File

@ -0,0 +1,55 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 用户优惠券表
* @TableName user_coupon
*/
@Data
@TableName("user_coupon")
public class UserCoupon implements Serializable {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 优惠券id
*/
private Long couponId;
/**
* 优惠券状态(0, 1, 2分别代表未使用已使用已过期)
*/
private Integer status;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,7 @@
package com.cultural.heritage.service.address;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.Contacts;
public interface ContactsService extends IService<Contacts> {
}

View File

@ -0,0 +1,12 @@
package com.cultural.heritage.service.address.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.ContactsMapper;
import com.cultural.heritage.model.entity.Contacts;
import com.cultural.heritage.service.address.ContactsService;
import org.springframework.stereotype.Service;
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService {
}

View File

@ -0,0 +1,7 @@
package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.Exchange;
public interface ExchangeService extends IService<Exchange> {
}

View File

@ -0,0 +1,7 @@
package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.UserCoupon;
public interface UserCouponService extends IService<UserCoupon> {
}

View File

@ -0,0 +1,11 @@
package com.cultural.heritage.service.good.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.ExchangeMapper;
import com.cultural.heritage.model.entity.Exchange;
import com.cultural.heritage.service.good.ExchangeService;
import org.springframework.stereotype.Service;
@Service
public class ExchangeServiceImpl extends ServiceImpl<ExchangeMapper, Exchange> implements ExchangeService{
}

View File

@ -0,0 +1,11 @@
package com.cultural.heritage.service.good.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.UserCouponMapper;
import com.cultural.heritage.model.entity.UserCoupon;
import com.cultural.heritage.service.good.UserCouponService;
import org.springframework.stereotype.Service;
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {
}

View File

@ -0,0 +1,7 @@
<?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.cultural.heritage.mapper.ContactsMapper">
</mapper>

View File

@ -0,0 +1,7 @@
<?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.cultural.heritage.mapper.ExchangeMapper">
</mapper>

View File

@ -0,0 +1,7 @@
<?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.cultural.heritage.mapper.UserCouponMapper">
</mapper>