You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

492 lines
16 KiB

  1. <?php
  2. /**
  3. * API接口类 处理逻辑
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm ApiService.php
  13. * Create By 2018/1/4 16:06 $
  14. */
  15. namespace backend\modules\hotel\models;
  16. use common\models\BaseResource;
  17. use common\models\BaseSupplierSale;
  18. class ApiService
  19. {
  20. /**
  21. * Des:更据日期及酒店ID获取子房型数据
  22. * Name: getHotelRoomListByDate
  23. * @param $params
  24. * @return array
  25. * @author 倪宗锋
  26. */
  27. public function getHotelRoomListByDate($params)
  28. {
  29. $runHotelDistrib = new RunHotelDistrib();
  30. $roomList = $runHotelDistrib->getAllRoomListByHotelDate($params);
  31. if (!is_array($roomList) || count($roomList) == 0) {
  32. return ['code' => 2, 'info' => '未获取到符合条件的数据'];
  33. }
  34. $day_cnt = round((strtotime($params['end_date']) - strtotime($params['start_date'])) / 3600 / 24);//入住天数
  35. $base_room_list = [];
  36. foreach ($roomList as $val) {
  37. //1、判断 是否可以预定
  38. $val['can_reserve'] = 1;
  39. if ($val['sub_room_sale_days'] != $day_cnt) {//子房型上架天数
  40. $val['can_reserve'] = 0;
  41. }
  42. if ($val['room_sale_days'] != $day_cnt) {//房型上架天数
  43. $val['can_reserve'] = 0;
  44. }
  45. if ($val['hotel_sale_days'] != $day_cnt) {//酒店上架天数
  46. $val['can_reserve'] = 0;
  47. }
  48. if ($val['room_open_days'] != $day_cnt) {//房型的房态开房天数
  49. $val['can_reserve'] = 0;
  50. }
  51. if ($val['org_room_open_days'] != $day_cnt) {//渠道房型房态 开房天数
  52. $val['can_reserve'] = 0;
  53. }
  54. if ($val['stock'] == 0) {//库存
  55. $val['can_reserve'] = 0;
  56. }
  57. $val['stock'] = intval($val['stock']);
  58. if ($val['LASTEST_BOOK_TIME'] != '-1') {//最后预定时间
  59. $date = explode(',', $val['LASTEST_BOOK_TIME']);
  60. $lastBookTime = date('Y-m-d', strtotime("+{$date['0']} d", strtotime($params['start_date']))) . ' ' . $date['1'];
  61. if (strtotime($lastBookTime) <= time()) {
  62. $val['can_reserve'] = 0;
  63. }
  64. }
  65. unset($val['LASTEST_BOOK_TIME']);
  66. //2、是否可以取消
  67. $val['can_cancel'] = 0;
  68. if ($val['LASTEST_CANCEL_DAY'] != '-1') {//最后取消时间
  69. $date = explode(',', $val['LASTEST_CANCEL_DAY']);
  70. $lastBookTime = date('Y-m-d', strtotime("+{$date['0']} d", strtotime($params['start_date']))) . ' ' . $date['1'];
  71. if (strtotime($lastBookTime) > time()) {
  72. $val['can_cancel'] = 1;
  73. }
  74. }
  75. unset($val['LASTEST_CANCEL_DAY']);
  76. //3、含早的名称
  77. $val['breakfast'] = $this->breakfast($val['BREAKFAST_INCLUDE']);
  78. unset($val['BREAKFAST_INCLUDE']);
  79. //4、设置房型礼包数据
  80. $val['gift_list'] = [];
  81. if (empty($val['gift']) == false) {
  82. $giftArr = explode('||||', $val['gift']);
  83. foreach ($giftArr as $giftVal) {
  84. $gifts = explode('|||', $giftVal);
  85. $gitDateArr = [];
  86. $gitDateArr['id'] = $gifts[0];
  87. $gitDateArr['name'] = $gifts[1];
  88. $gitDateArr['date'] = $gifts[2];
  89. $val['gift_list'][] = $gitDateArr;
  90. }
  91. uasort($val['gift_list'], 'static::sort_by_date');
  92. $val['gift_list'] = array_values($val['gift_list']);
  93. }
  94. $val['min_price'] = floatval($val['min_price']);
  95. $val['avg_price'] = floatval($val['avg_price']);
  96. $val['total_money'] = floatval($val['total_money']);
  97. unset($val['gift']);
  98. $val['date_price'] = $this->set_date_price($val['date_price']);
  99. //5、设置基础房型的数据
  100. if (empty($base_room_list[$val['base_room_type']])) {
  101. $bed_name = $this->getBedTypeName($val['BED_TYPE']);//获取床型名称
  102. $base_room_list[$val['base_room_type']]['base_room_name'] = $val['base_room_name'];//房型名称
  103. $base_room_list[$val['base_room_type']]['bed_name'] = $bed_name;//床型名称
  104. $base_room_list[$val['base_room_type']]['area_size'] = $val['area_size'];
  105. $base_room_list[$val['base_room_type']]['min_price'] = $val['min_price'];
  106. $base_room_list[$val['base_room_type']]['room_img'] = $val['room_img'];
  107. } else {
  108. if ($base_room_list[$val['base_room_type']]['min_price'] > $val['min_price']) {//价格取最小价
  109. $base_room_list[$val['base_room_type']]['min_price'] = $val['min_price'];
  110. }
  111. }
  112. unset($val['BED_TYPE']);
  113. $base_room_list[$val['base_room_type']]['room_list'][] = $val;
  114. }
  115. $base_room_list = array_values($base_room_list);
  116. uasort($base_room_list, 'static::sort_by_min_price');
  117. $base_room_list = array_values($base_room_list);
  118. return ['code' => 0, 'info' => 'success', 'list' => $base_room_list];
  119. }
  120. /**
  121. * Des:更据字段名称排序
  122. * Name: sort_by_name
  123. * @param $x
  124. * @param $y
  125. * @return int
  126. * @author 倪宗锋
  127. */
  128. static function sort_by_min_price($x, $y)
  129. {
  130. if (floatval($x['min_price']) > floatval($y['min_price'])) {
  131. return 1;
  132. } else {
  133. return -1;
  134. }
  135. }
  136. /**
  137. * Des:获取酒店展示信息
  138. * Name: getHotelInfo
  139. * @param $params
  140. * @return array
  141. * @author 倪宗锋
  142. */
  143. public function getHotelInfo($params)
  144. {
  145. $runHotel = new OperaHotel();
  146. $getInfo = $runHotel->getInfo($params['hotel_id']);
  147. //设置图片
  148. $getInfo['img_list'] = [];
  149. if (empty($getInfo['imgs']) == false) {
  150. $img_arr = explode('||', $getInfo['imgs']);
  151. foreach ($img_arr as $value) {
  152. if (empty($value) == '') {
  153. $getInfo['img_list'][] = $value;
  154. }
  155. }
  156. }
  157. $getInfo['img_cnt'] = count($getInfo['img_list']);
  158. unset($getInfo['imgs']);
  159. $return = ['code' => 0, 'info' => 'success', 'data' => $getInfo];
  160. return $return;
  161. }
  162. /**
  163. * Des:设置日期价格
  164. * Name: set_date_price
  165. * @param $date_price
  166. * @return array
  167. * @author 倪宗锋
  168. */
  169. private function set_date_price($date_price)
  170. {
  171. if (empty($date_price)) {
  172. return [];
  173. }
  174. $date_arr = explode("||", $date_price);
  175. $return = [];
  176. foreach ($date_arr as $val) {
  177. $price_s = explode('|', $val);
  178. if (!empty($price_s[0]) && !empty($price_s['1'])) {
  179. $return[] = [
  180. 'date' => $price_s[0],
  181. 'price' => $price_s[1]
  182. ];
  183. }
  184. }
  185. uasort($return, 'static::sort_by_date');
  186. $return = array_values($return);
  187. return $return;
  188. }
  189. /**
  190. * Des:更据字段名称排序
  191. * Name: sort_by_name
  192. * @param $x
  193. * @param $y
  194. * @return int
  195. * @author 倪宗锋
  196. */
  197. static function sort_by_date($x, $y)
  198. {
  199. return strcasecmp($x['date'], $y['date']);
  200. }
  201. /**
  202. * Des:更据床型ID获取名称
  203. * Name: getBedTypeName
  204. * @param $bedType string 床型(1:大床 2:双床 10:多床)组合的情况下用逗号隔开
  205. * @return string
  206. * @author 倪宗锋
  207. */
  208. public function getBedTypeName($bedType)
  209. {
  210. if (empty($bedType)) {
  211. return '';
  212. }
  213. $bedTypeArr = explode(',', $bedType);
  214. $bed_name = [];
  215. foreach ($bedTypeArr as $value) {
  216. if ($value == 1) {
  217. $bed_name[] = '大床';
  218. } elseif ($value == 2) {
  219. $bed_name[] = '双床';
  220. } elseif ($value == 10) {
  221. $bed_name[] = '多床';
  222. }
  223. }
  224. return implode('|', $bed_name);
  225. }
  226. /**
  227. * Des:获取含早名称
  228. * Name: breakfast
  229. * @param $breakfast
  230. * @return string
  231. * @author 倪宗锋
  232. */
  233. public function breakfast($breakfast)
  234. {
  235. switch ($breakfast) {
  236. case 0:
  237. $name = '无早';
  238. break;
  239. case 1:
  240. $name = '单早';
  241. break;
  242. case 2:
  243. $name = '双早';
  244. break;
  245. case 3:
  246. $name = '三早';
  247. break;
  248. case 4:
  249. $name = '四早';
  250. break;
  251. case 5:
  252. $name = '五早';
  253. break;
  254. case 6:
  255. $name = '六早';
  256. break;
  257. case 7:
  258. $name = '七早';
  259. break;
  260. case 8:
  261. $name = '八早';
  262. break;
  263. case 9:
  264. $name = '九早';
  265. break;
  266. case 10:
  267. $name = '十早';
  268. break;
  269. default:
  270. $name = '多早';
  271. }
  272. return $name;
  273. }
  274. /**
  275. * Function Description:微信端获取酒店列表缓存
  276. * Function Name: getHotelListCache
  277. *
  278. * @return array
  279. *
  280. * @author 娄梦宁
  281. */
  282. public function getHotelListCache()
  283. {
  284. #1:获取微信商城酒店销售方式
  285. $base_supplier_sale = BaseSupplierSale::getSaleType(\Yii::$app->params['wx_supplier_id']);
  286. if ($base_supplier_sale == '') {
  287. return [
  288. 'code' => 1,
  289. 'info' => '微信商城未被授权酒店销售!'
  290. ];
  291. }
  292. #2:获取未来三个月的子房型价格库存
  293. $start_date = date('Y-m-d');
  294. $end_date = date('Y-m-d', strtotime('+90 day'));
  295. $runHotelDistrib = new RunHotelDistrib();
  296. $room_type_list = $runHotelDistrib->getRoomTypeList($start_date, $end_date, $base_supplier_sale, \Yii::$app->params['wx_supplier_id']);
  297. #3:处理结果集
  298. if (empty($room_type_list)) {
  299. return [
  300. 'code' => 2,
  301. 'info' => '无可选择房型!',
  302. ];
  303. }
  304. $new_list = [];
  305. $time_stamp = time();
  306. foreach ($room_type_list as $val) {
  307. if (!isset($new_list[$val['hotel_id'] . '-' . $val['run_date']])) {
  308. #酒店基础信息录入
  309. $new_list[$val['hotel_id'] . '-' . $val['run_date']] = [
  310. 'hotel_id' => $val['hotel_id'],
  311. 'hotel_name' => $val['hotel_name'],
  312. 'star_level' => $val['star_level'],
  313. 'area_id' => $val['area_id'],
  314. 'area_name' => $val['area_name'],
  315. 'run_date' => $val['run_date'],
  316. 'is_promotion' => $val['is_promotion'],
  317. 'recommend_level' => $val['recommend_level'],
  318. 'sale_label' => $val['sale_label'],
  319. 'hotel_label' => $val['hotel_label'],
  320. 'hotel_image' => $val['hotel_image'],
  321. 'business_area' => $val['business_area'],
  322. 'brand' => $val['brand'],
  323. 'hotel_type' => $val['hotel_type'],
  324. 'time_stamp' => $time_stamp
  325. ];
  326. }
  327. #子房型早餐价格库存床型数据
  328. $new_list[$val['hotel_id'] . '-' . $val['run_date']]['room_info'][] = [
  329. 'room_type' => $val['room_type'],
  330. 'bed_type' => $val['bed_type'],
  331. 'breakfast_include' => $val['breakfast_include'],
  332. 'price' => $val['price'],
  333. 'remaining_count' => $val['stock'] == null ? 0 : $val['stock'],
  334. ];
  335. }
  336. return [
  337. 'code' => 0,
  338. 'info' => '酒店列表数据获取成功!',
  339. 'data' => array_values($new_list)
  340. ];
  341. }
  342. /**
  343. * Function Description:获取酒店列表筛选条件
  344. * Function Name: getHotelType
  345. *
  346. * @return array
  347. *
  348. * @author 李健
  349. */
  350. public function getHotelType()
  351. {
  352. $base_resource = new BaseResource();
  353. // 获取酒店类型711
  354. $hotel_type = [
  355. '0' => '不限',
  356. '1' => '五星',
  357. '2' => '四星',
  358. ];
  359. $hotel_type_info = $base_resource->getHotelType(711);
  360. foreach ($hotel_type_info as $v) {
  361. $hotel_type[] = $v['res_name'];
  362. }
  363. // 获取酒店品牌
  364. $hotel_brand = [
  365. '不限',
  366. ];
  367. $hotel_brand_info = $base_resource->getHotelType(712);
  368. foreach ($hotel_brand_info as $v) {
  369. $hotel_brand[] = $v['res_name'];
  370. }
  371. // 设施服务
  372. $hotel_installation = ['不限'];
  373. $hotel_installation_info = $base_resource->getHotelType(709);
  374. foreach ($hotel_installation_info as $v) {
  375. $hotel_installation[] = $v['res_name'];
  376. }
  377. // 酒店价格
  378. $hotel_price = [
  379. '0' => '不限',
  380. '1' => '¥200以下',
  381. '2' => '¥201-500',
  382. '3' => '¥501-800',
  383. '4' => '¥801以上',
  384. ];
  385. // 床型
  386. $hotel_bed = [
  387. '0' => '不限',
  388. '1' => '单床',
  389. '2' => '双床',
  390. '3' => '多床',
  391. ];
  392. // 早餐
  393. $hotel_breakfast = [
  394. '0' => '不限',
  395. '1' => '含早',
  396. '2' => '单早',
  397. '3' => '双早',
  398. ];
  399. $init = [
  400. [
  401. 'type_id' => 1,
  402. 'type_name' => '价格',
  403. 'list' => $this->initHotelType($hotel_price),
  404. 'selected' => true,
  405. 'allowMoreSelected' => false,
  406. ],
  407. [
  408. 'type_id' => 2,
  409. 'type_name' => '酒店类型',
  410. 'list' => $this->initHotelType($hotel_type),
  411. 'selected' => false,
  412. 'allowMoreSelected' => false,
  413. ],
  414. [
  415. 'type_id' => 3,
  416. 'type_name' => '品牌',
  417. 'list' => $this->initHotelType($hotel_brand),
  418. 'selected' => false,
  419. 'allowMoreSelected' => true,
  420. ],
  421. [
  422. 'type_id' => 4,
  423. 'type_name' => '酒店设施',
  424. 'list' => $this->initHotelType($hotel_installation),
  425. 'selected' => false,
  426. 'allowMoreSelected' => true,
  427. ],
  428. [
  429. 'type_id' => 5,
  430. 'type_name' => '床型',
  431. 'list' => $this->initHotelType($hotel_bed),
  432. 'selected' => false,
  433. 'allowMoreSelected' => false,
  434. ],
  435. [
  436. 'type_id' => 6,
  437. 'type_name' => '早餐',
  438. 'list' => $this->initHotelType($hotel_breakfast),
  439. 'selected' => false,
  440. 'allowMoreSelected' => false,
  441. ]
  442. ];
  443. return $init;
  444. }
  445. /**
  446. * Function Description:初始化酒店类型数据
  447. * Function Name: initHotelType
  448. * @param $arr
  449. *
  450. * @return array
  451. *
  452. * @author 李健
  453. */
  454. public function initHotelType($arr)
  455. {
  456. $arr2 = [];
  457. foreach ($arr as $v) {
  458. $flag = false;
  459. if ($v == '不限') {
  460. $flag = true;
  461. }
  462. $arr2[] = [
  463. 'name' => $v,
  464. 'selected' => $flag,
  465. ];
  466. }
  467. return $arr2;
  468. }
  469. }