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.
 
 
 
 

517 lines
21 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 张帅
  12. * PhpStorm through1.php
  13. * Create By 2016/11/30 18:00 $
  14. */
  15. header("Access-Control-Allow-Origin:*");
  16. //载入配置文件和连接数据库
  17. //区分是普通乘客还是分销员 0普通,1分销
  18. $user_type = 0;
  19. $user_id=$_COOKIE['user_id'];
  20. if (!$user_id) {
  21. echo json_encode(array("code" => 1,"iflogin"=>1, "info" => "用户未登录"));
  22. exit ;
  23. }
  24. //判断出发地 目的地 时间是否为空
  25. $start_area = empty($_POST['start_area'])?"0":$_POST['start_area'];
  26. $end_area = empty($_POST['end_area'])?"0":$_POST['end_area'];
  27. $run_date = empty($_POST['go_time'])?date("Y-m-d",time()+24*3600):$_POST['go_time'];
  28. $current_day = date("Y-m-d");
  29. $limit_time = date("H:i", strtotime("+10 minutes"));
  30. $bus_type = $_POST['bus_type'];
  31. if($bus_type == 255){
  32. $bus_type = "255,0,368";
  33. }
  34. $current_page = $_POST['currpage'];
  35. $page_size = $_POST['pagesize'];
  36. $channel_id = isset($_COOKIE['org_id']) ? $_COOKIE['org_id'] : 0;
  37. $list = runList($bus_type,$run_date,$start_area,$end_area,$current_page,$page_size, $channel_id);
  38. $run_list = $list['list'];
  39. if(count($run_list) == 0){
  40. $json['code'] = 0;
  41. $json['info'] = $CODE[0];
  42. $json['list'] = $list['list'];
  43. $json['totalnum'] = $list['totalnum'];
  44. echo json_encode($json);exit();
  45. }
  46. foreach($run_list as $key => $vel){
  47. if($vel['num'] > 20){
  48. $run_list[$key]['num'] = '充足';
  49. }elseif ($vel['num'] <= 0){
  50. $run_list[$key]['num'] = '售罄';
  51. }
  52. }
  53. $json['code'] = 0;
  54. $json['info'] = $CODE[0];
  55. $json['list'] = $run_list;
  56. $json['totalnum'] = $list['totalnum'];
  57. echo json_encode($json);exit();
  58. /**
  59. * Function Description:获取班次列表
  60. * Function Name: runList
  61. * @param $line_type
  62. * @param $run_date
  63. * @param $start_area
  64. * @param $end_area
  65. * @param $current_page
  66. * @param $page_size
  67. *
  68. * @return mixed
  69. *
  70. * @author 张帅
  71. */
  72. function runList($line_type,$run_date,$start_area,$end_area,$current_page,$page_size, $channel_id = 0){
  73. global $pdo;
  74. global $bus_type;
  75. //筛掉不能在分销现实的线路
  76. $user_id = $_COOKIE['user_id'];
  77. if($user_id == 136){
  78. $line_id_arr = array(213344,213343,213342,213341,213340,213339,1579,1578,451180);
  79. }else{
  80. $line_id_arr = array(213344,213343,213342,213341,213340,213339,1579,1578,451180);
  81. }
  82. $start_area = execStation($start_area,$line_type);
  83. $end_area = execStation($end_area,$line_type);
  84. if($user_id == 141 && $bus_type != 256){
  85. $start_area = '3376,3240,100437,3353';
  86. $end_area = '3376,3240,100437,3353,791';
  87. }elseif ($user_id == 121 && $bus_type != 256){
  88. $start_area = '3343,3240,100437,3353';
  89. $end_area = '3343,3240,100437,3353';
  90. }elseif ($user_id == 461 && $bus_type != 256){
  91. $start_area = '3341,100091,3308';
  92. $end_area = '3341,100091,3308';
  93. }
  94. if($start_area != '0'){
  95. $start_area1 = explode(',',$start_area);
  96. $start_area1 = '\'' . implode('',$start_area1) . '\'';
  97. }else{
  98. $start_area1 = "0";
  99. }
  100. if($end_area != '0'){
  101. $end_area1 = explode(',',$end_area);
  102. $end_area1 = '\'' . implode('',$end_area1) . '\'';
  103. }else{
  104. $end_area1 = "0";
  105. }
  106. //获取例外
  107. $sql = " SELECT ticket_id FROM opera_tickets_distrib WHERE SUPPLIER_ID={$channel_id} AND AUTHORITY_STATUS=0 ";
  108. $pre_result = $pdo->query($sql);
  109. $nosell_ticket_array = $pre_result->fetchAll(PDO::FETCH_ASSOC);
  110. $not_include_sql = "";
  111. if( $nosell_ticket_array != false ) {
  112. $nosell_ticket_id = array();
  113. foreach( $nosell_ticket_array as $nosell_info ) {
  114. $nosell_ticket_id[] = $nosell_info["ticket_id"];
  115. }
  116. $not_include_sql = " and r.prod_id NOT IN (".implode(",",$nosell_ticket_id).") ";
  117. }
  118. $now_date = date('Y-m-d', time());
  119. $start_row = ($current_page - 1) * $page_size;
  120. $sql = "select a.run_id,
  121. a.prod_id as line_id,
  122. a.start_area_id,
  123. (select area_name from base_area where id = a.start_area_id LIMIT 1) as start_area_name,
  124. a.end_area_id,
  125. if(a.line_type = 368,(SELECT line_name from opera_line where line_id = a.prod_id),(select area_name from base_area where id = a.end_area_id LIMIT 1)) as end_area_name,
  126. a.property,
  127. a.prod_name,
  128. a.run_date,
  129. if(INSTR(a.start_time,',')>0,substring_index(a.start_time,',',1),a.start_time) as start_time,
  130. if(INSTR(a.end_time,',')>0,concat(substring_index(a.end_time,',',1),'-',substring_index(a.end_time,',',-1)),a.end_time) as end_time,
  131. (select type_name from dict_type where id = a.seat_type ) as site_type,
  132. min(a.price) as price,
  133. min(a.price2) as price2,
  134. a.sale_type,
  135. (select min(total_count-saled_count) from run_stock where run_id = a.run_id and cancel_flag = 0 and seq_id >= a.start_order_id and seq_id < a.end_order_id and seat_type = a.seat_type) as num
  136. from (
  137. select distinct m.run_id,
  138. p.line_id as prod_id,
  139. p.line_code as property,
  140. p.line_name as prod_name,
  141. m.run_date,
  142. r.start_station_area_id as start_area_id,
  143. r.end_station_area_id as end_area_id,
  144. r.seat_type,
  145. p.line_type,
  146. (select min(station_order_id) from run_station where run_id = m.run_id and area_id = r.start_station_area_id and station_inout_type <> 114 ) as start_order_id,
  147. (select max(station_order_id) from run_station where run_id = m.run_id and area_id = r.end_station_area_id and station_inout_type <> 114 ) as end_order_id,
  148. (select group_concat(distinct start_time) from run_station where run_id = m.run_id and area_id = r.start_station_area_id ) as start_time,
  149. (select group_concat(distinct start_time) from run_station where run_id = m.run_id and area_id = r.end_station_area_id ) as end_time,
  150. (select min(prod_price) from opera_tickets where ticket_id = r.prod_id and cancel_flag = 0) as price,
  151. (select prod_price from opera_tickets_distrib where supplier_id = {$channel_id} and ticket_id = r.prod_id and cancel_flag = 0 and authority_status = 1 ) as price2,
  152. (select sale_type from opera_tickets_distrib where supplier_id = {$channel_id} and ticket_id = r.prod_id and cancel_flag = 0 and authority_status = 1 ) as sale_type
  153. from opera_line p, run_main m, run_prod r
  154. where
  155. p.line_id = m.prod_id
  156. and m.run_id = r.run_id
  157. and p.line_id = r.parent_prod_id
  158. and p.line_type in(" . $line_type . ")
  159. and p.cancel_flag = 0
  160. and p.line_id not in(" . implode(',',$line_id_arr) . ") {$not_include_sql}
  161. and p.is_onsale = 1
  162. and p.if_disabled = 0
  163. AND (SELECT ticket_type FROM opera_tickets WHERE ticket_id = r.prod_id AND cancel_flag = 0) = 1
  164. and r.cancel_flag = 0
  165. and m.run_status = 138
  166. and m.run_date >= '" . $now_date . "'
  167. and m.run_date = '" . $run_date . "' ".
  168. // "
  169. // and case p.sale_expired_type when 321 then if(date_add(date_add(m.run_date,interval -1 day), interval (substring_index(p.sale_expired_time,':',1)*60 + substring_index(p.sale_expired_time,':',-1)) minute) > now(),0=0,0=1)
  170. // when 322 then if(date_add(m.run_date, interval m.run_minutes -(substring_index(p.sale_expired_time,':',1)*60 + substring_index(p.sale_expired_time,':',-1)) minute)>= now(),0=0,0=1)
  171. // else 0=0 end
  172. // ". comment by qius on 20170517 暂时不引入提前关班的情况
  173. "
  174. and IF(TRIM(" . $start_area1 . ")=0,0=0,(r.start_station_area_id in(" . $start_area . ")))
  175. and IF(TRIM(" . $end_area1 . ")=0,0=0,(r.end_station_area_id in(" . $end_area . ")))
  176. ) a
  177. group by a.prod_id,a.start_area_id,a.end_area_id,a.run_date,a.start_time,a.end_time,a.seat_type
  178. order by a.start_time asc
  179. LIMIT " . $start_row . "," . $page_size;
  180. writeLog($sql);//test
  181. $result1 = $pdo->query($sql);
  182. $list = $result1->fetchAll(PDO::FETCH_ASSOC);
  183. if (count($list) == 0) {
  184. $result['list'] = array();
  185. $result['totalnum'] = 0;
  186. return $result;
  187. }
  188. $count_sql = "select count(run_id) as total_count
  189. from
  190. (select
  191. run_id
  192. from (
  193. select distinct m.run_id,
  194. p.line_id as prod_id,
  195. r.start_station_area_id as start_area_id,
  196. r.end_station_area_id as end_area_id,
  197. r.seat_type,
  198. (select group_concat(distinct start_time) from run_station where run_id = m.run_id and area_id = r.start_station_area_id ) as start_time,
  199. (select group_concat(distinct start_time) from run_station where run_id = m.run_id and area_id = r.end_station_area_id ) as end_time
  200. from opera_line p, run_main m, run_prod r
  201. where
  202. p.line_id = m.prod_id
  203. and m.run_id = r.run_id
  204. and p.line_id = r.parent_prod_id
  205. and p.line_type in(" . $line_type . ")
  206. and p.cancel_flag = 0
  207. and r.cancel_flag = 0
  208. and p.line_id not in(" . implode(',',$line_id_arr) . ") {$not_include_sql}
  209. and p.is_onsale = 1
  210. and p.if_disabled = 0
  211. AND (SELECT ticket_type FROM opera_tickets WHERE ticket_id = r.prod_id AND cancel_flag = 0) = 1
  212. and m.run_status = 138
  213. and m.run_date >= '" . $now_date . "'
  214. and m.run_date = '" . $run_date . "' ".
  215. // "
  216. // and case p.sale_expired_type when 321 then if(date_add(date_add(m.run_date,interval -1 day), interval (substring_index(p.sale_expired_time,':',1)*60 + substring_index(p.sale_expired_time,':',-1)) minute) > now(),0=0,0=1)
  217. // when 322 then if(date_add(m.run_date, interval m.run_minutes -(substring_index(p.sale_expired_time,':',1)*60 + substring_index(p.sale_expired_time,':',-1)) minute)>= now(),0=0,0=1)
  218. // else 0=0 end
  219. // ".
  220. "
  221. and IF(TRIM(" . $start_area1 . ")=0,0=0,(r.start_station_area_id in(" . $start_area . ")))
  222. and IF(TRIM(" . $end_area1 . ")=0,0=0,(r.end_station_area_id in(" . $end_area . ")))
  223. ) a
  224. group by a.prod_id,a.start_area_id,a.end_area_id,a.start_time,a.end_time,a.seat_type) as b";
  225. $result2 = $pdo->query($count_sql);
  226. $list_count = $result2->fetchAll(PDO::FETCH_ASSOC);
  227. $list_count = $list_count[0]['total_count'];
  228. $result['list'] = $list;
  229. $result['totalnum'] = $list_count;
  230. return $result;
  231. }
  232. /**
  233. * Function Description:筛选可查询站点
  234. * Function Name: execStation
  235. * @param $area
  236. * @param $line_type
  237. *
  238. * @return array
  239. *
  240. * @author 张帅
  241. */
  242. function execStation($area,$line_type){
  243. //筛选可查询站点
  244. $user_id = $_COOKIE['user_id'];
  245. global $hotel_authority;
  246. $poi = getPoiInfoArr();
  247. $poi_info = $poi['poi_info_array'];
  248. if($area == '0'){
  249. if ($line_type == '256' && isset($hotel_authority[$user_id])) {
  250. if (is_array($hotel_authority[$user_id])) {
  251. foreach ($hotel_authority[$user_id] as $v) {
  252. $list[] = $v;
  253. }
  254. } else {
  255. $list[] = $hotel_authority[$user_id];
  256. }
  257. $list[] = 3240;
  258. $list[] = 100437;
  259. $area_str = implode(',',$list);
  260. } else {
  261. $area_str = $area;
  262. }
  263. }else{
  264. if (($line_type == '256' && isset($hotel_authority[$user_id])) || $poi_info[$area]['son_area_id'] == '') {
  265. $area_str = $area;
  266. }else{
  267. $area_str = $area . ',' . $poi_info[$area]['son_area_id'];
  268. }
  269. }
  270. return $area_str;
  271. }
  272. /**
  273. * Function Description:获取所有线路中开始POI-结束POI的连接关系
  274. * Function Name: getPoiId
  275. *
  276. * @return mixed
  277. *
  278. * @author 张帅
  279. */
  280. function getPoiId()
  281. {
  282. global $pdo;
  283. $sql = 'SELECT
  284. start_area,
  285. end_area,
  286. line_type
  287. FROM
  288. (
  289. SELECT
  290. SUBSTRING_INDEX(SUBSTRING_INDEX(b.start_area, \',\', sequence.help_topic_id),\',\' ,-1) AS start_area,
  291. SUBSTRING_INDEX(SUBSTRING_INDEX(b.end_area, \',\', sequence1.help_topic_id),\',\' ,-1) AS end_area,
  292. line_type
  293. FROM
  294. (select help_topic_id from mysql.help_topic where help_topic_id between 1 and 20 ) as sequence,
  295. (select help_topic_id from mysql.help_topic where help_topic_id between 1 and 20 )as sequence1,
  296. (
  297. SELECT @id := @id +1 as id,
  298. if(length(start_parent)=0,start_area_id,concat(start_area_id,\',\',start_parent)) as start_area,
  299. if(length(end_parent)=0,end_area_id,concat(end_area_id,\',\',end_parent)) as end_area,
  300. line_type
  301. FROM (
  302. SELECT distinct
  303. t.start_station_area_id as start_area_id,
  304. replace(substring(v1.parent_area_id_list,2,length(v1.parent_area_id_list)-2),\'}{\',\',\') AS start_parent,
  305. t.end_station_area_id as end_area_id,
  306. replace(substring(v2.parent_area_id_list,2,length(v2.parent_area_id_list)-2),\'}{\',\',\') AS end_parent,
  307. l.line_type
  308. FROM
  309. opera_line AS l,
  310. opera_tickets AS t,
  311. base_area_view as v1,
  312. base_area_view as v2
  313. WHERE
  314. l.line_id = t.line_id
  315. and v1.area_id = t.start_station_area_id
  316. and v2.area_id = t.end_station_area_id
  317. AND l.cancel_flag = 0
  318. AND l.if_disabled = 0
  319. AND l.line_type in(255,256)
  320. AND l.is_onsale = 1
  321. AND t.cancel_flag = 0
  322. AND t.start_station_area_id != 0
  323. AND t.end_station_area_id != 0
  324. ) a,
  325. (select @id:= 0) as i
  326. ) b
  327. WHERE
  328. sequence.help_topic_id BETWEEN 1 AND ( SELECT 1 + LENGTH(b.start_area) - LENGTH(REPLACE(b.start_area, \',\', \'\')))
  329. AND sequence1.help_topic_id BETWEEN 1 AND ( SELECT 1 + LENGTH(b.end_area) - LENGTH(REPLACE(b.end_area, \',\', \'\')))
  330. GROUP BY
  331. SUBSTRING_INDEX(SUBSTRING_INDEX(b.start_area, \',\', sequence.help_topic_id),\',\' ,-1),
  332. SUBSTRING_INDEX(SUBSTRING_INDEX(b.end_area, \',\', sequence1.help_topic_id),\',\' ,-1)
  333. )as tb';
  334. $result = $pdo->query($sql);
  335. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  336. return $rowset;
  337. }
  338. /**
  339. * Function Description:获取poi详情
  340. * Function Name: getPoiInfo
  341. * @param $poi_id
  342. *
  343. * @return array
  344. *
  345. * @author 张帅
  346. */
  347. function getPoiInfoDb($poi_id)
  348. {
  349. global $pdo;
  350. $sql = 'SELECT
  351. a.id as area_id,
  352. a.area_name,
  353. a.poi_type,
  354. REPLACE(substring(v.parent_area_id_list,2,length(v.parent_area_id_list)-2),\'}{\',\',\') AS parent_area_id,
  355. REPLACE(REPLACE(substring(v.parent_area_name_list,2),\'}{\',\',\'),\'}\',\'\') AS parent_area_name,
  356. v.area_level
  357. FROM
  358. base_area AS a,
  359. base_area_view AS v
  360. WHERE
  361. a.id = v.area_id
  362. AND a.id in (' . $poi_id . ')';
  363. $result = $pdo->query($sql);
  364. $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
  365. return $rowset;
  366. }
  367. /**
  368. * Function Description:获取站点详情
  369. * Function Name: getPoiInfoArr
  370. *
  371. * @return array|mixed
  372. *
  373. * @author 张帅
  374. */
  375. function getPoiInfoArr()
  376. {
  377. $result = get_memcache('FX_POI_ARR_LIST');
  378. if ($result) {
  379. return $result;
  380. }
  381. //1.获取POIid数组
  382. $poi_id = getPoiIdArr();
  383. //2.收集所有的poi_id
  384. $area_id_array = array();
  385. foreach ($poi_id as $key => $value) {
  386. if (!isset($area_id_array[$value['start_area']])) {
  387. $area_id_array[$value['start_area']] = $value['start_area'];
  388. }
  389. if (!isset($area_id_array[$value['end_area']])) {
  390. $area_id_array[$value['end_area']] = $value['end_area'];
  391. }
  392. }
  393. $poi_id = array_merge($poi_id);
  394. //3.根据poi_id获取poi详细信息
  395. if (count($area_id_array) > 0) {
  396. $poi_info_array = getPoiInfo($area_id_array);
  397. } else {
  398. $poi_info_array = array();
  399. }
  400. //4.根据线路类型及上下车类型对poi分组
  401. $poi_line_inout = getPoiByLineInout($poi_id);
  402. $result['poi_id'] = $poi_id;
  403. $result['poi_info_array'] = $poi_info_array;
  404. $result['poi_line_inout'] = $poi_line_inout;
  405. set_memcache('FX_POI_ARR_LIST', $result, 3600);
  406. return $result;
  407. }
  408. /**
  409. * Function Description:获取poi站点数组
  410. * Function Name: getPoiIdArr
  411. *
  412. * @return array
  413. *
  414. * @author 张帅
  415. */
  416. function getPoiIdArr()
  417. {
  418. $poi_array = getPoiId();
  419. return $poi_array;
  420. }
  421. /**
  422. * Function Description:获取poi详细信息
  423. * Function Name: getPoiInfo
  424. * @param $area_id_array
  425. *
  426. * @return array
  427. *
  428. * @author 张帅
  429. */
  430. function getPoiInfo($area_id_array)
  431. {
  432. $area_id_array = implode(',', $area_id_array);
  433. //1.获取POI的详细信息
  434. $poi_array = getPoiInfoDb($area_id_array);
  435. //2.整理poi数组
  436. $poi_info_array = array();
  437. foreach ($poi_array as $key => $vel) {
  438. $poi_info_array[$vel['area_id']] = $vel;
  439. }
  440. //3.整理poi的父子关系
  441. foreach ($poi_info_array as $key => $vel) {
  442. if (!empty($vel['parent_area_id'])) {
  443. $parent_id_arr = explode(',', $vel['parent_area_id']);
  444. $parent_name_arr = explode(',', $vel['parent_area_name']);
  445. $parent_area_arr = array();
  446. foreach ($parent_id_arr as $k => $v) {
  447. $parent_area_arr[$k]['area_id'] = $v;
  448. $parent_area_arr[$k]['area_name'] = $parent_name_arr[$k];
  449. $poi_info_array[$v]['son_area_id'][$key] = $key;
  450. }
  451. $poi_info_array[$key]['parent_area_arr'] = $parent_area_arr;
  452. } else {
  453. $poi_info_array[$key]['parent_area_arr'] = array();
  454. }
  455. }
  456. foreach ($poi_info_array as $key => $vel) {
  457. if (!isset($vel['son_area_id'])) {
  458. $poi_info_array[$key]['son_area_id'] = '';
  459. } else {
  460. $poi_info_array[$key]['son_area_id'] = implode(',', $vel['son_area_id']);
  461. }
  462. }
  463. return $poi_info_array;
  464. }
  465. /**
  466. * Function Description:根据线路类型及上下车类型对poi分组
  467. * Function Name: getPoiByLineInout
  468. * @param $poi_id
  469. *
  470. * @return mixed
  471. *
  472. * @author 张帅
  473. */
  474. function getPoiByLineInout($poi_id)
  475. {
  476. $poi_group = array();
  477. foreach ($poi_id as $k => $v) {
  478. if (!isset($poi_group[$v['line_type']]['start_area'][$v['start_area']])) {
  479. $poi_group[$v['line_type']]['start_area'][$v['start_area']] = $v['start_area'];
  480. }
  481. if (!isset($poi_group[$v['line_type']]['end_area'][$v['end_area']])) {
  482. $poi_group[$v['line_type']]['end_area'][$v['end_area']] = $v['end_area'];
  483. }
  484. }
  485. return $poi_group;
  486. }