您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

293 行
11 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use yii\db\ActiveRecord;
  4. use yii\db\Expression;
  5. use yii\db\Query;
  6. /**
  7. * This is the model class for table "opera_tickets".
  8. *
  9. * @property integer $TICKET_ID
  10. * @property integer $CREATE_USER_ID
  11. * @property string $CREATE_TIME
  12. * @property integer $UPDATE_USER_ID
  13. * @property string $UPDATE_TIME
  14. * @property integer $CANCEL_FLAG
  15. * @property integer $LINE_ID
  16. * @property string $TICKET_NAME
  17. * @property integer $TICKET_TYPE
  18. * @property integer $START_STATION_RES_ID
  19. * @property integer $END_STATION_RES_ID
  20. * @property integer $START_STATION_AREA_ID
  21. * @property integer $END_STATION_AREA_ID
  22. * @property integer $SEAT_TYPE
  23. * @property integer $HUMAN_TYPE
  24. * @property string $PROD_PRICE
  25. * @property string $CUS_PRICE
  26. * @property string $COST_PRICE
  27. * @property string $IS_ONSALE
  28. */
  29. class OperaTickets extends ActiveRecord
  30. {
  31. /**
  32. * @inheritdoc
  33. */
  34. public static function tableName()
  35. {
  36. return 'opera_tickets';
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['CREATE_TIME'], 'required'],
  45. [['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'],
  46. [['UPDATE_TIME'], 'safe'],
  47. [['PROD_PRICE', 'CUS_PRICE', 'COST_PRICE'], 'number'],
  48. [['CREATE_TIME'], 'string', 'max' => 20],
  49. [['TICKET_NAME'], 'string', 'max' => 50],
  50. [['TICKET_ID'], 'unique'],
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'TICKET_ID' => 'Ticket ID',
  60. 'CREATE_USER_ID' => '记录创建用户ID',
  61. 'CREATE_TIME' => '记录创建时间',
  62. 'UPDATE_USER_ID' => '记录最后更新用户ID',
  63. 'UPDATE_TIME' => '记录最后更新时间',
  64. 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
  65. 'LINE_ID' => '线路ID',
  66. 'TICKET_NAME' => '票种名称',
  67. 'TICKET_TYPE' => '单程往返标志 TICKET_TYPE=1单程票,TICKET_TYPE=2往返票',
  68. 'START_STATION_RES_ID' => '上车站ID',
  69. 'END_STATION_RES_ID' => '下车站ID',
  70. 'START_STATION_AREA_ID' => '出发地ID',
  71. 'END_STATION_AREA_ID' => '目的地ID',
  72. 'SEAT_TYPE' => '座位类型,72-普通座',
  73. 'HUMAN_TYPE' => '人群属性 159-成人',
  74. 'PROD_PRICE' => '分销价',
  75. 'CUS_PRICE' => '零售价',
  76. 'COST_PRICE' => '预估成本',
  77. 'IS_ONSALE' => '是否可售,1可售0不可售',
  78. 'IS_CHECKED' => '是否需要检票,0:不检票,1:要检票',
  79. ];
  80. }
  81. /**
  82. * Function Description:通过票种id获取票种详情
  83. * Function Name: getTicketListByTicketId
  84. * @param array $ticket_id_arr 票种id数组
  85. * @param int $user_id 用户id
  86. *
  87. * @return array|\yii\db\ActiveRecord[]
  88. *
  89. * @author 张帅
  90. */
  91. public function getTicketListByTicketId($ticket_id_arr, $user_id, $ticket_id_arr2 = false, $ticket_id_arr3 = [])
  92. {
  93. $baseUser = new BaseUser();
  94. $mainCorpId = $baseUser->getMainCorpIdById($user_id);
  95. $sql_where = [
  96. 'and',
  97. ['=', 't.cancel_flag', 0],
  98. ['=', 't.ticket_type', 1],
  99. ['!=', 't.start_station_area_id', 0],
  100. ['!=', 't.end_station_area_id', 0],
  101. ['!=', 't.start_station_res_id', 0],
  102. ['!=', 't.end_station_res_id', 0],
  103. ['=', 'l.tailor_type', 0],
  104. ['!=', 'l.main_corp_id', 21],
  105. ['in', 'ticket_id', $ticket_id_arr],
  106. [
  107. 'or',
  108. ['in', 'ticket_id', $ticket_id_arr3],
  109. ['=', 'l.main_corp_id', $mainCorpId['main_corp_id']]
  110. ]
  111. ];
  112. if ($ticket_id_arr2 != false) {
  113. $sql_where[] = ['not in', 'ticket_id', $ticket_id_arr2];
  114. }
  115. if ($user_id == 450) {
  116. $sql_where[] = ['=', 'l.main_corp_id', 4];
  117. }
  118. $result = self::find()
  119. ->select([
  120. 'start_area_id' => 't.start_station_area_id',//出发地ID
  121. 'start_area_name' => BaseArea::find()->select('area_name')->where('id = t.start_station_area_id')->limit(1),//出发地name
  122. 'end_area_id' => 't.end_station_area_id',//目的地ID
  123. 'end_area_name' => BaseArea::find()->select('area_name')->where('id = t.end_station_area_id')->limit(1),//目的地name
  124. 'start_res_id' => 't.start_station_res_id',//上车站ID
  125. 'start_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.start_station_res_id')->limit(1),//上车站name
  126. 'end_res_id' => 't.end_station_res_id',//下车站ID
  127. 'end_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.end_station_res_id')->limit(1),//下车站name
  128. 'start_res_longitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.start_station_res_id and type_id=212 ')->limit(1),//上车站经度
  129. 'start_res_latitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.start_station_res_id and type_id=213 ')->limit(1),//上车站维度
  130. 'end_res_longitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.end_station_res_id and type_id=212 ')->limit(1),//下车站经度
  131. 'end_res_latitude' => BaseResourceProperty::find()->select('property')->where('res_id = t.end_station_res_id and type_id=213 ')->limit(1),//下车站维度
  132. 't.ticket_id',//票种id
  133. 't.line_id',//线路id
  134. 'line_type' => 'l.line_type',//线路类型
  135. 'line_name' => 'l.line_name',//线路类型
  136. 't.prod_price',//价格
  137. ])
  138. ->from(self::tableName() . ' as t')
  139. ->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
  140. ->where($sql_where)
  141. ->indexBy('ticket_id')
  142. // ->createCommand()->getRawSql();
  143. ->asArray()->all();
  144. return $result;
  145. }
  146. /**
  147. * Function Description:整理产品数据
  148. * Function Name: getBusProductList
  149. * @param string $prod_str 选购产品,格式为{去程/返程标志(1:去程 2:返程),去程/返程班次ID,票种ID,票种单价,预定数量,车号}...
  150. *
  151. * @return array
  152. *
  153. * @author 张帅
  154. */
  155. public function getBusProductList($prod_str)
  156. {
  157. $result = [];
  158. $prod_str = substr($prod_str, 1, -1);
  159. $prod_arr = explode('}{', $prod_str);
  160. foreach ($prod_arr as $key => $vel) {
  161. $vel = explode(',', $vel);
  162. $result[$vel[2]]['if_back'] = $vel[0];//1:去程 2:返程
  163. $result[$vel[2]]['run_id'] = $vel[1];//班次id
  164. $result[$vel[2]]['ticket_id'] = $vel[2];//票种id
  165. $result[$vel[2]]['price'] = $vel[3];//单价
  166. $result[$vel[2]]['num'] = $vel[4];//数量
  167. $result[$vel[2]]['bus_order_id'] = isset($vel[5]) ? $vel[5] : 0;//车号
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Function Description:获取该线路已有票种信息
  173. * Function Name: getExistedTickets
  174. * @param int $line_id 线路id
  175. *
  176. * @return array
  177. *
  178. * @author 温依莅
  179. */
  180. public static function getExistedTickets($line_id)
  181. {
  182. $res = self::find()->select([
  183. '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'
  184. ])->where([
  185. 'line_id' => $line_id, 'cancel_flag' => 0
  186. ])->indexBy(function ($row) {
  187. return $row['start_station_res_id'] . '-' . $row['end_station_res_id'];
  188. })->asArray()->all();
  189. return $res;
  190. }
  191. /**
  192. * Function Description:根据线路id和上下车站点获取票种信息
  193. * Function Name: getTicketByRes
  194. * @param $line_id
  195. * @param $start_res_id
  196. * @param $end_res_id
  197. *
  198. * @return array
  199. * @author 温依莅
  200. */
  201. public static function getTicketByRes($line_id, $start_res_id, $end_res_id)
  202. {
  203. $res = self::find()->select([
  204. 'ticket_id', 'line_id', 'start_station_res_id', 'end_station_res_id', 'cost_price', 'prod_price', 'cus_price'
  205. ])->where([
  206. 'line_id' => $line_id,
  207. 'start_station_res_id' => $start_res_id,
  208. 'end_station_res_id' => $end_res_id,
  209. 'is_onsale' => 1,
  210. 'cancel_flag' => 0
  211. ])->asArray()->limit(1)->one();
  212. return $res;
  213. }
  214. /**
  215. * Function Description:查询运行车票类型
  216. * Function Name: getRunTicketsType
  217. * @param $run_id
  218. * @param $prod_id
  219. *
  220. * @return array|ActiveRecord[]
  221. *
  222. * @author 李健
  223. */
  224. public function getRunTicketsType($run_id, $prod_id)
  225. {
  226. $select = [
  227. 'start_station_area_id as SID',
  228. 'end_station_area_id as EID',
  229. 'seat_type'
  230. ];
  231. $res1 = (new Query())
  232. ->select($select)
  233. ->distinct()
  234. ->from(self::tableName())
  235. ->where(['and', ['=', 'line_id', $prod_id], ['=', 'cancel_flag', 0]]);
  236. $res2 = (new Query())
  237. ->select($select)
  238. ->distinct()
  239. ->from(RunProd::tableName())
  240. ->where(['and', ['=', 'parent_prod_id', $prod_id], ['=', 'run_id', $run_id], ['=', 'cancel_flag', 0]]);
  241. $s = [
  242. 'a.SID',
  243. 'START_AREA' => new Expression('(select area_name from ' . BaseArea::tableName() . ' where id=a.SID and cancel_flag=0)'),
  244. 'a.EID',
  245. 'END_AREA' => new Expression('(select area_name from ' . BaseArea::tableName() . ' where id=a.EID and cancel_flag=0)'),
  246. 'SEAT' => new Expression('(select type_name from ' . DictType::tableName() . ' where id=a.seat_type)'),
  247. 'CROWD' => new Expression('""'),
  248. 'PRICE' => new Expression('0.00'),
  249. 'sel' => new Expression('0'),
  250. ];
  251. $res = self::find()
  252. ->select($s)
  253. ->from(['a' => $res1])
  254. ->leftJoin(['b' => $res2], 'a.sid=b.sid and a.eid=b.eid and a.seat_type=b.seat_type')
  255. ->where(['and', ['is', 'b.sid', null], ['is', 'b.eid', null], ['is', 'b.seat_type', null]])
  256. ->asArray()
  257. ->all();
  258. return $res;
  259. }
  260. /**
  261. * Function Description:根据线路id和出发时间获取班次id
  262. * Function Name: getLineIdByTickedId
  263. * @param int $ticket 票id
  264. * @return int
  265. */
  266. public function getLineIdByTickedId($ticket)
  267. {
  268. $res = self::find()->select([
  269. 'line_id'
  270. ])->where([
  271. 'ticket_id' => $ticket,
  272. ])->limit(1)->asArray()->one();
  273. return $res['line_id'];
  274. }
  275. }