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.
 
 
 
 
 

423 lines
15 KiB

  1. <?php
  2. date_default_timezone_set('GMT');
  3. //载入配置文件和连接数据库
  4. $start_area = trim($_POST['start_area_id']);
  5. $start_res = trim($_POST['start_res_id']);
  6. $end_area = trim($_POST['end_area_id']);
  7. $end_res = trim($_POST['end_res_id']);
  8. $run_id = trim($_POST['run_id']);
  9. $price_type = 1;
  10. //$line_id = 152248;
  11. $line_id = '';//线路ID
  12. $run_date = '';//出发时间
  13. $price_name = '';//分销价或零售价
  14. //寻找返程对应线路编码
  15. $line_code = '';
  16. $line_str1 = '';
  17. $line_str2 = '';
  18. $line_str3 = '';
  19. $line_str_back = '';
  20. $back_line = '';
  21. //巴士类型
  22. $bus_type = '';
  23. //班次站点信息
  24. $run_station = array();
  25. //顺序站点
  26. $res_station_seq = array();
  27. //余票信息
  28. $ticket_over = array();
  29. $ticket_over_array = array();
  30. $ticket_over_id = array();
  31. //途经站点
  32. $res_array = array();
  33. $cen_station = array();//只有ID
  34. $center_station = array();
  35. //全程公里数
  36. $center_distance = 0;
  37. //票种信息
  38. $ticket_info = array();
  39. $ticket_go = array();
  40. $ticket_back = array();
  41. $ticket = array();
  42. $back_ticket_go = array();
  43. $back_ticket_back = array();
  44. //线路可选的票种数组
  45. $line_ticket_id = array();
  46. $line_ticket_info = array();
  47. //中间时间
  48. $center_time = '';
  49. //上车站数组
  50. $start_res_array = array();
  51. //下车站数组
  52. $end_res_array = array();
  53. //地图
  54. $map = array();
  55. //终极数组
  56. $final = array();
  57. if($price_type ==1){
  58. $price_name = "cus_price";
  59. }else{
  60. $price_name = "prod_price";
  61. }
  62. $sql = "SELECT
  63. a.run_id,
  64. a.run_status,
  65. a.run_date,
  66. a.prod_id,
  67. b.start_time,
  68. b.station_order_id,
  69. b.station_res_id,
  70. b.station_inout_type,
  71. b.area_id
  72. FROM
  73. run_main AS a,
  74. run_station AS b
  75. WHERE
  76. a.run_id = b.run_id
  77. AND a.run_id = " . $run_id . "
  78. AND a.run_status = 138
  79. ORDER BY
  80. a.run_id,
  81. b.station_order_id ";
  82. $result=$pdo->query($sql);
  83. $run_station = $result->fetchAll(PDO::FETCH_ASSOC);
  84. writeLog($sql);
  85. $line_id = $run_station[0]['prod_id'];
  86. $run_date = $run_station[0]['run_date'];
  87. $sql = "SELECT
  88. run_id,
  89. res_id,
  90. seq_id,
  91. prod_id,
  92. total_count,
  93. saled_count
  94. FROM
  95. run_stock
  96. WHERE
  97. run_id = " . $run_id . "
  98. AND seat_type = 72
  99. ORDER BY
  100. run_id ASC,
  101. seq_id ASC";
  102. $result=$pdo->query($sql);
  103. $ticket_over_id = $result->fetchAll(PDO::FETCH_ASSOC);
  104. writeLog($sql);
  105. foreach($ticket_over_id as $k => $v){
  106. $ticket_over[$v['res_id']] = $v['total_count'] - $v['saled_count'];
  107. }
  108. $sql = "select prod_id from run_prod where run_id = " . $run_id;
  109. $result=$pdo->query($sql);
  110. $line_ticket_id = $result->fetchAll(PDO::FETCH_ASSOC);
  111. writeLog($sql);
  112. foreach($line_ticket_id as $k => $v){
  113. $line_ticket_info[$v['prod_id']] = $v['prod_id'];
  114. }
  115. foreach($line_array[$line_id]['ticket_info']['go'] as $k => $v){
  116. if(in_array($k,$line_ticket_info) && $v['start_area'] == $start_area && $v['end_area'] == $end_area){
  117. $start_res_array[$v['start_res']]['res_id'] = $v['start_res'];
  118. $start_res_array[$v['start_res']]['res_name'] = $v['start_res_name'];
  119. $end_res_array[$v['end_res']]['res_id'] = $v['end_res'];
  120. $end_res_array[$v['end_res']]['res_name'] = $v['end_res_name'];
  121. }else{
  122. unset($line_array[$line_id]['ticket_info']['go'][$k]);
  123. }
  124. }
  125. //print_r($start_res_array);print_r($end_res_array);
  126. foreach($run_station as $k => $v){
  127. $res_id_array[$v['station_res_id']] = $v['station_res_id'];
  128. $res_station_seq[$v['station_res_id']] = $v['station_order_id'];
  129. if(isset($start_res_array[$v['station_res_id']]) && ($v['station_inout_type'] == 108 || $v['station_inout_type'] == 109)){
  130. $start_res_array[$v['station_res_id']]['start_time'] = $v['start_time'];
  131. }elseif(isset($end_res_array[$v['station_res_id']])){
  132. unset($start_res_array[$v['station_res_id']]);
  133. }
  134. if(isset($end_res_array[$v['station_res_id']]) && ($v['station_inout_type'] == 110 || $v['station_inout_type'] == 109)){
  135. $end_res_array[$v['station_res_id']]['start_time'] = $v['start_time'];
  136. }elseif(isset($end_res_array[$v['station_res_id']])){
  137. unset($end_res_array[$v['station_res_id']]);
  138. }
  139. }
  140. foreach($start_res_array as $k => $v){
  141. $res_array1[$v['start_time']] = $v;
  142. }
  143. foreach($end_res_array as $k => $v){
  144. $res_array1[$v['start_time']] = $v;
  145. }
  146. ksort($res_array1);
  147. foreach($line_array[$line_id]['ticket_info']['go'] as $k=>$v){
  148. if($v['start_area'] == $start_area && $v['start_res'] == $start_res && $v['end_area'] == $end_area && $v['end_res'] == $end_res){
  149. $ticket_info = $line_array[$line_id]['ticket_info']['go'][$k];
  150. }
  151. }
  152. if(!empty($ticket_info)){
  153. //将基本数据放入数组中
  154. $final['start_res_id'] = $start_res;
  155. $final['start_res_name'] = $ticket_info['start_res_name'];
  156. $final['end_res_id'] = $end_res;
  157. $final['end_res_name'] = $ticket_info['end_res_name'];
  158. if($end_res_array[$final['end_res_id']]['start_time'] < $start_res_array[$final['start_res_id']]['start_time']){
  159. $center_time = strtotime($end_res_array[$final['end_res_id']]['start_time']) + 24*3600 - strtotime($start_res_array[$final['start_res_id']]['start_time']);
  160. }else{
  161. $center_time = strtotime($end_res_array[$final['end_res_id']]['start_time']) - strtotime($start_res_array[$final['start_res_id']]['start_time']);
  162. }
  163. $final['start_time'] = $start_res_array[$final['start_res_id']]['start_time'];
  164. $final['end_time'] = $end_res_array[$final['end_res_id']]['start_time'];
  165. $final['center_time'] = date('用时G小时i分钟',$center_time);
  166. //print_r($final);
  167. //途经站点信息($center_station经度,纬度,里程)
  168. $sta_status = 0;
  169. foreach($res_array1 as $k => $v){
  170. if($v['res_id'] == $final['start_res_id']){
  171. $sta_status++;
  172. }
  173. if($sta_status > 0){
  174. $cen_station[$v['res_id']] = $v['res_id'];
  175. }
  176. if($v['res_id'] == $final['end_res_id']){
  177. $sta_status = 0;
  178. }
  179. $res_array[$v['res_id']] = $v;
  180. }
  181. $sql = "SELECT
  182. a.res_id,
  183. a.property AS longitude,
  184. b.property AS latitude,
  185. c.distance
  186. FROM
  187. base_resource_property AS a,
  188. base_resource_property AS b,
  189. opera_product_station AS c
  190. WHERE
  191. a.res_id = c.res_id
  192. AND a.res_id = b.res_id
  193. AND a.type_id = 212
  194. AND b.type_id = 213
  195. AND a.cancel_flag = 0
  196. AND b.cancel_flag = 0
  197. AND a.res_id IN (" . implode(",",$cen_station) . ")
  198. AND c.prod_id = " . $line_id . "
  199. GROUP BY
  200. a.res_id";
  201. $result=$pdo->query($sql);
  202. $cen_station = $result->fetchAll(PDO::FETCH_ASSOC);
  203. writeLog($sql);
  204. foreach($cen_station as $v){
  205. $cen_station1[$res_station_seq[$v['res_id']]] = $v;
  206. }
  207. ksort($cen_station1);
  208. foreach($cen_station1 as $v){
  209. $center_station[$v['res_id']] = $v;
  210. $center_station[$v['res_id']]['res_name'] = $res_array[$v['res_id']]['res_name'];
  211. //全程公里数
  212. //$center_distance += $v['distance'];
  213. }
  214. $sql = "select order_id,res_id,distance from opera_product_station where prod_id = " . $line_id . " order by order_id";
  215. $result=$pdo->query($sql);
  216. $cen_distance_array = $result->fetchAll(PDO::FETCH_ASSOC);
  217. writeLog($sql);
  218. $cen_status = 0;
  219. foreach($cen_distance_array as $k => $v){
  220. if($v['res_id'] == $start_res){
  221. $cen_status++;
  222. }
  223. if($cen_status > 0){
  224. $center_distance += $v['distance'];
  225. }
  226. if($v['res_id'] == $end_res){
  227. break;
  228. }
  229. }
  230. //print_r($center_station);
  231. //全程公里数
  232. if($center_distance != 0){
  233. $final['distance'] = (string)$center_distance;
  234. }else{
  235. $final['distance'] = '30';
  236. }
  237. //票种信息
  238. $ticket_go = $ticket_info;
  239. $ticket_back = array();
  240. foreach($line_array[$line_id]['ticket_info']['back'] as $k => $v){
  241. if($v['start_area'] == $start_area && $v['start_res'] == $start_res && $v['end_area'] == $end_area && $v['end_res'] == $end_res){
  242. $ticket_back = $v;
  243. }
  244. }
  245. $tick_over_status = 0;
  246. foreach($ticket_over as $k => $v){
  247. if($k == $final['end_res_id']){
  248. break;
  249. }
  250. if($k == $final['start_res_id'] || $tick_over_status > 0){
  251. $ticket_over_array[] = $v;
  252. $tick_over_status++;
  253. }
  254. }
  255. asort($ticket_over_array);
  256. $ticket_over_array = array_values($ticket_over_array);
  257. if(!empty($ticket_go) && !empty($ticket_back)){
  258. //根据线路编码找相反线路
  259. if($bus_type == 255){
  260. $line_code = $line_array[$line_id]['line_info']['line_no'];
  261. $line_str1 = substr($line_code,0,2);
  262. $line_str2 = substr($line_code,2,2);
  263. $line_str3 = substr($line_code,4,2);
  264. $line_str_back = $line_str2.$line_str1.$line_str3;
  265. foreach($line_array as $line_key => $line_vel){
  266. if(strpos($line_vel['line_info']['line_no'],$line_str_back) !== false){
  267. $back_line = $line_key;
  268. break;
  269. }
  270. }
  271. if(!empty($back_line)){
  272. foreach($line_array[$back_line]['ticket_info']['go'] as $k => $v){
  273. if($v['start_area'] == $end_area && $v['end_area'] == $start_area){
  274. $back_ticket_go = $v;
  275. break;
  276. }
  277. }
  278. foreach($line_array[$back_line]['ticket_info']['back'] as $k => $v){
  279. if($v['start_area'] == $end_area && $v['end_area'] == $start_area){
  280. $back_ticket_back = $v;
  281. break;
  282. }
  283. }
  284. }
  285. }else{
  286. $line_code = $line_array[$line_id]['line_info']['prod_name'];
  287. $line_str = explode("-",$line_code);
  288. $line_str_back = $line_str[1] . "-" . $line_str[0];
  289. foreach($line_array as $line_key => $line_vel){
  290. if($line_vel['line_info']['prod_name'] == $line_str_back){
  291. $back_line = $line_key;
  292. break;
  293. }
  294. }
  295. if(!empty($back_line)){
  296. foreach($line_array[$back_line]['ticket_info']['go'] as $k => $v){
  297. if($v['start_area'] == $end_area && $v['end_area'] == $start_area){
  298. $back_ticket_go = $v;
  299. break;
  300. }
  301. }
  302. foreach($line_array[$back_line]['ticket_info']['back'] as $k => $v){
  303. if($v['start_area'] == $end_area && $v['end_area'] == $start_area){
  304. $back_ticket_back = $v;
  305. break;
  306. }
  307. }
  308. }
  309. //print_r($back_line);
  310. }
  311. //print_r($back_ticket_go); print_r($back_ticket_back);
  312. if(!empty($back_ticket_go) && !empty($back_ticket_back)){
  313. $ticket['ticket_id_go'] = $ticket_go['prod_id'];
  314. $ticket['go_price'] = (string)((int)$ticket_go[$price_name]);
  315. $ticket['ticket_id_back'] = $ticket_back['prod_id'];
  316. $ticket['discount_price'] = (string)(ceil($ticket_go[$price_name] + $back_ticket_go[$price_name] - $ticket_back[$price_name] - $back_ticket_back[$price_name]));
  317. setcookie("go_price_go", $ticket['go_price'], time() + 36000);
  318. setcookie("go_price_back", $ticket_back[$price_name], time() + 36000);
  319. $ticket['ticket_over'] = (string)$ticket_over_array[0];
  320. }else{
  321. $ticket['ticket_id_go'] = $ticket_go['prod_id'];;
  322. $ticket['go_price'] = (string)((int)$ticket_go[$price_name]);
  323. $ticket['ticket_id_back'] = "";
  324. $ticket['back_price'] = "";
  325. $ticket['discount_price'] = "0";
  326. $ticket['ticket_over'] = (string)$ticket_over_array[0];
  327. }
  328. }elseif(!empty($ticket_go) ){
  329. $ticket['ticket_id_go'] = $ticket_go['prod_id'];;
  330. $ticket['go_price'] = (string)((int)$ticket_go[$price_name]);
  331. $ticket['ticket_id_back'] = "";
  332. $ticket['discount_price'] = "0";
  333. $ticket['ticket_over'] = (string)$ticket_over_array[0];
  334. }else{
  335. $ticket['ticket_id_go'] = "";
  336. $ticket['go_price'] = "";
  337. $ticket['ticket_id_back'] = "";
  338. $ticket['discount_price'] = "0";
  339. $ticket['ticket_over'] = '0';
  340. }
  341. $final['ticket'] = $ticket;
  342. $final['ticket']['site'] = "普通座";
  343. $final['ticket']['run_type'] = "单程";
  344. //地图
  345. $map = array_merge($center_station);
  346. $final['map']['start'] = $map[0];
  347. $final['map']['center'] = array_merge(array_slice($map,1,-1));
  348. $map_end = array_merge(array_slice($map,-1,1));
  349. $final['map']['end'] = $map_end[0];
  350. $json = array();
  351. $json['code'] = '0';
  352. $json['info'] = "返回数据成功";
  353. $json['list'] = $final;
  354. }else{
  355. $json = array();
  356. $json['code'] = '1';
  357. $json['info'] = "无票种";
  358. $json['list'] = array();
  359. }
  360. //print_r($json);
  361. echo json_encode($json);
  362. ?>