|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
-
- namespace backend\modules\api\models;
-
- use yii\db\ActiveRecord;
- use yii\db\Expression;
-
- /**
- * This is the model class for table "run_prod".
- *
- * @property integer $ID
- * @property integer $RUN_ID
- * @property integer $PROD_ID
- * @property integer $LIST_SEQ_ID
- * @property integer $MAX_COUNT
- * @property integer $SALED_COUNT
- * @property integer $CANCEL_FLAG
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $START_STATION_RES_ID
- * @property integer $START_STATION_AREA_ID
- * @property integer $END_STATION_RES_ID
- * @property integer $END_STATION_AREA_ID
- * @property integer $HUMAN_TYPE
- * @property integer $SEAT_TYPE
- * @property integer $PARENT_PROD_ID
- */
- class RunProd extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'run_prod';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['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'],
- [['CREATE_USER_ID'], 'required'],
- [['CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'RUN_ID' => 'Run ID',
- 'PROD_ID' => 'Prod ID',
- 'LIST_SEQ_ID' => 'List Seq ID',
- 'MAX_COUNT' => 'Max Count',
- 'SALED_COUNT' => 'Saled Count',
- 'CANCEL_FLAG' => 'Cancel Flag',
- 'CREATE_USER_ID' => 'Create User ID',
- 'CREATE_TIME' => 'Create Time',
- 'UPDATE_USER_ID' => 'Update User ID',
- 'UPDATE_TIME' => 'Update Time',
- 'START_STATION_RES_ID' => 'Start Station Res ID',
- 'START_STATION_AREA_ID' => 'Start Station Area ID',
- 'END_STATION_RES_ID' => 'End Station Res ID',
- 'END_STATION_AREA_ID' => 'End Station Area ID',
- 'HUMAN_TYPE' => 'Human Type',
- 'SEAT_TYPE' => 'Seat Type',
- 'PARENT_PROD_ID' => 'Parent Prod ID',
- ];
- }
-
- /**
- * Function Description:通过班次id获取所有的ticket_id
- * Function Name: getTicketIdListByRunId
- * @param array $run_id_arr 班次id数组
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getTicketIdListByRunId($run_id_arr)
- {
- $result = self::find()
- ->select('prod_id as ticket_id')
- ->where([
- 'and',
- ['in', 'run_id', $run_id_arr],
- ['=', 'cancel_flag', 0],
- ])
- ->groupBy('ticket_id')
- ->asArray()->all();
- return $result;
- }
-
- /**
- * Function Description:根据班次和票种id获取产品详情列表
- * Function Name: getBusProductListByRunTicket
- * @param array $run_ticket_arr 产品详情列表
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getBusProductListByRunTicket($run_ticket_arr)
- {
- #region 1.班次票种筛选条件
- $and_where = [
- 'or',
- ];
- foreach ($run_ticket_arr as $key => $vel) {
- $and_where[] = [
- 'and',
- ['=', 'rp.run_id', $vel['run_id']],
- ['=', 'rp.prod_id', $vel['ticket_id']],
- ];
- }
- #endregion
-
- #region 2.查询结果
- $result = self::find()
- ->select([
- 'run_id' => 'rm.run_id',//班次id
- 'run_date' => 'rm.run_date',//出发日期
- 'run_time' => 'rm.run_time',//出发时间
- 'run_minutes' => 'rm.run_minutes',//出发分钟数
- 'ticket_id' => 'rp.prod_id',//票种id
- 'ticket_name' => 't.ticket_name',//票种名称
- 'seat_type' => 't.seat_type',//座位类型
- 'human_type' => 't.human_type',//人群属性
- 'line_id' => 't.line_id',//线路id
- 'line_name' => 'l.line_name',//线路名称
- 'start_res_id' => 't.start_station_res_id',//上车站id
- 'start_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.start_station_res_id')->limit(1),//上车站name
- 'start_area_id' => 't.start_station_area_id',//出发地id
- 'start_area_name' => BaseArea::find()->select('area_name')->where('id = t.start_station_area_id')->limit(1),//出发地name
- 'start_seq_id' => 's1.station_order_id',//开始站顺序号
- 'start_time' => 's1.start_time',//开始时间
- 'start_minutes' => 's1.start_minutes',//开始分钟数
- 'checkport_res_id' => 's1.checkport_res_id',//检票口id
- 'checkport_res_name' => BaseResource::find()->select('res_name')->where('res_id = s1.checkport_res_id')->limit(1),//检票口name
- 'end_res_id' => 't.end_station_res_id',//下车站id
- 'end_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.end_station_res_id')->limit(1),//下车站name
- 'end_area_id' => 't.end_station_area_id',//目的地id
- 'end_area_name' => BaseArea::find()->select('area_name')->where('id = t.end_station_area_id')->limit(1),//目的地name
- 'end_seq_id' => 's2.station_order_id',//结束站顺序号
- 'end_time' => 's2.start_time',//结束时间
- 'end_minutes' => 's2.start_minutes',//结束分钟数
- 'prod_price' => 't.prod_price',//分销价
- 'cus_price' => 't.cus_price',//零售价
- ])
- ->from(self::tableName() . ' as rp')
- ->leftJoin(RunMain::tableName() . ' as rm', 'rp.run_id = rm.run_id')
- ->leftJoin(OperaTickets::tableName() . ' as t', 'rp.prod_id = t.ticket_id')
- ->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
- ->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')
- ->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')
- ->where([
- 'and',
- ['=', 'rp.cancel_flag', 0],
- ['=', 't.cancel_flag', 0],
- ])
- ->andWhere($and_where)
- ->indexBy('ticket_id')
- ->asArray()->all();
- #endregion
-
- return $result;
- }
-
- /**
- * Function Description:获取票种信息
- * Function Name: getTicketType
- * @param $run_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 李健
- */
- public function getTicketType($run_id)
- {
- $select = [
- 'area1.area_name as start_area',
- 'area2.area_name as end_area',
- 'dict1.type_name as seat',
- 'dict2.type_name as croed',
- 'price' => new Expression('if(price.prod_price is null,0,price.prod_price)'),
- 'max_count',
- 'saled_count as sale_count'
- ];
- $where = ['and'];
- $where[] = ['=', 'run.run_id', $run_id];
- $where[] = ['=', 'run.cancel_flag', 0];
- $res = self::find()
- ->select($select)
- ->distinct()
- ->from(self::tableName() . ' run')
- ->leftJoin(OperaTickets::tableName() . ' price', 'price.ticket_id=run.prod_id and price.cancel_flag=0')
- ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=run.start_station_area_id')
- ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=run.end_station_area_id')
- ->leftJoin(DictType::tableName() . ' dict1', 'dict1.id=run.seat_type')
- ->leftJoin(DictType::tableName() . ' dict2', 'dict2.id=run.human_type')
- ->where($where)
- ->asArray()
- ->all();
- return $res;
- }
-
- /**
- * Function Description:获取票种信息
- * Function Name: getTicketInfo
- * @param $run_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 李健
- */
- public function getTicketInfo($run_id)
- {
- $select = [
- 'area1.area_name as start_area',
- 'area2.area_name as end_area',
- 'dict1.type_name as seat',
- 'dict2.type_name as crowd',
- 'price' => new Expression('if(price.prod_price is null,0,price.prod_price)'),
- ];
- $where = ['and'];
- $where[] = ['=', 'run.run_id', $run_id];
- $where[] = ['=', 'run.cancel_flag', 0];
- $res = self::find()
- ->select($select)
- ->distinct()
- ->from(self::tableName() . ' run')
- ->leftJoin(OperaTickets::tableName() . ' price', 'price.ticket_id=run.prod_id and price.cancel_flag=0')
- ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=run.start_station_area_id')
- ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=run.end_station_area_id')
- ->leftJoin(DictType::tableName() . ' dict1', 'dict1.id=run.seat_type')
- ->leftJoin(DictType::tableName() . ' dict2', 'dict2.id=run.human_type')
- ->where($where)
- ->asArray()
- ->all();
- return $res;
- }
-
-
- /**
- * Function Description:获取线路信息
- * Function Name: getRunTicketType
- * @param $run_id
- * @param $prod_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 李健
- */
- public function getRunTicketType($run_id, $prod_id)
- {
- $select = [
- 'p.start_station_area_id as SID',
- 'area1.area_name as START_AREA',
- 'p.end_station_area_id as EID',
- 'area2.area_name as END_AREA',
- 'dict.type_name as SEAT',
- 'CROWD' => new Expression('""'),
- 'PRICE' => new Expression('0.00'),
- 'sel' => new Expression('1'),
- ];
- $where = ['and'];
- $where[] = ['=', 'p.run_id', $run_id];
- $where[] = ['=', 'p.parent_prod_id', $prod_id];
- $where[] = ['=', 'p.cancel_flag', 0];
-
- $res = self::find()
- ->select($select)
- ->from(self::tableName() . ' p')
- ->leftJoin(BaseArea::tableName() . ' area1', 'area1.id=p.start_station_area_id and area1.cancel_flag=0')
- ->leftJoin(BaseArea::tableName() . ' area2', 'area2.id=p.end_station_area_id and area2.cancel_flag=0')
- ->leftJoin(DictType::tableName() . ' dict', 'dict.id=p.seat_type')
- ->where($where)
- ->asArray()
- ->all();
- return $res;
- }
-
- /**
- * Function Description:根据run_id获取相应票种信息
- * Function Name: GetProdInfoByRunId
- * @param $runid
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 冒炎
- */
- public function GetProdInfoByRunId($runid)
- {
- $list = self::find()
- ->select([
- 'ID',
- 'PROD_ID',
- 'START_STATION_AREA_ID',
- 'END_STATION_AREA_ID',
- 'SEAT_TYPE'
- ])
- ->from(self::tableName())
- ->where(['=', 'RUN_ID', $runid])
- ->asArray()
- ->all();
- return $list;
- }
-
- /**
- * Des:班次信息
- * Name: getSchedule
- * @param $fromDate
- * @param $fromCityId
- * @param $toCityId
- * @return array|ActiveRecord[]
- * @author 倪宗锋
- */
- public function getSchedule($fromDate, $fromCityId, $toCityId,$line_id)
- {
- $select = [
- 'fromTime' => 'b.run_time',
- '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)"),
- '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 )"),
- ];
- $where = [
- 'and',
- [
- 'or',
- ['=','c.parent_id',$fromCityId],
- ['=','c.ID',$fromCityId]
- ],
- [
- 'or',
- ['=','d.parent_id',$toCityId],
- ['=','d.ID',$toCityId]
- ],
- ['=','b.RUN_DATE',$fromDate],
- ['=','b.RUN_STATUS',138],
- ['=','a.CANCEL_FLAG',0],
- ['=','b.PROD_ID',$line_id],
- ];
- $list = self::find()->select($select)
- ->from(self::tableName() . ' a')
- ->innerJoin(RunMain::tableName() . ' b', 'a.RUN_ID = b.RUN_ID')
- ->innerJoin(BaseArea::tableName().' c', 'a.START_STATION_AREA_ID = c.ID')
- ->innerJoin(BaseArea::tableName().' d', 'a.END_STATION_AREA_ID = d.ID')
- ->where($where)
- ->asArray()
- ->groupBy('a.run_id')
- ->all();
- if (is_array($list) && empty($list['0']['leftTicketCnt']) == false) {
- $return = [];
- foreach ($list as $val) {
- $arr = [];
- $busArray = explode('-', $val['bus_info']);
- $arr['advanceBookMinutes'] = 5;
- $arr['fromTime'] = $val['fromTime'];
- $arr['leftTicketCnt'] = $val['leftTicketCnt'] > 0 ? $val['leftTicketCnt'] : 0;
- $arr['busLicense'] = $busArray['0'];
- $arr['vehicleDesc'] = '';
- $arr['seatCnt'] = $busArray['3'];
- $arr['driverName'] = $busArray['1'];
- $arr['driverMobile'] = $busArray['2'];
- $arr['travelMeal'] = '';
- $return[] = $arr;
- }
- } else {
- $return = [];
- }
- return $return;
- }
- }
|