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.
 
 
 
 
 

258 regels
14 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/9/19
  6. * Time: 13:07
  7. */
  8. require_once 'common.php';
  9. class submitOrder extends virtifyUsers
  10. {
  11. function setOrder($prodID, $runID, $orderID, $run_date, $num, $station_start, $station_end)
  12. {
  13. $lineInfo = array(
  14. "449860" => array( //元通浦口线:五塘广场->
  15. "location_str" => "32.1107459388,118.7773547217;31.9958864195,118.7214606940;32.0669047363,118.6480796003;32.1066028030,118.6864933063;32.2440220358,118.7538198552;32.1262075684,120.6999317242;32.0572268781,120.7459405916;32.0230078984,120.9027142484;31.9860627150,120.8867945067;31.9732029880,120.8984345866",
  16. "station_str" => "五塘广场;元通地铁站;南工大地铁站;明发城市广场;葛塘客运站;九华收费站;九圩港客运站;校西客运站;政务中心;中南世纪城",
  17. "station_type" => "0;0;0;0;0;1;1;1;1;1",
  18. "station_time" => "0,20,14,15,20,126,18,25,10,18"
  19. ),
  20. "450036" => array(//南站仙林 南京南站-》
  21. "location_str" => "31.9716001666,118.7971965314;32.0496327360,118.8945605812;32.0848861808,118.9048846157;32.0988533068,118.9304284265;32.1262075684,120.6999317242;32.0572268781,120.7459405916;32.0230078984,120.9027142484;31.9860627150,120.8867945067;31.9732029880,120.8984345866",
  22. "station_str" => "南京南站;马群地铁站;仙鹤门地铁站;仙林中心站;九华收费站;九圩港客运站;校西客运站;政务中心;中南世纪城",
  23. "station_type" => "0;0;0;0;1;1;1;1;1",
  24. "station_time" => "0,20,17,10,126,18,25,10,18"
  25. ),
  26. "450037" => array(//南站仙林线 南通大学-》
  27. "location_str" => "31.9732029880,120.8984345866;31.9860627150,120.8867945067;32.0230078984,120.9027142484;32.0572268781,120.7459405916;32.1262075684,120.6999317242;32.0988533068,118.9304284265;32.0848861808,118.9048846157;32.0496327360,118.8945605812;31.9716001666,118.7971965314",
  28. "station_str" => "中南世纪城;政务中心;校西客运站;九圩港客运站;九华收费站;仙林中心站;仙鹤门地铁站;马群地铁站;南京南站",
  29. "station_type" => "0;0;0;0;0;1;1;1;1",
  30. "station_time" => "0,18,10,25,18,126,10,17,20"
  31. ),
  32. "449865" => array(//元通浦口线:南通大学->
  33. "location_str" => "31.9732029880,120.8984345866;31.9860627150,120.8867945067;32.0230078984,120.9027142484;32.0572268781,120.7459405916;32.1262075684,120.6999317242;32.2440220358,118.7538198552;32.1066028030,118.6864933063;32.0669047363,118.6480796003;31.9958864195,118.7214606940;32.1107459388,118.7773547217",
  34. "station_str" => "中南世纪城;政务中心;校西客运站;九圩港客运站;九华收费站;葛塘客运站;明发城市广场;南工大地铁站;元通地铁站;五塘广场",
  35. "station_type" => "0;0;0;0;0;1;1;1;1;1",
  36. "station_time" => "0,18,10,25,18,126,20,15,14,20"
  37. )
  38. );
  39. //线路1订单列表 元通浦口线:五塘广场->元通地铁站->南工大地铁站->明发城市广场->葛塘客运站->||九华收费站->九圩港客运站->校西客运站->政务中心->南通大学
  40. $order["449860"] = array("1,0,0", "1,0,0", "1,0,0", "1,0,0", "1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0"); //"上车或者下车,订单列表,订单人数"
  41. //线路2
  42. $order["450036"] = array("1,0,0", "1,0,0", "1,0,0", "1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0");
  43. //线路3
  44. $order["449865"] = array("1,0,0", "1,0,0", "1,0,0", "1,0,0", "1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0");
  45. //线路4
  46. $order["450037"] = array("1,0,0", "1,0,0", "1,0,0", "1,0,0", "1,0,0", "-1,0,0", "-1,0,0", "-1,0,0", "-1,0,0");
  47. $orderService = new createOrderService();
  48. $mem = new Memcache();
  49. $mem->connect('127.0.0.1', 11211);
  50. $res = $mem->get("orderList[$runID]");
  51. if ($res) {//该班次已经有班次
  52. $i = 0;
  53. foreach ($res as $k => $v) { //$k:车号 $v:车上的订单号列表
  54. //及算当前车里面的人数
  55. foreach ($v as $item) {
  56. $arr = explode(",", $item);
  57. if ($arr[0] = "1") //说明是上车点
  58. {
  59. $i += $arr[2]; //该车上的总人数
  60. }
  61. }
  62. if ($i >= 6) {
  63. continue;
  64. }
  65. //将该订单添加到当前的车,并更新数据库表run_bus_station_view
  66. $str_order = "";
  67. foreach ($v as $station_id => $station_str) { //$station_id:站点ID $station_str:站点信息
  68. $order_arr = explode(",", $station_str);
  69. if ($station_start == $station_id) { //上车点
  70. if ($order_arr[1] == 0) {
  71. $v[$station_id] = "1" . "," . $orderID . "," . ($order_arr[2] + $num);
  72. $str_order .= $orderID . ";";
  73. } else {
  74. $v[$station_id] = "1" . "," . $order_arr[1] . "|" . $orderID . "," . ($order_arr[2] + $num);
  75. $str_order .= $order_arr[1] . "|" . $orderID . ";";
  76. }
  77. } elseif ($station_end == $station_id) //下车点
  78. {
  79. if ($order_arr[1] == 0) {
  80. $v[$station_id] = "-1" . "," . $orderID . "," . "0";
  81. $str_order .= $orderID . ";";
  82. } else {
  83. $v[$station_id] = "-1" . "," . $order_arr[1] . "|" . $orderID . "," . "0";
  84. $str_order .= $order_arr[1] . "|" . $orderID . ";";
  85. }
  86. } else {
  87. $str_order .= $order_arr[1] . ";";
  88. }
  89. /*if ($order_arr[1] == 0) {
  90. $str_order .= $order_arr[1] . ";";
  91. } else {
  92. $str_order .= $order_arr[1] . "|" . $orderID . ";";
  93. }*/
  94. }
  95. $str_order = rtrim($str_order, ';');
  96. $bus_order[$k] = $v;
  97. $mem->set("orderList[$runID]", $bus_order);
  98. $ress = $this->set_run_station($runID, $k, $prodID, $str_order);
  99. if ($ress) {
  100. $data['code'] = '0';
  101. $data['info'] = '派车成功';
  102. $res_arr['order_id'] = $orderID;
  103. $res_arr['bus_no'] = $k;
  104. $data['result'] = $res_arr;
  105. return $data;
  106. } else {
  107. $data['code'] = '102';
  108. $data['info'] = '插入run_bus_station_view失败,派车失败';
  109. return $data;
  110. }
  111. }
  112. //增派新的车
  113. $stock = $orderService->stock($runID);
  114. if ($stock['bus_num'] < 1) {
  115. $data['code'] = '101';
  116. $data['info'] = '当前班次无可派车辆';
  117. return $data;
  118. }
  119. $str_station_time = "";
  120. $station_time = $run_date;
  121. $arr_station_time = explode(",", $lineInfo[$prodID]['station_time']);
  122. foreach ($arr_station_time as $v) {
  123. $station_time = date("Y-m-d H:i", strtotime($station_time) + $v * 60); //上下车时间
  124. $str_station_time .= $station_time . ";";
  125. }
  126. //拼接订单列表串
  127. $str_order = "";
  128. foreach ($order[$prodID] as $k => $item) {
  129. if ($station_start == $k) { //上车点
  130. $order[$prodID][$k] = "1" . "," . $orderID . "," . $num;
  131. }
  132. if ($station_end == $k) //下车点
  133. {
  134. $order[$prodID][$k] = "-1" . "," . $orderID . "," . 0;
  135. }
  136. $order_arr = explode(",", $order[$prodID][$k]);
  137. $str_order .= $order_arr[1] . ";";
  138. }
  139. $str_order = rtrim($str_order, ';');
  140. $bus_order[$stock['next_blank_car']] = $order[$prodID];
  141. $mem->set("orderList[$runID]", $bus_order);
  142. //TODO:将订单信息存入数据库中
  143. $result = $this->set_run_station($runID, $stock['next_blank_car'], $prodID, $str_order, $lineInfo[$prodID]['station_str'], $lineInfo[$prodID]['location_str'], $lineInfo[$prodID]['station_type'], $str_station_time);
  144. if ($result) {
  145. $data['code'] = '0';
  146. $data['info'] = '派车成功';
  147. $res_arr['order_id'] = $orderID;
  148. $res_arr['bus_no'] = $stock['next_blank_car'];
  149. $data['result'] = $res_arr;
  150. return $data;
  151. } else {
  152. $data['code'] = '102';
  153. $data['info'] = '插入run_bus_station_view失败,派车失败';
  154. return $data;
  155. }
  156. } else { //该班次的第一单
  157. $stock = $orderService->stock($runID);
  158. if ($stock['bus_num'] > 1) {
  159. $data['code'] = '101';
  160. $data['info'] = '当前班次无可派车辆';
  161. return $data;
  162. }
  163. $str_station_time = "";
  164. $station_time = $run_date;
  165. $arr_station_time = explode(",", $lineInfo[$prodID]['station_time']);
  166. foreach ($arr_station_time as $v) {
  167. $station_time = date("Y-m-d H:i", strtotime($station_time) + $v * 60); //上下车时间
  168. $str_station_time .= $station_time . ";";
  169. }
  170. $str_order = "";
  171. foreach ($order[$prodID] as $k => $item) {
  172. if ($station_start == $k) { //上车点
  173. $order[$prodID][$k] = "1" . "," . $orderID . "," . $num;
  174. }
  175. if ($station_end == $k) //下车点
  176. {
  177. $order[$prodID][$k] = "-1" . "," . $orderID . "," . "0";
  178. }
  179. $order_arr = explode(",", $order[$prodID][$k]);
  180. $str_order .= $order_arr[1] . ";";
  181. }
  182. $str_order = rtrim($str_order, ';');
  183. $bus_order[$stock['next_blank_car']] = $order[$prodID];
  184. $mem->set("orderList[$runID]", $bus_order);
  185. //TODO:将订单信息存入数据库中
  186. /*echo $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(" . $runID . ",'" . $stock['next_blank_car'] . "','" . $prodID . "','" . $str_order . "','" . $lineInfo[$prodID]['station_str'] . "','" . $lineInfo[$prodID]['location_str'] . "','" . $lineInfo[$prodID]['station_type'] . "','" . $str_station_time . "',NOW());";*/
  187. $result = $this->set_run_station($runID, $stock['next_blank_car'], $prodID, $str_order, $lineInfo[$prodID]['station_str'], $lineInfo[$prodID]['location_str'], $lineInfo[$prodID]['station_type'], $str_station_time);
  188. if ($result) {
  189. $data['code'] = '0';
  190. $data['info'] = '派车成功';
  191. $res_arr['order_id'] = $orderID;
  192. $res_arr['bus_no'] = $stock['next_blank_car'];
  193. $data['result'] = $res_arr;
  194. return $data;
  195. } else {
  196. $data['code'] = '102';
  197. $data['info'] = '插入run_bus_station_view失败,派车失败';
  198. return $data;
  199. }
  200. }
  201. }
  202. public function set_run_station($run_id, $bus_no, $prod_id, $order_id, $station = "", $lat_long = "", $station_type = "", $station_time = "")
  203. {
  204. $pdo = conn();
  205. $sql = "SELECT id from run_bus_station_view where run_id = '" . $run_id . "' and bus_no='" . $bus_no . "'";
  206. $result = $pdo->query($sql);
  207. $row = $result->fetchAll(PDO::FETCH_ASSOC);
  208. if (!isset($row[0]['id'])) {
  209. $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());";
  210. writeLog("更新run_bus_station_view的sql语句" . json_encode($sql));
  211. $result = $pdo->query($sql);
  212. if (!$result) {
  213. echo $sql;
  214. return false;
  215. }
  216. } else {
  217. $sql = "UPDATE run_bus_station_view set cancel_flag =0,order_id='" . $order_id . "',update_time=NOW() where run_id =" . $run_id . " and bus_no =" . $bus_no;
  218. $result = $pdo->query($sql);
  219. writeLog("更新run_bus_station_view的sql语句" . json_encode($sql));
  220. if (!$result) {
  221. echo $sql;
  222. return FALSE;
  223. }
  224. }
  225. return true;
  226. }
  227. }
  228. $prodID = isset($_REQUEST['prodID']) ? $_REQUEST['prodID'] : false;
  229. $runID = isset($_REQUEST['runID']) ? $_REQUEST['runID'] : false;
  230. $orderID = isset($_REQUEST['orderID']) ? $_REQUEST['orderID'] : false;
  231. $run_date = isset($_REQUEST['run_date']) ? $_REQUEST['run_date'] : false;
  232. $num = isset($_REQUEST['num']) ? $_REQUEST['num'] : false;
  233. $station_start = $_REQUEST['station_start'];
  234. $station_end = $_REQUEST['station_end'];
  235. /*$station_start = isset($_REQUEST['station_start']) ? $_REQUEST['station_start'] : false;
  236. $station_end = isset($_REQUEST['station_end']) ? $_REQUEST['station_end'] : false;*/
  237. if ($prodID && $runID && $orderID && $run_date && $num) {
  238. $a = new submitOrder();
  239. $res = $a->setOrder($prodID, $runID, $orderID, $run_date, $num, $station_start, $station_end);
  240. echo json_encode($res);
  241. } else {
  242. echo $prodID . "--" . $runID . "--" . $orderID . "--" . $run_date . "--" . $num . "--" . $station_start . "--" . $station_end;
  243. }