|
- <?php
-
- namespace backend\modules\hotel\models;
-
- use yii\db\ActiveRecord;
- use yii\db\Expression;
-
- /**
- * This is the model class for table "channel_room_relation".
- *
- * @property string $ChannelRoomId
- * @property integer $RoomId
- * @property integer $ChannelId
- */
- class RoomRelation extends ActiveRecord
- {
- const MAPPING_SET_TYPE_DELETE_HOTEL_PRICE = -4;
- const MAPPING_SET_TYPE_DELETE_HOTEL = -3;
- const MAPPING_SET_TYPE_DELETE_ROOM_PRICE = -2;
- const MAPPING_SET_TYPE_DELETE_ROOM = -1;
- const MAPPING_SET_TYPE_CHANGE = 0;
- const MAPPING_SET_TYPE_NEW = 1;
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'channel_room_relation';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['ChannelRoomId', 'RoomId', 'ChannelId'], 'required'],
- [['RoomId', 'ChannelId'], 'integer'],
- [['ChannelRoomId'], 'string', 'max' => 20],
- [['ChannelRoomId', 'ChannelId'], 'unique', 'targetAttribute' => ['ChannelRoomId', 'ChannelId'], 'message' => 'The combination of 渠道房型ID and 渠道ID has already been taken.'],
- [['RoomId', 'ChannelId'], 'unique', 'targetAttribute' => ['RoomId', 'ChannelId'], 'message' => 'The combination of 本地房型ID and 渠道ID has already been taken.'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ChannelRoomId' => '渠道房型ID',
- 'RoomId' => '本地房型ID',
- 'ChannelId' => '渠道ID',
- ];
- }
-
- /**
- * User: wangxj
- *
- * 获取蜘蛛房型id
- *
- * @param $id
- * @param $c_type
- *
- * @return bool|integer
- */
- public static function getSpiderId($id, $c_type)
- {
- $mapping = RoomRelation::findOne(['ChannelRoomId' => $id, 'ChannelId' => $c_type]);
- if ($mapping == null) {
- return 0;
- } else {
- return $mapping->RoomId;
- }
- }
-
- /**
- * User: wangxj
- *
- * 获取渠道房型id
- *
- * @param $id
- * @param $c_type
- *
- * @return bool|integer
- */
- public static function getChannelId($id, $c_type)
- {
- $mapping = RoomRelation::findOne(['RoomId' => $id, 'ChannelId' => $c_type]);
- if ($mapping == null) {
- return 0;
- } else {
- return $mapping->ChannelRoomId;
- }
- }
-
- /**
- * Des:检测mapping是否已经存在
- * Name: checkIsCanMapping
- * @param $params array channelRoomId 渠道ID channelId 渠道ID roomId 房型ID
- * @return int
- * @author 倪宗锋
- */
- public function checkIsCanMapping($params)
- {
- $select = ['count(1) as cnt'];//查询自动
- $where = [//过滤条件
- 'and',
- ['=', 'ChannelId', $params['channelId']],
-
- // 'or',
- // ['=', 'ChannelRoomId', $params['channelRoomId']],
- ['=', 'RoomId', $params['roomId']]
-
- ];
- $result = self::find()->select($select)
- ->where($where)
- ->asArray()
- ->one();
- return $result['cnt'];
- }
-
- /**
- * Des:绑定酒店 已存在唯一性索引 可以直接插入,不会造成数据冲突
- * Name: mapping
- * @param $params
- * @return bool
- * @throws \Exception
- * @author 倪宗锋
- */
- public function mapping($params)
- {
- $data = [
- 'ChannelRoomId' => $params['channelRoomId'],
- 'ChannelId' => $params['channelId'],
- 'RoomId' => $params['roomId'],
- ];
- $this->setAttributes($data);
- return $this->save();
- }
-
- /**
- * Des:解除关联
- * Name: unMapping
- * @param $params
- * @return int
- * @throws \yii\db\Exception
- * @author 倪宗锋
- */
- public function unMapping($params)
- {
- //清除当前酒店的房型数据
- $deleteWhere = [
- 'and',
- ['=', 'ChannelRoomId', $params['channelRoomId']],
- ['=', 'ChannelId', $params['channelId']],
- ['=', 'RoomId', $params['roomId']]
- ];
- return \Yii::$app->db->createCommand()->delete(static::tableName(), $deleteWhere)->execute();
- }
-
- /**
- * Des:获取所有的mapping信息用于检测mapping
- * Name: getAllRelationForCheckMapping
- * @return array
- * @author 倪宗锋
- */
- public function getAllRelationForCheckMapping()
- {
- $select = [
- new Expression("c.HotelId as 'hotel_id'"),
- new Expression("a.RoomId as 'room_id'"),
- "a.ChannelRoomId",
- "b.ChannelHotelID",
- 'a.ChannelId'
- ];//查询自动
- $result = self::find()->select($select)
- ->from(static::tableName() . ' a')
- ->innerJoin(ChannelRoom::tableName() . ' b', 'a.ChannelRoomId = b.RoomId and a.ChannelId = b.ChannelID')
- ->innerJoin(HotelRelation::tableName() . ' c', 'b.ChannelHotelID=c.ChannelHotelId and b.ChannelID=c.ChannelId')
- ->asArray()
- ->all();
- return $result;
- }
-
- /**
- * Des:判断是否可以解除酒店
- * Name: checkUnMappingHotel
- * @param $params
- * @return bool
- * @author 倪宗锋
- */
- public function checkUnMappingHotel($params)
- {
- $select = [
- new Expression("count(1) cnt"),
- ];//查询自动
- $where = [
- 'and',
- ['=', 'b.ChannelHotelID', $params['channelHotelId']],
- ['=', 'b.ChannelId', $params['channelId']],
- ];
- $result = self::find()->select($select)
- ->from(static::tableName() . ' a')
- ->innerJoin(ChannelRoom::tableName() . ' b', 'a.ChannelRoomId = b.RoomId and a.ChannelId = b.ChannelID')
- ->where($where)
- ->asArray()
- ->one();
- $cnt = $result['cnt'];
- if ($cnt > 0) {
- return false;
- } else {
- return true;
- }
- }
-
-
- }
|