金币系统后端
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.

106 lines
3.7 KiB

  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.domain.entity.Activity;
  3. import com.example.demo.mapper.ActivityMapper;
  4. import com.example.demo.sevice.ActivityService;
  5. import com.github.pagehelper.PageHelper;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.cache.CacheManager;
  9. import org.springframework.cache.annotation.CacheConfig;
  10. import org.springframework.cache.annotation.CacheEvict;
  11. import org.springframework.stereotype.Service;
  12. import com.github.pagehelper.PageInfo;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import java.util.Date;
  15. import java.util.List;
  16. @Service
  17. @Transactional
  18. @RequiredArgsConstructor
  19. @CacheConfig(cacheNames = "activity")
  20. public class ActivityServiceImpl implements ActivityService {
  21. @Autowired
  22. ActivityMapper activityMapper;
  23. @Autowired
  24. private CacheManager cacheManager;
  25. @Override
  26. @CacheEvict(value = {"activity"}, allEntries = true)
  27. public int add(Activity activity) {
  28. return activityMapper.add(activity);
  29. }
  30. @CacheEvict(value = "activity",allEntries = true)
  31. @Override
  32. public int edit(Activity activity) {
  33. activity.setActivityFlag(0);
  34. return activityMapper.edit(activity);
  35. }
  36. @Override
  37. public List<Activity> search(Activity activity) {
  38. return activityMapper.select(activity);
  39. }
  40. @CacheEvict(value = {"activity"}, allEntries = true)
  41. @Override
  42. public PageInfo<Activity> searchForPage(Integer pageNum, Integer pageSize, Activity activity) {
  43. // PageHelper.startPage(pageNum, pageSize);
  44. // List<Activity> list = activityMapper.select(activity);
  45. // return new PageInfo<>(list);
  46. PageHelper.startPage(pageNum, pageSize);
  47. List<Activity> list = activityMapper.select(activity);
  48. Date nowDate = new Date();
  49. list.forEach(activity1 -> {
  50. // 假设 getStartTime() 和 getEndTime() 返回的是 java.util.Date 类型
  51. Date startTime = activity1.getStartTime();
  52. Date endTime = activity1.getEndTime();
  53. // 使用 Date 类的 before 和 after 方法进行比较
  54. if (startTime != null &&nowDate.before(startTime)) {
  55. activity1.setStatus(0); // 设置状态为 0
  56. } else if (endTime != null &&nowDate.after(endTime)) {
  57. activity1.setStatus(2); // 设置状态为 2
  58. } else {
  59. activity1.setStatus(1); // 设置状态为 1
  60. }
  61. // 保存修改后的对象
  62. activityMapper.edit(activity1);
  63. });
  64. return new PageInfo<>(list);
  65. }
  66. }
  67. // @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #activity.hashCode() ")
  68. // @Override
  69. // public PageInfo<Activity> searchForPage(Integer pageNum, Integer pageSize, Activity activity) {
  70. // PageHelper.startPage(pageNum, pageSize);
  71. // List<Activity> list = activityMapper.select(activity);
  72. // LocalDateTime nowDate = LocalDateTime.now();
  73. //
  74. // for (Activity activity1 : list) {
  75. // Date startTime = activity1.getStartTime();
  76. // Date endTime = activity1.getEndTime();
  77. //
  78. // if (nowDate.isBefore(startTime)) {
  79. // activity1.setStatus(0); // 设置状态为 0
  80. // } else if (nowDate.isAfter(endTime)) {
  81. // activity1.setStatus(2); // 设置状态为 2
  82. // } else {
  83. // activity1.setStatus(1); // 设置状态为 1
  84. // }
  85. //
  86. // // 保存修改后的对象
  87. // activityMapper.edit(activity1);
  88. // }
  89. //
  90. // // 返回分页信息
  91. // return new PageInfo<>(list);
  92. // }
  93. //}