| | |
| | | import com.supersavedriving.user.modular.system.util.MallBook.model.*; |
| | | import com.supersavedriving.user.modular.system.util.MallBook.util.Transfer; |
| | | import com.supersavedriving.user.modular.system.util.MallBook.util.TrhRequest; |
| | | import com.supersavedriving.user.modular.system.util.juhe.WeatherUtil; |
| | | import com.supersavedriving.user.modular.system.util.mongodb.model.Location; |
| | | import com.supersavedriving.user.modular.system.warpper.*; |
| | | import io.swagger.models.auth.In; |
| | | import org.redisson.api.RLock; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.slf4j.Logger; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.MathContext; |
| | | import java.math.RoundingMode; |
| | | import java.net.InetAddress; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | |
| | | @Override |
| | | public ResultUtil travelOrder(Integer uid, TravelOrder travelOrder) throws Exception { |
| | | RLock lock1 = redissonClient.getLock("travelOrder:" + uid); |
| | | boolean lock2 = lock1.tryLock(5, TimeUnit.SECONDS); |
| | | boolean lock2 = lock1.tryLock(5, 10, TimeUnit.SECONDS); |
| | | if(!lock2){ |
| | | return ResultUtil.error("系统繁忙,请稍后重试"); |
| | | } |
| | |
| | | appUserService.updateById(appUser); |
| | | } |
| | | |
| | | List<Order> orderList = new ArrayList<>(); |
| | | for (Integer i = 0; i < travelOrder.getDriverNum(); i++) { |
| | | order.setId(null); |
| | | order.setCode(UUIDUtil.getTimeStr() + UUIDUtil.getNumberRandom(5)); |
| | | boolean insert = this.insert(order); |
| | | if(insert){ |
| | | //推送状态 |
| | | pushUtil.pushOrderStatus(uid, 1, order.getId(), order.getState()); |
| | | if(null != order.getDriverId()){ |
| | | PushOrderInfoWarpper pushOrderInfoWarpper = new PushOrderInfoWarpper(); |
| | | pushOrderInfoWarpper.setId(order.getId()); |
| | | pushOrderInfoWarpper.setState(order.getState()); |
| | | pushUtil.pushOrderInfo(order.getDriverId(), 2, pushOrderInfoWarpper); |
| | | }else{ |
| | | //推单 |
| | | Order finalOrder = order; |
| | | new Thread(()->{ |
| | | pushOrder(finalOrder.getId()); |
| | | }).start(); |
| | | } |
| | | orderList.add(order); |
| | | } |
| | | } |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | orderList.forEach(order -> { |
| | | //推送状态 |
| | | pushUtil.pushOrderStatus(uid, 1, order.getId(), order.getState()); |
| | | if(null != order.getDriverId()){ |
| | | PushOrderInfoWarpper pushOrderInfoWarpper = new PushOrderInfoWarpper(); |
| | | pushOrderInfoWarpper.setId(order.getId()); |
| | | pushOrderInfoWarpper.setState(order.getState()); |
| | | pushUtil.pushOrderInfo(order.getDriverId(), 2, pushOrderInfoWarpper); |
| | | }else{ |
| | | //推单 |
| | | pushOrder(order.getId(), uid); |
| | | } |
| | | }); |
| | | } |
| | | }).start(); |
| | | |
| | | redisUtil.setStrValue("newOrder", "true"); |
| | | return ResultUtil.success(order.getId()); |
| | | }catch (Exception e){ |
| | |
| | | /** |
| | | * 订单推送逻辑 |
| | | */ |
| | | public void pushOrder(Long orderId){ |
| | | public void pushOrder(Long orderId, Integer uid){ |
| | | System.err.println("开始推单:" + orderId); |
| | | // RLock lock1 = redissonClient.getLock("pushOrder:" + uid); |
| | | try { |
| | | Order order = this.selectById(orderId); |
| | | /** |
| | |
| | | * 合适司:积分 > 评分 > 距离 |
| | | * 3.司机没有接单直接将订单置入大厅 |
| | | */ |
| | | boolean lock = redisUtil.lock(5); |
| | | if(!lock){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | Thread.sleep(3000);//等待3秒 |
| | | lock = redisUtil.lock(5); |
| | | if(lock){ |
| | | break; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | } |
| | | if(!lock){ |
| | | order.setHallOrder(1); |
| | | this.updateById(order); |
| | | ExtraPushOrder(order); |
| | | redisUtil.unlock(); |
| | | } |
| | | // boolean lock2 = lock1.tryLock(5, 10, TimeUnit.SECONDS); |
| | | // if(!lock2){ |
| | | // order.setHallOrder(1); |
| | | // this.updateById(order); |
| | | // ExtraPushOrder(order); |
| | | // } |
| | | SystemConfig systemConfig = systemConfigService.selectOne(new EntityWrapper<SystemConfig>().eq("type", 1)); |
| | | if(null == systemConfig){ |
| | | redisUtil.unlock(); |
| | | return; |
| | | } |
| | | JSONObject jsonObject = JSON.parseObject(systemConfig.getContent()); |
| | |
| | | } |
| | | |
| | | if(null != driver){ |
| | | System.err.println("完成司机查找:" + driver); |
| | | //防止多笔订单推给一个司机 |
| | | Driver driver1 = driverService.selectById(driver); |
| | | driver1.setServerStatus(2); |
| | |
| | | ExtraPushOrder(order); |
| | | redisUtil.setStrValue("lobbyOrder", "true"); |
| | | } |
| | | redisUtil.unlock(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | redisUtil.unlock(); |
| | | }finally { |
| | | // lock1.unlock(); |
| | | } |
| | | |
| | | } |