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.
|
|
package com.example.demo.serviceImpl.coin;
import com.example.demo.domain.vo.coin.RechargeActivity;import com.example.demo.mapper.coin.RechargeActivityCenterMapper;import com.example.demo.service.coin.RechargeActivityCenterService;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;
import java.time.LocalDateTime;import java.util.Date;import java.util.List;import java.util.Objects;import java.util.regex.Pattern;
/** * @program: gold-java * @ClassName RechargeActivityCenterServiceImpl * @description: 充值活动中心服务实现类 * @author: Double * @create: 2025−10-28 14:40 * @Version 1.0 **/
@Servicepublic class RechargeActivityCenterServiceImpl implements RechargeActivityCenterService {
@Autowired private RechargeActivityCenterMapper rechargeActivityCenterMapper;
private static final String ACTIVITY_NAME_REGEX = "^[\\u4e00-\\u9fa5a-zA-Z0-9,。!??、;:\"'‘’“”()《》【】—~~,.:;!()\\[\\]\\-_&+=]{1,100}$"; private static final Pattern ACTIVITY_NAME_PATTERN = Pattern.compile(ACTIVITY_NAME_REGEX); // 根据ID查询活动
@Override public PageInfo<RechargeActivity> queryActivity(Integer pageNum, Integer pageSize, RechargeActivity activity) { updateStatus(); PageHelper.startPage(pageNum, pageSize); List<RechargeActivity> activityList = rechargeActivityCenterMapper.queryActivity(activity); return new PageInfo<>(activityList); }
// 新增活动
@Override public void addActivity(RechargeActivity activity) { String activityName = activity.getActivityName();
// 1. 长度校验
if (activityName.length() > 100) { throw new IllegalArgumentException("活动名称不能超过100个字符"); }
// 2. 名称重复校验
RechargeActivity activityByName = rechargeActivityCenterMapper.queryActivityByName(activity); if (activityByName != null) { throw new IllegalArgumentException("活动名称已存在"); }
// 3. 字符格式校验(提取非法字符)
if (!ACTIVITY_NAME_PATTERN.matcher(activityName).matches()) { // 反向匹配,提取所有不符合规则的字符
StringBuilder illegalChars = new StringBuilder(); for (char c : activityName.toCharArray()) { if (!ACTIVITY_NAME_PATTERN.matcher(String.valueOf(c)).matches()) { // 去重,避免重复输出同一非法字符
if (illegalChars.indexOf(String.valueOf(c)) == -1) { illegalChars.append(c).append("、"); } } } // 去除最后一个顿号,拼接提示信息
String illegalStr = illegalChars.deleteCharAt(illegalChars.length() - 1).toString(); throw new IllegalArgumentException( String.format("活动名称包含非法字符:%s,仅允许包含汉字、英文字母、数字及常见标点符号(中英文标点:,。!??、;:\"'‘’“”()《》【】—~~,.:;!()[]-_&+=)", illegalStr) ); }
// 4. 时间范围判断,设置对应状态
Date startTime = activity.getStartTime(); Date endTime = activity.getEndTime(); Date now = new Date(); // 当前系统本地时间
if (now.before(startTime)) { activity.setStatus("0"); // 未开始
} else if (now.after(endTime)) { activity.setStatus("2"); // 已结束
} else { activity.setStatus("1"); // 进行中
}
rechargeActivityCenterMapper.addActivity(activity); }
// 更新活动
@Override public void updateActivity(RechargeActivity activity) { Date startTime = activity.getStartTime(); Date endTime = activity.getEndTime();
//获取当前系统时间(本地时间,与活动时间时区保持一致)
Date now = new Date();
//时间范围判断,设置对应状态
if (now.before(startTime)) { // 当前时间 < 开始时间 → 未开始(状态0)
activity.setStatus("0"); } else if (now.after(endTime)) { // 当前时间 > 结束时间 → 已结束(状态2)
activity.setStatus("2"); } else { // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
activity.setStatus("1"); } rechargeActivityCenterMapper.updateActivity(activity); if(Objects.equals(activity.getBusinessBelong(), "客户归属地")){ rechargeActivityCenterMapper.updateActivityArea(activity); } }
@Override public void updateStatus() { // 查询所有活动
List<RechargeActivity> activityList = rechargeActivityCenterMapper.listActivities();
if (activityList == null || activityList.isEmpty()) { return; }
Date now = new Date();
// 遍历所有活动,更新状态
for (RechargeActivity activity : activityList) { Date startTime = activity.getStartTime(); Date endTime = activity.getEndTime();
// 跳过时间为空的活动
if (startTime == null || endTime == null) { continue; }
String newStatus;
// 时间范围判断,设置对应状态
if (now.before(startTime)) { // 当前时间 < 开始时间 → 未开始(状态0)
newStatus = "0"; } else if (now.after(endTime)) { // 当前时间 > 结束时间 → 已结束(状态2)
newStatus = "2"; } else { // 开始时间 ≤ 当前时间 ≤ 结束时间 → 进行中(状态1)
newStatus = "1"; }
// 只有当状态发生变化时才更新
if (!newStatus.equals(activity.getStatus())) { activity.setStatus(newStatus); rechargeActivityCenterMapper.updateActivity(activity); } } }
// 删除活动
@Override public void deleteActivity(RechargeActivity activity) { rechargeActivityCenterMapper.deleteActivity(activity); }}
|