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.

Bus.php 18 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm Prod.php
  13. * Create By 2017/6/20 14:40 $
  14. */
  15. namespace zzcx\service\home;
  16. use common\models\BusTicket;
  17. use common\models\OrderMain;
  18. use common\models\ProdCategory;
  19. use common\models\ProdComment;
  20. use common\models\ProdMain;
  21. use common\util\CurlInterface;
  22. use common\util\OrderUtil;
  23. use common\util\Util;
  24. class Bus
  25. {
  26. /**
  27. * Des:获取产品详情
  28. * Name: getDetail
  29. * @param $pro_cate_id
  30. * @param $start_poi
  31. * @param $end_poi
  32. * @return array
  33. * @author 倪宗锋
  34. */
  35. public function getDetail($pro_cate_id, $start_poi, $end_poi)
  36. {
  37. //参数校验
  38. if (Util::checkPattern('intVal', $pro_cate_id) == false) {
  39. return Util::returnArrEr('参数错误');
  40. }
  41. $cache = \Yii::$app->getCache();
  42. $prod_detail = $cache->get('ZZWX_ProdDetail_' . $pro_cate_id);
  43. if (empty($prod_detail['prod_arr'])) {
  44. $prod_category = new ProdCategory();
  45. $prod_main = new ProdMain();
  46. $prod_detail = $prod_category->getBusProdDetail($pro_cate_id);
  47. //子产品列表
  48. $prod_arr = $prod_main->getProdArr($pro_cate_id);
  49. if (count($prod_arr)) {
  50. foreach ($prod_arr as $key => $val) {
  51. $prod_arr[$key]['prod_price'] = ' -- ';
  52. }
  53. }
  54. $prod_detail['prod_arr'] = $prod_arr;
  55. $cache->set('ZZWX_ProdDetail_' . $pro_cate_id, $prod_detail, 60 * 10);//缓存10分钟
  56. }
  57. if ($prod_detail['delete_flag'] == 1) {
  58. return Util::returnArrEr('该产品已经下架!');
  59. }
  60. //获取主线路售卖日期
  61. $getRunDate = static::getLineSaleDate($pro_cate_id, 0, $start_poi, $end_poi);
  62. $prod_detail['run_date'] = $getRunDate['data']['list'];
  63. return Util::returnArrSu('', $prod_detail);
  64. }
  65. /**
  66. * Des:获取主线路产品库存日历
  67. * Name: getLineSaleDate
  68. * @param $pro_cate_id
  69. * @param $if_back
  70. * @param $start_poi
  71. * @param $end_poi
  72. * @param $date
  73. * @return array
  74. * @author 倪宗锋
  75. */
  76. public function getLineSaleDate($pro_cate_id, $if_back, $start_poi = '', $end_poi = '', $date = '')
  77. {
  78. if (Util::checkPattern('intVal', $pro_cate_id) == false) {
  79. return Util::returnArrEr('params error');
  80. }
  81. $prodCate = new ProdCategory();
  82. $prodInfo = $prodCate->getBusProdDetail($pro_cate_id);
  83. if ($if_back) {
  84. if (empty($start_poi) == false && empty($end_poi) == false) {//如果用户设置了其实和终点poi则以用户设置poi为准
  85. $end_area_id = $start_poi;
  86. $start_area_id = $end_poi;
  87. } else {
  88. $end_area_id = $prodInfo['start_area_id'];//站点 起始站POI作为终点站POI
  89. $start_area_id = $prodInfo['end_area_id'];//站点 终点站POI作为起始站POI
  90. }
  91. $return = static::getLineSaleDateFromCs('', $start_area_id, $end_area_id);//获取两个POI之间的班次库存
  92. } else {
  93. if (empty($prodInfo['sign'])) {
  94. return Util::returnArrEr('该产品已下架!');
  95. }
  96. $line_id = $prodInfo['sign'];//线路ID
  97. if (empty($start_poi) == false && empty($end_poi) == false) {//如果用户设置了其实和终点poi则以用户设置poi为准
  98. $return = static::getLineSaleDateFromCs($line_id, $start_poi, $end_poi);//获取线路的班次库存
  99. } else {
  100. $return = static::getLineSaleDateFromCs($line_id);//获取线路的班次库存
  101. }
  102. }
  103. //将比今天或者指定日期小的日期剔除
  104. if (empty($return['data']['list']['0']) == false) {
  105. if (empty($date)) {
  106. $date = date('Y-m-d');
  107. }
  108. foreach ($return['data']['list'] as $key => $val) {
  109. if (strtotime($val) < strtotime($date)) {
  110. unset($return['data']['list'][$key]);
  111. } else {
  112. break;
  113. }
  114. }
  115. }
  116. $return['data']['list'] = array_values($return['data']['list']);
  117. return $return;
  118. }
  119. /**
  120. * Des:获取主线路的按日期查询班次及库存
  121. * Name: getLineBusStock
  122. * @param $params array
  123. * date 出发时间
  124. * pro_cate_id 产品品类ID
  125. * if_back 是否返程
  126. * @return array
  127. * @author 倪宗锋
  128. */
  129. public static function getLineBusStock($params)
  130. {
  131. if (Util::checkPattern('intVal', $params['pro_cate_id']) == false) {
  132. return Util::returnArrEr('params error!');
  133. }
  134. /**从cs获取主线路班次库存*/
  135. $getBusStock = static::getBusStockForLineFromCs($params);
  136. if ($getBusStock['flag'] == false) {
  137. return $getBusStock;
  138. }
  139. /**设置前端使用的价格库存*/
  140. $setReturn = static::setTicketReturn($getBusStock['data'], $params);
  141. return $setReturn;
  142. }
  143. /**
  144. * Des:从cs获取主线路班次库存
  145. * Name: getBusStockForLineFromCs
  146. * @param $params array
  147. * @return array
  148. * @author 倪宗锋
  149. */
  150. public static function getBusStockForLineFromCs($params)
  151. {
  152. $date = $params['date'];
  153. //如果用户搜索了用户的
  154. if ($params['if_back']) {
  155. if (empty($params['start_poi']) == false && empty($params['end_poi']) == false) {//如果用户设置了其实和终点poi则以用户设置poi为准
  156. $end_area_id = $params['start_poi'];
  157. $start_area_id = $params['end_poi'];
  158. } else {
  159. $prodCate = new ProdCategory();
  160. $prodInfo = $prodCate->getBusProdDetail($params['pro_cate_id']);
  161. $end_area_id = $prodInfo['start_area_id'];//站点 起始站POI作为终点站POI
  162. $start_area_id = $prodInfo['end_area_id'];//站点 终点站POI作为起始站POI
  163. }
  164. $return = static::getGoBusStockForArea($start_area_id, $end_area_id, $date);//获取两个POI之间的班次库存
  165. } else {
  166. $prodCate = new ProdCategory();
  167. $prodInfo = $prodCate->getBusProdDetail($params['pro_cate_id']);
  168. if (empty($prodInfo['sign'])) {
  169. return Util::returnArrEr('该产品已下架!');
  170. }
  171. $line_id = $prodInfo['sign'];//线路ID
  172. $return = static::getGoBusStockForLine($line_id, $date);//获取线路的班次库存
  173. /******************************增加售卖截止时间判断*****************************/
  174. $close_sale_time = $prodInfo['close_sale_time'];
  175. if ($close_sale_time == '') $close_sale_time = 24;
  176. $s_time = strtotime($date) + $close_sale_time * 60 * 60;
  177. $curr_h_i = date('H:i:s', $s_time);
  178. if ($curr_h_i == '00:00:00') $curr_h_i = '24:00:00';
  179. //1.判断是否存在订单了
  180. $orderMain = new OrderMain();
  181. $param = ["prod_cate_id" => $params['pro_cate_id'], "start_date" => $date, "start_time" => $curr_h_i, "small24" => $close_sale_time < 0];
  182. $boolExist = $orderMain->existOrder($param);
  183. if (!$boolExist) {
  184. if ($s_time <= time()) {
  185. return Util::returnArrEr('该日的班次已截止售卖,请选择其它日期');
  186. }
  187. }
  188. /******************************华丽的分割线*****************************/
  189. }
  190. return $return;
  191. }
  192. /**
  193. * Des:设置获取班次返回值
  194. * Name: setTicketReturn
  195. * @param $data
  196. * @param $params
  197. * @return array
  198. * @author 倪宗锋
  199. */
  200. public static function setTicketReturn($data, $params)
  201. {
  202. /**获取所有去重后的票种ID*/
  203. $ticketIds = static::getTicketByCSTicket($data);
  204. if (empty($ticketIds['ticket'])) {
  205. return Util::returnArrEr('当前日期票已售完,请选择其他日期!');
  206. }
  207. /**获取票种对应的站点数组*/
  208. $bus_ticket = new BusTicket();
  209. $ticketArr = $bus_ticket->getTicketAndLineByIDs($ticketIds['ticket'], $params);
  210. foreach ($ticketIds['runBusArr'] as $key => $val) {
  211. $ticketIds['runBusArr'][$key]['start_station_list'] = static::getBusArrBYTicketList($val['ticket_list'], $ticketArr, $params['date']);
  212. unset($ticketIds['runBusArr'][$key]['ticket_list']);
  213. if (count($ticketIds['runBusArr'][$key]['start_station_list']) <= 0) {//如果没有可选站点则 删除该班次
  214. unset($ticketIds['runBusArr'][$key]);
  215. continue;
  216. }
  217. $ticketIds['runBusArr'][$key]['run_time'] = $ticketIds['runBusArr'][$key]['start_station_list'][0]['start_res_time'];//第一个有效站点的时间作为班次的时间
  218. foreach ($ticketIds['runBusArr'][$key]['start_station_list'] as $start_station_val) {
  219. if (strtotime($start_station_val['start_res_time']) <= strtotime($ticketIds['runBusArr'][$key]['run_time'])) {
  220. $ticketIds['runBusArr'][$key]['run_time'] = $start_station_val['start_res_time'];
  221. }
  222. }
  223. // if (empty($params['run_time']) == false && $params['run_time'] != $ticketIds['runBusArr'][$key]['run_time']) {
  224. // unset($ticketIds['runBusArr'][$key]);
  225. // }
  226. }
  227. ksort($ticketIds['runBusArr']);
  228. if (empty($ticketIds['runBusArr'])) {
  229. return Util::returnArrEr('没有库存,请重选日期');
  230. }
  231. return Util::returnArrSu('', ['station_list' => array_values($ticketIds['runBusArr'])]);
  232. }
  233. /**
  234. * Des:凭借前端班次数组
  235. * Name: getBusArrBYTicketList
  236. * @param $ticketList
  237. * @param $ticketArr
  238. * @param $date
  239. * @return array
  240. * @author 倪宗锋
  241. */
  242. public static function getBusArrBYTicketList($ticketList, $ticketArr, $date)
  243. {
  244. $theArr = [];
  245. foreach ($ticketList as $key => $val) {
  246. /**判断时间*/
  247. $stat_time = strtotime($date . ' ' . $val['start_time'] . ':00');
  248. if ($stat_time <= time()) {
  249. continue;
  250. }
  251. foreach ($ticketArr as $tkey => $tVal) {
  252. if (isset($tVal['end_station_list'][$key])) {
  253. $sArr = [];
  254. if (empty($theArr[$tkey])) {
  255. $theArr[$tkey]['start_res_id'] = $tVal['start_res_id'];
  256. $theArr[$tkey]['start_res_name'] = $tVal['start_res_name'];
  257. if (isset($val['prod_num'])) {
  258. $theArr[$tkey]['start_res_time'] = $val['start_time'];
  259. } else {//兼容之前的版本
  260. $theArr[$tkey]['start_res_time'] = '00:00:00';
  261. }
  262. $theArr[$tkey]['end_station_list'] = [];
  263. }
  264. $sArr['station_id'] = $tVal['end_station_list'][$key]['end_res_id'];
  265. $sArr['station_name'] = $tVal['end_station_list'][$key]['end_res_name'];
  266. $sArr['pro_cate_id'] = $tVal['end_station_list'][$key]['pro_cate_id'];
  267. if (isset($val['prod_num'])) {
  268. $sArr['count'] = $val['prod_num'];
  269. $sArr['end_res_time'] = $val['end_time'];
  270. } else {//兼容之前的版本
  271. $sArr['count'] = $val;
  272. $sArr['end_res_time'] = '00:00';
  273. }
  274. $theArr[$tkey]['end_station_list'][] = $sArr;
  275. }
  276. }
  277. }
  278. return array_values($theArr);
  279. }
  280. /**
  281. * Des:获取TICKET字符串
  282. * Name: getTicketByCSTicket
  283. * @param $busArr
  284. * @return array
  285. * @author 倪宗锋
  286. */
  287. public static function getTicketByCSTicket($busArr)
  288. {
  289. $ticketArr = [];
  290. $runBusArr = [];
  291. foreach ($busArr as $val) {
  292. if (count($val['ticket_list']) == 0) {
  293. continue;
  294. }
  295. $cnt = 0;
  296. foreach ($val['ticket_list'] as $key => $tiVal) {
  297. if ($tiVal > 0) {
  298. $cnt++;
  299. $ticketArr[$key] = $key;//去重保存
  300. }
  301. }
  302. if ($cnt) {//没有库存的不保存
  303. $runBusArr[$val['run_time']] = $val;
  304. }
  305. }
  306. return ['ticket' => array_values($ticketArr), 'runBusArr' => $runBusArr];
  307. }
  308. /**
  309. * Des:获取新路某天的各班次及票库存数据
  310. * Name: getGoBusStockForLine
  311. * @param $line_id
  312. * @param $date
  313. * @return array
  314. * @author 倪宗锋
  315. */
  316. public static function getGoBusStockForLine($line_id, $date)
  317. {
  318. $time = time();
  319. $siteConfig = Util::getSiteConfig();
  320. $send_data = [
  321. 'auth_code' => Util::authCodeForCs($time),
  322. 'user_key' => $siteConfig['adm_user_key'],
  323. 'user' => $siteConfig['adm_user'],
  324. 'user_id' => $siteConfig['adm_user_id'],
  325. 'request_time' => $time,
  326. 'action' => 'get_run_ticket_array_by_date_line',
  327. 'line_id' => $line_id,
  328. 'run_date' => $date,
  329. 'org_id' => $siteConfig['adm_source_id']
  330. ];
  331. $curlInterface = new CurlInterface($send_data, 4);
  332. $curlInterface->setBaseUrl('');
  333. $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
  334. if ($ticket_arr_cs['code'] == 0) {
  335. return Util::returnArrSu('', $ticket_arr_cs['data']);
  336. }
  337. return Util::returnArrEr('当前日期所有票已售完!');
  338. }
  339. /**
  340. * Des:获取新路某天的各班次及票库存数据
  341. * Name: getGoBusStockForLine
  342. * @param $start_area_id
  343. * @param $end_area_id
  344. * @param $date
  345. * @return array
  346. * @author 倪宗锋
  347. */
  348. public static function getGoBusStockForArea($start_area_id, $end_area_id, $date)
  349. {
  350. $time = time();
  351. $siteConfig = Util::getSiteConfig();
  352. $send_data = [
  353. 'auth_code' => Util::authCodeForCs($time),
  354. 'user_key' => $siteConfig['adm_user_key'],
  355. 'user' => $siteConfig['adm_user'],
  356. 'user_id' => $siteConfig['adm_user_id'],
  357. 'request_time' => $time,
  358. 'action' => 'get_run_ticket_array_by_date_area',
  359. 'start_area_id' => $start_area_id,
  360. 'end_area_id' => $end_area_id,
  361. 'run_date' => $date,
  362. 'org_id' => $siteConfig['adm_source_id']
  363. ];
  364. $curlInterface = new CurlInterface($send_data, 4);
  365. $curlInterface->setBaseUrl('');
  366. $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
  367. if ($ticket_arr_cs['code'] == 0 && empty($ticket_arr_cs['data']) == false) {
  368. return Util::returnArrSu('', $ticket_arr_cs['data']);
  369. }
  370. return Util::returnArrEr('当前日期所有票已售完!',[]);
  371. }
  372. /**
  373. * Des:获取某天经过某两个站点的poi的线路及上车poi首站的时间集合
  374. * Name: getLineRunTimeByPoi
  375. * @param $start_poi
  376. * @param $end_poi
  377. * @param $date
  378. * @return array
  379. * @author 倪宗锋
  380. */
  381. public static function getLineRunTimeByPoi($start_poi, $end_poi, $date)
  382. {
  383. $time = time();
  384. $siteConfig = Util::getSiteConfig();
  385. $send_data = [
  386. 'auth_code' => Util::authCodeForCs($time),
  387. 'user_key' => $siteConfig['adm_user_key'],
  388. 'user' => $siteConfig['adm_user'],
  389. 'user_id' => $siteConfig['adm_user_id'],
  390. 'request_time' => $time,
  391. 'action' => 'get_run_line_by_poi',
  392. 'start_poi' => $start_poi,
  393. 'end_poi' => $end_poi,
  394. 'date' => $date,
  395. 'org_id' => $siteConfig['adm_source_id']
  396. ];
  397. $curlInterface = new CurlInterface($send_data, 4);
  398. $curlInterface->setBaseUrl('');
  399. $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
  400. if (isset($ticket_arr_cs['code']) && $ticket_arr_cs['code'] == 0) {
  401. return Util::returnArrSu('', $ticket_arr_cs['data']);
  402. }
  403. return Util::returnArrEr('获取数据失败!');
  404. }
  405. /**
  406. * Des:获取主线路库存
  407. * Name: getLineSaleDate
  408. * @param string $line_id
  409. * @param string $start_area_id
  410. * @param string $end_area_id
  411. * @return array
  412. * @author 倪宗锋
  413. */
  414. public static function getLineSaleDateFromCs($line_id = '', $start_area_id = '', $end_area_id = '')
  415. {
  416. $time = time();
  417. $siteConfig = Util::getSiteConfig();
  418. $send_data = [
  419. 'auth_code' => Util::authCodeForCs($time),
  420. 'user_key' => $siteConfig['adm_user_key'],
  421. 'user' => $siteConfig['adm_user'],
  422. 'user_id' => $siteConfig['adm_user_id'],
  423. 'request_time' => $time,
  424. 'action' => 'get_area_available_date_array',
  425. 'start_area_id' => $start_area_id,
  426. 'end_area_id' => $end_area_id,
  427. 'line_id' => $line_id,
  428. 'org_id' => $siteConfig['adm_source_id']
  429. ];
  430. $curlInterface = new CurlInterface($send_data, 4);
  431. $curlInterface->setBaseUrl('');
  432. $ticket_arr_cs = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
  433. if ($ticket_arr_cs['code'] == 0) {
  434. return Util::returnArrSu('', ['list' => $ticket_arr_cs['data']]);
  435. }
  436. return Util::returnArrSu('', ['list' => []]);
  437. }
  438. }