IndentScheduler.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.sqx.scheduler.indent;
  2. import com.sqx.modules.app.dao.UserDao;
  3. import com.sqx.modules.app.entity.UserEntity;
  4. import com.sqx.modules.app.service.UserService;
  5. import com.sqx.modules.common.entity.CommonInfo;
  6. import com.sqx.modules.common.service.CommonInfoService;
  7. import com.sqx.modules.errand.dao.TbIndentDao;
  8. import com.sqx.modules.errand.entity.TbIndent;
  9. import com.sqx.modules.goods.dao.GoodsShopDao;
  10. import com.sqx.modules.goods.entity.GoodsShop;
  11. import com.sqx.modules.utils.SenInfoCheckUtil;
  12. import com.sqx.scheduler.config.SchedulerLock;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.redisson.api.RLock;
  16. import org.redisson.api.RedissonClient;
  17. import org.springframework.scheduling.annotation.Async;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.concurrent.TimeUnit;
  26. /**
  27. * 跑腿订单定时任务
  28. *
  29. * @author : codingliang
  30. * @date : 2024-09-09 12:28
  31. */
  32. @Slf4j
  33. @Component
  34. @RequiredArgsConstructor
  35. public class IndentScheduler {
  36. private final CommonInfoService commonInfoService;
  37. private final TbIndentDao tbIndentDao;
  38. private final GoodsShopDao goodsShopDao;
  39. private final UserService userService;
  40. private final UserDao userDao;
  41. private final RedissonClient redissonClient;
  42. /**
  43. * 定时自动给骑手推单
  44. * 每分钟运行一次
  45. */
  46. @Async
  47. @Scheduled(cron = "0 * * * * ?", zone = "Asia/Shanghai")
  48. public void autoSendOrder(){
  49. RLock lock = redissonClient.getLock(SchedulerLock.INDENT_AUTO_PUSH_LOCK);
  50. boolean locked = false;
  51. try {
  52. locked = lock.tryLock(0, 120, TimeUnit.SECONDS);
  53. if (!locked) {
  54. log.info("未获取到自动给骑手推单锁,跳过本次执行");
  55. return;
  56. }
  57. log.info("自动给骑手推单任务开始运行");
  58. CommonInfo three = commonInfoService.findOne(304);
  59. if("是".equals(three.getValue())){
  60. //查看所有支付以后两分钟后的订单(排除同城服务订单)
  61. CommonInfo two = commonInfoService.findOne(302);
  62. CommonInfo one = commonInfoService.findOne(303);
  63. CommonInfo four = commonInfoService.findOne(273);
  64. CommonInfo five = commonInfoService.findOne(310);
  65. Integer maxNum = Integer.parseInt(commonInfoService.findOne(342).getValue());
  66. Double cashDeposit = Double.valueOf(four.getValue());
  67. Double distance = Double.valueOf(one.getValue());
  68. String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  69. String date = getCurrentTime(Integer.valueOf(two.getValue()));
  70. List<TbIndent> tbIndents = tbIndentDao.selectSendOrder(date);
  71. //遍历这些订单,查出距离这些订单最近的骑手(骑手需要没有接单,且在订单位置xx米内)
  72. for(int i = 0;i<tbIndents.size();i++){
  73. GoodsShop goodsShop = goodsShopDao.selectById(tbIndents.get(i).getShopId());
  74. try{
  75. if("3".equals(tbIndents.get(i).getIndentType())&&"0".equals(tbIndents.get(i).getBuyType())){
  76. //没有起点位置,只有终点位置。通过终点位置查距离内最近的骑手
  77. Long userId = tbIndentDao.selectRiderByDictance(tbIndents.get(i).getUserLng(), tbIndents.get(i).getUserLat(),
  78. distance,cashDeposit,maxNum);
  79. if(userId!=null){
  80. tbIndentDao.indentReceiv(userId, tbIndents.get(i).getIndentNumber(), format,"4");
  81. //自动推单以后,消息通知骑手
  82. UserEntity userEntity = userDao.selectById(userId);
  83. List<String> msgList=new ArrayList<>();
  84. msgList.add("接单项目");
  85. msgList.add(userEntity.getNickName());
  86. msgList.add(format);
  87. msgList.add("系统自动推单");
  88. SenInfoCheckUtil.sendRiderMsg(userEntity.getRiderOpenId(),five.getValue(),msgList,4);
  89. userService.pushToSingleRider("系统自动推单", "系统已为您自动派单,请及时前往派送!", userEntity.getRiderClientid());
  90. userService.sendMsgDXB(userEntity.getPhone(), "autosend", 0);
  91. }
  92. }else {
  93. if(goodsShop!=null&&goodsShop.getAutoSendOrder()!=null&&goodsShop.getAutoSendOrder()==0){
  94. //有起点位置,通过起点位置查距离内最近的骑手
  95. Long userId = tbIndentDao.selectRiderByDictance(tbIndents.get(i).getShopLng(), tbIndents.get(i).getShopLat(),
  96. distance, cashDeposit,maxNum);
  97. if(userId!=null){
  98. tbIndentDao.indentReceiv(userId, tbIndents.get(i).getIndentNumber(), format,"3");
  99. // tbIndentDao.indentReceiving(userId, tbIndents.get(i).getIndentNumber(), format);
  100. //自动推单以后,消息通知骑手
  101. UserEntity userEntity = userDao.selectById(userId);
  102. List<String> msgList=new ArrayList<>();
  103. msgList.add("接单项目");
  104. msgList.add(userEntity.getNickName());
  105. msgList.add(format);
  106. msgList.add("系统自动推单");
  107. SenInfoCheckUtil.sendRiderMsg(userEntity.getRiderOpenId(),five.getValue(),msgList,4);
  108. userService.pushToSingleRider("系统自动推单", "系统已为您自动派单,请及时前往派送!", userEntity.getRiderClientid());
  109. userService.sendMsgDXB(userEntity.getPhone(), "autosend", 0);
  110. }
  111. }
  112. }
  113. } catch (Exception e){
  114. e.printStackTrace();
  115. log.error("定时任务自动退单异常,异常订单id:"+tbIndents.get(i).getIndentId()+",异常:"+e.getMessage(),e);
  116. }
  117. }
  118. }
  119. log.info("自动给骑手推单任务运行成功");
  120. } catch (Exception e) {
  121. log.error("自动给骑手推单任务开始运行失败,【{}】", e);
  122. } finally {
  123. if (locked && lock.isHeldByCurrentThread()) {
  124. lock.unlock();
  125. }
  126. }
  127. }
  128. private String getCurrentTime(int i){
  129. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  130. Calendar beforeTime = Calendar.getInstance();
  131. beforeTime.add(Calendar.MINUTE, -i);
  132. Date beforeD = beforeTime.getTime();
  133. String time = sdf.format(beforeD);
  134. return time;
  135. }
  136. }