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.

167 lines
6.3 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.Objects;
  13. import java.util.regex.Pattern;
  14. /**
  15. * @program: gold-java
  16. * @ClassName RechargeActivityCenterServiceImpl
  17. * @description: 充值活动中心服务实现类
  18. * @author: Double
  19. * @create: 202510-28 14:40
  20. * @Version 1.0
  21. **/
  22. @Service
  23. public class RechargeActivityCenterServiceImpl implements RechargeActivityCenterService {
  24. @Autowired
  25. private RechargeActivityCenterMapper rechargeActivityCenterMapper;
  26. private static final String ACTIVITY_NAME_REGEX = "^[\\u4e00-\\u9fa5a-zA-Z0-9,。!??、;:\"'‘’“”()《》【】—~~,.:;!()\\[\\]\\-_&+=]{1,100}$";
  27. private static final Pattern ACTIVITY_NAME_PATTERN = Pattern.compile(ACTIVITY_NAME_REGEX);
  28. // 根据ID查询活动
  29. @Override
  30. public PageInfo<RechargeActivity> queryActivity(Integer pageNum, Integer pageSize, RechargeActivity activity) {
  31. updateStatus();
  32. PageHelper.startPage(pageNum, pageSize);
  33. List<RechargeActivity> activityList = rechargeActivityCenterMapper.queryActivity(activity);
  34. return new PageInfo<>(activityList);
  35. }
  36. // 新增活动
  37. @Override
  38. public void addActivity(RechargeActivity activity) {
  39. String activityName = activity.getActivityName();
  40. // 1. 长度校验
  41. if (activityName.length() > 100) {
  42. throw new IllegalArgumentException("活动名称不能超过100个字符");
  43. }
  44. // 2. 名称重复校验
  45. RechargeActivity activityByName = rechargeActivityCenterMapper.queryActivityByName(activity);
  46. if (activityByName != null) {
  47. throw new IllegalArgumentException("活动名称已存在");
  48. }
  49. // 3. 字符格式校验(提取非法字符)
  50. if (!ACTIVITY_NAME_PATTERN.matcher(activityName).matches()) {
  51. // 反向匹配,提取所有不符合规则的字符
  52. StringBuilder illegalChars = new StringBuilder();
  53. for (char c : activityName.toCharArray()) {
  54. if (!ACTIVITY_NAME_PATTERN.matcher(String.valueOf(c)).matches()) {
  55. // 去重,避免重复输出同一非法字符
  56. if (illegalChars.indexOf(String.valueOf(c)) == -1) {
  57. illegalChars.append(c).append("、");
  58. }
  59. }
  60. }
  61. // 去除最后一个顿号,拼接提示信息
  62. String illegalStr = illegalChars.deleteCharAt(illegalChars.length() - 1).toString();
  63. throw new IllegalArgumentException(
  64. String.format("活动名称包含非法字符:%s,仅允许包含汉字、英文字母、数字及常见标点符号(中英文标点:,。!??、;:\"'‘’“”()《》【】—~~,.:;!()[]-_&+=)", illegalStr)
  65. );
  66. }
  67. // 4. 时间范围判断,设置对应状态
  68. Date startTime = activity.getStartTime();
  69. Date endTime = activity.getEndTime();
  70. Date now = new Date(); // 当前系统本地时间
  71. if (now.before(startTime)) {
  72. activity.setStatus("0"); // 未开始
  73. } else if (now.after(endTime)) {
  74. activity.setStatus("2"); // 已结束
  75. } else {
  76. activity.setStatus("1"); // 进行中
  77. }
  78. rechargeActivityCenterMapper.addActivity(activity);
  79. }
  80. // 更新活动
  81. @Override
  82. public void updateActivity(RechargeActivity activity) {
  83. Date startTime = activity.getStartTime();
  84. Date endTime = activity.getEndTime();
  85. //获取当前系统时间(本地时间,与活动时间时区保持一致)
  86. Date now = new Date();
  87. //时间范围判断,设置对应状态
  88. if (now.before(startTime)) {
  89. // 当前时间 < 开始时间 → 未开始(状态0)
  90. activity.setStatus("0");
  91. } else if (now.after(endTime)) {
  92. // 当前时间 > 结束时间 → 已结束(状态2)
  93. activity.setStatus("2");
  94. } else {
  95. // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
  96. activity.setStatus("1");
  97. }
  98. rechargeActivityCenterMapper.updateActivity(activity);
  99. if(Objects.equals(activity.getBusinessBelong(), "客户归属地")){
  100. rechargeActivityCenterMapper.updateActivityArea(activity);
  101. }
  102. }
  103. @Override
  104. public void updateStatus() {
  105. // 查询所有活动
  106. List<RechargeActivity> activityList = rechargeActivityCenterMapper.listActivities();
  107. if (activityList == null || activityList.isEmpty()) {
  108. return;
  109. }
  110. Date now = new Date();
  111. // 遍历所有活动,更新状态
  112. for (RechargeActivity activity : activityList) {
  113. Date startTime = activity.getStartTime();
  114. Date endTime = activity.getEndTime();
  115. // 跳过时间为空的活动
  116. if (startTime == null || endTime == null) {
  117. continue;
  118. }
  119. String newStatus;
  120. // 时间范围判断,设置对应状态
  121. if (now.before(startTime)) {
  122. // 当前时间 < 开始时间 → 未开始(状态0)
  123. newStatus = "0";
  124. } else if (now.after(endTime)) {
  125. // 当前时间 > 结束时间 → 已结束(状态2)
  126. newStatus = "2";
  127. } else {
  128. // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
  129. newStatus = "1";
  130. }
  131. // 只有当状态发生变化时才更新
  132. if (!newStatus.equals(activity.getStatus())) {
  133. activity.setStatus(newStatus);
  134. rechargeActivityCenterMapper.updateActivity(activity);
  135. }
  136. }
  137. }
  138. // 删除活动
  139. @Override
  140. public void deleteActivity(RechargeActivity activity) {
  141. rechargeActivityCenterMapper.deleteActivity(activity);
  142. }
  143. }