SpringTaskConfig.java 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.ruoyi.system.utils;
  2. import com.ruoyi.common.utils.DateUtils;
  3. import com.ruoyi.system.domain.PostCollections;
  4. import com.ruoyi.system.domain.PostCollectionsSystem;
  5. import com.ruoyi.system.mapper.PostCollectionsMapper;
  6. import com.ruoyi.system.mapper.PostCollectionsSystemMapper;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.scheduling.annotation.EnableScheduling;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. import java.util.Date;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. @Configuration
  15. @EnableScheduling
  16. public class SpringTaskConfig {
  17. @Autowired
  18. private PostCollectionsSystemMapper postCollectionsSystemMapper;
  19. @Autowired
  20. private PostCollectionsMapper postCollectionsMapper;
  21. /**
  22. * 每10秒查询数据库修改type的状态
  23. */
  24. @Scheduled(cron = "0/10 * * * * ?")
  25. private void cron() {
  26. Date nowDate = DateUtils.getNowDate();
  27. PostCollectionsSystem postCollectionsSystem = new PostCollectionsSystem();
  28. List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
  29. postCollectionsSystemList = postCollectionsSystemList.stream().map((item) -> {
  30. Date startTime = item.getStartTime();
  31. Date endTime = item.getEndTime();
  32. if (null != startTime && null != endTime) {
  33. if (nowDate.before(startTime)) {
  34. item.setType((long) 1);
  35. } else if (nowDate.after(endTime)) {
  36. item.setType((long) 2);
  37. } else {
  38. item.setType((long) 0);
  39. }
  40. }
  41. return item;
  42. }).collect(Collectors.toList());
  43. postCollectionsSystemMapper.updateBatchById(postCollectionsSystemList);
  44. }
  45. /**
  46. * 每10秒查询数据库藏品套系的时间,根据套系时间自动修改藏品状态
  47. */
  48. @Scheduled(cron = "0/10 * * * * ?")
  49. private void cronA() {
  50. Date nowDate = DateUtils.getNowDate();
  51. PostCollectionsSystem postCollectionsSystem = new PostCollectionsSystem();
  52. List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
  53. postCollectionsSystemList = postCollectionsSystemList.stream().map((item) -> {
  54. Long systemId = item.getId();
  55. Date startTime = item.getStartTime();
  56. Date endTime = item.getEndTime();
  57. return item;
  58. }).collect(Collectors.toList());
  59. for (int i = 0; i < postCollectionsSystemList.size(); i++) {
  60. Date startTime = postCollectionsSystemList.get(i).getStartTime();
  61. Date endTime = postCollectionsSystemList.get(i).getEndTime();
  62. List<PostCollections> postCollectionsList = postCollectionsMapper.selectPostCollectionsList(postCollectionsSystemList.get(i).getId());
  63. for (int j = 0; j < postCollectionsList.size(); j++) {
  64. PostCollections postCollections = new PostCollections();
  65. postCollections.setId(postCollectionsList.get(j).getId());
  66. postCollections.setSystemId(postCollectionsSystemList.get(i).getId());
  67. postCollections.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getTime()));
  68. if (null!=startTime&&null!=endTime){
  69. if (nowDate.before(startTime)){
  70. postCollections.setStatus("0");
  71. }
  72. if (nowDate.after(endTime)){
  73. postCollections.setStatus("3");
  74. }
  75. if(startTime.before(nowDate)&&endTime.after(nowDate)&&
  76. null!=postCollectionsList.get(j).getCollectionsNumber()&&
  77. postCollectionsList.get(j).getCollectionsNumber() > 0 ) {
  78. postCollections.setStatus("2");
  79. }
  80. if(startTime.before(nowDate)&&endTime.after(nowDate)&&
  81. null!=postCollectionsList.get(j).getCollectionsNumber()&&
  82. postCollectionsList.get(j).getCollectionsNumber() == 0 ) {
  83. postCollections.setStatus("1");
  84. }
  85. }
  86. postCollectionsMapper.updatePostCollectionsBySysetmIdAndId(postCollections);
  87. }
  88. }
  89. }
  90. }