添加方法: 生成一个新的 VipTimeRangeJson

This commit is contained in:
liuchengqian 2023-02-15 17:26:23 +08:00
parent 47c94551fa
commit 7659e78e08
4 changed files with 93 additions and 51 deletions

View File

@ -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);

View File

@ -35,11 +35,6 @@ public interface SysUserDao extends JpaRepository<SysUserEntity, Long>, 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<SysUserEntity, Long>, 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);
/**
* 记录用户登录次数
*/

View File

@ -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<SysUserEntity> 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

View File

@ -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<VipTimeRangeBean.VipTimeRangeItemBean> list = new ArrayList<>();
list.add(vipTimeRangeItemBean);
VipTimeRangeBean vipTimeRangeBean = new VipTimeRangeBean();
vipTimeRangeBean.setList(list);
return JsonUtils.<VipTimeRangeBean>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<VipTimeRangeBean.VipTimeRangeItemBean> 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.<VipTimeRangeBean>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.<VipTimeRangeBean>serialize(vipTimeRangeBean);
} else {
throw new RuntimeException("设置的过期时间必须晚于当次的VIP开始时间");
}
}
}
public static LocalDateTime convert(String formattedTime) {
return LocalDateTime.parse(formattedTime, DateTimeUtils.DATE_TIME_FORMATTER_1);
}
}