From abe291342c4f2f1e216e225132d3e9b4f90dbb7e Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Fri, 1 Nov 2024 16:53:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E5=85=91=E6=8D=A2=E4=BC=98=E6=83=A0=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../address/ContactsController.java | 24 +++++++ .../controller/good/CouponController.java | 70 +++++++++++++++++++ .../heritage/mapper/ContactsMapper.java | 7 ++ .../heritage/mapper/ExchangeMapper.java | 7 ++ .../heritage/mapper/UserCouponMapper.java | 8 +++ .../dto/exchange/ExchangeAddRequest.java | 42 +++++++++++ .../heritage/model/entity/Address.java | 4 +- .../heritage/model/entity/Category.java | 4 +- .../heritage/model/entity/Contacts.java | 67 ++++++++++++++++++ .../heritage/model/entity/Coupon.java | 2 +- .../heritage/model/entity/Exchange.java | 67 ++++++++++++++++++ .../cultural/heritage/model/entity/Good.java | 2 +- .../cultural/heritage/model/entity/User.java | 2 +- .../heritage/model/entity/UserCoupon.java | 55 +++++++++++++++ .../service/address/ContactsService.java | 7 ++ .../address/impl/ContactsServiceImpl.java | 12 ++++ .../service/good/ExchangeService.java | 7 ++ .../service/good/UserCouponService.java | 7 ++ .../good/impl/ExchangeServiceImpl.java | 11 +++ .../good/impl/UserCouponServiceImpl.java | 11 +++ src/main/resources/mapper/ContactsMapper.xml | 7 ++ src/main/resources/mapper/ExchangeMapper.xml | 7 ++ .../resources/mapper/UserCouponMapper.xml | 7 ++ 23 files changed, 430 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/controller/address/ContactsController.java create mode 100644 src/main/java/com/cultural/heritage/mapper/ContactsMapper.java create mode 100644 src/main/java/com/cultural/heritage/mapper/ExchangeMapper.java create mode 100644 src/main/java/com/cultural/heritage/mapper/UserCouponMapper.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/Contacts.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/Exchange.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/UserCoupon.java create mode 100644 src/main/java/com/cultural/heritage/service/address/ContactsService.java create mode 100644 src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java create mode 100644 src/main/java/com/cultural/heritage/service/good/ExchangeService.java create mode 100644 src/main/java/com/cultural/heritage/service/good/UserCouponService.java create mode 100644 src/main/java/com/cultural/heritage/service/good/impl/ExchangeServiceImpl.java create mode 100644 src/main/java/com/cultural/heritage/service/good/impl/UserCouponServiceImpl.java create mode 100644 src/main/resources/mapper/ContactsMapper.xml create mode 100644 src/main/resources/mapper/ExchangeMapper.xml create mode 100644 src/main/resources/mapper/UserCouponMapper.xml diff --git a/src/main/java/com/cultural/heritage/controller/address/ContactsController.java b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java new file mode 100644 index 0000000..2088062 --- /dev/null +++ b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java @@ -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; + + + + +} diff --git a/src/main/java/com/cultural/heritage/controller/good/CouponController.java b/src/main/java/com/cultural/heritage/controller/good/CouponController.java index 3526030..600c00d 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CouponController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CouponController.java @@ -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 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 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, "删除兑换记录成功"); + } + } diff --git a/src/main/java/com/cultural/heritage/mapper/ContactsMapper.java b/src/main/java/com/cultural/heritage/mapper/ContactsMapper.java new file mode 100644 index 0000000..54a4c9e --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/ContactsMapper.java @@ -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 { +} diff --git a/src/main/java/com/cultural/heritage/mapper/ExchangeMapper.java b/src/main/java/com/cultural/heritage/mapper/ExchangeMapper.java new file mode 100644 index 0000000..5dd7b27 --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/ExchangeMapper.java @@ -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 { +} diff --git a/src/main/java/com/cultural/heritage/mapper/UserCouponMapper.java b/src/main/java/com/cultural/heritage/mapper/UserCouponMapper.java new file mode 100644 index 0000000..c153c23 --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/UserCouponMapper.java @@ -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 { + +} diff --git a/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java new file mode 100644 index 0000000..fbd027b --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/Address.java b/src/main/java/com/cultural/heritage/model/entity/Address.java index d781909..8052a46 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Address.java +++ b/src/main/java/com/cultural/heritage/model/entity/Address.java @@ -10,8 +10,8 @@ import java.io.Serializable; import java.util.Date; /** - * 地址表 - * @TableName address + * 地址 + * @TableName address */ @Data @TableName("address") diff --git a/src/main/java/com/cultural/heritage/model/entity/Category.java b/src/main/java/com/cultural/heritage/model/entity/Category.java index 2165be2..179ebb6 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Category.java +++ b/src/main/java/com/cultural/heritage/model/entity/Category.java @@ -11,8 +11,8 @@ import java.io.Serializable; import java.util.Date; /** - * 商品类别表 - * @TableName category + * 商品类别 + * @TableName category */ @Data @TableName("category") diff --git a/src/main/java/com/cultural/heritage/model/entity/Contacts.java b/src/main/java/com/cultural/heritage/model/entity/Contacts.java new file mode 100644 index 0000000..70ea6d6 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/Contacts.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/Coupon.java b/src/main/java/com/cultural/heritage/model/entity/Coupon.java index 4414792..a69cc44 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Coupon.java +++ b/src/main/java/com/cultural/heritage/model/entity/Coupon.java @@ -12,7 +12,7 @@ import java.io.Serializable; import java.util.Date; /** - * 优惠券列表 + * 优惠券 * @TableName coupon */ @Data diff --git a/src/main/java/com/cultural/heritage/model/entity/Exchange.java b/src/main/java/com/cultural/heritage/model/entity/Exchange.java new file mode 100644 index 0000000..84b7995 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/Exchange.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/Good.java b/src/main/java/com/cultural/heritage/model/entity/Good.java index 77d7113..9394c20 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Good.java +++ b/src/main/java/com/cultural/heritage/model/entity/Good.java @@ -11,7 +11,7 @@ import java.io.Serial; import java.util.Date; /** - * 商品表 + * 商品 * @TableName good */ @TableName(value = "good") diff --git a/src/main/java/com/cultural/heritage/model/entity/User.java b/src/main/java/com/cultural/heritage/model/entity/User.java index d8942f4..2c7524b 100644 --- a/src/main/java/com/cultural/heritage/model/entity/User.java +++ b/src/main/java/com/cultural/heritage/model/entity/User.java @@ -11,7 +11,7 @@ import java.io.Serializable; import java.util.Date; /** - * 用户表 + * 用户 * @TableName user */ @TableName(value = "user") diff --git a/src/main/java/com/cultural/heritage/model/entity/UserCoupon.java b/src/main/java/com/cultural/heritage/model/entity/UserCoupon.java new file mode 100644 index 0000000..11eee30 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/UserCoupon.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/service/address/ContactsService.java b/src/main/java/com/cultural/heritage/service/address/ContactsService.java new file mode 100644 index 0000000..d1121ad --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/address/ContactsService.java @@ -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 { +} diff --git a/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java b/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java new file mode 100644 index 0000000..bb6b82f --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java @@ -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 implements ContactsService { + +} diff --git a/src/main/java/com/cultural/heritage/service/good/ExchangeService.java b/src/main/java/com/cultural/heritage/service/good/ExchangeService.java new file mode 100644 index 0000000..d9e9bbd --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/ExchangeService.java @@ -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 { +} diff --git a/src/main/java/com/cultural/heritage/service/good/UserCouponService.java b/src/main/java/com/cultural/heritage/service/good/UserCouponService.java new file mode 100644 index 0000000..220bc28 --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/UserCouponService.java @@ -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 { +} diff --git a/src/main/java/com/cultural/heritage/service/good/impl/ExchangeServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/ExchangeServiceImpl.java new file mode 100644 index 0000000..9b9ea23 --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/impl/ExchangeServiceImpl.java @@ -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 implements ExchangeService{ +} diff --git a/src/main/java/com/cultural/heritage/service/good/impl/UserCouponServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/UserCouponServiceImpl.java new file mode 100644 index 0000000..82ff262 --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/good/impl/UserCouponServiceImpl.java @@ -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 implements UserCouponService { +} diff --git a/src/main/resources/mapper/ContactsMapper.xml b/src/main/resources/mapper/ContactsMapper.xml new file mode 100644 index 0000000..cddfd61 --- /dev/null +++ b/src/main/resources/mapper/ContactsMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/ExchangeMapper.xml b/src/main/resources/mapper/ExchangeMapper.xml new file mode 100644 index 0000000..aa91bdb --- /dev/null +++ b/src/main/resources/mapper/ExchangeMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/UserCouponMapper.xml b/src/main/resources/mapper/UserCouponMapper.xml new file mode 100644 index 0000000..109cd9e --- /dev/null +++ b/src/main/resources/mapper/UserCouponMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file