You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

294 lines
12 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/9/7
  6. * Time: 19:57
  7. */
  8. require_once __DIR__ . '/../api/common.php';
  9. class createOrderService extends virtifyUsers
  10. {
  11. /**
  12. *插入run_bus_station_view的函数
  13. * @param type $run_id RUN_ID
  14. * @param type $bus_no 车号
  15. * @param type $prod_id prod_id
  16. * @param type $order_id 该车次的订单号 123,123,123。。。。
  17. * @param type $station 该车经过的站名 按经过顺序 上海,某某酒店,第三站点
  18. * @param type $lat_long 该车站点的经纬度 按站点顺序 31.54565,1215656456;31.57685745,121.545654
  19. * @param type $station_type 该车经过站点的 类型 1,1,0,1 上车站0 下车站1
  20. * @param type $station_time 该车经过各个站点的时间点 按经过顺序 12:10,15:20,18:30
  21. */
  22. public function set_run_station($run_id, $bus_no, $prod_id, $order_id, $station, $lat_long, $station_type, $station_time)
  23. {
  24. $pdo = conn();
  25. $sql = "SELECT id from run_bus_station_view where run_id = '" . $run_id . "' and bus_no='" . $bus_no . "'";
  26. $result = $pdo->query($sql);
  27. $row = $result->fetchAll(PDO::FETCH_ASSOC);
  28. if (!isset($row[0]['id'])) {
  29. $sql = "INSERT into run_bus_station_view(run_id,bus_no,prod_id,order_id,station,lat_long,station_type,station_time,update_time) VALUES(" . $run_id . ",'" . $bus_no . "','" . $prod_id . "','" . $order_id . "','" . $station . "','" . $lat_long . "','" . $station_type . "','" . $station_time . "',NOW());";
  30. writeLog("更新run_bus_station_view的sql语句" . json_encode($sql));
  31. $result = $pdo->query($sql);
  32. if (!$result) {
  33. return false;
  34. }
  35. } else {
  36. $sql = "UPDATE run_bus_station_view set cancel_flag =0,order_id='" . $order_id . "',station='" . $station . "',lat_long='" . $lat_long . "',station_type='" . $station_type . "',station_time='" . $station_time . "',update_time=NOW() where run_id =" . $run_id . " and bus_no =" . $bus_no;
  37. $result = $pdo->query($sql);
  38. writeLog("更新run_bus_station_view的sql语句" . json_encode($sql));
  39. if (!$result) {
  40. return FALSE;
  41. }
  42. }
  43. return true;
  44. }
  45. /**
  46. * 更新订单的开始时间和结束时间
  47. * @param $order_id
  48. * @param $start_time
  49. * @param $end_time
  50. */
  51. public function update_order_time($order_id, $start_time, $end_time)
  52. {
  53. $pdo = conn();
  54. $sql = "update order_main set PROD_START_STATION_TIME = '" . $start_time . "',PROD_END_STATION_TIME = '" . $end_time . "' where order_id =" . $order_id . " or parent_order_id =" . $order_id;
  55. $pdo->exec($sql);
  56. }
  57. /**
  58. * @param $runid 班次ID
  59. * @return int 返回库存量
  60. */
  61. public function stock($runid)
  62. {
  63. $pdo = conn();
  64. $sql = "select seat_count,saled_count,BUS_ORDER_ID from run_bus where cancel_flag = 0 AND run_id = " . $runid . " ORDER BY BUS_ORDER_ID ASC ";
  65. $result = $pdo->query($sql);
  66. $stock = $result->fetchAll(PDO::FETCH_ASSOC);
  67. writeLog($sql);
  68. $run_stock = 0;
  69. $bus_num = 0;
  70. $blank_car_order = false;
  71. foreach ($stock as $key => $value) {
  72. if ($value['saled_count'] <= 0) {
  73. $bus_num++;
  74. }
  75. $run_stock += ($value['seat_count'] - $value['saled_count']);
  76. if ($value['saled_count'] <= 0 && $blank_car_order == false) {
  77. $blank_car_order = $value['BUS_ORDER_ID'];
  78. }
  79. }
  80. $stock = array("bus_num" => $bus_num, "run_stock" => $run_stock, "next_blank_car" => $blank_car_order); //返回的是车辆剩余信息和座位剩余信息
  81. return $stock;
  82. }
  83. /* public function add_stock($runid, $bus_order_id, $num)
  84. {
  85. $pdo = conn();
  86. $sql = "update run_bus set SALED_COUNT = SALED_COUNT+{$num} where RUN_ID = " . $runid . " AND BUS_ORDER_ID = {$bus_order_id} AND CANCEL_FLAG = 0 ";
  87. $result = $pdo->exec($sql);
  88. }*/
  89. /**
  90. *生成时间矩阵
  91. * @param $orderList 未锁定的订单池
  92. * @param $beginX_Y 上车点纬经度
  93. * @param $endX_Y 下车点纬经度
  94. * @return array|bool 返回时间矩阵
  95. */
  96. public function timeMatrix($orderList, $X_Y, $tag)
  97. {
  98. //读取订单池的订单
  99. $count = count($orderList); //此订单进入之前订单池已经有的订单个数
  100. $str_beginX_Y = "";
  101. //$str_endX_Y = "";
  102. foreach ($orderList as $k => $v) {
  103. if ($tag == 1) {
  104. $str_beginX_Y .= $v['startx_y'] . '|';
  105. } else {
  106. $str_beginX_Y .= $v['endx_y'] . '|';
  107. }
  108. }
  109. //通过新的经纬度和订单池里面的订单经纬度计算出新的时间矩阵
  110. $str_beginX_Y .= $X_Y;
  111. //var_dump($str_beginX_Y);
  112. $data = array('origins' => $str_beginX_Y, 'destinations' => $str_beginX_Y, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK);
  113. $res = commonUtils::send_get(commonUtils::$URL, $data);
  114. $shortTimeList = json_decode($res, true);
  115. $time = array();
  116. if (isset($shortTimeList['status']) && $shortTimeList['status'] == 0) {
  117. $i = 1;
  118. $j = 1;
  119. foreach ($shortTimeList['result'] as $v) {
  120. $time[$j][$i] = ceil(($v['duration']['value']) / 60);
  121. if ($i % ($count + 1) == 0) { //生成2*(count+1)乘以2*(count+1)的时间矩阵
  122. $j++;
  123. $i = 0;
  124. }
  125. if ($i == $j) //将对角线元素置为零(由于上面的if,对角线的最后一个元素不会被置为0)
  126. {
  127. $time[$j][$i] = 0;
  128. }
  129. $max = ($count + 1);
  130. if (isset($time[$max][$max])) { //将对角线最后一个元素也置为0
  131. $time[$max][$max] = 0;
  132. }
  133. /* if ($j > ($count + 1) && $j <= $max && $i <= ($count + 1)) { //将矩阵的左下角不符合情况的值置为0
  134. $time[$j][$i] = -1;
  135. if (isset($time[$j][0])) {
  136. unset($time[$j][0]); //该方法会在$j>订单数的数组中多加入一个键值为0的元素,所以在此处进行处理
  137. }
  138. }*/
  139. $i++;
  140. }
  141. return $time; //产生时间矩阵
  142. }
  143. }
  144. /**
  145. * 根据订单池查询订单池内的人数
  146. * @param $orderPool
  147. */
  148. public function poolNum($orderPool)
  149. {
  150. $poolNum = 0;
  151. foreach ($orderPool as $item) {
  152. $poolNum += $item['num'];
  153. }
  154. return $poolNum;
  155. }
  156. /**
  157. *用于输入上车点和下车点后返回上车时间和下车时间
  158. * @param $orderID
  159. * @param $beginX_Y
  160. * @param $endX_Y
  161. * @param $run_date
  162. * @return mixed
  163. */
  164. public function firstOrderNeedTime($beginX_Y, $endX_Y, $run_date)
  165. {
  166. if (($beginX_Y == "32.116682,118.784105" && $endX_Y == "32.011714,120.894448") || ($beginX_Y == "32.011714,120.894448" && $endX_Y == "32.116682,118.784105")) {
  167. $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y);
  168. $start1 = strtotime($run_date) + 5 * 60;
  169. $start2 = strtotime($run_date) - 10 * 60;
  170. $end1 = (strtotime($run_date) + $time * 60) + 5 * 60;
  171. $end2 = (strtotime($run_date) + $time * 60) - 10 * 60;
  172. $result['code'] = 0;
  173. $result['result'] = array($run_date, date("Y-m-d H:i", strtotime($run_date) + $time * 60));
  174. $result['str_time'] = array(date("H:i", $start2) . '--' . date("H:i", $start1), date("H:i", $end2) . '--' . date("H:i", $end1));
  175. return $result;
  176. } else {
  177. $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y);
  178. $temp_time_on_the_way = strtotime($run_date) + 210 * 60;
  179. $arrive = date("Y-m-d H:i", $temp_time_on_the_way);
  180. $receive_time = date("Y-m-d H:i", $temp_time_on_the_way - $time * 60);
  181. $result['code'] = 0;
  182. $result['result'] = array($receive_time, $arrive);
  183. $start1 = $temp_time_on_the_way + 5 * 60;
  184. $start2 = $temp_time_on_the_way - 10 * 60;
  185. $end1 = ($temp_time_on_the_way - $time * 60) + 5 * 60;
  186. $end2 = ($temp_time_on_the_way - $time * 60) - 10 * 60;
  187. $result['str_time'] = array(date("H:i", $end2) . '--' . date("H:i", $end1), date("H:i", $start2) . '--' . date("H:i", $start1));
  188. return $result;
  189. }
  190. }
  191. /**
  192. *用于每个车的第一个订单生效后的调配顺序
  193. * @param $orderID
  194. * @param $beginX_Y
  195. * @param $endX_Y
  196. * @param $run_date
  197. * @return mixed
  198. */
  199. public function firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date)
  200. {
  201. //说明是携程下进来的订单
  202. if (($beginX_Y == "32.116682,118.784105" && $endX_Y == "32.011714,120.894448") || ($beginX_Y == "32.011714,120.894448" && $endX_Y == "32.116682,118.784105")) {
  203. $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y);
  204. $temp_time_on_the_way = strtotime($run_date);
  205. $arrive = date("Y-m-d H:i", $temp_time_on_the_way + $time * 60);
  206. $result = array(0 => array("order_id" => $orderID, "time" => $run_date), 1 => array("order_id" => $orderID, "time" => $arrive));
  207. } else { //c端的订单
  208. $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y);
  209. $temp_time_on_the_way = strtotime($run_date) + 210 * 60;
  210. $arrive = date("Y-m-d H:i", $temp_time_on_the_way);
  211. $receive_time = date("Y-m-d H:i", $temp_time_on_the_way - $time * 60);
  212. $result = array(0 => array("order_id" => $orderID, "time" => $receive_time), 1 => array("order_id" => $orderID, "time" => $arrive));
  213. }
  214. return $result;
  215. }
  216. /**
  217. * 向run_bus_station_view表中插入数据
  218. * @param $mem
  219. * @param $orderService
  220. * @param $runID
  221. * @param $res
  222. * @param $bus_order_num
  223. * @param $prodID
  224. */
  225. function makeOrder($mem, $runID, $res, $bus_order_num, $prodID)
  226. {
  227. if ($res == false) {
  228. $pdo = conn();
  229. $sql = "UPDATE run_bus_station_view set cancel_flag =1,update_time=NOW() where run_id =" . $runID . " and bus_no =" . $bus_order_num;
  230. $result = $pdo->query($sql);
  231. return true;
  232. }
  233. $station = "";
  234. $lat_long = "";
  235. $station_type = "";
  236. $station_time = "";
  237. $order_id_list = "";
  238. $order_arr = $mem->get("orderList[$runID]");
  239. $order_index_array = array();
  240. foreach ($order_arr as $bus_id => $orderlist) {
  241. $orderNum = count($orderlist);
  242. foreach ($orderlist as $num => $orderInfo) {
  243. $orderInfo["bus_id"] = $bus_id;
  244. $orderInfo["start_num"] = $num;
  245. $orderInfo["bus_order_count"] = $orderNum;
  246. $order_index_array[$orderInfo["order_id"]] = $orderInfo;
  247. }
  248. }
  249. $start_flag = false;
  250. foreach ($res as $id => $arr) {
  251. if (!isset($order_index_array[$arr['order_id']])) {
  252. continue;
  253. }
  254. $order_info = $order_index_array[$arr['order_id']];
  255. if (!isset($order_info["meet_flag"]) || $order_info["meet_flag"] != 1) {
  256. $order_info["meet_flag"] = 1;
  257. $order_index_array[$arr['order_id']] = $order_info;
  258. $order_id_list .= $arr['order_id'] . ";";
  259. $station .= $order_info['begin_name'] . ";";
  260. $lat_long .= $order_info['startx_y'] . ";";
  261. $station_type .= "0" . ";";
  262. $station_time .= $arr['time'] . ";";
  263. } else {
  264. $order_id_list .= $arr['order_id'] . ";";
  265. $station .= $order_info['end_name'] . ";";
  266. $lat_long .= $order_info['endx_y'] . ";";
  267. $station_type .= "1" . ";";
  268. $station_time .= $arr['time'] . ";";
  269. }
  270. }
  271. $order_id_list = rtrim($order_id_list, ';');
  272. $station = rtrim($station, ';');
  273. $lat_long = rtrim($lat_long, ';');
  274. $station_type = rtrim($station_type, ';');
  275. $station_time = rtrim($station_time, ';');
  276. $res = $this->set_run_station($runID, $bus_order_num, $prodID, $order_id_list, $station, $lat_long, $station_type, $station_time);
  277. return $res;
  278. }
  279. }