UserFollowTask.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.goafanti.common.task;
  2. import com.goafanti.common.constant.AFTConstants;
  3. import com.goafanti.common.dao.PublicReleaseCountMapper;
  4. import com.goafanti.common.dao.PublicReleaseMapper;
  5. import com.goafanti.common.dao.TOrderNewMapper;
  6. import com.goafanti.common.model.OutPublicReleaseCount;
  7. import com.goafanti.common.model.PublicRelease;
  8. import com.goafanti.common.model.PublicReleaseCount;
  9. import com.goafanti.common.model.outUserFollowCount;
  10. import com.goafanti.common.utils.DateUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import java.math.BigDecimal;
  18. import java.util.ArrayList;
  19. import java.util.Calendar;
  20. import java.util.List;
  21. @Component
  22. public class UserFollowTask {
  23. @Autowired
  24. private PublicReleaseMapper publicReleaseMapper;
  25. @Autowired
  26. private PublicReleaseCountMapper publicReleaseCountMapper;
  27. @Autowired
  28. private TOrderNewMapper tOrderNewMapper;
  29. /**
  30. * 每月1号凌晨5点轮询统计
  31. *
  32. */
  33. @Scheduled(cron = "0 0 5 1 * ?")
  34. public void autoTask() throws InterruptedException {
  35. userFollowStatistics(null);
  36. }
  37. // @RequestMapping(value = "/open/userFollowStatistics", method = RequestMethod.GET)
  38. public void manualTask(Integer m) throws InterruptedException {
  39. userFollowStatistics(m);
  40. }
  41. public void userFollowStatistics(Integer m) throws InterruptedException {
  42. //获取上月1号到本月1号的外出
  43. Calendar now=getNowDate(m);
  44. Calendar date=getNowDate(m);
  45. String ym=getYearMonth(m);
  46. date.set(Calendar.MONTH,now.get(Calendar.MONTH)-1);
  47. List<OutPublicReleaseCount> list =publicReleaseMapper.getTimeUserFollow(date.getTime(),now.getTime());
  48. for (OutPublicReleaseCount out : list) {
  49. out.setDate(DateUtils.formatDate(date.getTime(),AFTConstants.YYYYMM));
  50. List<BigDecimal> aList=tOrderNewMapper.selectByUidAndNewUser(out.getUid(),out.getAid(),out.getNewUser(),date.getTime(),now.getTime());
  51. if (aList.isEmpty()){
  52. out.setSign(0);
  53. out.setTotal(new BigDecimal(0));
  54. }else{
  55. out.setSign(aList.size());
  56. BigDecimal total=new BigDecimal(0);
  57. for (BigDecimal bigDecimal : aList) {
  58. total=total.add(bigDecimal);
  59. }
  60. out.setTotal(total);
  61. }
  62. }
  63. List<PublicReleaseCount> newList=new ArrayList<>();
  64. if (list != null && list.size() > 0) {
  65. for (int i = 0; i < list.size(); i++) {
  66. newList.add((PublicReleaseCount)list.get(i));
  67. if (50 == newList.size() || i == list.size() - 1) {
  68. if (newList.size() > 0) {
  69. publicReleaseCountMapper.insertBatch(newList);
  70. }
  71. newList.clear();
  72. Thread.sleep(2000);
  73. }
  74. }
  75. }
  76. }
  77. public Calendar getNowDate(Integer m){
  78. Calendar now=Calendar.getInstance();
  79. if (m!=null)now.set(2021,m,1);
  80. now.set(Calendar.HOUR_OF_DAY, 0);
  81. now.clear(Calendar.MINUTE);
  82. now.clear(Calendar.SECOND);
  83. now.clear(Calendar.MILLISECOND);
  84. return now;
  85. }
  86. public String getYearMonth(Integer m){
  87. Calendar now=Calendar.getInstance();
  88. if (m!=null)now.set(2021,m,1);
  89. int y=now.get(Calendar.YEAR);
  90. int mo=now.get(Calendar.MONTH);
  91. if(mo<1){
  92. mo=12;
  93. y--;
  94. }
  95. return y+"-"+mo;
  96. }
  97. }