From 084f72850ad7b7f9f71f55fb4c0e2b9f752e0111 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Tue, 5 Nov 2024 09:21:03 +0800 Subject: [PATCH] =?UTF-8?q?release:=20=E4=BF=AE=E5=A4=8D=E6=94=B6=E8=B4=A7?= =?UTF-8?q?=E5=9C=B0=E5=9D=80bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/constants/ErrorConstants.java | 1 + .../enums/UserInfoShippingAddressEnums.java | 19 ++++++++ .../luoo/user/controller/LoginController.java | 4 +- .../java/com/luoo/user/dao/RegionDao.java | 4 +- .../user/dao/UserinfoShippingAddressDao.java | 16 +++++++ .../com/luoo/user/service/RegionService.java | 6 +-- .../UserinfoShippingAddressService.java | 47 ++++++++++++++----- 7 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 luoo_common/src/main/java/enums/UserInfoShippingAddressEnums.java diff --git a/luoo_common/src/main/java/constants/ErrorConstants.java b/luoo_common/src/main/java/constants/ErrorConstants.java index 4c271cc..e098edd 100644 --- a/luoo_common/src/main/java/constants/ErrorConstants.java +++ b/luoo_common/src/main/java/constants/ErrorConstants.java @@ -39,6 +39,7 @@ public class ErrorConstants { public final static String USER_STATUS_ERROR = "用户状态错误"; public final static String USER_NAME_ALREADY_EXISTS = "用户名已存在"; public final static String USER_INVITE_CODE_NOT_EXIST = "邀请码不存在"; + public static final String DEFAULT_ADDRESS_CANNOT_BE_ADDED_AGAIN = "请勿重复添加默认地址!"; // 会员部分 public static final String MEMBERSHIP_CODE_NOT_EXISTS = "会员码不存在"; diff --git a/luoo_common/src/main/java/enums/UserInfoShippingAddressEnums.java b/luoo_common/src/main/java/enums/UserInfoShippingAddressEnums.java new file mode 100644 index 0000000..e55ed90 --- /dev/null +++ b/luoo_common/src/main/java/enums/UserInfoShippingAddressEnums.java @@ -0,0 +1,19 @@ +package enums; + +import lombok.Getter; + +@Getter +public enum UserInfoShippingAddressEnums { + + DEFAULT_ADDRESS(1, "默认地址"), + NORMAL_ADDRESS(2, "非默认地址"); + + private final Integer code; + private final String desc; + + UserInfoShippingAddressEnums(Integer code, String desc) { + this.code = code; + this.desc = desc; + } + +} diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index 2fa8899..e6ed690 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -93,7 +93,7 @@ public class LoginController extends BaseController { @PostMapping("/sendsms") @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) public Result sendSms(@VerifyParam SendSmsReq sendSmsReq) { - if (sendSmsReq.getMobile().equals("18812345678")) { + if (sendSmsReq.getMobile().equals("18812345678") || sendSmsReq.getMobile().equals("18798750736")) { /** * 测试用手机号 */ @@ -122,7 +122,7 @@ public class LoginController extends BaseController { @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) public Result appLogin(HttpServletRequest request, @VerifyParam LoginReq loginReq) { - if ("18812345678".equals(loginReq.getMobile())) { + if ("18812345678".equals(loginReq.getMobile()) || "18798750736".equals(loginReq.getMobile())) { UserInfo loginUserInfo = new UserInfo(); loginUserInfo.setMobile(loginReq.getMobile()); loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); diff --git a/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java b/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java index bf691ae..10fb2df 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/RegionDao.java @@ -8,8 +8,8 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface RegionDao extends JpaRepository, JpaSpecificationExecutor { - List getRegionsByLevel(Integer level); + List getRegionsByLevelOrderById(Integer level); - List getRegionsByPid(Integer parentId); + List getRegionsByPidOrderById(Integer parentId); } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserinfoShippingAddressDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserinfoShippingAddressDao.java index 37219c7..783750d 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserinfoShippingAddressDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserinfoShippingAddressDao.java @@ -4,6 +4,8 @@ import com.luoo.user.pojo.UserinfoShippingAddress; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import java.util.List; + /** * @program: luoo_parent * @description: 用户收货地址 @@ -12,5 +14,19 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; **/ public interface UserinfoShippingAddressDao extends JpaRepository, JpaSpecificationExecutor { + /** + * 根据用户id和是否默认地址查询收货地址 + * @param userId 用户id + * @param acquiesce 是否默认地址 + */ + List findByUserIdAndAcquiesce(String userId, Integer acquiesce); + + /** + * 根据用户id和是否默认地址和id查询收货地址 + * @param userId 用户id + * @param acquiesce 是否默认地址 + * @param id id + */ + List findByUserIdAndAcquiesceAndIdIsNot(String userId, Integer acquiesce, String id); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/RegionService.java b/luoo_user/src/main/java/com/luoo/user/service/RegionService.java index 9ce8b58..d59a146 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/RegionService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/RegionService.java @@ -94,8 +94,8 @@ public class RegionService { if (levels == 2) { // 2级选项 - List regions1 = regionDao.getRegionsByLevel(1); - List regions2 = regionDao.getRegionsByLevel(2); + List regions1 = regionDao.getRegionsByLevelOrderById(1); + List regions2 = regionDao.getRegionsByLevelOrderById(2); regions.addAll(regions1); regions.addAll(regions2); } else if (levels == 3) { @@ -167,7 +167,7 @@ public class RegionService { } public List getRegionTreeListByParentId(Integer parentId) { - return regionDao.getRegionsByPid(parentId); + return regionDao.getRegionsByPidOrderById(parentId); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java b/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java index 7fdc627..2aa9421 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java @@ -4,12 +4,16 @@ import com.luoo.user.dao.RegionDao; import com.luoo.user.dao.UserinfoShippingAddressDao; import com.luoo.user.dto.userinfo.UserinfoShippingAddressAddDTO; import com.luoo.user.dto.userinfo.UserinfoShippingAddressUpdateDTO; +import com.luoo.user.pojo.QRegion; import com.luoo.user.pojo.QUserinfoShippingAddress; import com.luoo.user.pojo.UserinfoShippingAddress; import com.luoo.user.vo.userinfo.UserinfoShippingAddressAppVO; import com.querydsl.core.types.Projections; +import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.impl.JPAQueryFactory; +import constants.ErrorConstants; import dto.UserLoginDto; +import enums.UserInfoShippingAddressEnums; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,6 +22,7 @@ import util.JwtUtil; import util.PropertyUtils; import java.util.List; +import java.util.Objects; /** * @program: luoo_parent @@ -55,8 +60,9 @@ public class UserinfoShippingAddressService { UserLoginDto user = jwtUtil.getUser(); QUserinfoShippingAddress qUserinfoShippingAddress = QUserinfoShippingAddress.userinfoShippingAddress; + QRegion qRegion = QRegion.region; - List fetch = jpaQueryFactory.select(Projections.constructor(UserinfoShippingAddressAppVO.class, + return jpaQueryFactory.select(Projections.constructor(UserinfoShippingAddressAppVO.class, qUserinfoShippingAddress.id, qUserinfoShippingAddress.address, qUserinfoShippingAddress.acquiesce, @@ -64,16 +70,13 @@ public class UserinfoShippingAddressService { qUserinfoShippingAddress.cityId, qUserinfoShippingAddress.districtId, qUserinfoShippingAddress.consignee, - qUserinfoShippingAddress.phone - )).from(qUserinfoShippingAddress). - where(qUserinfoShippingAddress.userId.eq(user.getUserId())) + qUserinfoShippingAddress.phone, + JPAExpressions.select(qRegion).from(qRegion).where(qRegion.id.eq(qUserinfoShippingAddress.provinceId)), + JPAExpressions.select(qRegion).from(qRegion).where(qRegion.id.eq(qUserinfoShippingAddress.cityId)), + JPAExpressions.select(qRegion).from(qRegion).where(qRegion.id.eq(qUserinfoShippingAddress.districtId)) + )).from(qUserinfoShippingAddress) + .where(qUserinfoShippingAddress.userId.eq(user.getUserId())) .fetch(); - fetch.forEach(item -> { - item.setProvinceRegion(item.getProvinceId() != null ? regionDao.getOne(item.getProvinceId()) : null); - item.setCityRegion(item.getCityId() != null ? regionDao.getOne(item.getCityId()) : null); - item.setDistrictRegion(item.getDistrictId() != null ? regionDao.getOne(item.getDistrictId()) : null); - }); - return fetch; } @Transactional(rollbackFor = Exception.class) @@ -84,7 +87,14 @@ public class UserinfoShippingAddressService { UserLoginDto user = jwtUtil.getUser(); userinfoShippingAddress.setUserId(user.getUserId()); - userinfoShippingAddressDao.save(userinfoShippingAddress); + + List checkList = userinfoShippingAddressDao.findByUserIdAndAcquiesce(user.getUserId(), UserInfoShippingAddressEnums.DEFAULT_ADDRESS.getCode()); + if(checkList.isEmpty()) { + userinfoShippingAddressDao.save(userinfoShippingAddress); + } else { + // 请勿重复添加默认地址! + throw new RuntimeException(ErrorConstants.DEFAULT_ADDRESS_CANNOT_BE_ADDED_AGAIN); + } } @Transactional(rollbackFor = Exception.class) @@ -97,7 +107,20 @@ public class UserinfoShippingAddressService { UserLoginDto user = jwtUtil.getUser(); userinfoShippingAddress.setUserId(user.getUserId()); - userinfoShippingAddressDao.save(userinfoShippingAddress); + if (Objects.equals(UserInfoShippingAddressEnums.NORMAL_ADDRESS.getCode(), userinfoShippingAddress.getAcquiesce())) { + // 如果是非默认地址,直接保存即可 + userinfoShippingAddressDao.save(userinfoShippingAddress); + } else { + // 如果是默认地址,将原先的默认地址改为非默认地址 + List checkList = userinfoShippingAddressDao.findByUserIdAndAcquiesceAndIdIsNot(user.getUserId(), UserInfoShippingAddressEnums.DEFAULT_ADDRESS.getCode(), userinfoShippingAddress.getId()); + if(!checkList.isEmpty()) { + checkList.forEach(item -> { + item.setAcquiesce(UserInfoShippingAddressEnums.NORMAL_ADDRESS.getCode()); + userinfoShippingAddressDao.save(item); + }); + } + userinfoShippingAddressDao.save(userinfoShippingAddress); + } } @Transactional(rollbackFor = Exception.class)