|
- <?php
-
- namespace backend\modules\api\models;
-
- use backend\modules\api\controllers\SubmitOrderController;
- use backend\modules\api\logic\SubmitBusAgentOrder;
- use backend\modules\api\models\BaseArea;
- use backend\modules\zzcs\models\RunBusDelay;
- use yii\db\ActiveRecord;
- use Yii;
- use yii\db\Expression;
- use yii\db\Query;
-
- /**
- * This is the model class for table "run_main".
- *
- * @property integer $ID
- * @property integer $RUN_ID
- * @property integer $RUN_LOCK_TYPE
- * @property string $RUN_DATE
- * @property string $RUN_TIME
- * @property integer $RUN_MINUTES
- * @property integer $PROD_ID
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $RUN_STATUS
- * @property integer $PRE_SALE_DAY
- * @property integer $RECEIVING
- * @property integer $HT_BUY_TYPE
- * @property string $HT_BASE_PRICE
- * @property string $HT_MID_PRICE
- * @property string $HT_ORDER_PRICE
- * @property integer $HT_CAN_SELL
- * @property integer $HT_BREAKFAST
- * @property integer $HT_IS_DELETE
- * @property integer $START_STATION_RES_ID
- * @property integer $START_STATION_AREA_ID
- * @property integer $END_STATION_RES_ID
- * @property integer $END_STATION_AREA_ID
- */
- class RunMain extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'run_main';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['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'],
- [['HT_BASE_PRICE', 'HT_MID_PRICE', 'HT_ORDER_PRICE'], 'number'],
- [['RUN_DATE', 'RUN_TIME', 'CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
- [['RUN_ID', 'PROD_ID'], 'unique', 'targetAttribute' => ['RUN_ID', 'PROD_ID'], 'message' => 'The combination of Run ID and Prod ID has already been taken.'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'RUN_ID' => 'Run ID',
- 'RUN_LOCK_TYPE' => 'Run Lock Type',
- 'RUN_DATE' => 'Run Date',
- 'RUN_TIME' => 'Run Time',
- 'RUN_MINUTES' => 'Run Minutes',
- 'PROD_ID' => 'Prod ID',
- 'CREATE_USER_ID' => 'Create User ID',
- 'CREATE_TIME' => 'Create Time',
- 'UPDATE_USER_ID' => 'Update User ID',
- 'UPDATE_TIME' => 'Update Time',
- 'RUN_STATUS' => 'Run Status',
- 'PRE_SALE_DAY' => 'Pre Sale Day',
- 'RECEIVING' => 'Receiving',
- 'HT_BUY_TYPE' => '酒店专用,采购类型',
- 'HT_BASE_PRICE' => '酒店专用,采购价',
- 'HT_MID_PRICE' => '酒店专用,分销价',
- 'HT_ORDER_PRICE' => '酒店专用,零售价',
- 'HT_CAN_SELL' => '酒店专用,销售开关:1可售 2不可售',
- 'HT_BREAKFAST' => '酒店专用,早餐',
- 'HT_IS_DELETE' => '酒店专用,删除:0未删除 1删除',
- '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',
- ];
- }
-
- /**
- * Function Description:通过日期获取班次id
- * Function Name: getRunListByDate
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getRunListByDate($start_date, $end_date)
- {
- $result = self::find()
- ->select('run_id')
- ->where([
- 'and',
- ['>=', 'run_date', $start_date],
- ['<=', 'run_date', $end_date],
- ['=', 'run_status', 138],
- ])
- ->asArray()->all();
- return $result;
- }
-
- /**
- * Function Description:通过出发日期和票种id获取班次数据
- * Function Name: getRunListByDateTicket
- * @param string $run_date 出发日期
- * @param int $ticket_id 票种id
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getRunListByDateTicket($run_date, $ticket_id)
- {
- $result = self::find()
- ->select([
- 'rm.run_id',//班次id
- 'start_date' => 'rm.run_date',//状态
- 'start_time' => 'rs.start_time',//开始时间
- 'run_status' => 'rm.run_status',//状态
- 'start_station_status' => 'rs.station_inout_type',//开始站状态
- 'end_station_status' => 'rs2.station_inout_type',//结束站状态
- //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
- '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'),
- ])
- ->from(self::tableName() . ' as rm')
- ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
- ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
- ->where([
- 'and',
- ['=', 'rm.run_date', $run_date],
- ['=', 'rp.prod_id', $ticket_id],
- ])
- ->groupBy('run_id')
- ->asArray()->all();
-
- $current_time = date("Y-m-d H:i", strtotime("+30 minutes"));
- $return_array = array();
- foreach ($result as $run_info_tmp) {
- $start_time = $run_info_tmp["start_date"] . " " . $run_info_tmp["start_time"];
- 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) {
- $run_info_tmp["count"] = 0;
- } else {
- $run_info_tmp["count"] = $run_info_tmp["count"] > 3 ? $run_info_tmp["count"] - 3 : 0;
- }
- $return_array[] = $run_info_tmp;
- }
-
- return $return_array;
- }
-
- /**
- * Function Description:通过出发日期和票种id获取班次数据
- * Function Name: getRunListByDateTicket
- * @param string $run_date 出发日期
- * @param int $ticket_id 票种id
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getRunListBySnEDateTicket($start_date, $end_date, $ticket_id)
- {
- $result = self::find()
- ->select([
- 'rm.run_id',//班次id
- 'start_date' => 'rm.run_date',//状态
- 'start_time' => 'rs.start_time',//开始时间
- 'run_status' => 'rm.run_status',//状态
- 'start_station_status' => 'rs.station_inout_type',//开始站状态
- 'end_station_status' => 'rs2.station_inout_type',//结束站状态
- //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
- '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'),
- ])
- ->from(self::tableName() . ' as rm')
- ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
- ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
- ->where([
- 'and',
- ['>=', 'rm.run_date', $start_date],
- ['<=', 'rm.run_date', $end_date],
- ['=', 'rp.prod_id', $ticket_id],
- ])
- ->groupBy('run_id')
- ->asArray()->all();
-
- $current_time = date("Y-m-d H:i", strtotime("+30 minutes"));
- $return_array = array();
- foreach ($result as $run_info_tmp) {
- $start_time = $run_info_tmp["start_date"] . " " . $run_info_tmp["start_time"];
- 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) {
- $run_info_tmp["count"] = 0;
- } else {
- $run_info_tmp["count"] = $run_info_tmp["count"] > 3 ? $run_info_tmp["count"] - 3 : 0;
- }
- $return_array[] = $run_info_tmp;
- }
-
- return $return_array;
- }
-
- /**
- * Function Description:检查run_x中是否有班次
- * Function Name: checkRunX
- * @param array $date_run_arr 日期班次数组
- *
- * @return bool
- *
- * @author 张帅
- */
- public function checkRunX($date_run_arr)
- {
- #region 没有班次直接返回错误
- if (count($date_run_arr) == 0) {
- return false;
- }
- #endregion
-
- #region 判断每个班次是否存在run_x
- foreach ($date_run_arr as $key => $vel) {
- $sql = '' . 'select
- count(*) as total_count
- from run_' . $key . '
- where run_id in (' . implode(',', $vel) . ')group by run_id';
- $res = Yii::$app->db->createCommand($sql)->queryAll();
-
- #region 如果有不存在班次的直接返回错误
- if (count($res) != count($vel)) {
- return false;
- }
-
- foreach ($res as $res_key => $res_vel) {
- if ($res_vel['total_count'] == 0) {
- return false;
- }
- }
- #endregion
- }
- #endregion
-
- return true;
- }
-
- /**
- * Function Description:获取所有的座位
- * Function Name: getRunBunSeat
- * @param array $run_bus_arr 班次信息
- *
- * @return array|bool
- *
- * @author 张帅
- */
- public function getRunBusSeat($run_bus_arr)
- {
- $result = [];
- foreach ($run_bus_arr as $key => $vel) {
- $sql_and = '';
- if ($vel['bus_order_id'] != 0) {
- $sql_and = ' and bus_order_id = ' . $vel['bus_order_id'];
- }
-
- $sql = '' . 'select
- 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
- from
- run_' . $vel['run_date'] . '
- where
- run_id = ' . $vel['run_id'] . '
- and order_id >= ' . $vel['start_seq_id'] . '
- and order_id < ' . $vel['end_seq_id'] . '
- and seat_type = ' . $vel['seat_type'] . $sql_and . '
- and cancel_flag = 0
- and seat_status = 0
- group by
- run_id,bus_order_id,seat_type,seat_seq_id,seat_name
- order by seat_seq_id ASC';
- $res = Yii::$app->db->createCommand($sql)->queryAll();
- if (count($res) == 0) {
- return false;
- }
-
- //分车归理数据
- $res_one = [];
- foreach ($res as $res_key => $res_vel) {
- if ($res_vel['seat_count'] == $vel['seat_count']) {
- $res_one[$res_vel['bus_order_id']][$res_vel['seat_seq_id']] = $res_vel;
- }
- }
-
- //挑出可以放订单的车次,否则返回错误
- $result_one = [];
- foreach ($res_one as $res_key => $res_vel) {
- if (count($res_vel) >= $vel['num']) {
- $result_one = $res_vel;
- break;
- }
- }
- if (count($result_one) == 0) {
- return false;
- }
-
- //通过票种索引塞入数据
- $result[$vel['ticket_id']] = $result_one;
- }
- return $result;
- }
-
- /**
- * Function Description:通过日期和线路获取班次id
- * Function Name: getRunListByDateLine
- * @param string $run_date 出发日期
- * @param int $line_id 线路id
- *
- * @return array|ActiveRecord[]
- *
- * @author 张帅
- */
- public function getRunTicketIdListByRunId($run_date, $line_id)
- {
- $result = self::find()
- ->select([
- 'rm.run_id',
- 'rm.run_time',
- 'ticket_id' => 'rp.prod_id',
- 'start_area_id' => 'rp.start_station_area_id',
- 'start_res_id' => 'rp.start_station_res_id',
- 'end_area_id' => 'rp.end_station_area_id',
- 'end_res_id' => 'rp.end_station_res_id',
- ])
- ->from(self::tableName() . ' as rm')
- ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->innerJoin(RunStation::tableName() . ' as rs1', ' rs1.station_res_id = rp.start_station_res_id AND rs1.run_id=rm.run_id')
- ->innerJoin(RunStation::tableName() . ' as rs2', ' rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id')
- ->innerJoin(OperaTickets::tableName() . ' as ot', 'ot.ticket_id=rp.prod_id and ot.is_onsale=1 and ot.cancel_flag=0')
- ->where([
- 'and',
- ['=', 'rm.run_date', $run_date],
- ['=', 'rm.prod_id', $line_id],
- ['=', 'rm.run_status', 138],
- ['=', 'rp.cancel_flag', 0],
- ['in', 'rs1.station_inout_type', [108, 109]],
- ['in', 'rs2.station_inout_type', [110, 109]],
- ])
- ->asArray()->all();
-
- return $result;
- }
-
- /**
- * Function Description:通过日期和出发地目的地获取班次id
- * Function Name: getRunListByDateLine
- * @param string $run_date 出发日期
- * @param int $start_area 开始站id
- * @param int $end_area 结束站id
- *
- * @return array|ActiveRecord[]
- *
- * @author 张帅
- */
- public function getRunTicketIdListByArea($run_date, $start_area, $end_area, $supplier_id = 164, $user_id = 698)
- {
- $baseUser = new BaseUser();
- $mainCorpId = $baseUser->getMainCorpIdById($user_id);
- $where = [
- 'and',
- ['=', 'rm.run_date', $run_date],
- ['=', 'rm.run_status', 138],
- ['=', 'rp.cancel_flag', 0],
- ['in', 'rs1.station_inout_type', [108, 109]],
- ['in', 'rs2.station_inout_type', [110, 109]],
- ];
- if (empty($start_area) == false) {
- $where[] = ['=', 'rp.start_station_area_id', $start_area];
- }
- if (empty($end_area) == false) {
- $where[] = ['=', 'rp.end_station_area_id', $end_area];
- }
- if ($mainCorpId['main_corp_id'] == '34') {
- $where[] = ['=', 'ol.MAIN_CORP_ID', 34];
- }
-
- $result = self::find()
- ->select([
- 'rm.run_id',
- 'rm.run_time',
- 'ticket_id' => 'rp.prod_id',
- 'start_area_id' => 'rp.start_station_area_id',
- 'start_res_id' => 'rp.start_station_res_id',
- 'end_area_id' => 'rp.end_station_area_id',
- 'end_res_id' => 'rp.end_station_res_id',
- ])
- ->from(self::tableName() . ' as rm')
- ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->innerJoin(RunStation::tableName() . ' as rs1', ' rs1.station_res_id = rp.start_station_res_id AND rs1.run_id=rm.run_id')
- ->innerJoin(RunStation::tableName() . ' as rs2', ' rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id')
- ->innerJoin(OperaTickets::tableName() . ' as ot', 'ot.ticket_id=rp.prod_id and ot.is_onsale=1 and ot.cancel_flag=0')
- ->innerJoin(OperaLine::tableName() . ' as ol', 'ol.LINE_ID = ot.LINE_ID and ol.CANCEL_FLAG=0')
- ->where($where)
- ->asArray()->all();
- $check_agent = new SubmitBusAgentOrder();
-
- foreach ($result as $key => $val) {
- $tmp_list = $check_agent->getTicketAgentInfo($val['ticket_id'], $supplier_id, $user_id, $mainCorpId['main_corp_id']);
- if ($tmp_list['code'] != 0) {
- unset($result[$key]);
- }
- }
- return $result;
- }
-
- public function getRunTicketIdListByAreaNDate($start_date, $end_date, $start_area, $end_area, $line_id)
- {
- $where_array = [
- 'and',
- ['>=', 'rm.run_date', $start_date],
- ['<=', 'rm.run_date', $end_date],
- ['=', 'rm.run_status', 138],
- ['=', 'rp.cancel_flag', 0],
- ];
- if ($line_id != false && $line_id != '') {
- $where_array[] = ['=', 'rm.prod_id', $line_id];
- }
- if ($start_area != false && $start_area != '') {
- $where_array[] = ['=', 'rp.start_station_area_id', $start_area];
- }
- if ($end_area != false && $end_area != '') {
- $where_array[] = ['=', 'rp.end_station_area_id', $end_area];
- }
-
- $result = self::find()
- ->select([
- 'rm.run_id',
- 'rm.run_date',
- 'rm.run_time',
- 'ticket_id' => 'rp.prod_id',
- 'start_area_id' => 'rp.start_station_area_id',
- 'start_res_id' => 'rp.start_station_res_id',
- 'end_area_id' => 'rp.end_station_area_id',
- 'end_res_id' => 'rp.end_station_res_id',
- ])
- ->from(self::tableName() . ' as rm')
- ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->where($where_array)
- ->asArray()->all();
-
- return $result;
- }
-
- /**
- * Function Description:通过run_id获取班次信息
- * Function Name: getRunInfoByRunId
- * @param int $run_id 班次id
- *
- * @return array|bool|null|ActiveRecord
- *
- * @author 张帅
- */
- public function getRunInfoByRunId($run_id)
- {
- $result = self::find()
- ->select([
- 'run_id',//班次id
- 'run_date',//出发日期
- 'run_time',//出发时间
- 'line_id' => 'prod_id',//线路id
- 'run_status',
- ])
- ->where([
- 'and',
- ['=', 'run_status', 138],
- ['=', 'run_id', $run_id],
- ])
- ->asArray()->one();
-
- if (count($result) == 0) {
- return false;
- }
-
- return $result;
- }
-
- /**
- * Function Description:获取起始时间
- * Function Name: getStationStartTime
- * @param $run_ticket_arr
- *
- * @return array
- *
- * @author LUOCJ
- */
- function getStationStartTime($run_ticket_arr)
- {
- $data = [];
- foreach ($run_ticket_arr as $k => $v) {
- $data_one = $this->getStationTime($v['ticket_id'], $v['run_id']);
- $data[] = $data_one;
- }
- return $data;
- }
-
- /**
- * Function Description:循环获取站点时间数据
- * Function Name: getStationTime
- * @param $ticket_id
- * @param $run_id
- *
- * @return array
- *
- * @author LUOCJ
- */
- public function getStationTime($ticket_id, $run_id)
- {
- $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')
- ->leftJoin('run_station rs_a', 'opera_tickets.START_STATION_RES_ID = rs_a.STATION_RES_ID and rs_a.run_id=' . $run_id)
- ->leftJoin('run_station rs_b', 'opera_tickets.END_STATION_RES_ID = rs_b.STATION_RES_ID and rs_b.run_id=' . $run_id)
- ->where(['and', ['=', 'opera_tickets.ticket_id', $ticket_id], ['=', 'cancel_flag', 0]])->one();
-
- return array(
- 'ticket_id' => $ticket_id,
- 'run_id' => $run_id,
- 'start_res_id' => $result['start_res_id'],
- 'end_res_id' => $result['end_res_id'],
- 'start_time' => $result['start_time'],
- 'end_time' => $result['end_time'],
- );
- }
-
- /**
- * Function Description:获取结束时间
- * Function Name: getStationStartTime
- * @param $run_ticket_arr
- *
- * @return array
- *
- * @author LUOCJ
- */
- function getStationEndTime($run_ticket_arr)
- {
- $ticket_arr = [];
- foreach ($run_ticket_arr as $k => $v) {
- $ticket_arr[] = $v['ticket_id'];
- }
- $run_id = isset($run_ticket_arr[0]['run_id']) ? $run_ticket_arr[0]['run_id'] : '';
- if ($run_id == '') {
- return [];
- }
- $result = (new Query())->select(['ticket_id', 'start_time'])->from('opera_tickets')
- ->innerJoin('run_station', 'opera_tickets.END_STATION_RES_ID = run_station.STATION_RES_ID and run_station.run_id= ' . $run_id)
- ->where(['and', ['in', 'opera_tickets.ticket_id', $ticket_arr], ['=', 'cancel_flag', 0]])->groupBy('opera_tickets.ticket_id')->all();
- $fin = [];
- foreach ($result as $k => $v) {
- $fin[$v['ticket_id']] = $v['start_time'];
- }
- return $fin;
- }
-
- /**
- * Function Description:根据线路id和出发时间获取班次id
- * Function Name: getRunIdByLine
- * @param int $line_id 线路id
- * @param string $run_date 出发日期
- * @param string $start_time 班次出发时间
- * @return int
- * @author 温依莅
- */
- public static function getRunIdByLine($line_id, $run_date, $start_time)
- {
- $res = self::find()->select([
- 'run_id', 'run_date', 'run_time', 'prod_id as line_id',
- ])->where([
- 'prod_id' => $line_id,
- 'run_date' => $run_date,
- 'run_time' => $start_time,
- "run_status"=>138
- ])->limit(1)->asArray()->one();
- return $res['run_id'];
- }
-
- /**
- * Function Description:根据起始poi终点poi 和日期获取 当天可用线路及班次出发时间
- * Function Name: getRunLineByArea
- * @param $date
- * @param $start_poi
- * @param $end_poi
- *
- * @return array|ActiveRecord[]
- *
- * @author 娄梦宁
- */
- public function getRunLineByArea($date, $start_poi, $end_poi)
- {
- $select = [
- 'line_id' => 'rm.prod_id',
- '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))'),
- ];
- $result = self::find()
- ->select($select)
- ->from(self::tableName() . ' as rm')
- ->innerJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->where([
- 'and',
- ['=', 'rm.run_date', $date],
- ['=', 'rm.run_status', 138],
- ['=', 'rp.cancel_flag', 0],
- ['=', 'rp.start_station_area_id', $start_poi],
- ['=', 'rp.end_station_area_id', $end_poi],
- ])
- ->groupBy('rm.prod_id')
- // ->createCommand()->getRawSql();
- ->asArray()->all();
- return $result;
- }
-
- /**
- * Function Description:获取线路运行信息
- * Function Name: getRunInfo
- * @param $run_id
- *
- * @return array|null|ActiveRecord
- *
- * @author 李健
- */
- public function getRunInfo($run_id)
- {
- $select = [
- 'ope.line_code as property',
- 'ope.line_name as prod_name',
- 'run.run_date',
- 'run.run_time',
- 'run.run_status',
- 'run.pre_sale_day'
- ];
- $where = ['and'];
- $where[] = ['=', 'run_id', $run_id,];
- $res = self::find()
- ->select($select)
- ->from(self::tableName() . ' run')
- ->innerJoin(OperaLine::tableName() . ' ope', 'run.prod_id=line_id')
- ->where($where)
- ->asArray()
- ->one();
- return $res;
- }
-
- /**
- * Function Description:
- * Function Name: getLineInfo
- * @param $run_id
- *
- * @return mixed
- *
- * @author 李健
- */
- public function getLineInfo($run_id)
- {
- $select = [
- 'concat(line.line_code,"(",line.line_name,")") as name',
- 'run.prod_id as lineid',
- 'run.pre_sale_day',
- 'run.run_date'
- ];
- $where = ['and'];
- $where[] = ['=', 'run.run_id', $run_id];
- $where[] = ['=', 'line.cancel_flag', 0];
- $where[] = ['=', 'line.if_disabled', 0];
-
- $res = self::find()
- ->select($select)
- ->from(self::tableName() . ' run')
- ->innerJoin(OperaLine::tableName() . ' line', 'run.prod_id=line.line_id')
- ->where($where)
- ->asArray()
- ->one();
- return $res;
- }
-
- /**
- * Function Description:获取途径站点
- * Function Name: getRunStation
- * @param $run_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 李健
- */
- public function getRunStation($run_id)
- {
- $select = [
- 'm.PROD_ID',
- 's.start_time as DATE_TIME',
- 's.STATION_RES_ID',
- 'b.res_name as SITES',
- 's.station_inout_type as UP_DOWN_TYPE',
- 's.checkport_res_id as CHECKPORT',
- 'group_concat(concat(b2.res_id,":",b2.res_name)) as TICKET_GATE'
- ];
- $where = ['and'];
- $where[] = ['=', 'm.run_id', $run_id];
- $res = self::find()
- ->select($select)
- ->from(self::tableName() . ' m')
- ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
- ->leftJoin(BaseResource::tableName() . ' b', 'b.res_id=s.station_res_id and b.cancel_flag=0')
- ->leftJoin(BaseResource::tableName() . ' b2', 'b2.parent_id=s.station_res_id and b2.res_type_id=79 and b2.cancel_flag=0')
- ->where($where)
- ->groupBy('s.station_order_id')
- ->asArray()
- ->all();
- return $res;
- }
-
- /**
- * Function Description:获取prod_id
- * Function Name: getProdId
- * @param $run_id
- *
- * @return array|null|ActiveRecord
- *
- * @author 李健
- */
- public function getProdId($run_id)
- {
- $res = self::find()
- ->select('prod_id')
- ->from(self::tableName())
- ->where(['=', 'run_id', $run_id])
- ->asArray()
- ->one();
- return $res;
- }
-
- /**
- * Function Description:获取班次的出发日期
- * Function Name: getStartDate
- * @param $run_id
- *
- * @return array|null|ActiveRecord
- *
- * @author 李健
- */
- public function getStartDate($run_id)
- {
- $res = self::find()
- ->select('run_date as time')
- ->from(self::tableName())
- ->where('run_id=' . $run_id)
- ->asArray()
- ->one();
- return $res;
- }
-
- 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)
- {
- $rule = '';
- if ($order_rule == 2) {
- $rule = 'm.run_date asc , s.start_time asc ';
- } else if ($order_rule == 3) {
- $rule = 'saled_count desc';
- } else {
- $rule = '';
- }
- $base_area = new BaseArea();
- $select = [
- 'm.run_id as run_id',
- 'p.line_code as property',
- 'p.line_name as prod_name',
- 'm.run_date',
- 's.start_time',
- 'res_name' => new Expression('(select res_name from ' . BaseResource::tableName() . ' where res_id=s.station_res_id and cancel_flag=0 limit 1)'),
- 'p.line_type as bus_type',
- 'b.bus_order_id as car_number',
- '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 )",
- 'b.seat_count',
- 'b.seat_count as max_count',
- 'b.cost_type',
- 'b.cost_price',
- 'b.cost_motorcade_id as motorcade_id',// 承运车队
- 'b.send_driver_name',
- 'b.send_driver_mobile',
- 'b.send_bus_no',
- 'motorcade_name' => new Expression('(select supplier_name from ' . BaseSupplier::tableName() . ' where id=b.cost_motorcade_id limit 1)'),
- 'run_status' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=b.run_bus_status limit 1)'),//运营状态
- 'receiving' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=b.send_status limit 1)'),
- '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)"),
- '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)"),
- ];
- $where = ['and'];
- $where[] = ['>=', 'run_date', $start_date];
- $where[] = ['<=', 'run_date', $end_date];
- if ($run_status > 0) {
- $where[] = ['=', 'b.run_bus_status', $run_status];
- }
- $where[] = ['=', 's.station_order_id', 1];
- if (trim($line) != '') {
- $where[] = ['or', ['like', 'p.line_code', $line], ['like', 'p.line_name', $line]];
- }
- if ($bus_type != 0) {
- $where[] = ['=', 'p.line_type', $bus_type];
- }
- if (count($opera_org_id) > 0 || $opera_org_id['opera_org_id'] != '') {
- $opera_org_id = explode(',', $opera_org_id['opera_org_id']);
- $where[] = ['in', 'p.top_org_id', $opera_org_id];
- }
-
- if (count($main_corp_id) > 0 && $main_corp_id['main_corp_id'] != '') {
- $main_corp_id = explode(',', $main_corp_id['main_corp_id']);
- $where[] = ['in', 'p.main_corp_id', $main_corp_id];
- }
-
- if ($time != '-1:-1') {
- $where[] = ['=', 'm.run_time', $time];
- }
-
- //查询限制条数
- $offset = ($current_page - 1) * $page_size;
- $list = self::find()
- ->select($select)
- ->from(self::tableName() . ' m')
- ->innerJoin(RunBus::tableName() . ' b', 'm.run_id=b.run_id and b.cancel_flag=0')
- ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
- ->innerJoin(OperaLine::tableName() . ' p', 'm.prod_id=p.line_id and p.cancel_flag=0')
- ->where($where)
- ->orderBy($rule)
- ->offset($offset)
- ->limit($page_size)
- // ->createCommand()
- // ->getRawSql();
- ->asArray()
- ->all();
-
- // 获取总条数
- $total_row = self::find()
- ->from(self::tableName() . ' m')
- ->innerJoin(RunBus::tableName() . ' b', 'm.run_id=b.run_id and b.cancel_flag=0')
- ->innerJoin(RunStation::tableName() . ' s', 'm.run_id=s.run_id')
- ->innerJoin(OperaLine::tableName() . ' p', 'm.prod_id=p.line_id and p.cancel_flag=0')
- ->where($where)
- ->count();
-
- $page_arr = $base_area->getPage($total_row, $page_size, $current_page);
- $data = [
- 'list' => $list,
- 'page_size_arr' => [10, 30, 50, 100],
- 'page' => [
- 'page_size' => $page_size,
- 'current_page' => $current_page,
- 'total_row' => $total_row,
- 'total_page' => ceil($total_row / $page_size)
- ],
- 'page_arr' => $page_arr
- ];
- return $data;
-
-
- }
-
- /**
- * Function Description:获取班次车次座位详细信息
- * Function Name: getRunxSeatInfo
- * @param $run_x
- * @param $run_id
- * @param $run_date
- * @param $bus_order_id
- *
- * @return array
- *
- * @author 娄梦宁
- */
- public function getRunxSeatInfo($run_x, $run_id, $run_date, $bus_order_id)
- {
- $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
- 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)";
- $res = Yii::$app->db->createCommand($sql)->queryAll();
- return $res;
- }
-
- /**
- * Function Description:根据run_id获取相应记录是否存在
- * Function Name: GetCountByRunId
- * @param $runid
- *
- * @return int|string
- *
- * @author 冒炎
- */
- public function GetCountByRunId($runid)
- {
- $result = self::find()->from(self::tableName())->where(['=', 'run_id', $runid])->count();
- return $result;
- }
-
- /**
- * Function Description:根据run_id获取对应数据相关信息
- * Function Name: GetSomeInfoByRunId
- * @param $runid
- *
- * @return array|null|ActiveRecord
- *
- * @author 冒炎
- */
- public function GetSomeInfoByRunId($runid)
- {
- $rs = self::find()
- ->select([
- 'parent_prod_id' => 'prod_id',
- 'run_date'
- ])
- ->from(self::tableName())
- ->where(['=', 'run_id', $runid])
- ->asArray()
- ->one();
- return $rs;
- }
-
- /**
- * Function Description:修改车次の修改主表数据
- * Function Name: UpdateRunBusMain
- * @param $startime
- * @param $sellday
- * @param $run_time
- * @param $run_minutes
- * @param $runid
- *
- * @return bool
- *
- * @author 冒炎
- */
- public function UpdateRunBusMain($startime, $sellday, $run_time, $run_minutes, $runid)
- {
- self::updateAll(['RUN_DATE' => $startime, 'PRE_SALE_DAY' => $sellday, 'RUN_TIME' => $run_time, 'RUN_MINUTES' => $run_minutes], ['run_id' => $runid]);
-
- }
-
- /**
- * Function Description:获取是否选座状态
- * Function Name: GetRunxAllowSelectSeat
- * @param $run_x
- * @param $run_id
- * @param $order_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 娄梦宁
- */
- public function GetRunxAllowSelectSeat($run_x, $run_id, $order_id)
- {
- $result = self::find()->select('allow_select_seat')->from($run_x)
- ->where(['and', ['=', 'run_id', $run_id], ['in', 'order_main_id', $order_id], ['=', 'cancel_flag', 0]])
- ->asArray()->all();
- return $result;
- }
-
-
- /**
- * Function Description:根据出发日期获取prod_id(s)
- * Function Name: getProdByDate
- * @param $fromDate
- * @return array|ActiveRecord[]
- * @author 田玲菲
- */
- public function getLineArrayByDateAndArea($fromDate, $fromCityId, $toCityId)
- {
- $where = [
- 'and',
- ['=', 'a.RUN_DATE', $fromDate],
- [
- 'or',
- ['=', 'c.parent_id', $fromCityId],
- ['=', 'c.ID', $fromCityId]
- ],
- [
- 'or',
- ['=', 'd.parent_id', $toCityId],
- ['=', 'd.ID', $toCityId]
- ],
- [
- 'or',
- ['=', 'e.MAIN_CORP_ID', 1],
- ['is not', 'f.line_id', null]
- ],
- ['=', 'b.CANCEL_FLAG', 0],
- ['=', 'a.RUN_STATUS', 138],
- ];
- $select = [
- 'line_id' => 'a.prod_id',
- ];
- $getResult = self::find()->select($select)
- ->from(self::tableName() . ' a')
- ->innerJoin(RunProd::tableName() . ' b', 'a.RUN_ID = b.RUN_ID')
- ->innerJoin(BaseArea::tableName() . ' c', 'b.START_STATION_AREA_ID = c.ID')
- ->innerJoin(BaseArea::tableName() . ' d', 'b.END_STATION_AREA_ID = d.ID')
- ->innerJoin(OperaLine::tableName() . ' e', 'a.PROD_ID = e.LINE_ID')
- ->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')
- ->where($where)
- ->groupBy('a.prod_id')
- ->asArray()
- ->all();
- return $getResult;
- }
-
-
- /**
- * Function Description:根据prod_id查找最近(1个月内)出发的最大日期
- * Function Name: getMaxRunDate
- * @param $prod_id
- * @return array
- * @author 田玲菲
- */
- public function getMaxRunDate($prod_id)
- {
- $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;
- $res = Yii::$app->db->createCommand($sql)->queryAll();
- return $res;
- }
-
- /***
- * Des:获取巴士库存
- * Name: getStockByDateLineIdRes
- * @param $line_id
- * @param $run_date
- * @param $time
- * @param $start_res
- * @param $end_res
- * @return array|ActiveRecord[]
- * @author 倪宗锋
- */
- public function getStockByDateLineIdRes($line_id, $run_date, $time, $start_res, $end_res)
- {
- $result = self::find()
- ->select([
- 'rm.run_id',//班次id
- 'start_date' => 'rm.run_date',//状态
- 'start_time' => 'rs.start_time',//开始时间
- 'run_status' => 'rm.run_status',//状态
- 'start_station_status' => 'rs.station_inout_type',//开始站状态
- 'end_station_status' => 'rs2.station_inout_type',//结束站状态
- //'count' => RunBus::find()->select("max(seat_count-saled_count)")->where('run_id = rm.run_id'),
- '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'),
- ])
- ->from(self::tableName() . ' as rm')
- ->leftJoin(RunProd::tableName() . ' as rp', 'rm.run_id = rp.run_id')
- ->leftJoin(RunStation::tableName() . ' as rs', 'rs.station_res_id = rp.start_station_res_id AND rs.run_id=rm.run_id ')
- ->leftJoin(RunStation::tableName() . ' as rs2', 'rs2.station_res_id = rp.end_station_res_id AND rs2.run_id=rm.run_id ')
- ->where([
- 'and',
- ['=', 'rm.run_date', $run_date],
- ['=', 'rm.run_status', 138],
- ['=', 'rm.prod_id', $line_id],
- ['=', 'rs.station_res_id', $start_res],
- ['=', 'rs2.station_res_id', $end_res],
- ])
- ->asArray()->all();
- return $result;
- }
-
- }
|