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.
 
 
 
 

544 lines
16 KiB

  1. <?php
  2. /*
  3. Author:XM
  4. Compeny:Spiders Travel
  5. */
  6. require_once '../Common/Mysql.php';
  7. if (ALLOW_ORIGIN)
  8. header("Access-Control-Allow-Origin:*");
  9. $userid=getUserId();
  10. if ($userid ===false){
  11. echo json_encode(array("code"=>1,"info"=>"用户未登录"));
  12. exit;
  13. }
  14. $lineid=$_GET['lineid'];
  15. $startime=$_GET['startime'];
  16. $endtime=$_GET['endtime'];
  17. $week=$_GET['week'];
  18. $allstation=$_GET['allstation'];
  19. $allcar=$_GET['allcar'];
  20. $allticket=$_GET['allticket'];
  21. $sellday=$_GET['sellday'];
  22. date_default_timezone_set('PRC'); //设置中国时区
  23. $current_time=date("Y-m-d H:i:s");//获取当前时间
  24. $yii_url = 'http://'.CS1_DOMAIN.'/api/dynamic-bus/run-bus-new-save';
  25. $send_data = array(
  26. 'line_id'=>$lineid,
  27. 'start_time'=>$startime,
  28. 'end_time'=>$endtime,
  29. 'week'=>$week,
  30. 'all_station'=>$allstation,
  31. 'all_car'=>$allcar,
  32. 'all_ticket'=>$allticket,
  33. 'sell_day'=>$sellday,
  34. 'current_time'=>$current_time,
  35. 'user_id'=>$userid
  36. );
  37. $res = httpRequest($yii_url,$send_data);
  38. echo $res;
  39. exit;
  40. //$testsql=$userid.",".$lineid.",'".$startime."','".$endtime."','".$week."','".$allstation."','".$allcar."','".$allticket."',".$sellday;
  41. /*$json['code'] = '154';
  42. $json['info'] =$allticket;
  43. echo json_encode($json);
  44. exit;*/
  45. //判断lineid有效性
  46. $line_id = isset($lineid)?trim($lineid):false;
  47. if(!$line_id )
  48. {
  49. $json['code'] = '154';
  50. $json['info'] = '线路产品参数错误';
  51. echo json_encode($json);
  52. exit;
  53. }
  54. //获得run_main表数据数量并判断星期日期有效性
  55. for($i=0;$i<strlen($week);$i++){
  56. $arr[$i] = $week[$i];
  57. }
  58. $start_time = strtotime($startime);
  59. $end_time = strtotime($endtime);
  60. $record_num = 0;
  61. for($i = $start_time; $i <= $end_time; $i += 86400) {
  62. if(in_array(date('w',$i),$arr)){
  63. $record_num++;
  64. }
  65. }
  66. if($record_num == 0){
  67. $json['code'] = '2';
  68. $json['info'] = '日期星期选择有误';
  69. echo json_encode($json) ;
  70. }/*else{
  71. $json['code'] = '2';
  72. $json['info'] = $record_num;
  73. echo json_encode($json) ;
  74. }*/
  75. //在计数器里生成run_id
  76. $pdo=conn();
  77. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//mysql错误时抛出异常
  78. $pdo->beginTransaction(); //开始事务
  79. try{
  80. $get_run_id_update_sql = "UPDATE BASE_UNIQUE_ID2 SET UNIQUE_ID = UNIQUE_ID + ${record_num} WHERE ID = 3;";
  81. $get_run_id_result = $pdo->exec($get_run_id_update_sql);
  82. $get_run_id_select_sql = "SELECT UNIQUE_ID As total_num FROM BASE_UNIQUE_ID2 WHERE ID = 3";
  83. $result1 = $pdo->query($get_run_id_select_sql);
  84. $result1_rowset = $result1->fetchAll(PDO::FETCH_ASSOC);
  85. $start_num = $result1_rowset[0]["total_num"]-$record_num+1;
  86. $end_num = $result1_rowset[0]["total_num"];
  87. /*$result1->closeCursor();*/
  88. if (empty($result1_rowset)) {
  89. throw new PDOException('生成run_id失败');
  90. }
  91. //处理get参数allstation
  92. //获取station数量
  93. $station_count = substr_count($allstation,'{');
  94. if($station_count == 0){
  95. $json['code'] = '2';
  96. $json['info'] = '数据有误';
  97. echo json_encode($json) ;
  98. }
  99. $station_arr = (explode('}',$allstation));
  100. array_pop($station_arr);
  101. $station_str1 = $station_arr[0];
  102. $station_arr1 = explode(',',$station_str1);
  103. $run_minutes = $station_arr1[1];//得到all_station中的第一个run_minutes
  104. $station_str2 = floor($run_minutes/60);
  105. $station_str3 = ($run_minutes%60)<10?($run_minutes%60) . '0' :($run_minutes%60);
  106. $run_time = $station_str2 . ':' . $station_str3; //拼出对应的run_time
  107. //处理run_station数据
  108. $station_res_ids = array();
  109. $station_run_minutes = array();
  110. $station_inout_types = array();
  111. $station_checkport_ids = array();
  112. $station_run_times = array();
  113. foreach($station_arr as $val ) {
  114. $val = substr($val, 1);
  115. $station_arr2 = explode(',', $val);
  116. $station_str4 = floor($station_arr2[1] / 60);
  117. $station_str5 = ($station_arr2[1] % 60) < 10 ? ($station_arr2[1] % 60) . '0' : ($station_arr2[1] % 60);
  118. $station_run_time = $station_str4 . ':' . $station_str5; //拼出$run_time
  119. $station_res_ids[] = $station_arr2[0];
  120. $station_run_minutes[] = $station_arr2[1];
  121. $station_inout_types[] = $station_arr2[2];
  122. $station_checkport_ids[] = $station_arr2[3];
  123. $station_run_times[] = $station_run_time;
  124. }
  125. //station_order_id 和 area_id 从opera_station表里面取
  126. $opera_station_sql = "SELECT order_id,
  127. res_id,
  128. area_id
  129. FROM opera_station WHERE LINE_ID = {$lineid}";
  130. $opera_station_result = $pdo->query($opera_station_sql);
  131. $opera_station_rowset = $opera_station_result->fetchAll(PDO::FETCH_ASSOC);
  132. if (empty($opera_station_rowset) ) {
  133. throw new PDOException('获取站点信息失败');
  134. }
  135. $opera_station_array = array();
  136. foreach( $opera_station_rowset as $opera_station_info ) {
  137. $opera_station_array[$opera_station_info["res_id"]] = $opera_station_info;
  138. }
  139. /*$opera_station_result->closeCursor();*/
  140. //处理参数$allcar
  141. $car_count = substr_count($allcar,'{');//获得数据组数
  142. if($car_count == 0){
  143. $json['code'] = '2';
  144. $json['info'] = '数据有误';
  145. echo json_encode($json) ;
  146. }
  147. $car_arr = (explode('}',$allcar));
  148. array_pop($car_arr);
  149. //处理run_bus数据
  150. $bus_type_res_ids = array();
  151. $allow_select_seats = array();
  152. $brand_res_ids = array();
  153. $cost_motorcade_ids = array();
  154. $cost_types = array();
  155. $cost_prices = array();
  156. foreach($car_arr as $val ) {
  157. $val = substr($val, 1);
  158. $car_arr2 = explode(',', $val);
  159. $bus_type_res_ids[] = $car_arr2[0];
  160. $allow_select_seats[] = $car_arr2[2];
  161. $brand_res_ids[] = $car_arr2[3];
  162. $cost_motorcade_ids[] = $car_arr2[4];
  163. $cost_types[] = $car_arr2[5];
  164. $cost_prices[]=$car_arr2[6];
  165. }
  166. //处理参数allticket
  167. $ticket_count = substr_count($allticket,'{');//获得数据组数
  168. if($ticket_count == 0){
  169. $json['code'] = '2';
  170. $json['info'] = '数据有误';
  171. echo json_encode($json) ;
  172. }
  173. $ticket_arr = (explode('}',$allticket));
  174. array_pop($ticket_arr);
  175. //处理run_prod数据
  176. $start_station_area_ids = array();
  177. $end_station_area_ids = array();
  178. $seat_types = array();
  179. $human_types = array();
  180. $max_counts = array();
  181. $opera_tickets_results = array();
  182. foreach($ticket_arr as $val ) {
  183. $val = substr($val, 1);
  184. $ticket_arr2 = explode(',', $val);
  185. $start_station_area_ids[] = $ticket_arr2[0];
  186. $end_station_area_ids[]=$ticket_arr2[1];
  187. $seat_types[] = $ticket_arr2[2];
  188. $human_types[] = $ticket_arr2[3];
  189. $max_counts[] = $ticket_arr2[4];
  190. $opera_tickets_sql = "SELECT
  191. TICKET_ID,
  192. START_STATION_RES_ID,
  193. END_STATION_RES_ID
  194. FROM
  195. opera_tickets
  196. WHERE
  197. START_STATION_AREA_ID = $ticket_arr2[0]
  198. AND END_STATION_AREA_ID = $ticket_arr2[1]
  199. AND SEAT_TYPE = $ticket_arr2[2]
  200. AND HUMAN_TYPE = $ticket_arr2[3]
  201. AND LINE_ID = $lineid
  202. AND CANCEL_FLAG = 0";
  203. $opera_tickets_result = $pdo->query($opera_tickets_sql);
  204. $opera_tickets_results[] = $opera_tickets_result->fetchAll(PDO::FETCH_ASSOC);
  205. $tes = array(
  206. 'code'=>0,
  207. 'info'=>$opera_tickets_results
  208. );
  209. echo json_encode($tes);
  210. exit;
  211. if (empty($opera_tickets_result) ) {
  212. throw new PDOException('获取车票信息失败');
  213. }
  214. }
  215. //生成run_x表
  216. $monarr=array();
  217. $start_month = date('Y-m',strtotime($startime));
  218. /*$aaa = strtotime("$start_month+1 month");
  219. $bbb = strtotime('2018-01');
  220. $aab = strtotime($endtime);*/
  221. for($i=0; (strtotime("$start_month+$i month")) <= strtotime($endtime); $i++){
  222. $timex = strtotime("$start_month+$i month");
  223. $monarr[] = 'run_' . date('Ym',$timex); // 取得递增月;
  224. }
  225. foreach ($monarr as $val ){
  226. $create_run_x_sql .= "create table if not EXISTS {$val}
  227. (ID int(11) not null auto_increment,PRIMARY KEY (ID),
  228. RUN_ID int(11) NOT NULL DEFAULT 0,
  229. RUN_LOCK_TYPE int(11) NOT NULL DEFAULT 0,
  230. RUN_DATE varchar(20) NOT NULL DEFAULT '',
  231. LOCK_COUNT int(11) NOT NULL DEFAULT 1,
  232. ORDER_ID int(11) NOT NULL DEFAULT 0,
  233. STATION_RES_ID int(11) NOT NULL DEFAULT 0,
  234. STATION_INOUT_TYPE int(11) NOT NULL DEFAULT 0,
  235. BUS_ORDER_ID int(11) NOT NULL DEFAULT 0,
  236. BUS_RES_ID int(11) NOT NULL DEFAULT 0,
  237. SEAT_X int(11) NOT NULL DEFAULT 0,
  238. SEAT_Y int(11) NOT NULL DEFAULT 0,
  239. SEAT_TYPE int(11) NOT NULL DEFAULT 0,
  240. SEAT_SEQ_ID int(11) NOT NULL DEFAULT 0,
  241. SEAT_NAME varchar(100) NOT NULL DEFAULT '',
  242. SEAT_STATUS int(11) NOT NULL DEFAULT 0,
  243. CREATE_USER_ID int(11) NOT NULL DEFAULT 0,
  244. CREATE_TIME varchar(20) NOT NULL DEFAULT '',
  245. ORDER_MAIN_ID int(11) NOT NULL DEFAULT 0,
  246. ORDER_MAIN_PROD_ID int(11) NOT NULL DEFAULT 0,
  247. ORDER_PROD_ID int(11) NOT NULL DEFAULT 0,
  248. ORDER_SEQ_ID int(11) NOT NULL DEFAULT 0,
  249. ORDER_LOCK_USER_ID int(11) NOT NULL DEFAULT 0,
  250. ORDER_LOCK_TIME datetime NOT NULL,
  251. ORDER_LOCK_END_TIME datetime NOT NULL,
  252. HUMAN_TYPE int(11) NOT NULL DEFAULT 0,
  253. AREA_ID int(11) NOT NULL DEFAULT 0,
  254. ALLOW_SELECT_SEAT int(11) NOT NULL DEFAULT 0,
  255. CHECK_USER_ID INT(11) NOT NULL DEFAULT 0,
  256. CHECK_TIME VARCHAR(20) DEFAULT '' NOT NULL,
  257. CANCEL_FLAG TINYINT NOT NULL DEFAULT 1,
  258. INDEX IX_{$val}(RUN_ID, BUS_ORDER_ID) USING BTREE );";
  259. }
  260. $create_run_x_result = $pdo->exec($create_run_x_sql);
  261. //TODO
  262. //数据写入数据库
  263. //插入run_main表
  264. $tmp_run_date = $startime;
  265. for($insert_run_id = $start_num;$insert_run_id<=$end_num;$insert_run_id++){
  266. $tmp_run_x = 'run_' . date('Ym',strtotime($tmp_run_date));
  267. $run_main_sql = "INSERT INTO run_main (run_id,run_lock_type,run_date,run_time,run_minutes,prod_id,
  268. create_user_id,create_time,update_user_id,update_time,run_status)
  269. VALUES ( {$insert_run_id},
  270. 121,
  271. '{$tmp_run_date}',
  272. '{$run_time}',
  273. {$run_minutes},
  274. {$lineid},
  275. {$userid},
  276. '{$current_time}',
  277. {$userid},
  278. '{$current_time}',
  279. 137)";
  280. $run_main_result = $pdo->exec($run_main_sql);
  281. /*if ($pdo->errorCode() != '00000') {
  282. $errorArr = $pdo->errorInfo();
  283. throw new PDOException($errorArr[2]);
  284. }*/
  285. //$tmp_run_date = date('Y-m-d',strtotime('+1 days',strtotime($tmp_run_date)));
  286. //插入run_station表
  287. $run_station_sql='';
  288. for($i=0;$i<$station_count;$i++){
  289. $opera_station_info = $opera_station_array[$station_res_ids[$i]];
  290. $station_order_id = $opera_station_info["order_id"];
  291. $area_id = $opera_station_info["area_id"];
  292. $run_station_sql .= "INSERT INTO run_station (run_id,prod_id,start_time,start_minutes,station_res_id,
  293. station_inout_type,checkport_res_id,create_user_id,create_time,station_order_id,area_id)
  294. VALUES({$insert_run_id},
  295. {$lineid},
  296. '{$station_run_times[$i]}',
  297. {$station_run_minutes[$i]},
  298. {$station_res_ids[$i]},
  299. {$station_inout_types[$i]},
  300. {$station_checkport_ids[$i]},
  301. {$userid},
  302. '{$current_time}',
  303. {$station_order_id},
  304. {$area_id}
  305. );";
  306. }
  307. $run_station_result = $pdo->exec($run_station_sql);
  308. /*if ($pdo->errorCode() != '00000') {
  309. $errorArr = $pdo->errorInfo();
  310. throw new PDOException($errorArr[2]);
  311. }*/
  312. //插入run_bus表
  313. $run_bus_sql='';
  314. $run_x_sql='';
  315. for($j=0;$j<$car_count;$j++){
  316. $run_bus_sql .= "INSERT INTO run_bus (create_user_id,create_time,update_user_id,run_id,bus_order_id,
  317. bus_type_res_id,allow_select_seat,brand_res_id,cost_motorcade_id,cost_type,cost_price,seat_count)
  318. VALUES ({$userid},
  319. '{$current_time}',
  320. {$userid},
  321. {$insert_run_id},
  322. {$j}+1,
  323. {$bus_type_res_ids[$j]},
  324. {$allow_select_seats[$j]},
  325. {$brand_res_ids[$j]},
  326. {$cost_motorcade_ids[$j]},
  327. {$cost_types[$j]},
  328. {$cost_prices[$j]},
  329. (select count(*) from base_resource_matrix
  330. where RES_ID = {$bus_type_res_ids[$j]}
  331. and pos_type not in (0,104,105)
  332. and cancel_flag = 0)
  333. );";
  334. //同时插入run_x表
  335. $run_x_sql .= "INSERT INTO {$tmp_run_x}
  336. ( RUN_ID,
  337. RUN_LOCK_TYPE,
  338. RUN_DATE,
  339. ORDER_ID,
  340. STATION_RES_ID,
  341. STATION_INOUT_TYPE,
  342. BUS_ORDER_ID,
  343. BUS_RES_ID,
  344. SEAT_X,
  345. SEAT_Y,
  346. SEAT_TYPE,
  347. SEAT_SEQ_ID,
  348. SEAT_NAME,
  349. SEAT_STATUS,
  350. CREATE_USER_ID,
  351. CREATE_TIME,
  352. ORDER_LOCK_TIME,
  353. ORDER_LOCK_END_TIME,
  354. AREA_ID,
  355. ALLOW_SELECT_SEAT)
  356. SELECT
  357. {$insert_run_id},
  358. 121,
  359. '{$tmp_run_date}',
  360. b.station_order_id,
  361. b.station_res_id,
  362. b.station_inout_type,
  363. {$j}+1,
  364. {$bus_type_res_ids[$j]},
  365. a.POS_X,
  366. a.POS_Y,
  367. a.POS_TYPE,
  368. a.POS_SEQ_ID,
  369. a.POS_NAME,
  370. 0,
  371. {$userid},
  372. '{$current_time}',
  373. '{$current_time}',
  374. '{$current_time}',
  375. b.area_id,
  376. {$allow_select_seats[$j]}
  377. FROM
  378. base_resource_matrix AS a,
  379. run_station AS b
  380. WHERE
  381. a.RES_ID = {$bus_type_res_ids[$j]}
  382. AND a.cancel_flag = 0
  383. AND b.run_id = {$insert_run_id}
  384. /*AND b.STATION_ORDER_ID <> (
  385. SELECT
  386. max(STATION_ORDER_ID)
  387. FROM
  388. run_station
  389. WHERE
  390. run_id = {$insert_run_id}
  391. )*/;";
  392. }
  393. $run_bus_result = $pdo->exec($run_bus_sql);
  394. /*if ($pdo->errorCode() != '00000') {
  395. $errorArr = $pdo->errorInfo();
  396. throw new PDOException($errorArr[2]);
  397. }*/
  398. $run_x_result = $pdo->exec($run_x_sql);
  399. $tmp_run_date = date('Y-m-d',strtotime('+1 days',strtotime($tmp_run_date)));
  400. /*if ($pdo->errorCode() != '00000') {
  401. $errorArr = $pdo->errorInfo();
  402. throw new PDOException($errorArr[2]);
  403. }*/
  404. //插入run_prod表
  405. $run_prod_sql='';
  406. for($k = 0;$k < $ticket_count;$k++){
  407. foreach($opera_tickets_results[$k] as $val ){
  408. $run_prod_sql .= "INSERT INTO run_prod
  409. ( RUN_ID,
  410. PARENT_PROD_ID,
  411. PROD_ID,
  412. LIST_SEQ_ID,
  413. MAX_COUNT,
  414. SALED_COUNT,
  415. CREATE_USER_ID,
  416. CREATE_TIME,
  417. UPDATE_USER_ID,
  418. START_STATION_RES_ID,
  419. START_STATION_AREA_ID,
  420. END_STATION_RES_ID,
  421. END_STATION_AREA_ID,
  422. HUMAN_TYPE,
  423. SEAT_TYPE)
  424. VALUES (
  425. {$insert_run_id},
  426. {$lineid},
  427. {$val['TICKET_ID']},
  428. 0,
  429. {$max_counts[$k]},
  430. 0,
  431. {$userid},
  432. '{$current_time}',
  433. {$userid},
  434. {$val['START_STATION_RES_ID']},
  435. {$start_station_area_ids[$k]},
  436. {$val['END_STATION_RES_ID']},
  437. {$end_station_area_ids[$k]},
  438. {$human_types[$k]},
  439. {$seat_types[$k]}
  440. );";
  441. }
  442. $run_prod_result = $pdo->exec($run_prod_sql);
  443. /*if ($pdo->errorCode() != '00000') {
  444. $errorArr = $pdo->errorInfo();
  445. throw new PDOException($errorArr[2]);
  446. }*/
  447. }
  448. //插入run_stock表
  449. $run_stock_sql='';
  450. for($l = 0;$l < $station_count;$l++){
  451. $run_stock_sql .= "INSERT INTO run_stock(
  452. run_id,
  453. seq_id,
  454. res_id,
  455. prod_id,
  456. create_user_id,
  457. create_time,
  458. update_time,
  459. update_user_id,
  460. total_count,
  461. seat_type)
  462. SELECT
  463. {$insert_run_id},
  464. {$l}+1,
  465. {$station_res_ids[$l]},
  466. {$lineid},
  467. {$userid},
  468. '{$current_time}',
  469. '{$current_time}',
  470. {$userid},
  471. count(ID),
  472. seat_type
  473. FROM
  474. {$tmp_run_x}
  475. WHERE
  476. RUN_ID = {$insert_run_id}
  477. AND STATION_RES_ID = {$station_res_ids[$l]}
  478. GROUP BY SEAT_TYPE;";
  479. }
  480. $run_stock_result = $pdo->exec($run_stock_sql);
  481. /*if ($pdo->errorCode() != '00000') {
  482. $errorArr = $pdo->errorInfo();
  483. throw new PDOException($errorArr[2]);
  484. }*/
  485. }
  486. $pdo->commit();
  487. $data = array();
  488. $data['code'] = '0';
  489. $data['info'] = '添加班次成功';
  490. echo json_encode($data);
  491. }catch(PDOException $e){
  492. $pdo->rollBack();
  493. $data = array();
  494. $data['code'] = '2';
  495. $data['info'] = $e->getMessage();
  496. $data['error_info'] = $e->getMessage();
  497. echo json_encode($data);
  498. exit;
  499. }
  500. /*$sql="call sp_bus_run_new_save(".$userid.",".$lineid.",'".$startime."','".$endtime."','".$week."','".$allstation."','".$allcar."','".$allticket."',".$sellday.")";
  501. writeLog("run_bus_new_save: ".$sql);
  502. if (is_object($pdo)) {
  503. $result = $pdo->query($sql);
  504. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  505. $data['code'] = "0";
  506. $data['info'] = "";
  507. $data['save'] = $rowset[0];
  508. echo json_encode($data);
  509. }*/