|
- <?php
-
- namespace backend\modules\api\models;
-
- use yii\db\ActiveRecord;
- use yii\db\Expression;
- use yii\db\Query;
-
- /**
- * This is the model class for table "opera_tickets".
- *
- * @property integer $TICKET_ID
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $CANCEL_FLAG
- * @property integer $LINE_ID
- * @property string $TICKET_NAME
- * @property integer $TICKET_TYPE
- * @property integer $START_STATION_RES_ID
- * @property integer $END_STATION_RES_ID
- * @property integer $START_STATION_AREA_ID
- * @property integer $END_STATION_AREA_ID
- * @property integer $SEAT_TYPE
- * @property integer $HUMAN_TYPE
- * @property string $PROD_PRICE
- * @property string $CUS_PRICE
- * @property string $COST_PRICE
- * @property string $IS_ONSALE
- */
- class OperaTickets extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'opera_tickets';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CREATE_TIME'], 'required'],
- [['TICKET_ID', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'CANCEL_FLAG', 'LINE_ID', 'TICKET_TYPE', 'START_STATION_RES_ID', 'END_STATION_RES_ID', 'START_STATION_AREA_ID', 'END_STATION_AREA_ID', 'SEAT_TYPE', 'HUMAN_TYPE', 'IS_ONSALE', 'IS_CHECKED'], 'integer'],
- [['UPDATE_TIME'], 'safe'],
- [['PROD_PRICE', 'CUS_PRICE', 'COST_PRICE'], 'number'],
- [['CREATE_TIME'], 'string', 'max' => 20],
- [['TICKET_NAME'], 'string', 'max' => 50],
- [['TICKET_ID'], 'unique'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'TICKET_ID' => 'Ticket ID',
- 'CREATE_USER_ID' => '记录创建用户ID',
- 'CREATE_TIME' => '记录创建时间',
- 'UPDATE_USER_ID' => '记录最后更新用户ID',
- 'UPDATE_TIME' => '记录最后更新时间',
- 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
- 'LINE_ID' => '线路ID',
- 'TICKET_NAME' => '票种名称',
- 'TICKET_TYPE' => '单程往返标志 TICKET_TYPE=1单程票,TICKET_TYPE=2往返票',
- 'START_STATION_RES_ID' => '上车站ID',
- 'END_STATION_RES_ID' => '下车站ID',
- 'START_STATION_AREA_ID' => '出发地ID',
- 'END_STATION_AREA_ID' => '目的地ID',
- 'SEAT_TYPE' => '座位类型,72-普通座',
- 'HUMAN_TYPE' => '人群属性 159-成人',
- 'PROD_PRICE' => '分销价',
- 'CUS_PRICE' => '零售价',
- 'COST_PRICE' => '预估成本',
- 'IS_ONSALE' => '是否可售,1可售0不可售',
- 'IS_CHECKED' => '是否需要检票,0:不检票,1:要检票',
- ];
- }
-
- /**
- * Function Description:通过票种id获取票种详情
- * Function Name: getTicketListByTicketId
- * @param array $ticket_id_arr 票种id数组
- * @param int $user_id 用户id
- *
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getTicketListByTicketId($ticket_id_arr, $user_id, $ticket_id_arr2 = false, $ticket_id_arr3 = [])
- {
- $baseUser = new BaseUser();
- $mainCorpId = $baseUser->getMainCorpIdById($user_id);
- $sql_where = [
- 'and',
- ['=', 't.cancel_flag', 0],
- ['=', 't.ticket_type', 1],
- ['!=', 't.start_station_area_id', 0],
- ['!=', 't.end_station_area_id', 0],
- ['!=', 't.start_station_res_id', 0],
- ['!=', 't.end_station_res_id', 0],
- ['=', 'l.tailor_type', 0],
- ['!=', 'l.main_corp_id', 21],
- ['in', 'ticket_id', $ticket_id_arr],
- [
- 'or',
- ['in', 'ticket_id', $ticket_id_arr3],
- ['=', 'l.main_corp_id', $mainCorpId['main_corp_id']]
- ]
- ];
-
- if ($ticket_id_arr2 != false) {
- $sql_where[] = ['not in', 'ticket_id', $ticket_id_arr2];
- }
-
- if ($user_id == 450) {
- $sql_where[] = ['=', 'l.main_corp_id', 4];
- }
-
- $result = self::find()
- ->select([
- '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
- '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
- '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
- '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
- 'start_res_longitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.start_station_res_id and type_id=212 ')->limit(1),//上车站经度
- 'start_res_latitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.start_station_res_id and type_id=213 ')->limit(1),//上车站维度
- 'end_res_longitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.end_station_res_id and type_id=212 ')->limit(1),//下车站经度
- 'end_res_latitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.end_station_res_id and type_id=213 ')->limit(1),//下车站维度
- 't.ticket_id',//票种id
- 't.line_id',//线路id
- 'line_type' => 'l.line_type',//线路类型
- 'line_name' => 'l.line_name',//线路类型
- 't.prod_price',//价格
- ])
- ->from(self::tableName() . ' as t')
- ->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
- ->where($sql_where)
- ->indexBy('ticket_id')
- // ->createCommand()->getRawSql();
- ->asArray()->all();
-
- return $result;
- }
-
- /**
- * Function Description:整理产品数据
- * Function Name: getBusProductList
- * @param string $prod_str 选购产品,格式为{去程/返程标志(1:去程 2:返程),去程/返程班次ID,票种ID,票种单价,预定数量,车号}...
- *
- * @return array
- *
- * @author 张帅
- */
- public function getBusProductList($prod_str)
- {
- $result = [];
- $prod_str = substr($prod_str, 1, -1);
- $prod_arr = explode('}{', $prod_str);
- foreach ($prod_arr as $key => $vel) {
- $vel = explode(',', $vel);
- $result[$vel[2]]['if_back'] = $vel[0];//1:去程 2:返程
- $result[$vel[2]]['run_id'] = $vel[1];//班次id
- $result[$vel[2]]['ticket_id'] = $vel[2];//票种id
- $result[$vel[2]]['price'] = $vel[3];//单价
- $result[$vel[2]]['num'] = $vel[4];//数量
- $result[$vel[2]]['bus_order_id'] = isset($vel[5]) ? $vel[5] : 0;//车号
- }
-
- return $result;
- }
-
- /**
- * Function Description:获取该线路已有票种信息
- * Function Name: getExistedTickets
- * @param int $line_id 线路id
- *
- * @return array
- *
- * @author 温依莅
- */
- public static function getExistedTickets($line_id)
- {
- $res = self::find()->select([
- 'ticket_id', 'line_id', 'ticket_type', 'seat_type', 'human_type', 'start_station_res_id', 'start_station_area_id', 'end_station_res_id', 'end_station_area_id', 'cost_price', 'prod_price', 'cus_price', 'is_onsale'
- ])->where([
- 'line_id' => $line_id, 'cancel_flag' => 0
- ])->indexBy(function ($row) {
- return $row['start_station_res_id'] . '-' . $row['end_station_res_id'];
- })->asArray()->all();
- return $res;
- }
-
- /**
- * Function Description:根据线路id和上下车站点获取票种信息
- * Function Name: getTicketByRes
- * @param $line_id
- * @param $start_res_id
- * @param $end_res_id
- *
- * @return array
- * @author 温依莅
- */
- public static function getTicketByRes($line_id, $start_res_id, $end_res_id)
- {
- $res = self::find()->select([
- 'ticket_id', 'line_id', 'start_station_res_id', 'end_station_res_id', 'cost_price', 'prod_price', 'cus_price'
- ])->where([
- 'line_id' => $line_id,
- 'start_station_res_id' => $start_res_id,
- 'end_station_res_id' => $end_res_id,
- 'is_onsale' => 1,
- 'cancel_flag' => 0
- ])->asArray()->limit(1)->one();
- return $res;
- }
-
-
- /**
- * Function Description:查询运行车票类型
- * Function Name: getRunTicketsType
- * @param $run_id
- * @param $prod_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 李健
- */
- public function getRunTicketsType($run_id, $prod_id)
- {
- $select = [
- 'start_station_area_id as SID',
- 'end_station_area_id as EID',
- 'seat_type'
- ];
- $res1 = (new Query())
- ->select($select)
- ->distinct()
- ->from(self::tableName())
- ->where(['and', ['=', 'line_id', $prod_id], ['=', 'cancel_flag', 0]]);
- $res2 = (new Query())
- ->select($select)
- ->distinct()
- ->from(RunProd::tableName())
- ->where(['and', ['=', 'parent_prod_id', $prod_id], ['=', 'run_id', $run_id], ['=', 'cancel_flag', 0]]);
- $s = [
- 'a.SID',
- 'START_AREA' => new Expression('(select area_name from ' . BaseArea::tableName() . ' where id=a.SID and cancel_flag=0)'),
- 'a.EID',
- 'END_AREA' => new Expression('(select area_name from ' . BaseArea::tableName() . ' where id=a.EID and cancel_flag=0)'),
- 'SEAT' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=a.seat_type)'),
- 'CROWD' => new Expression('""'),
- 'PRICE' => new Expression('0.00'),
- 'sel' => new Expression('0'),
- ];
- $res = self::find()
- ->select($s)
- ->from(['a' => $res1])
- ->leftJoin(['b' => $res2], 'a.sid=b.sid and a.eid=b.eid and a.seat_type=b.seat_type')
- ->where(['and', ['is', 'b.sid', null], ['is', 'b.eid', null], ['is', 'b.seat_type', null]])
- ->asArray()
- ->all();
- return $res;
- }
-
- /**
- * Function Description:根据线路id和出发时间获取班次id
- * Function Name: getLineIdByTickedId
- * @param int $ticket 票id
- * @return int
- */
- public function getLineIdByTickedId($ticket)
- {
- $res = self::find()->select([
- 'line_id'
- ])->where([
- 'ticket_id' => $ticket,
- ])->limit(1)->asArray()->one();
- return $res['line_id'];
- }
- }
|