選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

413 行
13 KiB

  1. <?php
  2. namespace backend\modules\hotel\models;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\db\Expression;
  6. /**
  7. * This is the model class for table "opera_hotel_room".
  8. *
  9. * @property integer $ID
  10. * @property integer $CANCEL_FLAG
  11. * @property integer $CREATE_USER_ID
  12. * @property string $CREATE_TIME
  13. * @property integer $UPDATE_USER_ID
  14. * @property string $UPDATE_TIME
  15. * @property integer $HOTEL_ID
  16. * @property integer $PARENT_ROOM_TYPE
  17. * @property integer $ROOM_TYPE
  18. * @property string $ROOM_NAME
  19. * @property integer $OCCUPANCY_LIMIT
  20. * @property integer $BREAKFAST_INCLUDE
  21. * @property integer $IS_ONSALE
  22. * @property integer $LASTEST_BOOK_TIME
  23. * @property integer $LASTEST_CANCEL_DAY
  24. */
  25. class OperaHotelRoom extends \yii\db\ActiveRecord
  26. {
  27. const IS_ONSALE = 1;
  28. const IS_OFFSALE = 0;
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'opera_hotel_room';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'HOTEL_ID', 'PARENT_ROOM_TYPE', 'ROOM_TYPE', 'OCCUPANCY_LIMIT', 'BREAKFAST_INCLUDE', 'IS_ONSALE'], 'integer'],
  43. [['UPDATE_TIME'], 'safe'],
  44. [['CREATE_TIME'], 'string', 'max' => 20],
  45. [['ROOM_NAME', 'LATEST_BOOKING_TIME'], 'string', 'max' => 100],
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'ID' => 'ID',
  55. 'CANCEL_FLAG' => 'Cancel Flag',
  56. 'CREATE_USER_ID' => '记录创建用户ID',
  57. 'CREATE_TIME' => '记录创建时间',
  58. 'UPDATE_USER_ID' => '记录最后更新用户ID',
  59. 'UPDATE_TIME' => '记录最后更新时间',
  60. 'HOTEL_ID' => '酒店ID,对应opera_hotel.hotel_id',
  61. 'PARENT_ROOM_TYPE' => '基础房型,对应base_resource.res_id',
  62. 'ROOM_TYPE' => '子房型ID',
  63. 'ROOM_NAME' => '子房型名称',
  64. 'OCCUPANCY_LIMIT' => '入住人数限制 0:无限制',
  65. 'BREAKFAST_INCLUDE' => '是否含早,对应dict_type.id',
  66. 'IS_ONSALE' => '该子房型是否上线 1为上线 0为下线',
  67. 'LATEST_BOOKING_TIME' => '最晚预订时间、最晚取消时间',
  68. 'BED_TYPE' => '子房型床型',
  69. ];
  70. }
  71. /**
  72. * User: wangxj
  73. *
  74. * 根据ID,返回ROOM_TYPE,以便下单使用
  75. *
  76. * @param $id
  77. * @return int
  78. */
  79. public static function getSpiderRoomType($id)
  80. {
  81. $room = OperaHotelRoom::findOne($id);
  82. if ($room != null) {
  83. return $room->ROOM_TYPE;
  84. } else {
  85. return 0;
  86. }
  87. }
  88. /**
  89. * Notes:
  90. * User: Steven
  91. * Date: 2018/1/18
  92. * Time: 14:56
  93. * @param $child_bed_type
  94. * @param $parent_bed_type
  95. * @return string
  96. */
  97. public static function getBedDesc($child_bed_type, $parent_bed_type)
  98. {
  99. $bed_name = '';
  100. if ($child_bed_type != 0) {
  101. $bed_name .= $child_bed_type == 1 ? '大床' : ($child_bed_type == 2 ? '双床' : '多床');
  102. } else {
  103. $bed_arr = explode(',', $parent_bed_type);
  104. foreach ($bed_arr as $item) {
  105. $bed_desc = $item == 1 ? '大' : ($item == 2 ? '双' : '多');
  106. $bed_name .= $bed_name == '' ? $bed_desc : ' / ' . $bed_desc;
  107. }
  108. $bed_name .= '床';
  109. }
  110. return $bed_name;
  111. }
  112. /**
  113. * User: wangxj
  114. *
  115. * 获取房型设置的最晚取消时间
  116. *
  117. * @param $run_date string
  118. * @return string timestamp
  119. */
  120. public function getLatestCancel($run_date)
  121. {
  122. $book_str = $this->LASTEST_CANCEL_DAY;
  123. if ($book_str == -1) { //不可取消 直接返回1970年的时间戳
  124. $time = strtotime('1970-01-01');
  125. return $time;
  126. }
  127. //0,23:30
  128. $arr = explode(',', $book_str);
  129. $endTime = strtotime($run_date) + 86400;
  130. $hour_arr = explode(':', $arr[1]);
  131. $lastTime = $endTime - $arr[0] * 86400 - (24 - $hour_arr[0]) * 3600;
  132. return $lastTime;
  133. }
  134. public function getHotelRelation()
  135. {
  136. return $this->hasOne(HotelRelation::className(), ['HotelId' => 'HOTEL_ID']);
  137. }
  138. public function getRunHotel()
  139. {
  140. return $this->hasMany(RunHotel::className(), ['HOTEL_ID' => 'HOTEL_ID', 'BASE_ROOM_TYPE' => 'PARENT_ROOM_TYPE']);
  141. }
  142. public function getRoomRelation()
  143. {
  144. return $this->hasOne(RoomRelation::className(), ['RoomId' => 'ID']);
  145. }
  146. public function getOperaHotelBaseRoom()
  147. {
  148. return $this->hasOne(OperaHotelBaseRoom::className(), ['MAIN_ID' => 'PARENT_ROOM_TYPE']);
  149. }
  150. public function getOperaHotel()
  151. {
  152. return $this->hasOne(OperaHotel::className(), ['HOTEL_ID' => 'HOTEL_ID']);
  153. }
  154. public function getOperaRoomDistrib()
  155. {
  156. return $this->hasOne(OperaRoomDistrib::className(), ['ROOM_ID' => 'ID']);
  157. }
  158. public function getHotelRoomInfo($channel_room_id)
  159. {
  160. $sql = "select b.HOTEL_ID,c.HOTEL_NAME,b.ROOM_NAME,b.INNER_IDENTIFY,a.ChannelRoomId,c.PRINCIPAL,d.USER_NAME from channel_room_relation a
  161. LEFT JOIN opera_hotel_room b on a.RoomId=b.ID and b.CANCEL_FLAG=0
  162. LEFT JOIN opera_hotel c on b.HOTEL_ID=c.HOTEL_ID and c.CANCEL_FLAG=0
  163. LEFT JOIN base_user d on c.PRINCIPAL=d.ID
  164. where a.ChannelRoomId={$channel_room_id} and a.ChannelId=" . Yii::$app->params['ctrip']['relation_supplier_id'];
  165. $connection = Yii::$app->db;
  166. $res = $connection->createCommand($sql)->queryAll();
  167. return $res;
  168. }
  169. /**
  170. * 根据酒店id与基础房型id查询该基础房型下的所有子房型
  171. */
  172. public function getAllRoom($hotel_id, $base_room)
  173. {
  174. // $sql = 'SELECT ID,ROOM_NAME FROM opera_hotel_room WHERE ID not in (SELECT room_id FROM price_compared_room) AND CANCEL_FLAG = 0 AND HOTEL_ID = '.$hotel_id .' AND PARENT_ROOM_TYPE = '.$base_room;
  175. // $res = Yii::$app -> db -> createCommand($sql) -> queryAll();
  176. $res = OperaHotelRoom::find()
  177. ->select(['ID', 'CONCAT(ROOM_NAME, \'|\', INNER_IDENTIFY) as CON_NAME'])
  178. ->where(['HOTEL_ID' => $hotel_id, 'PARENT_ROOM_TYPE' => $base_room, 'CANCEL_FLAG' => 0])
  179. ->asArray()
  180. // -> createCommand() -> getSql();
  181. ->all();
  182. return $res;
  183. }
  184. /**
  185. * @Author wanglg
  186. * @Desc 根据房型id获取阿里推送房型数据,暂时不用
  187. */
  188. public function getRoomInfo($room_id)
  189. {
  190. $res = OperaHotelRoom::find()->select(['a.ROOM_TYPE', 'a.ROOM_NAME', 'a.PARENT_ROOM_TYPE', 'a.HOTEL_ID'])
  191. ->joinWith('operaHotelBaseRoom as b')
  192. ->from('opera_hotel_room as a')
  193. ->where(['a.ID' => $room_id, 'a.CANCEL_FLAG' => 0])
  194. ->asArray()->one();
  195. return $res;
  196. }
  197. /**
  198. * Notes:获取可下单的酒店
  199. * User: Steven
  200. * Date: 2018/1/18
  201. * Time: 13:32
  202. */
  203. public function getHotelProduct($param)
  204. {
  205. $query = OperaHotelRoom::find()->select(['a.*', 'c.MAIN_ID'])
  206. ->joinWith('operaHotel b')
  207. ->joinWith('operaHotelBaseRoom c')
  208. ->where(['a.cancel_flag' => 0, 'b.CANCEL_FLAG' => 0, 'a.IS_ONSALE' => 1, 'b.HOTEL_STATUS' => 1])
  209. ->groupBy(['a.HOTEL_ID', 'a.PARENT_ROOM_TYPE', 'a.ROOM_TYPE'])
  210. ->from('opera_hotel_room a');
  211. $dataProvider = new ActiveDataProvider([
  212. 'query' => $query,
  213. 'pagination' => [
  214. 'pageSize' => 20,
  215. ],
  216. ]);
  217. if (!empty(trim($param['HOTEL_NAME']))) {
  218. $query->andFilterWhere(['like', 'b.HOTEL_NAME', trim($param['HOTEL_NAME'])]);
  219. }
  220. if (!empty(trim($param['INNER_IDENTIFY']))) {
  221. $query->andFilterWhere(['like', 'a.INNER_IDENTIFY', trim($param['INNER_IDENTIFY'])]);
  222. }
  223. if (!empty(trim($param['ROOM_NAME']))) {
  224. $query->andFilterWhere(['like', 'a.ROOM_NAME', trim($param['ROOM_NAME'])]);
  225. }
  226. return $dataProvider;
  227. }
  228. /**
  229. * Des:獲取房型數組
  230. * Name: getSaleRoomList
  231. * @param $baseRoomId
  232. * @return array
  233. * @author 倪宗锋
  234. */
  235. public function getSaleRoomList($baseRoomId, $channel_id)
  236. {
  237. $select = [
  238. 'a.id',
  239. 'a.room_name',
  240. 'b.channel_mapping_id'
  241. ];
  242. $where = [
  243. 'and',
  244. ['=', 'a.PARENT_ROOM_TYPE', $baseRoomId],
  245. ['=', 'a.CANCEL_FLAG', 0]
  246. ];
  247. $getList = self::find()->select($select)
  248. ->from(self::tableName() . ' a')
  249. ->leftJoin(OperaRoomDistrib::tableName() . ' b', 'a.ID = b.ROOM_ID and b.DISTRIB_ID=' . $channel_id)
  250. ->where($where)
  251. ->groupBy('a.ID')
  252. ->asArray()
  253. ->all();
  254. if (!is_array($getList)) {
  255. return [];
  256. }
  257. return array_values($getList);
  258. }
  259. /**
  260. * Des:獲取房型數組 对象
  261. * Name: getSaleRoomListForShow
  262. * @param $baseRoomId
  263. * @return ActiveDataProvider
  264. * @author 倪宗锋
  265. */
  266. public function getSaleRoomListForShow($baseRoomId, $channel_id)
  267. {
  268. $where = [
  269. 'and',
  270. ['=', 'a.PARENT_ROOM_TYPE', $baseRoomId],
  271. ['=', 'a.CANCEL_FLAG', 0]
  272. ];
  273. $query = self::find()
  274. ->from(self::tableName() . ' a')
  275. ->joinWith(['operaRoomDistrib' => function ($query) use ($channel_id) {
  276. $query->onCondition(['opera_room_distrib.DISTRIB_ID' => $channel_id]);
  277. }])
  278. ->where($where);
  279. $dataProvider = new ActiveDataProvider([
  280. 'query' => $query,
  281. 'sort' => false,
  282. 'pagination' => [
  283. 'pagesize' => 100,
  284. ],
  285. ]);
  286. return $dataProvider;
  287. }
  288. /**
  289. * Des:获取房型关联信息
  290. * Name: getRoomInfoForSetSaleRoom
  291. * @param $params
  292. * @return array
  293. * @author 倪宗锋
  294. */
  295. public function getRoomInfoForSetSaleRoom($params)
  296. {
  297. $where = [
  298. 'and',
  299. ['=', 'a.ID', $params['cs_sale_room_id']],
  300. ['=', 'a.CANCEL_FLAG', 0]
  301. ];
  302. $select = [
  303. 'cs_sale_room_id' => 'a.ID',
  304. 'room_name' => 'a.ROOM_NAME',
  305. 'base_room_id' => 'a.PARENT_ROOM_TYPE',
  306. 'sale_room_id' => 'b.CHANNEL_MAPPING_ID',
  307. 'distrib_id' => 'b.ID',
  308. 'base_room_name' => 'c.BASE_ROOM_NAME',
  309. 'd.sub_base_room_id'
  310. ];
  311. $info = self::find()->select($select)
  312. ->from(self::tableName() . ' a')
  313. ->leftJoin(OperaRoomDistrib::tableName() . ' b', " a.ID = b.ROOM_ID and b.DISTRIB_ID = {$params['channel_id']} and b.CANCEL_FLAG = 0")
  314. ->innerJoin(OperaHotelBaseRoom::tableName() . ' c', 'a.PARENT_ROOM_TYPE = c.MAIN_ID and c.CANCEL_FLAG = 0 ')
  315. ->leftJoin(ChannelBaseRoomMapping::tableName().' d' ,' c.MAIN_ID = d.base_room_id and d.cancel_flag = 0')
  316. ->where($where)
  317. ->asArray()
  318. ->one();
  319. return $info;
  320. }
  321. /**
  322. * Des:获取新增售卖房型 需要使用的参数
  323. * Name: getDataForAddSaleRoom
  324. * @param $params
  325. * @return array
  326. * @author 倪宗锋
  327. */
  328. public function getDataForAddSaleRoom($params)
  329. {
  330. $select = [
  331. 'b.master_hotel_id',
  332. 'b.sub_hotel_id',
  333. 'b.master_base_room_id',
  334. 'b.sub_base_room_id',
  335. 'a.BREAKFAST_INCLUDE',
  336. 'a.OCCUPANCY_LIMIT',
  337. 'BED_TYPE' => new Expression('if(a.BED_TYPE=0,c.BED_TYPE,a.BED_TYPE)')
  338. ];
  339. $where = [
  340. 'and',
  341. ['=', 'a.ID', $params['cs_sale_room_id']],
  342. ['=', 'a.CANCEL_FLAG', 0],
  343. ['=', 'b.CANCEL_FLAG', 0]
  344. ];
  345. $getInfo = self::find()->select($select)
  346. ->from(self::tableName() . ' a')
  347. ->innerJoin(ChannelBaseRoomMapping::tableName() . ' b', 'a.PARENT_ROOM_TYPE = b.base_room_id')
  348. ->innerJoin(OperaHotelBaseRoom::tableName() . ' c', 'a.PARENT_ROOM_TYPE = c.MAIN_ID')
  349. ->where($where)
  350. ->asArray()
  351. ->one();
  352. return $getInfo;
  353. }
  354. /***
  355. * Des:获取基础房型下面已经关联的售卖房型的数量
  356. * Name: getChannelByBaseRoom
  357. * @param $param
  358. * @param $channel_arr
  359. * @return int
  360. * @author 倪宗锋
  361. */
  362. public function getChannelByBaseRoom($param, $channel_arr)
  363. {
  364. $select = [
  365. 'cnt' => new Expression("COUNT(CHANNEL_MAPPING_ID)")
  366. ];
  367. $where = [
  368. 'and',
  369. ['=', 'a.PARENT_ROOM_TYPE', $param['base_room_id']],
  370. ['!=', 'b.CHANNEL_MAPPING_ID', 0],
  371. ['=', 'a.CANCEL_FLAG', 0],
  372. ['=', 'a.CANCEL_FLAG', 0],
  373. ['in', 'b.DISTRIB_ID', $channel_arr],
  374. ];
  375. $getCnt = self::find()->select($select)
  376. ->from(self::tableName() . ' a')
  377. ->innerJoin(OperaRoomDistrib::tableName() . ' b', 'a.ID = b.ROOM_ID')
  378. ->where($where)
  379. ->asArray()
  380. ->one();
  381. if (empty($getCnt['cnt'])) {
  382. return 0;
  383. }
  384. return $getCnt['cnt'];
  385. }
  386. }