From 7659e78e0830c2cfd33e15d65c332053c92fb436 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 15 Feb 2023 17:26:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=EF=BC=9A?= =?UTF-8?q?=20=E7=94=9F=E6=88=90=E4=B8=80=E4=B8=AA=E6=96=B0=E7=9A=84=20Vip?= =?UTF-8?q?TimeRangeJson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/controller/SysUserController.java | 9 +- .../java/com/xkrs/straw/dao/SysUserDao.java | 12 --- .../service/impl/SysUserServiceImpl.java | 38 --------- .../xkrs/straw/utils/VipTimeRangeUtils.java | 85 +++++++++++++++++++ 4 files changed, 93 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/xkrs/controller/SysUserController.java b/src/main/java/com/xkrs/controller/SysUserController.java index 6e01635..7371cd3 100644 --- a/src/main/java/com/xkrs/controller/SysUserController.java +++ b/src/main/java/com/xkrs/controller/SysUserController.java @@ -13,6 +13,7 @@ import com.xkrs.straw.dao.SysUserDao; import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.model.helper.SMSHelper; import com.xkrs.straw.service.SysUserService; +import com.xkrs.straw.utils.VipTimeRangeUtils; import com.xkrs.utils.AliYunSmsUtils; import com.xkrs.utils.ListUtils; import com.xkrs.utils.RandomUtil; @@ -29,6 +30,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; import javax.servlet.http.HttpServletRequest; +import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.TimeUnit; @@ -81,7 +83,12 @@ public class SysUserController { if (targetEntityOptional.isPresent()) { SysUserEntity targetEntity = targetEntityOptional.get(); if (!TextUtils.isEmpty(overTime)) { - targetEntity.setOverTime(overTime); + //获取LocalDateTime格式的过期时间 + LocalDateTime newLocalDateTime = VipTimeRangeUtils.convert(overTime); + //生成新的VIP时间范围Json内容 + String newVipTimeRangeJson = VipTimeRangeUtils.obtainNewVipTimeRangeJson(targetEntity.getVipTimeRangeJson(), newLocalDateTime); + //更新字段 + targetEntity.setVipTimeRangeJson(newVipTimeRangeJson); } if (!TextUtils.isEmpty(remark)) { targetEntity.setRemark(remark); diff --git a/src/main/java/com/xkrs/straw/dao/SysUserDao.java b/src/main/java/com/xkrs/straw/dao/SysUserDao.java index e28cd03..b0ded8e 100644 --- a/src/main/java/com/xkrs/straw/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/straw/dao/SysUserDao.java @@ -35,11 +35,6 @@ public interface SysUserDao extends JpaRepository, JpaSpeci @Query(value = "SELECT * FROM sys_user WHERE user_name = :userName", nativeQuery = true) SysUserEntity selectByUserName(@Param("userName") String userName); - /** - * 根据用户名查询用户信息 - */ - SysUserEntity findByUserName(String userName); - /** * 软删除系统用户根据id */ @@ -88,13 +83,6 @@ public interface SysUserDao extends JpaRepository, JpaSpeci @Modifying(clearAutomatically = true) void updateOverTime(Long userId, String overTime); - /** - * 将账户的期限修改为永久有效 - */ - @Query(value = "update sys_user set over_time = '永久' and day_num = 1 where id = ?", nativeQuery = true) - @Modifying(clearAutomatically = true) - void updateDayNum(Long userId); - /** * 记录用户登录次数 */ diff --git a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java index bb2de36..14d0ce5 100644 --- a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java @@ -62,9 +62,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 检查用户名是否存在 - * - * @param userName - * @return */ @Override public boolean checkUserName(String userName) { @@ -74,8 +71,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 新增用户 - * - * @param sysUserQo */ @Transactional(rollbackFor = Exception.class) @Override @@ -199,9 +194,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 更新用户 - * - * @param sysUserQo - * @return */ @Transactional(rollbackFor = Exception.class) @Override @@ -214,9 +206,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 软删除普通用户 - * - * @param id - * @return */ @Transactional(rollbackFor = Exception.class) @Override @@ -236,8 +225,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 查询用户的信息 - * - * @return */ @Override public List selectAllUser() { @@ -246,8 +233,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 启用 - * - * @param userId */ @Transactional(rollbackFor = Exception.class) @Override @@ -257,8 +242,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 禁用 - * - * @param userId */ @Transactional(rollbackFor = Exception.class) @Override @@ -268,12 +251,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 用户修改密码 - * - * @param oldPassword - * @param newPassword - * @param confirmPassword - * @param sysUserEntity - * @return */ @Transactional(rollbackFor = Exception.class) @Override @@ -292,11 +269,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 管理员修改用户的密码 - * - * @param userId - * @param newPassword - * @param confirmPassword - * @return */ @Transactional(rollbackFor = Exception.class) @Override @@ -325,10 +297,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 修改账号到期时间 - * - * @param userId - * @param overTime - * @return */ @Transactional(rollbackFor = Exception.class) @Override @@ -339,9 +307,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 记录用户登录次数 - * - * @param userId - * @param loginNum */ @Transactional(rollbackFor = Exception.class) @Override @@ -351,9 +316,6 @@ public class SysUserServiceImpl implements SysUserService { /** * 记录用户登录时间 - * - * @param userId - * @param loginLastTime */ @Transactional(rollbackFor = Exception.class) @Override diff --git a/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java b/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java index 7e58fac..ec244ab 100644 --- a/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java +++ b/src/main/java/com/xkrs/straw/utils/VipTimeRangeUtils.java @@ -99,5 +99,90 @@ public class VipTimeRangeUtils { return vipLocalDateTimeRangePairList; } + /** + * 生成一个新的 VipTimeRangeJson + */ + public static String obtainNewVipTimeRangeJson(String oldVipTimeRangeJson, LocalDateTime newLocalDateTime) { + //没有默认VIP时间范围的情况,是新注册用户 + if (TextUtils.isEmpty(oldVipTimeRangeJson)) { + LocalDateTime startLocalDateTime = LocalDateTime.now(); + if (startLocalDateTime.isBefore(newLocalDateTime)) { + VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean = new VipTimeRangeBean.VipTimeRangeItemBean(); + vipTimeRangeItemBean.setStartTime(DateTimeUtils.DATE_TIME_FORMATTER_1.format(startLocalDateTime)); + vipTimeRangeItemBean.setEndTime(DateTimeUtils.DATE_TIME_FORMATTER_1.format(newLocalDateTime)); + List list = new ArrayList<>(); + list.add(vipTimeRangeItemBean); + VipTimeRangeBean vipTimeRangeBean = new VipTimeRangeBean(); + vipTimeRangeBean.setList(list); + return JsonUtils.serialize(vipTimeRangeBean); + } else { + throw new RuntimeException("初始化的过期时间必须晚于当前时间"); + } + } + //oldVipTimeRangeJson不为空说明不是新注册用户,解析该用户的VIP时间范围 + VipTimeRangeBean vipTimeRangeBean = JsonUtils.deserialize(oldVipTimeRangeJson, VipTimeRangeBean.class); + if (vipTimeRangeBean == null || vipTimeRangeBean.getList() == null || vipTimeRangeBean.getList().size() == 0) { + throw new RuntimeException("已保存的VIP时间范围反序列化失败"); + } + List list = vipTimeRangeBean.getList(); + //校验数据合法性 + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanA : list) { + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBeanB : list) { + if (vipTimeRangeItemBeanA != vipTimeRangeItemBeanB) { + LocalDateTime startLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime endLocalDateTimeA = LocalDateTime.parse(vipTimeRangeItemBeanA.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime startLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime endLocalDateTimeB = LocalDateTime.parse(vipTimeRangeItemBeanB.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (startLocalDateTimeB.isBefore(startLocalDateTimeA) && endLocalDateTimeB.isAfter(startLocalDateTimeA)) { + throw new RuntimeException("VIP开始结束时间存在交叉现象1,系统数据错误"); + } + if (startLocalDateTimeB.isBefore(endLocalDateTimeA) && endLocalDateTimeB.isAfter(endLocalDateTimeA)) { + throw new RuntimeException("VIP开始结束时间存在交叉现象2,系统数据错误"); + } + } + } + } + //获取结束时间最大的那个Pair + VipTimeRangeBean.VipTimeRangeItemBean lastVipTimeRangeItemBean = list.get(0); + for (VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean : list) { + if (lastVipTimeRangeItemBean != vipTimeRangeItemBean) { + LocalDateTime endLocalDateTime = LocalDateTime.parse(vipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + LocalDateTime lastEndLocalDateTime = LocalDateTime.parse(lastVipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (endLocalDateTime.isAfter(lastEndLocalDateTime)) { + lastVipTimeRangeItemBean = vipTimeRangeItemBean; + } + } + } + //获取到结束时间最大的那个Pair,用它的结束时间和当前时间做比较 + LocalDateTime lastEndLocalDateTime = LocalDateTime.parse(lastVipTimeRangeItemBean.getEndTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (lastEndLocalDateTime.isBefore(LocalDateTime.now())) { + //最大结束时间早于当前时间,说明该用户已过期,是普通用户。 + //调整普通用户过期时间的方案是添加新的Pair,开始时间是当前时间,结束时间是传入的时间 + LocalDateTime startLocalDateTime = LocalDateTime.now(); + if (startLocalDateTime.isBefore(newLocalDateTime)) { + VipTimeRangeBean.VipTimeRangeItemBean vipTimeRangeItemBean = new VipTimeRangeBean.VipTimeRangeItemBean(); + vipTimeRangeItemBean.setStartTime(DateTimeUtils.DATE_TIME_FORMATTER_1.format(startLocalDateTime)); + vipTimeRangeItemBean.setEndTime(DateTimeUtils.DATE_TIME_FORMATTER_1.format(newLocalDateTime)); + list.add(vipTimeRangeItemBean); + return JsonUtils.serialize(vipTimeRangeBean); + } else { + throw new RuntimeException("为断更用户添加的过期时间必须晚于当前时间"); + } + } else { + //最大结束时间晚于当前时间,说明该用户未过期,是VIP用户。 + //调整VIP用户过期时间的方案是调整结束时间为传入的时间 + LocalDateTime startLocalDateTime = LocalDateTime.parse(lastVipTimeRangeItemBean.getStartTime(), DateTimeUtils.DATE_TIME_FORMATTER_1); + if (startLocalDateTime.isBefore(newLocalDateTime)) { + lastVipTimeRangeItemBean.setEndTime(DateTimeUtils.DATE_TIME_FORMATTER_1.format(newLocalDateTime)); + return JsonUtils.serialize(vipTimeRangeBean); + } else { + throw new RuntimeException("设置的过期时间必须晚于当次的VIP开始时间"); + } + } + } + + public static LocalDateTime convert(String formattedTime) { + return LocalDateTime.parse(formattedTime, DateTimeUtils.DATE_TIME_FORMATTER_1); + } }