Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

1079 řádky
39 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use backend\modules\api\controllers\SubmitOrderController;
  4. use backend\modules\api\logic\SubmitBusAgentOrder;
  5. use backend\modules\api\models\BaseArea;
  6. use backend\modules\zzcs\models\RunBusDelay;
  7. use yii\db\ActiveRecord;
  8. use Yii;
  9. use yii\db\Expression;
  10. use yii\db\Query;
  11. /**
  12. * This is the model class for table "run_main".
  13. *
  14. * @property integer $ID
  15. * @property integer $RUN_ID
  16. * @property integer $RUN_LOCK_TYPE
  17. * @property string $RUN_DATE
  18. * @property string $RUN_TIME
  19. * @property integer $RUN_MINUTES
  20. * @property integer $PROD_ID
  21. * @property integer $CREATE_USER_ID
  22. * @property string $CREATE_TIME
  23. * @property integer $UPDATE_USER_ID
  24. * @property string $UPDATE_TIME
  25. * @property integer $RUN_STATUS
  26. * @property integer $PRE_SALE_DAY
  27. * @property integer $RECEIVING
  28. * @property integer $HT_BUY_TYPE
  29. * @property string $HT_BASE_PRICE
  30. * @property string $HT_MID_PRICE
  31. * @property string $HT_ORDER_PRICE
  32. * @property integer $HT_CAN_SELL
  33. * @property integer $HT_BREAKFAST
  34. * @property integer $HT_IS_DELETE
  35. * @property integer $START_STATION_RES_ID
  36. * @property integer $START_STATION_AREA_ID
  37. * @property integer $END_STATION_RES_ID
  38. * @property integer $END_STATION_AREA_ID
  39. */
  40. class RunMain extends ActiveRecord
  41. {
  42. /**
  43. * @inheritdoc
  44. */
  45. public static function tableName()
  46. {
  47. return 'run_main';
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['RUN_ID', 'RUN_LOCK_TYPE', 'RUN_MINUTES', 'PROD_ID', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'RUN_STATUS', 'PRE_SALE_DAY', 'RECEIVING', 'HT_BUY_TYPE', 'HT_CAN_SELL', 'HT_BREAKFAST', 'HT_IS_DELETE', 'START_STATION_RES_ID', 'START_STATION_AREA_ID', 'END_STATION_RES_ID', 'END_STATION_AREA_ID'], 'integer'],
  56. [['HT_BASE_PRICE', 'HT_MID_PRICE', 'HT_ORDER_PRICE'], 'number'],
  57. [['RUN_DATE', 'RUN_TIME', 'CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
  58. [['RUN_ID', 'PROD_ID'], 'unique', 'targetAttribute' => ['RUN_ID', 'PROD_ID'], 'message' => 'The combination of Run ID and Prod ID has already been taken.'],
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'ID' => 'ID',
  68. 'RUN_ID' => 'Run ID',
  69. 'RUN_LOCK_TYPE' => 'Run Lock Type',
  70. 'RUN_DATE' => 'Run Date',
  71. 'RUN_TIME' => 'Run Time',
  72. 'RUN_MINUTES' => 'Run Minutes',
  73. 'PROD_ID' => 'Prod ID',
  74. 'CREATE_USER_ID' => 'Create User ID',
  75. 'CREATE_TIME' => 'Create Time',
  76. 'UPDATE_USER_ID' => 'Update User ID',
  77. 'UPDATE_TIME' => 'Update Time',
  78. 'RUN_STATUS' => 'Run Status',
  79. 'PRE_SALE_DAY' => 'Pre Sale Day',
  80. 'RECEIVING' => 'Receiving',
  81. 'HT_BUY_TYPE' => '酒店专用,采购类型',
  82. 'HT_BASE_PRICE' => '酒店专用,采购价',
  83. 'HT_MID_PRICE' => '酒店专用,分销价',
  84. 'HT_ORDER_PRICE' => '酒店专用,零售价',
  85. 'HT_CAN_SELL' => '酒店专用,销售开关:1可售 2不可售',
  86. 'HT_BREAKFAST' => '酒店专用,早餐',
  87. 'HT_IS_DELETE' => '酒店专用,删除:0未删除 1删除',
  88. 'START_STATION_RES_ID' => 'Start Station Res ID',
  89. 'START_STATION_AREA_ID' => 'Start Station Area ID',
  90. 'END_STATION_RES_ID' => 'End Station Res ID',
  91. 'END_STATION_AREA_ID' => 'End Station Area ID',
  92. ];
  93. }
  94. /**
  95. * Function Description:通过日期获取班次id
  96. * Function Name: getRunListByDate
  97. * @param string $start_date 开始日期
  98. * @param string $end_date 结束日期
  99. *
  100. * @return array|\yii\db\ActiveRecord[]
  101. *
  102. * @author 张帅
  103. */
  104. public function getRunListByDate($start_date, $end_date)
  105. {
  106. $result = self::find()
  107. ->select('run_id')
  108. ->where([
  109. 'and',
  110. ['>=', 'run_date', $start_date],
  111. ['<=', 'run_date', $end_date],
  112. ['=', 'run_status', 138],
  113. ])
  114. ->asArray()->all();
  115. return $result;
  116. }
  117. /**
  118. * Function Description:通过出发日期和票种id获取班次数据
  119. * Function Name: getRunListByDateTicket
  120. * @param string $run_date 出发日期
  121. * @param int $ticket_id 票种id
  122. *
  123. * @return array|\yii\db\ActiveRecord[]
  124. *
  125. * @author 张帅
  126. */
  127. public function getRunListByDateTicket($run_date, $ticket_id)
  128. {
  129. $result = self::find()
  130. ->select([
  131. 'rm.run_id',//班次id
  132. 'start_date' => 'rm.run_date',//状态
  133. 'start_time' => 'rs.start_time',//开始时间
  134. 'run_status' => 'rm.run_status',//状态
  135. 'start_station_status' => 'rs.station_inout_type',//开始站状态
  136. 'end_station_status' => 'rs2.station_inout_type',//结束站状态
  137. //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
  138. 'count' => RunStock::find()->select("min(total_count-saled_count)")->where('run_id = rm.run_id and seat_type=72 and seq_id >= rs.station_order_id and seq_id < rs2.station_order_id'),
  139. ])
  140. ->from(self::tableName() . ' as rm')
  141. ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  142. ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
  143. ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
  144. ->where([
  145. 'and',
  146. ['=', 'rm.run_date', $run_date],
  147. ['=', 'rp.prod_id', $ticket_id],
  148. ])
  149. ->groupBy('run_id')
  150. ->asArray()->all();
  151. $current_time = date("Y-m-d H:i", strtotime("+30 minutes"));
  152. $return_array = array();
  153. foreach ($result as $run_info_tmp) {
  154. $start_time = $run_info_tmp["start_date"] . " " . $run_info_tmp["start_time"];
  155. if ($current_time >= $start_time || $run_info_tmp["run_status"] != 138 || $run_info_tmp["start_station_status"] == 114 || $run_info_tmp["end_station_status"] == 114) {
  156. $run_info_tmp["count"] = 0;
  157. } else {
  158. $run_info_tmp["count"] = $run_info_tmp["count"] > 3 ? $run_info_tmp["count"] - 3 : 0;
  159. }
  160. $return_array[] = $run_info_tmp;
  161. }
  162. return $return_array;
  163. }
  164. /**
  165. * Function Description:通过出发日期和票种id获取班次数据
  166. * Function Name: getRunListByDateTicket
  167. * @param string $run_date 出发日期
  168. * @param int $ticket_id 票种id
  169. *
  170. * @return array|\yii\db\ActiveRecord[]
  171. *
  172. * @author 张帅
  173. */
  174. public function getRunListBySnEDateTicket($start_date, $end_date, $ticket_id)
  175. {
  176. $result = self::find()
  177. ->select([
  178. 'rm.run_id',//班次id
  179. 'start_date' => 'rm.run_date',//状态
  180. 'start_time' => 'rs.start_time',//开始时间
  181. 'run_status' => 'rm.run_status',//状态
  182. 'start_station_status' => 'rs.station_inout_type',//开始站状态
  183. 'end_station_status' => 'rs2.station_inout_type',//结束站状态
  184. //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
  185. 'count' => RunStock::find()->select("min(total_count-saled_count)")->where('run_id = rm.run_id and seat_type=72 and seq_id >= rs.station_order_id and seq_id < rs2.station_order_id'),
  186. ])
  187. ->from(self::tableName() . ' as rm')
  188. ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  189. ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
  190. ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
  191. ->where([
  192. 'and',
  193. ['>=', 'rm.run_date', $start_date],
  194. ['<=', 'rm.run_date', $end_date],
  195. ['=', 'rp.prod_id', $ticket_id],
  196. ])
  197. ->groupBy('run_id')
  198. ->asArray()->all();
  199. $current_time = date("Y-m-d H:i", strtotime("+30 minutes"));
  200. $return_array = array();
  201. foreach ($result as $run_info_tmp) {
  202. $start_time = $run_info_tmp["start_date"] . " " . $run_info_tmp["start_time"];
  203. if ($current_time >= $start_time || $run_info_tmp["run_status"] != 138 || $run_info_tmp["start_station_status"] == 114 || $run_info_tmp["end_station_status"] == 114) {
  204. $run_info_tmp["count"] = 0;
  205. } else {
  206. $run_info_tmp["count"] = $run_info_tmp["count"] > 3 ? $run_info_tmp["count"] - 3 : 0;
  207. }
  208. $return_array[] = $run_info_tmp;
  209. }
  210. return $return_array;
  211. }
  212. /**
  213. * Function Description:检查run_x中是否有班次
  214. * Function Name: checkRunX
  215. * @param array $date_run_arr 日期班次数组
  216. *
  217. * @return bool
  218. *
  219. * @author 张帅
  220. */
  221. public function checkRunX($date_run_arr)
  222. {
  223. #region 没有班次直接返回错误
  224. if (count($date_run_arr) == 0) {
  225. return false;
  226. }
  227. #endregion
  228. #region 判断每个班次是否存在run_x
  229. foreach ($date_run_arr as $key => $vel) {
  230. $sql = '' . 'select
  231. count(*) as total_count
  232. from run_' . $key . '
  233. where run_id in (' . implode(',', $vel) . ')group by run_id';
  234. $res = Yii::$app->db->createCommand($sql)->queryAll();
  235. #region 如果有不存在班次的直接返回错误
  236. if (count($res) != count($vel)) {
  237. return false;
  238. }
  239. foreach ($res as $res_key => $res_vel) {
  240. if ($res_vel['total_count'] == 0) {
  241. return false;
  242. }
  243. }
  244. #endregion
  245. }
  246. #endregion
  247. return true;
  248. }
  249. /**
  250. * Function Description:获取所有的座位
  251. * Function Name: getRunBunSeat
  252. * @param array $run_bus_arr 班次信息
  253. *
  254. * @return array|bool
  255. *
  256. * @author 张帅
  257. */
  258. public function getRunBusSeat($run_bus_arr)
  259. {
  260. $result = [];
  261. foreach ($run_bus_arr as $key => $vel) {
  262. $sql_and = '';
  263. if ($vel['bus_order_id'] != 0) {
  264. $sql_and = ' and bus_order_id = ' . $vel['bus_order_id'];
  265. }
  266. $sql = '' . 'select
  267. group_concat(id) as run_x_id,run_id,bus_order_id,seat_x,seat_y,seat_type,seat_seq_id,seat_name,count(id) as seat_count
  268. from
  269. run_' . $vel['run_date'] . '
  270. where
  271. run_id = ' . $vel['run_id'] . '
  272. and order_id >= ' . $vel['start_seq_id'] . '
  273. and order_id < ' . $vel['end_seq_id'] . '
  274. and seat_type = ' . $vel['seat_type'] . $sql_and . '
  275. and cancel_flag = 0
  276. and seat_status = 0
  277. group by
  278. run_id,bus_order_id,seat_type,seat_seq_id,seat_name
  279. order by seat_seq_id ASC';
  280. $res = Yii::$app->db->createCommand($sql)->queryAll();
  281. if (count($res) == 0) {
  282. return false;
  283. }
  284. //分车归理数据
  285. $res_one = [];
  286. foreach ($res as $res_key => $res_vel) {
  287. if ($res_vel['seat_count'] == $vel['seat_count']) {
  288. $res_one[$res_vel['bus_order_id']][$res_vel['seat_seq_id']] = $res_vel;
  289. }
  290. }
  291. //挑出可以放订单的车次,否则返回错误
  292. $result_one = [];
  293. foreach ($res_one as $res_key => $res_vel) {
  294. if (count($res_vel) >= $vel['num']) {
  295. $result_one = $res_vel;
  296. break;
  297. }
  298. }
  299. if (count($result_one) == 0) {
  300. return false;
  301. }
  302. //通过票种索引塞入数据
  303. $result[$vel['ticket_id']] = $result_one;
  304. }
  305. return $result;
  306. }
  307. /**
  308. * Function Description:通过日期和线路获取班次id
  309. * Function Name: getRunListByDateLine
  310. * @param string $run_date 出发日期
  311. * @param int $line_id 线路id
  312. *
  313. * @return array|ActiveRecord[]
  314. *
  315. * @author 张帅
  316. */
  317. public function getRunTicketIdListByRunId($run_date, $line_id)
  318. {
  319. $result = self::find()
  320. ->select([
  321. 'rm.run_id',
  322. 'rm.run_time',
  323. 'ticket_id' => 'rp.prod_id',
  324. 'start_area_id' => 'rp.start_station_area_id',
  325. 'start_res_id' => 'rp.start_station_res_id',
  326. 'end_area_id' => 'rp.end_station_area_id',
  327. 'end_res_id' => 'rp.end_station_res_id',
  328. ])
  329. ->from(self::tableName() . ' as rm')
  330. ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  331. ->innerJoin(RunStation::tableName() . ' as rs1', ' rs1.station_res_id = rp.start_station_res_id AND rs1.run_id=rm.run_id')
  332. ->innerJoin(RunStation::tableName() . ' as rs2', ' rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id')
  333. ->innerJoin(OperaTickets::tableName() . ' as ot', 'ot.ticket_id=rp.prod_id and ot.is_onsale=1 and ot.cancel_flag=0')
  334. ->where([
  335. 'and',
  336. ['=', 'rm.run_date', $run_date],
  337. ['=', 'rm.prod_id', $line_id],
  338. ['=', 'rm.run_status', 138],
  339. ['=', 'rp.cancel_flag', 0],
  340. ['in', 'rs1.station_inout_type', [108, 109]],
  341. ['in', 'rs2.station_inout_type', [110, 109]],
  342. ])
  343. ->asArray()->all();
  344. return $result;
  345. }
  346. /**
  347. * Function Description:通过日期和出发地目的地获取班次id
  348. * Function Name: getRunListByDateLine
  349. * @param string $run_date 出发日期
  350. * @param int $start_area 开始站id
  351. * @param int $end_area 结束站id
  352. *
  353. * @return array|ActiveRecord[]
  354. *
  355. * @author 张帅
  356. */
  357. public function getRunTicketIdListByArea($run_date, $start_area, $end_area, $supplier_id = 164, $user_id = 698)
  358. {
  359. $baseUser = new BaseUser();
  360. $mainCorpId = $baseUser->getMainCorpIdById($user_id);
  361. $where = [
  362. 'and',
  363. ['=', 'rm.run_date', $run_date],
  364. ['=', 'rm.run_status', 138],
  365. ['=', 'rp.cancel_flag', 0],
  366. ['in', 'rs1.station_inout_type', [108, 109]],
  367. ['in', 'rs2.station_inout_type', [110, 109]],
  368. ];
  369. if (empty($start_area) == false) {
  370. $where[] = ['=', 'rp.start_station_area_id', $start_area];
  371. }
  372. if (empty($end_area) == false) {
  373. $where[] = ['=', 'rp.end_station_area_id', $end_area];
  374. }
  375. if ($mainCorpId['main_corp_id'] == '34') {
  376. $where[] = ['=', 'ol.MAIN_CORP_ID', 34];
  377. }
  378. $result = self::find()
  379. ->select([
  380. 'rm.run_id',
  381. 'rm.run_time',
  382. 'ticket_id' => 'rp.prod_id',
  383. 'start_area_id' => 'rp.start_station_area_id',
  384. 'start_res_id' => 'rp.start_station_res_id',
  385. 'end_area_id' => 'rp.end_station_area_id',
  386. 'end_res_id' => 'rp.end_station_res_id',
  387. ])
  388. ->from(self::tableName() . ' as rm')
  389. ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  390. ->innerJoin(RunStation::tableName() . ' as rs1', ' rs1.station_res_id = rp.start_station_res_id AND rs1.run_id=rm.run_id')
  391. ->innerJoin(RunStation::tableName() . ' as rs2', ' rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id')
  392. ->innerJoin(OperaTickets::tableName() . ' as ot', 'ot.ticket_id=rp.prod_id and ot.is_onsale=1 and ot.cancel_flag=0')
  393. ->innerJoin(OperaLine::tableName() . ' as ol', 'ol.LINE_ID = ot.LINE_ID and ol.CANCEL_FLAG=0')
  394. ->where($where)
  395. ->asArray()->all();
  396. $check_agent = new SubmitBusAgentOrder();
  397. foreach ($result as $key => $val) {
  398. $tmp_list = $check_agent->getTicketAgentInfo($val['ticket_id'], $supplier_id, $user_id, $mainCorpId['main_corp_id']);
  399. if ($tmp_list['code'] != 0) {
  400. unset($result[$key]);
  401. }
  402. }
  403. return $result;
  404. }
  405. public function getRunTicketIdListByAreaNDate($start_date, $end_date, $start_area, $end_area, $line_id)
  406. {
  407. $where_array = [
  408. 'and',
  409. ['>=', 'rm.run_date', $start_date],
  410. ['<=', 'rm.run_date', $end_date],
  411. ['=', 'rm.run_status', 138],
  412. ['=', 'rp.cancel_flag', 0],
  413. ];
  414. if ($line_id != false && $line_id != '') {
  415. $where_array[] = ['=', 'rm.prod_id', $line_id];
  416. }
  417. if ($start_area != false && $start_area != '') {
  418. $where_array[] = ['=', 'rp.start_station_area_id', $start_area];
  419. }
  420. if ($end_area != false && $end_area != '') {
  421. $where_array[] = ['=', 'rp.end_station_area_id', $end_area];
  422. }
  423. $result = self::find()
  424. ->select([
  425. 'rm.run_id',
  426. 'rm.run_date',
  427. 'rm.run_time',
  428. 'ticket_id' => 'rp.prod_id',
  429. 'start_area_id' => 'rp.start_station_area_id',
  430. 'start_res_id' => 'rp.start_station_res_id',
  431. 'end_area_id' => 'rp.end_station_area_id',
  432. 'end_res_id' => 'rp.end_station_res_id',
  433. ])
  434. ->from(self::tableName() . ' as rm')
  435. ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  436. ->where($where_array)
  437. ->asArray()->all();
  438. return $result;
  439. }
  440. /**
  441. * Function Description:通过run_id获取班次信息
  442. * Function Name: getRunInfoByRunId
  443. * @param int $run_id 班次id
  444. *
  445. * @return array|bool|null|ActiveRecord
  446. *
  447. * @author 张帅
  448. */
  449. public function getRunInfoByRunId($run_id)
  450. {
  451. $result = self::find()
  452. ->select([
  453. 'run_id',//班次id
  454. 'run_date',//出发日期
  455. 'run_time',//出发时间
  456. 'line_id' => 'prod_id',//线路id
  457. 'run_status',
  458. ])
  459. ->where([
  460. 'and',
  461. ['=', 'run_status', 138],
  462. ['=', 'run_id', $run_id],
  463. ])
  464. ->asArray()->one();
  465. if (count($result) == 0) {
  466. return false;
  467. }
  468. return $result;
  469. }
  470. /**
  471. * Function Description:获取起始时间
  472. * Function Name: getStationStartTime
  473. * @param $run_ticket_arr
  474. *
  475. * @return array
  476. *
  477. * @author LUOCJ
  478. */
  479. function getStationStartTime($run_ticket_arr)
  480. {
  481. $data = [];
  482. foreach ($run_ticket_arr as $k => $v) {
  483. $data_one = $this->getStationTime($v['ticket_id'], $v['run_id']);
  484. $data[] = $data_one;
  485. }
  486. return $data;
  487. }
  488. /**
  489. * Function Description:循环获取站点时间数据
  490. * Function Name: getStationTime
  491. * @param $ticket_id
  492. * @param $run_id
  493. *
  494. * @return array
  495. *
  496. * @author LUOCJ
  497. */
  498. public function getStationTime($ticket_id, $run_id)
  499. {
  500. $result = (new Query())->select(['ticket_id', 'start_time' => 'rs_a.start_time', 'start_res_id' => 'rs_a.station_res_id', 'end_time' => 'rs_b.start_time', 'end_res_id' => 'rs_b.station_res_id'])->from('opera_tickets')
  501. ->leftJoin('run_station rs_a', 'opera_tickets.START_STATION_RES_ID = rs_a.STATION_RES_ID and rs_a.run_id=' . $run_id)
  502. ->leftJoin('run_station rs_b', 'opera_tickets.END_STATION_RES_ID = rs_b.STATION_RES_ID and rs_b.run_id=' . $run_id)
  503. ->where(['and', ['=', 'opera_tickets.ticket_id', $ticket_id], ['=', 'cancel_flag', 0]])->one();
  504. return array(
  505. 'ticket_id' => $ticket_id,
  506. 'run_id' => $run_id,
  507. 'start_res_id' => $result['start_res_id'],
  508. 'end_res_id' => $result['end_res_id'],
  509. 'start_time' => $result['start_time'],
  510. 'end_time' => $result['end_time'],
  511. );
  512. }
  513. /**
  514. * Function Description:获取结束时间
  515. * Function Name: getStationStartTime
  516. * @param $run_ticket_arr
  517. *
  518. * @return array
  519. *
  520. * @author LUOCJ
  521. */
  522. function getStationEndTime($run_ticket_arr)
  523. {
  524. $ticket_arr = [];
  525. foreach ($run_ticket_arr as $k => $v) {
  526. $ticket_arr[] = $v['ticket_id'];
  527. }
  528. $run_id = isset($run_ticket_arr[0]['run_id']) ? $run_ticket_arr[0]['run_id'] : '';
  529. if ($run_id == '') {
  530. return [];
  531. }
  532. $result = (new Query())->select(['ticket_id', 'start_time'])->from('opera_tickets')
  533. ->innerJoin('run_station', 'opera_tickets.END_STATION_RES_ID = run_station.STATION_RES_ID and run_station.run_id= ' . $run_id)
  534. ->where(['and', ['in', 'opera_tickets.ticket_id', $ticket_arr], ['=', 'cancel_flag', 0]])->groupBy('opera_tickets.ticket_id')->all();
  535. $fin = [];
  536. foreach ($result as $k => $v) {
  537. $fin[$v['ticket_id']] = $v['start_time'];
  538. }
  539. return $fin;
  540. }
  541. /**
  542. * Function Description:根据线路id和出发时间获取班次id
  543. * Function Name: getRunIdByLine
  544. * @param int $line_id 线路id
  545. * @param string $run_date 出发日期
  546. * @param string $start_time 班次出发时间
  547. * @return int
  548. * @author 温依莅
  549. */
  550. public static function getRunIdByLine($line_id, $run_date, $start_time)
  551. {
  552. $res = self::find()->select([
  553. 'run_id', 'run_date', 'run_time', 'prod_id as line_id',
  554. ])->where([
  555. 'prod_id' => $line_id,
  556. 'run_date' => $run_date,
  557. 'run_time' => $start_time,
  558. "run_status"=>138
  559. ])->limit(1)->asArray()->one();
  560. return $res['run_id'];
  561. }
  562. /**
  563. * Function Description:根据起始poi终点poi 和日期获取 当天可用线路及班次出发时间
  564. * Function Name: getRunLineByArea
  565. * @param $date
  566. * @param $start_poi
  567. * @param $end_poi
  568. *
  569. * @return array|ActiveRecord[]
  570. *
  571. * @author 娄梦宁
  572. */
  573. public function getRunLineByArea($date, $start_poi, $end_poi)
  574. {
  575. $select = [
  576. 'line_id' => 'rm.prod_id',
  577. 'run_time' => new Expression('GROUP_CONCAT(DISTINCT (select min(start_time) from run_station where run_id=rp.run_id and STATION_INOUT_TYPE !=110 and area_id=rp.start_station_area_id))'),
  578. ];
  579. $result = self::find()
  580. ->select($select)
  581. ->from(self::tableName() . ' as rm')
  582. ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  583. ->where([
  584. 'and',
  585. ['=', 'rm.run_date', $date],
  586. ['=', 'rm.run_status', 138],
  587. ['=', 'rp.cancel_flag', 0],
  588. ['=', 'rp.start_station_area_id', $start_poi],
  589. ['=', 'rp.end_station_area_id', $end_poi],
  590. ])
  591. ->groupBy('rm.prod_id')
  592. // ->createCommand()->getRawSql();
  593. ->asArray()->all();
  594. return $result;
  595. }
  596. /**
  597. * Function Description:获取线路运行信息
  598. * Function Name: getRunInfo
  599. * @param $run_id
  600. *
  601. * @return array|null|ActiveRecord
  602. *
  603. * @author 李健
  604. */
  605. public function getRunInfo($run_id)
  606. {
  607. $select = [
  608. 'ope.line_code as property',
  609. 'ope.line_name as prod_name',
  610. 'run.run_date',
  611. 'run.run_time',
  612. 'run.run_status',
  613. 'run.pre_sale_day'
  614. ];
  615. $where = ['and'];
  616. $where[] = ['=', 'run_id', $run_id,];
  617. $res = self::find()
  618. ->select($select)
  619. ->from(self::tableName() . ' run')
  620. ->innerJoin(OperaLine::tableName() . ' ope', 'run.prod_id=line_id')
  621. ->where($where)
  622. ->asArray()
  623. ->one();
  624. return $res;
  625. }
  626. /**
  627. * Function Description:
  628. * Function Name: getLineInfo
  629. * @param $run_id
  630. *
  631. * @return mixed
  632. *
  633. * @author 李健
  634. */
  635. public function getLineInfo($run_id)
  636. {
  637. $select = [
  638. 'concat(line.line_code,"(",line.line_name,")") as name',
  639. 'run.prod_id as lineid',
  640. 'run.pre_sale_day',
  641. 'run.run_date'
  642. ];
  643. $where = ['and'];
  644. $where[] = ['=', 'run.run_id', $run_id];
  645. $where[] = ['=', 'line.cancel_flag', 0];
  646. $where[] = ['=', 'line.if_disabled', 0];
  647. $res = self::find()
  648. ->select($select)
  649. ->from(self::tableName() . ' run')
  650. ->innerJoin(OperaLine::tableName() . ' line', 'run.prod_id=line.line_id')
  651. ->where($where)
  652. ->asArray()
  653. ->one();
  654. return $res;
  655. }
  656. /**
  657. * Function Description:获取途径站点
  658. * Function Name: getRunStation
  659. * @param $run_id
  660. *
  661. * @return array|ActiveRecord[]
  662. *
  663. * @author 李健
  664. */
  665. public function getRunStation($run_id)
  666. {
  667. $select = [
  668. 'm.PROD_ID',
  669. 's.start_time as DATE_TIME',
  670. 's.STATION_RES_ID',
  671. 'b.res_name as SITES',
  672. 's.station_inout_type as UP_DOWN_TYPE',
  673. 's.checkport_res_id as CHECKPORT',
  674. 'group_concat(concat(b2.res_id,":",b2.res_name)) as TICKET_GATE'
  675. ];
  676. $where = ['and'];
  677. $where[] = ['=', 'm.run_id', $run_id];
  678. $res = self::find()
  679. ->select($select)
  680. ->from(self::tableName() . ' m')
  681. ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
  682. ->leftJoin(BaseResource::tableName() . ' b', 'b.res_id=s.station_res_id and b.cancel_flag=0')
  683. ->leftJoin(BaseResource::tableName() . ' b2', 'b2.parent_id=s.station_res_id and b2.res_type_id=79 and b2.cancel_flag=0')
  684. ->where($where)
  685. ->groupBy('s.station_order_id')
  686. ->asArray()
  687. ->all();
  688. return $res;
  689. }
  690. /**
  691. * Function Description:获取prod_id
  692. * Function Name: getProdId
  693. * @param $run_id
  694. *
  695. * @return array|null|ActiveRecord
  696. *
  697. * @author 李健
  698. */
  699. public function getProdId($run_id)
  700. {
  701. $res = self::find()
  702. ->select('prod_id')
  703. ->from(self::tableName())
  704. ->where(['=', 'run_id', $run_id])
  705. ->asArray()
  706. ->one();
  707. return $res;
  708. }
  709. /**
  710. * Function Description:获取班次的出发日期
  711. * Function Name: getStartDate
  712. * @param $run_id
  713. *
  714. * @return array|null|ActiveRecord
  715. *
  716. * @author 李健
  717. */
  718. public function getStartDate($run_id)
  719. {
  720. $res = self::find()
  721. ->select('run_date as time')
  722. ->from(self::tableName())
  723. ->where('run_id=' . $run_id)
  724. ->asArray()
  725. ->one();
  726. return $res;
  727. }
  728. public function getRunList($start_date, $end_date, $run_status, $line, $bus_type, $opera_org_id, $main_corp_id, $time, $current_page, $page_size, $order_rule)
  729. {
  730. $rule = '';
  731. if ($order_rule == 2) {
  732. $rule = 'm.run_date asc , s.start_time asc ';
  733. } else if ($order_rule == 3) {
  734. $rule = 'saled_count desc';
  735. } else {
  736. $rule = '';
  737. }
  738. $base_area = new BaseArea();
  739. $select = [
  740. 'm.run_id as run_id',
  741. 'p.line_code as property',
  742. 'p.line_name as prod_name',
  743. 'm.run_date',
  744. 's.start_time',
  745. 'res_name' => new Expression('(select res_name from ' . BaseResource::tableName() . ' where res_id=s.station_res_id and cancel_flag=0 limit 1)'),
  746. 'p.line_type as bus_type',
  747. 'b.bus_order_id as car_number',
  748. 'saled_count' => "(select count(id) from order_main as o where o.run_id = m.run_id and o.RUN_BUS_ORDER_ID = b.bus_order_id and o.CANCEL_FLAG=0 and o.ORDER_STATUS !=148 and o.AGENT_LEVEL = 1 )",
  749. 'b.seat_count',
  750. 'b.seat_count as max_count',
  751. 'b.cost_type',
  752. 'b.cost_price',
  753. 'b.cost_motorcade_id as motorcade_id',// 承运车队
  754. 'b.send_driver_name',
  755. 'b.send_driver_mobile',
  756. 'b.send_bus_no',
  757. 'motorcade_name' => new Expression('(select supplier_name from ' . BaseSupplier::tableName() . ' where id=b.cost_motorcade_id limit 1)'),
  758. 'run_status' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=b.run_bus_status limit 1)'),//运营状态
  759. 'receiving' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=b.send_status limit 1)'),
  760. 'delay_time' => new Expression("ifnull((SELECT SUM(delay_minute) from " . RunBusDelay::tableName() . " WHERE run_id = m.run_id and bus_order_id = b.bus_order_id),0)"),
  761. 'is_send_message' => new Expression("ifnull((SELECT SUM(is_send_message) from " . RunBusDelay::tableName() . " WHERE run_id = m.run_id and bus_order_id = b.bus_order_id),0)"),
  762. ];
  763. $where = ['and'];
  764. $where[] = ['>=', 'run_date', $start_date];
  765. $where[] = ['<=', 'run_date', $end_date];
  766. if ($run_status > 0) {
  767. $where[] = ['=', 'b.run_bus_status', $run_status];
  768. }
  769. $where[] = ['=', 's.station_order_id', 1];
  770. if (trim($line) != '') {
  771. $where[] = ['or', ['like', 'p.line_code', $line], ['like', 'p.line_name', $line]];
  772. }
  773. if ($bus_type != 0) {
  774. $where[] = ['=', 'p.line_type', $bus_type];
  775. }
  776. if (count($opera_org_id) > 0 || $opera_org_id['opera_org_id'] != '') {
  777. $opera_org_id = explode(',', $opera_org_id['opera_org_id']);
  778. $where[] = ['in', 'p.top_org_id', $opera_org_id];
  779. }
  780. if (count($main_corp_id) > 0 && $main_corp_id['main_corp_id'] != '') {
  781. $main_corp_id = explode(',', $main_corp_id['main_corp_id']);
  782. $where[] = ['in', 'p.main_corp_id', $main_corp_id];
  783. }
  784. if ($time != '-1:-1') {
  785. $where[] = ['=', 'm.run_time', $time];
  786. }
  787. //查询限制条数
  788. $offset = ($current_page - 1) * $page_size;
  789. $list = self::find()
  790. ->select($select)
  791. ->from(self::tableName() . ' m')
  792. ->innerJoin(RunBus::tableName() . ' b', 'm.run_id=b.run_id and b.cancel_flag=0')
  793. ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
  794. ->innerJoin(OperaLine::tableName() . ' p', 'm.prod_id=p.line_id and p.cancel_flag=0')
  795. ->where($where)
  796. ->orderBy($rule)
  797. ->offset($offset)
  798. ->limit($page_size)
  799. // ->createCommand()
  800. // ->getRawSql();
  801. ->asArray()
  802. ->all();
  803. // 获取总条数
  804. $total_row = self::find()
  805. ->from(self::tableName() . ' m')
  806. ->innerJoin(RunBus::tableName() . ' b', 'm.run_id=b.run_id and b.cancel_flag=0')
  807. ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
  808. ->innerJoin(OperaLine::tableName() . ' p', 'm.prod_id=p.line_id and p.cancel_flag=0')
  809. ->where($where)
  810. ->count();
  811. $page_arr = $base_area->getPage($total_row, $page_size, $current_page);
  812. $data = [
  813. 'list' => $list,
  814. 'page_size_arr' => [10, 30, 50, 100],
  815. 'page' => [
  816. 'page_size' => $page_size,
  817. 'current_page' => $current_page,
  818. 'total_row' => $total_row,
  819. 'total_page' => ceil($total_row / $page_size)
  820. ],
  821. 'page_arr' => $page_arr
  822. ];
  823. return $data;
  824. }
  825. /**
  826. * Function Description:获取班次车次座位详细信息
  827. * Function Name: getRunxSeatInfo
  828. * @param $run_x
  829. * @param $run_id
  830. * @param $run_date
  831. * @param $bus_order_id
  832. *
  833. * @return array
  834. *
  835. * @author 娄梦宁
  836. */
  837. public function getRunxSeatInfo($run_x, $run_id, $run_date, $bus_order_id)
  838. {
  839. $sql = "select seat_seq_id,allow_select_seat,seat_status,order_main_id,order_id,seat_name,area_id,seat_x,seat_y,seat_type
  840. from $run_x where run_id=$run_id and run_date ='$run_date' and bus_order_id=$bus_order_id and cancel_flag=0 and seat_type not in (104,105)";
  841. $res = Yii::$app->db->createCommand($sql)->queryAll();
  842. return $res;
  843. }
  844. /**
  845. * Function Description:根据run_id获取相应记录是否存在
  846. * Function Name: GetCountByRunId
  847. * @param $runid
  848. *
  849. * @return int|string
  850. *
  851. * @author 冒炎
  852. */
  853. public function GetCountByRunId($runid)
  854. {
  855. $result = self::find()->from(self::tableName())->where(['=', 'run_id', $runid])->count();
  856. return $result;
  857. }
  858. /**
  859. * Function Description:根据run_id获取对应数据相关信息
  860. * Function Name: GetSomeInfoByRunId
  861. * @param $runid
  862. *
  863. * @return array|null|ActiveRecord
  864. *
  865. * @author 冒炎
  866. */
  867. public function GetSomeInfoByRunId($runid)
  868. {
  869. $rs = self::find()
  870. ->select([
  871. 'parent_prod_id' => 'prod_id',
  872. 'run_date'
  873. ])
  874. ->from(self::tableName())
  875. ->where(['=', 'run_id', $runid])
  876. ->asArray()
  877. ->one();
  878. return $rs;
  879. }
  880. /**
  881. * Function Description:修改车次の修改主表数据
  882. * Function Name: UpdateRunBusMain
  883. * @param $startime
  884. * @param $sellday
  885. * @param $run_time
  886. * @param $run_minutes
  887. * @param $runid
  888. *
  889. * @return bool
  890. *
  891. * @author 冒炎
  892. */
  893. public function UpdateRunBusMain($startime, $sellday, $run_time, $run_minutes, $runid)
  894. {
  895. self::updateAll(['RUN_DATE' => $startime, 'PRE_SALE_DAY' => $sellday, 'RUN_TIME' => $run_time, 'RUN_MINUTES' => $run_minutes], ['run_id' => $runid]);
  896. }
  897. /**
  898. * Function Description:获取是否选座状态
  899. * Function Name: GetRunxAllowSelectSeat
  900. * @param $run_x
  901. * @param $run_id
  902. * @param $order_id
  903. *
  904. * @return array|ActiveRecord[]
  905. *
  906. * @author 娄梦宁
  907. */
  908. public function GetRunxAllowSelectSeat($run_x, $run_id, $order_id)
  909. {
  910. $result = self::find()->select('allow_select_seat')->from($run_x)
  911. ->where(['and', ['=', 'run_id', $run_id], ['in', 'order_main_id', $order_id], ['=', 'cancel_flag', 0]])
  912. ->asArray()->all();
  913. return $result;
  914. }
  915. /**
  916. * Function Description:根据出发日期获取prod_id(s)
  917. * Function Name: getProdByDate
  918. * @param $fromDate
  919. * @return array|ActiveRecord[]
  920. * @author 田玲菲
  921. */
  922. public function getLineArrayByDateAndArea($fromDate, $fromCityId, $toCityId)
  923. {
  924. $where = [
  925. 'and',
  926. ['=', 'a.RUN_DATE', $fromDate],
  927. [
  928. 'or',
  929. ['=', 'c.parent_id', $fromCityId],
  930. ['=', 'c.ID', $fromCityId]
  931. ],
  932. [
  933. 'or',
  934. ['=', 'd.parent_id', $toCityId],
  935. ['=', 'd.ID', $toCityId]
  936. ],
  937. [
  938. 'or',
  939. ['=', 'e.MAIN_CORP_ID', 1],
  940. ['is not', 'f.line_id', null]
  941. ],
  942. ['=', 'b.CANCEL_FLAG', 0],
  943. ['=', 'a.RUN_STATUS', 138],
  944. ];
  945. $select = [
  946. 'line_id' => 'a.prod_id',
  947. ];
  948. $getResult = self::find()->select($select)
  949. ->from(self::tableName() . ' a')
  950. ->innerJoin(RunProd::tableName() . ' b', 'a.RUN_ID = b.RUN_ID')
  951. ->innerJoin(BaseArea::tableName() . ' c', 'b.START_STATION_AREA_ID = c.ID')
  952. ->innerJoin(BaseArea::tableName() . ' d', 'b.END_STATION_AREA_ID = d.ID')
  953. ->innerJoin(OperaLine::tableName() . ' e', 'a.PROD_ID = e.LINE_ID')
  954. ->leftJoin(OperaTicketsAgent::tableName() . ' f', 'a.PROD_ID = f.LINE_ID and f.CANCEL_FLAG = 0 and f.TO_MAIN_CORP_ID = 1 and f.IS_ONSALE = 1')
  955. ->where($where)
  956. ->groupBy('a.prod_id')
  957. ->asArray()
  958. ->all();
  959. return $getResult;
  960. }
  961. /**
  962. * Function Description:根据prod_id查找最近(1个月内)出发的最大日期
  963. * Function Name: getMaxRunDate
  964. * @param $prod_id
  965. * @return array
  966. * @author 田玲菲
  967. */
  968. public function getMaxRunDate($prod_id)
  969. {
  970. $sql = 'SELECT max(run_date) AS run_date FROM run_main AS rm WHERE rm.RUN_DATE <= date_add(NOW(), INTERVAL 1 MONTH) AND rm.RUN_DATE > DATE_FORMAT(NOW(),\'%Y-%m-%d\') AND prod_id = ' . $prod_id;
  971. $res = Yii::$app->db->createCommand($sql)->queryAll();
  972. return $res;
  973. }
  974. /***
  975. * Des:获取巴士库存
  976. * Name: getStockByDateLineIdRes
  977. * @param $line_id
  978. * @param $run_date
  979. * @param $time
  980. * @param $start_res
  981. * @param $end_res
  982. * @return array|ActiveRecord[]
  983. * @author 倪宗锋
  984. */
  985. public function getStockByDateLineIdRes($line_id, $run_date, $time, $start_res, $end_res)
  986. {
  987. $result = self::find()
  988. ->select([
  989. 'rm.run_id',//班次id
  990. 'start_date' => 'rm.run_date',//状态
  991. 'start_time' => 'rs.start_time',//开始时间
  992. 'run_status' => 'rm.run_status',//状态
  993. 'start_station_status' => 'rs.station_inout_type',//开始站状态
  994. 'end_station_status' => 'rs2.station_inout_type',//结束站状态
  995. //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
  996. 'count' => RunStock::find()->select("min(total_count-saled_count -3)")->where('run_id = rm.run_id and seat_type=72 and seq_id >= rs.station_order_id and seq_id < rs2.station_order_id'),
  997. ])
  998. ->from(self::tableName() . ' as rm')
  999. ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
  1000. ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
  1001. ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
  1002. ->where([
  1003. 'and',
  1004. ['=', 'rm.run_date', $run_date],
  1005. ['=', 'rm.run_status', 138],
  1006. ['=', 'rm.prod_id', $line_id],
  1007. ['=', 'rs.station_res_id', $start_res],
  1008. ['=', 'rs2.station_res_id', $end_res],
  1009. ])
  1010. ->asArray()->all();
  1011. return $result;
  1012. }
  1013. }