Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

381 linhas
14 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use yii\db\ActiveRecord;
  4. use yii\db\Expression;
  5. /**
  6. * This is the model class for table "run_prod".
  7. *
  8. * @property integer $ID
  9. * @property integer $RUN_ID
  10. * @property integer $PROD_ID
  11. * @property integer $LIST_SEQ_ID
  12. * @property integer $MAX_COUNT
  13. * @property integer $SALED_COUNT
  14. * @property integer $CANCEL_FLAG
  15. * @property integer $CREATE_USER_ID
  16. * @property string $CREATE_TIME
  17. * @property integer $UPDATE_USER_ID
  18. * @property string $UPDATE_TIME
  19. * @property integer $START_STATION_RES_ID
  20. * @property integer $START_STATION_AREA_ID
  21. * @property integer $END_STATION_RES_ID
  22. * @property integer $END_STATION_AREA_ID
  23. * @property integer $HUMAN_TYPE
  24. * @property integer $SEAT_TYPE
  25. * @property integer $PARENT_PROD_ID
  26. */
  27. class RunProd extends ActiveRecord
  28. {
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'run_prod';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['RUN_ID', 'PROD_ID', 'LIST_SEQ_ID', 'MAX_COUNT', 'SALED_COUNT', 'CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'START_STATION_RES_ID', 'START_STATION_AREA_ID', 'END_STATION_RES_ID', 'END_STATION_AREA_ID', 'HUMAN_TYPE', 'SEAT_TYPE', 'PARENT_PROD_ID'], 'integer'],
  43. [['CREATE_USER_ID'], 'required'],
  44. [['CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'ID' => 'ID',
  54. 'RUN_ID' => 'Run ID',
  55. 'PROD_ID' => 'Prod ID',
  56. 'LIST_SEQ_ID' => 'List Seq ID',
  57. 'MAX_COUNT' => 'Max Count',
  58. 'SALED_COUNT' => 'Saled Count',
  59. 'CANCEL_FLAG' => 'Cancel Flag',
  60. 'CREATE_USER_ID' => 'Create User ID',
  61. 'CREATE_TIME' => 'Create Time',
  62. 'UPDATE_USER_ID' => 'Update User ID',
  63. 'UPDATE_TIME' => 'Update Time',
  64. 'START_STATION_RES_ID' => 'Start Station Res ID',
  65. 'START_STATION_AREA_ID' => 'Start Station Area ID',
  66. 'END_STATION_RES_ID' => 'End Station Res ID',
  67. 'END_STATION_AREA_ID' => 'End Station Area ID',
  68. 'HUMAN_TYPE' => 'Human Type',
  69. 'SEAT_TYPE' => 'Seat Type',
  70. 'PARENT_PROD_ID' => 'Parent Prod ID',
  71. ];
  72. }
  73. /**
  74. * Function Description:通过班次id获取所有的ticket_id
  75. * Function Name: getTicketIdListByRunId
  76. * @param array $run_id_arr 班次id数组
  77. *
  78. * @return array|\yii\db\ActiveRecord[]
  79. *
  80. * @author 张帅
  81. */
  82. public function getTicketIdListByRunId($run_id_arr)
  83. {
  84. $result = self::find()
  85. ->select('prod_id as ticket_id')
  86. ->where([
  87. 'and',
  88. ['in', 'run_id', $run_id_arr],
  89. ['=', 'cancel_flag', 0],
  90. ])
  91. ->groupBy('ticket_id')
  92. ->asArray()->all();
  93. return $result;
  94. }
  95. /**
  96. * Function Description:根据班次和票种id获取产品详情列表
  97. * Function Name: getBusProductListByRunTicket
  98. * @param array $run_ticket_arr 产品详情列表
  99. *
  100. * @return array|\yii\db\ActiveRecord[]
  101. *
  102. * @author 张帅
  103. */
  104. public function getBusProductListByRunTicket($run_ticket_arr)
  105. {
  106. #region 1.班次票种筛选条件
  107. $and_where = [
  108. 'or',
  109. ];
  110. foreach ($run_ticket_arr as $key => $vel) {
  111. $and_where[] = [
  112. 'and',
  113. ['=', 'rp.run_id', $vel['run_id']],
  114. ['=', 'rp.prod_id', $vel['ticket_id']],
  115. ];
  116. }
  117. #endregion
  118. #region 2.查询结果
  119. $result = self::find()
  120. ->select([
  121. 'run_id' => 'rm.run_id',//班次id
  122. 'run_date' => 'rm.run_date',//出发日期
  123. 'run_time' => 'rm.run_time',//出发时间
  124. 'run_minutes' => 'rm.run_minutes',//出发分钟数
  125. 'ticket_id' => 'rp.prod_id',//票种id
  126. 'ticket_name' => 't.ticket_name',//票种名称
  127. 'seat_type' => 't.seat_type',//座位类型
  128. 'human_type' => 't.human_type',//人群属性
  129. 'line_id' => 't.line_id',//线路id
  130. 'line_name' => 'l.line_name',//线路名称
  131. 'start_res_id' => 't.start_station_res_id',//上车站id
  132. 'start_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.start_station_res_id')->limit(1),//上车站name
  133. 'start_area_id' => 't.start_station_area_id',//出发地id
  134. 'start_area_name' => BaseArea::find()->select('area_name')->where('id = t.start_station_area_id')->limit(1),//出发地name
  135. 'start_seq_id' => 's1.station_order_id',//开始站顺序号
  136. 'start_time' => 's1.start_time',//开始时间
  137. 'start_minutes' => 's1.start_minutes',//开始分钟数
  138. 'checkport_res_id' => 's1.checkport_res_id',//检票口id
  139. 'checkport_res_name' => BaseResource::find()->select('res_name')->where('res_id = s1.checkport_res_id')->limit(1),//检票口name
  140. 'end_res_id' => 't.end_station_res_id',//下车站id
  141. 'end_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.end_station_res_id')->limit(1),//下车站name
  142. 'end_area_id' => 't.end_station_area_id',//目的地id
  143. 'end_area_name' => BaseArea::find()->select('area_name')->where('id = t.end_station_area_id')->limit(1),//目的地name
  144. 'end_seq_id' => 's2.station_order_id',//结束站顺序号
  145. 'end_time' => 's2.start_time',//结束时间
  146. 'end_minutes' => 's2.start_minutes',//结束分钟数
  147. 'prod_price' => 't.prod_price',//分销价
  148. 'cus_price' => 't.cus_price',//零售价
  149. ])
  150. ->from(self::tableName() . ' as rp')
  151. ->leftJoin(RunMain::tableName() . ' as rm', 'rp.run_id = rm.run_id')
  152. ->leftJoin(OperaTickets::tableName() . ' as t', 'rp.prod_id = t.ticket_id')
  153. ->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
  154. ->leftJoin(RunStation::tableName() . ' as s1', 'rm.run_id = s1.run_id and l.line_id = s1.prod_id and t.start_station_res_id = s1.station_res_id')
  155. ->leftJoin(RunStation::tableName() . ' as s2', 'rm.run_id = s2.run_id and l.line_id = s2.prod_id and t.end_station_res_id = s2.station_res_id')
  156. ->where([
  157. 'and',
  158. ['=', 'rp.cancel_flag', 0],
  159. ['=', 't.cancel_flag', 0],
  160. ])
  161. ->andWhere($and_where)
  162. ->indexBy('ticket_id')
  163. ->asArray()->all();
  164. #endregion
  165. return $result;
  166. }
  167. /**
  168. * Function Description:获取票种信息
  169. * Function Name: getTicketType
  170. * @param $run_id
  171. *
  172. * @return array|ActiveRecord[]
  173. *
  174. * @author 李健
  175. */
  176. public function getTicketType($run_id)
  177. {
  178. $select = [
  179. 'area1.area_name as start_area',
  180. 'area2.area_name as end_area',
  181. 'dict1.type_name as seat',
  182. 'dict2.type_name as croed',
  183. 'price' => new Expression('if(price.prod_price is null,0,price.prod_price)'),
  184. 'max_count',
  185. 'saled_count as sale_count'
  186. ];
  187. $where = ['and'];
  188. $where[] = ['=', 'run.run_id', $run_id];
  189. $where[] = ['=', 'run.cancel_flag', 0];
  190. $res = self::find()
  191. ->select($select)
  192. ->distinct()
  193. ->from(self::tableName() . ' run')
  194. ->leftJoin(OperaTickets::tableName() . ' price', 'price.ticket_id=run.prod_id and price.cancel_flag=0')
  195. ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=run.start_station_area_id')
  196. ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=run.end_station_area_id')
  197. ->leftJoin(DictType::tableName() . ' dict1', 'dict1.id=run.seat_type')
  198. ->leftJoin(DictType::tableName() . ' dict2', 'dict2.id=run.human_type')
  199. ->where($where)
  200. ->asArray()
  201. ->all();
  202. return $res;
  203. }
  204. /**
  205. * Function Description:获取票种信息
  206. * Function Name: getTicketInfo
  207. * @param $run_id
  208. *
  209. * @return array|ActiveRecord[]
  210. *
  211. * @author 李健
  212. */
  213. public function getTicketInfo($run_id)
  214. {
  215. $select = [
  216. 'area1.area_name as start_area',
  217. 'area2.area_name as end_area',
  218. 'dict1.type_name as seat',
  219. 'dict2.type_name as crowd',
  220. 'price' => new Expression('if(price.prod_price is null,0,price.prod_price)'),
  221. ];
  222. $where = ['and'];
  223. $where[] = ['=', 'run.run_id', $run_id];
  224. $where[] = ['=', 'run.cancel_flag', 0];
  225. $res = self::find()
  226. ->select($select)
  227. ->distinct()
  228. ->from(self::tableName() . ' run')
  229. ->leftJoin(OperaTickets::tableName() . ' price', 'price.ticket_id=run.prod_id and price.cancel_flag=0')
  230. ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=run.start_station_area_id')
  231. ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=run.end_station_area_id')
  232. ->leftJoin(DictType::tableName() . ' dict1', 'dict1.id=run.seat_type')
  233. ->leftJoin(DictType::tableName() . ' dict2', 'dict2.id=run.human_type')
  234. ->where($where)
  235. ->asArray()
  236. ->all();
  237. return $res;
  238. }
  239. /**
  240. * Function Description:获取线路信息
  241. * Function Name: getRunTicketType
  242. * @param $run_id
  243. * @param $prod_id
  244. *
  245. * @return array|ActiveRecord[]
  246. *
  247. * @author 李健
  248. */
  249. public function getRunTicketType($run_id, $prod_id)
  250. {
  251. $select = [
  252. 'p.start_station_area_id as SID',
  253. 'area1.area_name as START_AREA',
  254. 'p.end_station_area_id as EID',
  255. 'area2.area_name as END_AREA',
  256. 'dict.type_name as SEAT',
  257. 'CROWD' => new Expression('""'),
  258. 'PRICE' => new Expression('0.00'),
  259. 'sel' => new Expression('1'),
  260. ];
  261. $where = ['and'];
  262. $where[] = ['=', 'p.run_id', $run_id];
  263. $where[] = ['=', 'p.parent_prod_id', $prod_id];
  264. $where[] = ['=', 'p.cancel_flag', 0];
  265. $res = self::find()
  266. ->select($select)
  267. ->from(self::tableName() . ' p')
  268. ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=p.start_station_area_id and area1.cancel_flag=0')
  269. ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=p.end_station_area_id and area2.cancel_flag=0')
  270. ->leftJoin(DictType::tableName() . ' dict', 'dict.id=p.seat_type')
  271. ->where($where)
  272. ->asArray()
  273. ->all();
  274. return $res;
  275. }
  276. /**
  277. * Function Description:根据run_id获取相应票种信息
  278. * Function Name: GetProdInfoByRunId
  279. * @param $runid
  280. *
  281. * @return array|\yii\db\ActiveRecord[]
  282. *
  283. * @author 冒炎
  284. */
  285. public function GetProdInfoByRunId($runid)
  286. {
  287. $list = self::find()
  288. ->select([
  289. 'ID',
  290. 'PROD_ID',
  291. 'START_STATION_AREA_ID',
  292. 'END_STATION_AREA_ID',
  293. 'SEAT_TYPE'
  294. ])
  295. ->from(self::tableName())
  296. ->where(['=', 'RUN_ID', $runid])
  297. ->asArray()
  298. ->all();
  299. return $list;
  300. }
  301. /**
  302. * Des:班次信息
  303. * Name: getSchedule
  304. * @param $fromDate
  305. * @param $fromCityId
  306. * @param $toCityId
  307. * @return array|ActiveRecord[]
  308. * @author 倪宗锋
  309. */
  310. public function getSchedule($fromDate, $fromCityId, $toCityId,$line_id)
  311. {
  312. $select = [
  313. 'fromTime' => 'b.run_time',
  314. 'leftTicketCnt' => new Expression("(SELECT MIN(s.TOTAL_COUNT - s.SALED_COUNT -3) from run_stock s WHERE RUN_ID = a.RUN_ID and s.CANCEL_FLAG = 0 and s.SEAT_TYPE = 72)"),
  315. 'bus_info' => new Expression("(SELECT CONCAT(SEND_BUS_NO,'-',SEND_DRIVER_NAME,'-',SEND_DRIVER_MOBILE,'-',SEAT_COUNT) from run_bus t where t.RUN_ID = a.RUN_ID and t.CANCEL_FLAG = 0 ORDER BY (if((t.SEAT_COUNT - t.SALED_COUNT)=0,9999,t.SEAT_COUNT - t.SALED_COUNT)) LIMIT 1 )"),
  316. ];
  317. $where = [
  318. 'and',
  319. [
  320. 'or',
  321. ['=','c.parent_id',$fromCityId],
  322. ['=','c.ID',$fromCityId]
  323. ],
  324. [
  325. 'or',
  326. ['=','d.parent_id',$toCityId],
  327. ['=','d.ID',$toCityId]
  328. ],
  329. ['=','b.RUN_DATE',$fromDate],
  330. ['=','b.RUN_STATUS',138],
  331. ['=','a.CANCEL_FLAG',0],
  332. ['=','b.PROD_ID',$line_id],
  333. ];
  334. $list = self::find()->select($select)
  335. ->from(self::tableName() . ' a')
  336. ->innerJoin(RunMain::tableName() . ' b', 'a.RUN_ID = b.RUN_ID')
  337. ->innerJoin(BaseArea::tableName().' c', 'a.START_STATION_AREA_ID = c.ID')
  338. ->innerJoin(BaseArea::tableName().' d', 'a.END_STATION_AREA_ID = d.ID')
  339. ->where($where)
  340. ->asArray()
  341. ->groupBy('a.run_id')
  342. ->all();
  343. if (is_array($list) && empty($list['0']['leftTicketCnt']) == false) {
  344. $return = [];
  345. foreach ($list as $val) {
  346. $arr = [];
  347. $busArray = explode('-', $val['bus_info']);
  348. $arr['advanceBookMinutes'] = 5;
  349. $arr['fromTime'] = $val['fromTime'];
  350. $arr['leftTicketCnt'] = $val['leftTicketCnt'] > 0 ? $val['leftTicketCnt'] : 0;
  351. $arr['busLicense'] = $busArray['0'];
  352. $arr['vehicleDesc'] = '';
  353. $arr['seatCnt'] = $busArray['3'];
  354. $arr['driverName'] = $busArray['1'];
  355. $arr['driverMobile'] = $busArray['2'];
  356. $arr['travelMeal'] = '';
  357. $return[] = $arr;
  358. }
  359. } else {
  360. $return = [];
  361. }
  362. return $return;
  363. }
  364. }