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;
import com.example.demo.domain.entity.Activity; import com.example.demo.mapper.ActivityMapper; import com.example.demo.sevice.ActivityService; import com.github.pagehelper.PageHelper; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.github.pagehelper.PageInfo; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List;
@Service @Transactional @RequiredArgsConstructor @CacheConfig(cacheNames = "activity") public class ActivityServiceImpl implements ActivityService {
@Autowired ActivityMapper activityMapper;
@Override @CacheEvict(value = {"activity"}, allEntries = true) public int add(Activity activity) { return activityMapper.add(activity); } @CacheEvict(value = "activity",allEntries = true) @Override public int edit(Activity activity) { activity.setActivityFlag(0); return activityMapper.edit(activity); }
@Override public List<Activity> search(Activity activity) {
return activityMapper.select(activity); } @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #activity.hashCode() ") @Override public PageInfo<Activity> searchForPage(Integer pageNum, Integer pageSize, Activity activity) { // PageHelper.startPage(pageNum, pageSize);
// List<Activity> list = activityMapper.select(activity);
// return new PageInfo<>(list);
PageHelper.startPage(pageNum, pageSize); List<Activity> list = activityMapper.select(activity); Date nowDate = new Date(); list.forEach(activity1 -> { // 假设 getStartTime() 和 getEndTime() 返回的是 java.util.Date 类型
Date startTime = activity1.getStartTime(); Date endTime = activity1.getEndTime();
// 使用 Date 类的 before 和 after 方法进行比较
if (nowDate.before(startTime)) { activity1.setStatus(0); // 设置状态为 0
} else if (nowDate.after(endTime)) { activity1.setStatus(2); // 设置状态为 2
} else { activity1.setStatus(1); // 设置状态为 1
}
// 保存修改后的对象
activityMapper.edit(activity1); }); return new PageInfo<>(list); } }
// @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #activity.hashCode() ")
// @Override
// public PageInfo<Activity> searchForPage(Integer pageNum, Integer pageSize, Activity activity) {
// PageHelper.startPage(pageNum, pageSize);
// List<Activity> list = activityMapper.select(activity);
// LocalDateTime nowDate = LocalDateTime.now();
//
// for (Activity activity1 : list) {
// Date startTime = activity1.getStartTime();
// Date endTime = activity1.getEndTime();
//
// if (nowDate.isBefore(startTime)) {
// activity1.setStatus(0); // 设置状态为 0
// } else if (nowDate.isAfter(endTime)) {
// activity1.setStatus(2); // 设置状态为 2
// } else {
// activity1.setStatus(1); // 设置状态为 1
// }
//
// // 保存修改后的对象
// activityMapper.edit(activity1);
// }
//
// // 返回分页信息
// return new PageInfo<>(list);
// }
//}
|