Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

create_order.php 8.6 KiB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/8/30
  6. * Time: 14:17
  7. * 根据订单信息分配车辆创建订单
  8. */
  9. require_once __DIR__ . '/common.php';
  10. class createOrder extends virtifyUsers
  11. {
  12. /**
  13. * @param $mem memcache对象
  14. * @param $orderService orderService对象
  15. * @param $runID runID
  16. * @param $res 车调度结果
  17. * @param $bus_order_num 人数
  18. * @param $prodID prodID
  19. */
  20. function calShortTime($prodID, $runID, $orderID, $num, $beginX_Y, $begin_name, $endX_Y, $end_name, $run_date,$should_be_count=true,$bool_total_time=true)
  21. {
  22. $mem = new Memcache();
  23. $mem->connect('127.0.0.1', 11211);
  24. $PoolStatus = $mem->get("orderPoolStatus[$runID]"); //添加runid标记是为了区别是哪个时间的班次
  25. $unlock_orderlist = $mem->get("orderList[$runID]"); //拿到当前的未锁定的订单池进行进一步操作
  26. $orderService = new createOrderService();
  27. if ($PoolStatus) //存在订单状态池
  28. {
  29. foreach ($PoolStatus as $k => $v) {
  30. if (isset($v['status']) && $v['status'] == 1) { //还有未锁定的订单池
  31. $orderService->stock($runID);
  32. $value = $unlock_orderlist[$k];
  33. $pool_passager_num = $orderService->poolNum($value); //计算该订单池中总共的乘客数
  34. if (!$value || $pool_passager_num + $num > 6) { //说明该订单池不能加入该订单
  35. continue;
  36. }
  37. $begin_x_y = $orderService->timeMatrix($value, $beginX_Y, 1); //获取所有起点时间矩阵
  38. $end_x_y = $orderService->timeMatrix($value, $endX_Y, 2); //获取所有终点时间矩阵
  39. $new_order = array("order_id" => $orderID, "num" => $num, "startx_y" => $beginX_Y, "endx_y" => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name);
  40. $da = new dispatch_algorithm();
  41. $res = $da->dispatch($prodID, $value, $new_order, $begin_x_y, $end_x_y, $run_date, $should_be_count,$bool_total_time);//调用调配订单算法
  42. writeLog("调度算法返回结果:".json_encode($res));
  43. if (!$res) {
  44. continue;
  45. }
  46. $order = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name);
  47. array_push($unlock_orderlist[$k], $order);
  48. $mem->set("orderList[$runID]", $unlock_orderlist); //加入订单池
  49. //如果该订单池满6个人之后,就要将这个订单池的状态置为锁定
  50. if ($pool_passager_num + $num == 6) {
  51. $PoolStatus[$k]['status'] = 0;
  52. $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态
  53. //更改该订单池在状态池中的状态
  54. }
  55. //TODO:将订单信息存入数据库中
  56. $orderService->makeOrder($mem, $runID, $res, $k, $prodID);
  57. //更新order_main中该车的里面的其他订单的上车时间和下车时间
  58. $start_end_time = array();
  59. writeLog("开始结束时间--:".json_encode($res));
  60. foreach ($res as $id => $arr) {
  61. if (!isset($start_end_time[$arr['order_id']])) {
  62. $start_end_time[$arr['order_id']] = array();
  63. $start_end_time[$arr['order_id']]["start_time"] = $arr['time'];
  64. } else {
  65. $start_end_time[$arr['order_id']]["end_time"] = $arr['time'];
  66. $orderService->update_order_time($arr['order_id'], $start_end_time[$arr['order_id']]["start_time"], $arr['time']);
  67. }
  68. }
  69. $arr_result['order_id'] = $orderID;
  70. $arr_result['bus_no'] = $k;
  71. $order_time = array();
  72. foreach ($res as $num => $order) {
  73. if ($order["order_id"] == $orderID) {
  74. $order_time[] = $order['time'];
  75. }
  76. }
  77. $arr_result['start_time'] = $order_time[0];
  78. $arr_result['end_time'] = $order_time[1];
  79. $data['code'] = '0';
  80. $data['info'] = "配车成功,该订单已加入{$k}号车";
  81. $data['result'] = $arr_result;
  82. return $data;
  83. }
  84. }
  85. //如果该订单均不能符合当前存在的任意订单池,继续派车
  86. $stock = $orderService->stock($runID);
  87. if ($stock['bus_num'] < 1) {
  88. $data['code'] = '101';
  89. $data['info'] = '当前班次无可派车辆';
  90. return $data;
  91. }
  92. //$stock['next_blank_car'] 车ID
  93. $unlock_orderlist[$stock['next_blank_car']][] = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); //通过orderID标记不同的订单池
  94. $mem->set("orderList[$runID]", $unlock_orderlist); //加入订单池
  95. //初始化该订单池,状态值:0:已锁定;1:未锁定
  96. $PoolStatus[$stock['next_blank_car']] = array('status' => 1);
  97. $mem->set("orderPoolStatus[{$runID}]", $PoolStatus);
  98. //如果该订单池人数满6,状态池置为锁定
  99. if ($num >= 6) {
  100. $PoolStatus[$stock['next_blank_car']]['status'] = 0;
  101. $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态
  102. //更改该订单池在状态池中的状态
  103. }
  104. //TODO:将订单信息存入数据库中
  105. $order_res = $orderService->firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date);
  106. $orderService->makeOrder($mem, $runID, $order_res, $stock['next_blank_car'], $prodID);
  107. $result['code'] = 0;
  108. $result['info'] = "该订单不能与有订单的车辆匹配,加入{$stock['next_blank_car']}成功";
  109. $res_arr['order_id'] = $orderID;
  110. $res_arr['bus_no'] = $stock['next_blank_car'];
  111. $res_arr['start_time'] = $order_res[0]['time'];
  112. $res_arr['end_time'] = $order_res[1]['time'];
  113. $result['result'] = $res_arr;
  114. return $result;
  115. } else { //不存在当前班次的订单池,直接创建新的订单池以及订单状态池
  116. //这里还需要进行判断有没有可以派的车(即有没有超出规定车辆数量的限制) 需要从数据库读取当前
  117. $stock = $orderService->stock($runID);
  118. if ($stock['bus_num'] < 1) {
  119. $data['code'] = '101';
  120. $data['info'] = '当前班次无可派车辆(当前班次的第一个订单)';
  121. return $data;
  122. }
  123. //初始化该订单状态池,状态值:0:已锁定;1:未锁定
  124. $PoolStatus[$stock['next_blank_car']] = array('status' => 1);
  125. $mem->set("orderPoolStatus[$runID]", $PoolStatus);
  126. $orderList[$stock['next_blank_car']][] = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); //通过orderID标记不同的订单池
  127. $mem->set("orderList[$runID]", $orderList); //加入订单池
  128. //如果该订单池人数满6,状态池置为锁定
  129. if ($num >= 6) {
  130. $PoolStatus[$stock['next_blank_car']]['status'] = 0;
  131. $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态
  132. //更改该订单池在状态池中的状态
  133. }
  134. //TODO:将订单信息存入数据库中
  135. $order_res = $orderService->firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date);
  136. $orderService->makeOrder($mem, $runID, $order_res, $stock['next_blank_car'], $prodID);
  137. $result['code'] = 0;
  138. $result['info'] = "当前班次的第一个订单";
  139. $res_arr['order_id'] = $orderID;
  140. $res_arr['bus_no'] = $stock['next_blank_car'];
  141. $res_arr['start_time'] = $order_res[0]['time'];
  142. $res_arr['end_time'] = $order_res[1]['time'];
  143. $result['result'] = $res_arr;
  144. return $result;
  145. }
  146. }
  147. }