You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.5 KiB

  1. package com.example.demo.serviceImpl.coin;
  2. import com.example.demo.domain.vo.coin.RechargeActivity;
  3. import com.example.demo.mapper.coin.RechargeActivityCenterMapper;
  4. import com.example.demo.service.coin.RechargeActivityCenterService;
  5. import com.github.pagehelper.PageHelper;
  6. import com.github.pagehelper.PageInfo;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.time.LocalDateTime;
  10. import java.util.Date;
  11. import java.util.List;
  12. import java.util.regex.Pattern;
  13. /**
  14. * @program: gold-java
  15. * @ClassName RechargeActivityCenterServiceImpl
  16. * @description: 充值活动中心服务实现类
  17. * @author: Double
  18. * @create: 202510-28 14:40
  19. * @Version 1.0
  20. **/
  21. @Service
  22. public class RechargeActivityCenterServiceImpl implements RechargeActivityCenterService {
  23. @Autowired
  24. private RechargeActivityCenterMapper rechargeActivityCenterMapper;
  25. private static final String ACTIVITY_NAME_REGEX = "^[\\u4e00-\\u9fa5a-zA-Z0-9,。!?、;:\"'()《》【】——~,.:;!'()\\[\\]-_&+=]{1,50}$";
  26. private static final Pattern ACTIVITY_NAME_PATTERN = Pattern.compile(ACTIVITY_NAME_REGEX);
  27. // 根据ID查询活动
  28. @Override
  29. public PageInfo<RechargeActivity> queryActivity(Integer pageNum, Integer pageSize, RechargeActivity activity) {
  30. updateStatus();
  31. PageHelper.startPage(pageNum, pageSize);
  32. List<RechargeActivity> activityList = rechargeActivityCenterMapper.queryActivity(activity);
  33. return new PageInfo<>(activityList);
  34. }
  35. // 新增活动
  36. @Override
  37. public void addActivity(RechargeActivity activity) {
  38. if (activity.getActivityName().length() > 50) {
  39. throw new IllegalArgumentException("活动名称不能超过50个字符");
  40. }
  41. RechargeActivity activityByName = rechargeActivityCenterMapper.queryActivityByName(activity);
  42. if (activityByName != null) {
  43. throw new IllegalArgumentException("活动名称已存在");
  44. }
  45. // 3. 字符格式校验
  46. if (!ACTIVITY_NAME_PATTERN.matcher(activity.getActivityName()).matches()) {
  47. throw new IllegalArgumentException("活动名称仅允许包含汉字、英文字母、数字及常见标点符号(中英文标点:,。!?、;:\"'()《》【】——~,.:;!'()[]-_&+=)");
  48. }
  49. Date startTime = activity.getStartTime();
  50. Date endTime = activity.getEndTime();
  51. //获取当前系统时间(本地时间,与活动时间时区保持一致)
  52. Date now = new Date();
  53. //时间范围判断,设置对应状态
  54. if (now.before(startTime)) {
  55. // 当前时间 < 开始时间 → 未开始(状态0)
  56. activity.setStatus("0");
  57. } else if (now.after(endTime)) {
  58. // 当前时间 > 结束时间 → 已结束(状态2)
  59. activity.setStatus("2");
  60. } else {
  61. // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
  62. activity.setStatus("1");
  63. }
  64. rechargeActivityCenterMapper.addActivity(activity);
  65. }
  66. // 更新活动
  67. @Override
  68. public void updateActivity(RechargeActivity activity) {
  69. Date startTime = activity.getStartTime();
  70. Date endTime = activity.getEndTime();
  71. //获取当前系统时间(本地时间,与活动时间时区保持一致)
  72. Date now = new Date();
  73. //时间范围判断,设置对应状态
  74. if (now.before(startTime)) {
  75. // 当前时间 < 开始时间 → 未开始(状态0)
  76. activity.setStatus("0");
  77. } else if (now.after(endTime)) {
  78. // 当前时间 > 结束时间 → 已结束(状态2)
  79. activity.setStatus("2");
  80. } else {
  81. // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
  82. activity.setStatus("1");
  83. }
  84. rechargeActivityCenterMapper.updateActivity(activity);
  85. }
  86. @Override
  87. public void updateStatus() {
  88. // 查询所有活动
  89. List<RechargeActivity> activityList = rechargeActivityCenterMapper.listActivities();
  90. if (activityList == null || activityList.isEmpty()) {
  91. return;
  92. }
  93. Date now = new Date();
  94. // 遍历所有活动,更新状态
  95. for (RechargeActivity activity : activityList) {
  96. Date startTime = activity.getStartTime();
  97. Date endTime = activity.getEndTime();
  98. // 跳过时间为空的活动
  99. if (startTime == null || endTime == null) {
  100. continue;
  101. }
  102. String newStatus;
  103. // 时间范围判断,设置对应状态
  104. if (now.before(startTime)) {
  105. // 当前时间 < 开始时间 → 未开始(状态0)
  106. newStatus = "0";
  107. } else if (now.after(endTime)) {
  108. // 当前时间 > 结束时间 → 已结束(状态2)
  109. newStatus = "2";
  110. } else {
  111. // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
  112. newStatus = "1";
  113. }
  114. // 只有当状态发生变化时才更新
  115. if (!newStatus.equals(activity.getStatus())) {
  116. activity.setStatus(newStatus);
  117. rechargeActivityCenterMapper.updateActivity(activity);
  118. }
  119. }
  120. }
  121. // 删除活动
  122. @Override
  123. public void deleteActivity(RechargeActivity activity) {
  124. rechargeActivityCenterMapper.deleteActivity(activity);
  125. }
  126. }