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.
 
 
 
 
 
 

219 lines
5.9 KiB

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