Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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