添加方法: 生成一个新的 VipTimeRangeJson
This commit is contained in:
parent
47c94551fa
commit
7659e78e08
@ -13,6 +13,7 @@ import com.xkrs.straw.dao.SysUserDao;
|
|||||||
import com.xkrs.straw.model.entity.SysUserEntity;
|
import com.xkrs.straw.model.entity.SysUserEntity;
|
||||||
import com.xkrs.straw.model.helper.SMSHelper;
|
import com.xkrs.straw.model.helper.SMSHelper;
|
||||||
import com.xkrs.straw.service.SysUserService;
|
import com.xkrs.straw.service.SysUserService;
|
||||||
|
import com.xkrs.straw.utils.VipTimeRangeUtils;
|
||||||
import com.xkrs.utils.AliYunSmsUtils;
|
import com.xkrs.utils.AliYunSmsUtils;
|
||||||
import com.xkrs.utils.ListUtils;
|
import com.xkrs.utils.ListUtils;
|
||||||
import com.xkrs.utils.RandomUtil;
|
import com.xkrs.utils.RandomUtil;
|
||||||
@ -29,6 +30,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ -81,7 +83,12 @@ public class SysUserController {
|
|||||||
if (targetEntityOptional.isPresent()) {
|
if (targetEntityOptional.isPresent()) {
|
||||||
SysUserEntity targetEntity = targetEntityOptional.get();
|
SysUserEntity targetEntity = targetEntityOptional.get();
|
||||||
if (!TextUtils.isEmpty(overTime)) {
|
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)) {
|
if (!TextUtils.isEmpty(remark)) {
|
||||||
targetEntity.setRemark(remark);
|
targetEntity.setRemark(remark);
|
||||||
|
@ -35,11 +35,6 @@ public interface SysUserDao extends JpaRepository<SysUserEntity, Long>, JpaSpeci
|
|||||||
@Query(value = "SELECT * FROM sys_user WHERE user_name = :userName", nativeQuery = true)
|
@Query(value = "SELECT * FROM sys_user WHERE user_name = :userName", nativeQuery = true)
|
||||||
SysUserEntity selectByUserName(@Param("userName") String userName);
|
SysUserEntity selectByUserName(@Param("userName") String userName);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据用户名查询用户信息
|
|
||||||
*/
|
|
||||||
SysUserEntity findByUserName(String userName);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 软删除系统用户根据id
|
* 软删除系统用户根据id
|
||||||
*/
|
*/
|
||||||
@ -88,13 +83,6 @@ public interface SysUserDao extends JpaRepository<SysUserEntity, Long>, JpaSpeci
|
|||||||
@Modifying(clearAutomatically = true)
|
@Modifying(clearAutomatically = true)
|
||||||
void updateOverTime(Long userId, String overTime);
|
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);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录用户登录次数
|
* 记录用户登录次数
|
||||||
*/
|
*/
|
||||||
|
@ -62,9 +62,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查用户名是否存在
|
* 检查用户名是否存在
|
||||||
*
|
|
||||||
* @param userName
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean checkUserName(String userName) {
|
public boolean checkUserName(String userName) {
|
||||||
@ -74,8 +71,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增用户
|
* 新增用户
|
||||||
*
|
|
||||||
* @param sysUserQo
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -199,9 +194,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新用户
|
* 更新用户
|
||||||
*
|
|
||||||
* @param sysUserQo
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -214,9 +206,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 软删除普通用户
|
* 软删除普通用户
|
||||||
*
|
|
||||||
* @param id
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -236,8 +225,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询用户的信息
|
* 查询用户的信息
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<SysUserEntity> selectAllUser() {
|
public List<SysUserEntity> selectAllUser() {
|
||||||
@ -246,8 +233,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 启用
|
* 启用
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -257,8 +242,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 禁用
|
* 禁用
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -268,12 +251,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户修改密码
|
* 用户修改密码
|
||||||
*
|
|
||||||
* @param oldPassword
|
|
||||||
* @param newPassword
|
|
||||||
* @param confirmPassword
|
|
||||||
* @param sysUserEntity
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -292,11 +269,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员修改用户的密码
|
* 管理员修改用户的密码
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
* @param newPassword
|
|
||||||
* @param confirmPassword
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -325,10 +297,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改账号到期时间
|
* 修改账号到期时间
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
* @param overTime
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -339,9 +307,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录用户登录次数
|
* 记录用户登录次数
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
* @param loginNum
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
@ -351,9 +316,6 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录用户登录时间
|
* 记录用户登录时间
|
||||||
*
|
|
||||||
* @param userId
|
|
||||||
* @param loginLastTime
|
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
|
@ -99,5 +99,90 @@ public class VipTimeRangeUtils {
|
|||||||
return vipLocalDateTimeRangePairList;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user