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.
 
 
 
 
 
 

214 lines
6.1 KiB

  1. <?php
  2. namespace backend\modules\hotel\models;
  3. use yii\db\ActiveRecord;
  4. use yii\db\Exception;
  5. use yii\helpers\ArrayHelper;
  6. /**
  7. * This is the model class for table "channel_hotel_relation".
  8. *
  9. * @property string $ChannelHotelId
  10. * @property integer $HotelId
  11. * @property integer $ChannelId
  12. */
  13. class HotelRelation extends ActiveRecord
  14. {
  15. /**
  16. * @inheritdoc
  17. */
  18. public static function tableName()
  19. {
  20. return 'channel_hotel_relation';
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['ChannelHotelId', 'HotelId', 'ChannelId'], 'required'],
  29. [['HotelId', 'ChannelId'], 'integer'],
  30. [['ChannelHotelId'], 'string', 'max' => 20],
  31. [['HotelId', 'ChannelId'], 'unique', 'targetAttribute' => ['HotelId', 'ChannelId'], 'message' => 'The combination of 本地酒店ID and 渠道ID 1:携程 has already been taken.'],
  32. [['ChannelHotelId', 'ChannelId'], 'unique', 'targetAttribute' => ['ChannelHotelId', 'ChannelId'], 'message' => 'The combination of 渠道酒店ID and 渠道ID 1:携程 has already been taken.'],
  33. ];
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'ChannelHotelId' => '渠道酒店ID',
  42. 'HotelId' => '本地酒店ID',
  43. 'ChannelId' => '渠道ID 1:携程',
  44. ];
  45. }
  46. /**
  47. * User: wangxj
  48. *
  49. * 获取蜘蛛酒店id
  50. *
  51. * @param $id
  52. * @param $c_type
  53. *
  54. * @return bool|integer
  55. */
  56. public static function getSpiderId($id, $c_type)
  57. {
  58. $mapping = HotelRelation::findOne(['ChannelHotelId' => $id, 'ChannelId' => $c_type]);
  59. if ($mapping == null) {
  60. return 0;
  61. } else {
  62. return $mapping->HotelId;
  63. }
  64. }
  65. /**
  66. * User: wangxj
  67. *
  68. * 获取渠道酒店id
  69. *
  70. * @param $id
  71. * @param $c_type
  72. *
  73. * @return bool|integer
  74. */
  75. public static function getChannelId($id, $c_type)
  76. {
  77. $mapping = HotelRelation::findOne(['HotelId' => $id, 'ChannelId' => $c_type]);
  78. if ($mapping == null) {
  79. return 0;
  80. } else {
  81. return $mapping->ChannelHotelId;
  82. }
  83. }
  84. /**
  85. * Des:检测mapping是否已经存在
  86. * Name: checkIsCanMapping
  87. * @param $params array channelHotelId 渠道酒店ID channelId 渠道ID hotelId 酒店ID
  88. * @return int
  89. * @author 倪宗锋
  90. */
  91. public function checkIsCanMapping($params)
  92. {
  93. $select = ['count(1) as cnt'];//查询自动
  94. $where = [//过滤条件
  95. 'and',
  96. ['=', 'ChannelId', $params['channelId']],
  97. [
  98. 'or',
  99. ['=', 'ChannelHotelId', $params['channelHotelId']],
  100. ['=', 'HotelId', $params['hotelId']]
  101. ]
  102. ];
  103. $result = self::find()->select($select)
  104. ->where($where)
  105. ->asArray()
  106. ->one();
  107. return $result['cnt'];
  108. }
  109. /**
  110. * Des:绑定酒店 已存在唯一性索引 可以直接插入,不会造成数据冲突
  111. * Name: mapping
  112. * @param $params
  113. * @return bool
  114. * @throws \Exception
  115. * @author 倪宗锋
  116. */
  117. public function mapping($params)
  118. {
  119. $data = [
  120. 'ChannelHotelId' => $params['channelHotelId'],
  121. 'HotelId' => $params['hotelId'],
  122. 'ChannelId' => $params['channelId'],
  123. ];
  124. $this->setAttributes($data);
  125. $return = $this->insert(true);
  126. return $return;
  127. }
  128. /**
  129. * Des:
  130. * Name: getInfo
  131. * @param $params
  132. * @return mixed
  133. * @author 倪宗锋
  134. */
  135. public function checkRoomCanMapping($params)
  136. {
  137. $select = ['b.RoomId'];//查询自动
  138. $where = [//过滤条件
  139. 'and',
  140. ['=', 'a.ChannelId', $params['channelId']],
  141. ['=', 'b.ChannelId', $params['channelId']],
  142. ['=', 'c.ID', $params['roomId']],
  143. ['=', 'b.RoomId', $params['channelRoomId']]
  144. ];
  145. $result = self::find()->select($select)
  146. ->from(static::tableName() . ' as a')
  147. ->innerJoin(ChannelRoom::tableName() . ' as b', 'b.ChannelHotelID = a.ChannelHotelId')
  148. ->innerJoin(OperaHotelRoom::tableName() . ' as c', 'a.HotelId=c.HOTEL_ID')
  149. ->where($where)
  150. ->asArray()
  151. ->one();
  152. if (empty($result['RoomId']) || $result['RoomId'] != $params['channelRoomId']) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**
  158. * Des:解除关联
  159. * Name: unMapping
  160. * @param $params
  161. * @return int
  162. * @throws \yii\db\Exception
  163. * @author 倪宗锋
  164. */
  165. public function unMapping($params)
  166. {
  167. //清除当前酒店的房型数据
  168. $deleteWhere = [
  169. 'and',
  170. ['=', 'ChannelHotelID', $params['channelHotelId']],
  171. ['=', 'ChannelID', $params['channelId']]
  172. ];
  173. $flag = \Yii::$app->db->createCommand()->delete(static::tableName(), $deleteWhere)->execute();
  174. return $flag;
  175. }
  176. /**
  177. * 清空数据库中该酒店下所有房型数据
  178. *
  179. * 根本就没有用到对象的概念,基本全是空对象(初始化一个对象,和数据库没有关系),所以任何操作都要把数据传过来
  180. * 删除 ChannelRoom 和 RoomRelation 表数据
  181. *
  182. * @param $params array
  183. * @return bool
  184. * @author wangxj
  185. */
  186. public function clearRoom($params){
  187. //先查找出该酒店下所有房型
  188. $rooms = ChannelRoom::find()->where(['ChannelID'=> $params['channelId'], 'ChannelHotelID'=> $params['channelHotelId'], ])->all();
  189. $tran = \Yii::$app->db->beginTransaction();
  190. try{
  191. RoomRelation::deleteAll(['ChannelID'=> $params['channelId'], 'ChannelRoomId'=> ArrayHelper::map($rooms, 'RoomId', 'RoomId' )]);
  192. ChannelRoom::deleteAll(['ChannelID'=> $params['channelId'], 'ChannelHotelID'=> $params['channelHotelId']]);
  193. $tran->commit();
  194. return true;
  195. }catch (Exception $e){
  196. $tran->rollBack();
  197. return false;
  198. }
  199. }
  200. }