diff --git a/src/main/java/com/cultural/heritage/controller/address/AddressController.java b/src/main/java/com/cultural/heritage/controller/address/AddressController.java index b66ca9b..f841233 100644 --- a/src/main/java/com/cultural/heritage/controller/address/AddressController.java +++ b/src/main/java/com/cultural/heritage/controller/address/AddressController.java @@ -47,10 +47,9 @@ public class AddressController { } Address address = new Address(); BeanUtils.copyProperties(addressAddRequest, address); - boolean save = addressService.save(address); - if (!save) { - throw new BusinessException(ErrorCode.OPERATION_ERROR); - } + addressService.verifyIsDefault(address); + boolean result = addressService.save(address); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); } @@ -87,7 +86,7 @@ public class AddressController { } Address address = new Address(); BeanUtils.copyProperties(addressUpdateRequest, address); - System.out.println(address); + addressService.verifyIsDefault(address); boolean result = addressService.updateById(address); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); @@ -107,12 +106,7 @@ public class AddressController { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = addressQueryRequest.getId(); - QueryWrapper
addressQueryWrapper = new QueryWrapper<>(); - addressQueryWrapper.eq("userId", id); - List
list = addressService.list(addressQueryWrapper); - if (CollectionUtils.isEmpty(list)) { - list = new ArrayList<>(); - } + List
list = addressService.getUserAddressById(id); return ResultUtils.success(list); } diff --git a/src/main/java/com/cultural/heritage/controller/address/ContactsController.java b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java index 2088062..55ffccb 100644 --- a/src/main/java/com/cultural/heritage/controller/address/ContactsController.java +++ b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java @@ -1,13 +1,29 @@ package com.cultural.heritage.controller.address; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.cultural.heritage.common.BaseResponse; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.common.ResultUtils; +import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonRequest; +import com.cultural.heritage.model.dto.contacts.ContactsAddRequest; +import com.cultural.heritage.model.dto.contacts.ContactsUpdateRequest; +import com.cultural.heritage.model.entity.Contacts; import com.cultural.heritage.service.address.ContactsService; +import com.obs.services.internal.utils.RestUtils; import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.models.info.Contact; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("/contacts") @Slf4j @@ -19,6 +35,84 @@ public class ContactsController { private ContactsService contactsService; + /** + * 添加联系人 + * @param contactsAddRequest 联系人添加请求体 + * @return 是否添加成功 + */ + @PostMapping("/add") + public BaseResponse addContacts(@RequestBody ContactsAddRequest contactsAddRequest) { + if (contactsAddRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Contacts contacts = new Contacts(); + BeanUtils.copyProperties(contactsAddRequest, contacts); + contactsService.verifyIsDefault(contacts); + boolean result = contactsService.save(contacts); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + return ResultUtils.success(true); + } + + + + /** + * 更新联系人 + * @param contactsUpdateRequest 联系人更新请求体 + * @return 是否更新成功 + */ + @PostMapping("/update") + public BaseResponse updateContacts(@RequestBody ContactsUpdateRequest contactsUpdateRequest) { + if (contactsUpdateRequest == null || contactsUpdateRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Contacts contacts = new Contacts(); + BeanUtils.copyProperties(contactsUpdateRequest, contacts); + contactsService.verifyIsDefault(contacts); + boolean result = contactsService.updateById(contacts); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + return ResultUtils.success(true); + } + + + + + /** + * 删除联系人 + * @param contactsDeleteRequest 联系人删除请求体 + * @return 是否删除成功 + */ + @PostMapping("/delete") + public BaseResponse deleteContacts(@RequestBody CommonRequest contactsDeleteRequest) { + if (contactsDeleteRequest == null || contactsDeleteRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long id = contactsDeleteRequest.getId(); + boolean result = contactsService.removeById(id); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + return ResultUtils.success(true); + } + + + + /** + * 查询用户联系人信息 + * @param contactsQueryRequest 联系人查询请求体 + * @return 用户联系人列表 + */ + @PostMapping("/list") + public BaseResponse> listUserContacts(@RequestBody CommonRequest contactsQueryRequest) { + if (contactsQueryRequest == null || contactsQueryRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long id = contactsQueryRequest.getId(); + List list = contactsService.getUserAddressById(id); + return ResultUtils.success(list); + } + + + + + } 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 600c00d..d6eae3a 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CouponController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CouponController.java @@ -134,7 +134,7 @@ public class CouponController { * @return 是否兑换成功 */ @PostMapping("/exchange") - @Transactional + @Transactional(rollbackFor = Exception.class) public BaseResponse pointsExchangeCoupon(@RequestBody ExchangeAddRequest exchangeAddRequest) { if (exchangeAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); @@ -155,12 +155,35 @@ public class CouponController { user.setPoints(residual); boolean save = userService.updateById(user); ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR); + + // 更新优惠券的库存量 + Long couponId = exchangeAddRequest.getCouponId(); + Coupon coupon = couponService.getById(couponId); + if (coupon.getTotalNum() - quantity < 0) { + throw new BusinessException(ErrorCode.OPERATION_ERROR, "优惠券数量不足"); + } + coupon.setResidueNum(coupon.getTotalNum() - quantity); + boolean updateCoupon = couponService.updateById(coupon); + ThrowUtils.throwIf(!updateCoupon, ErrorCode.OPERATION_ERROR); + + // 校验用户限领量是否已达到上限 + Integer currentNum = couponService.verifyIsReachTheUpperLimit(userId, couponId); + int limitNum = 0; + if (currentNum == null) { + limitNum = quantity; + } else { + limitNum = currentNum + quantity; + } + if ( limitNum > coupon.getLimitNum()) { + throw new BusinessException(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); diff --git a/src/main/java/com/cultural/heritage/mapper/CouponMapper.java b/src/main/java/com/cultural/heritage/mapper/CouponMapper.java index b330c19..5c2b552 100644 --- a/src/main/java/com/cultural/heritage/mapper/CouponMapper.java +++ b/src/main/java/com/cultural/heritage/mapper/CouponMapper.java @@ -5,4 +5,8 @@ import com.cultural.heritage.model.entity.Coupon; public interface CouponMapper extends BaseMapper { + /** + * 校验用户限领量是否已达到上限 + */ + Integer verifyIsReachTheUpperLimit(Long userId, Long couponId); } diff --git a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java new file mode 100644 index 0000000..d14bd49 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java @@ -0,0 +1,40 @@ +package com.cultural.heritage.model.dto.contacts; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +/** + * 添加联系人请求体 + */ +@Data +public class ContactsAddRequest implements Serializable { + + + /** + * 联系人姓名 + */ + private String name; + + + /** + * 联系人手机号 + */ + private String phone; + + + /** + * 用户id + */ + private Long userId; + + /** + * 是否默认 + */ + private Integer isDefault; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java new file mode 100644 index 0000000..a554b42 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java @@ -0,0 +1,48 @@ +package com.cultural.heritage.model.dto.contacts; + +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 ContactsUpdateRequest implements Serializable { + + /** + * 联系人id + */ + private Long id; + + + /** + * 联系人姓名 + */ + private String name; + + + /** + * 联系人手机号 + */ + private String phone; + + + /** + * 用户id + */ + private Long userId; + + /** + * 是否默认 + */ + private Integer isDefault; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/service/address/AddressService.java b/src/main/java/com/cultural/heritage/service/address/AddressService.java index 442068e..3ed434d 100644 --- a/src/main/java/com/cultural/heritage/service/address/AddressService.java +++ b/src/main/java/com/cultural/heritage/service/address/AddressService.java @@ -3,6 +3,19 @@ package com.cultural.heritage.service.address; import com.baomidou.mybatisplus.extension.service.IService; import com.cultural.heritage.model.entity.Address; +import java.util.List; + public interface AddressService extends IService
{ + /** + * 校验是否添加或更新了默认地址 + */ + void verifyIsDefault(Address address); + + + /** + * 根据userId获取用户地址 + */ + List
getUserAddressById(Long id); + } diff --git a/src/main/java/com/cultural/heritage/service/address/ContactsService.java b/src/main/java/com/cultural/heritage/service/address/ContactsService.java index d1121ad..d3a49e4 100644 --- a/src/main/java/com/cultural/heritage/service/address/ContactsService.java +++ b/src/main/java/com/cultural/heritage/service/address/ContactsService.java @@ -1,7 +1,21 @@ package com.cultural.heritage.service.address; import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.entity.Address; import com.cultural.heritage.model.entity.Contacts; +import io.swagger.v3.oas.models.info.Contact; + +import java.util.List; public interface ContactsService extends IService { + /** + * 校验是否添加或更新了默认联系人 + */ + void verifyIsDefault(Contacts contacts); + + + /** + * 根据userId获取用户联系人 + */ + List getUserAddressById(Long id); } diff --git a/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java b/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java index 681c3fe..ae91a49 100644 --- a/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java @@ -1,12 +1,38 @@ package com.cultural.heritage.service.address.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.AddressMapper; import com.cultural.heritage.model.entity.Address; import com.cultural.heritage.service.address.AddressService; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class AddressServiceImpl extends ServiceImpl implements AddressService { + @Override + public void verifyIsDefault(Address address) { + Integer isDefault = address.getIsDefault(); + Long userId = address.getUserId(); + List
list = this.getUserAddressById(userId); + if (isDefault == 1) { + list.forEach(item -> item.setIsDefault(0)); + } + boolean result = this.updateBatchById(list); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + } + + @Override + public List
getUserAddressById(Long userId) { + QueryWrapper
queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + List
list = this.list(queryWrapper); + return list; + } + } 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 index bb6b82f..df90c72 100644 --- a/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java @@ -1,12 +1,37 @@ package com.cultural.heritage.service.address.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.ThrowUtils; 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; +import java.util.List; + @Service public class ContactsServiceImpl extends ServiceImpl implements ContactsService { + + @Override + public void verifyIsDefault(Contacts contacts) { + Integer isDefault = contacts.getIsDefault(); + Long userId = contacts.getUserId(); + List list = this.getUserAddressById(userId); + if (isDefault == 1) { + list.forEach(item -> item.setIsDefault(0)); + } + boolean result = this.updateBatchById(list); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + } + + @Override + public List getUserAddressById(Long userId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + List list = this.list(queryWrapper); + return list; + } } diff --git a/src/main/java/com/cultural/heritage/service/good/CouponService.java b/src/main/java/com/cultural/heritage/service/good/CouponService.java index aeca103..abe51bf 100644 --- a/src/main/java/com/cultural/heritage/service/good/CouponService.java +++ b/src/main/java/com/cultural/heritage/service/good/CouponService.java @@ -13,4 +13,9 @@ public interface CouponService extends IService { */ QueryWrapper getQueryWrapper(CouponQueryRequest couponQueryRequest); + + /** + * 校验用户限领量是否已达到上限 + */ + Integer verifyIsReachTheUpperLimit(Long userId, Long couponId); } diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CouponServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CouponServiceImpl.java index cbf60a1..3255780 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CouponServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CouponServiceImpl.java @@ -42,4 +42,9 @@ public class CouponServiceImpl extends ServiceImpl impleme sortField); return queryWrapper; } + + @Override + public Integer verifyIsReachTheUpperLimit(Long userId, Long couponId) { + return this.baseMapper.verifyIsReachTheUpperLimit(userId, couponId); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 82ff315..0fc2a7b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -31,6 +31,7 @@ mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: false + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: logic-delete-field: isDelete #全局逻辑删除的实体字段名 diff --git a/src/main/resources/mapper/CouponMapper.xml b/src/main/resources/mapper/CouponMapper.xml index abfbee1..a539068 100644 --- a/src/main/resources/mapper/CouponMapper.xml +++ b/src/main/resources/mapper/CouponMapper.xml @@ -3,5 +3,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + \ No newline at end of file