|
- <?php
-
- namespace backend\modules\hotel\models;
-
- use yii\db\ActiveRecord;
- use yii\db\Exception;
- use yii\helpers\ArrayHelper;
-
- /**
- * This is the model class for table "channel_hotel_relation".
- *
- * @property string $ChannelHotelId
- * @property integer $HotelId
- * @property integer $ChannelId
- */
- class HotelRelation extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'channel_hotel_relation';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['ChannelHotelId', 'HotelId', 'ChannelId'], 'required'],
- [['HotelId', 'ChannelId'], 'integer'],
- [['ChannelHotelId'], 'string', 'max' => 20],
- [['HotelId', 'ChannelId'], 'unique', 'targetAttribute' => ['HotelId', 'ChannelId'], 'message' => 'The combination of 本地酒店ID and 渠道ID 1:携程 has already been taken.'],
- [['ChannelHotelId', 'ChannelId'], 'unique', 'targetAttribute' => ['ChannelHotelId', 'ChannelId'], 'message' => 'The combination of 渠道酒店ID and 渠道ID 1:携程 has already been taken.'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ChannelHotelId' => '渠道酒店ID',
- 'HotelId' => '本地酒店ID',
- 'ChannelId' => '渠道ID 1:携程',
- ];
- }
-
- /**
- * User: wangxj
- *
- * 获取蜘蛛酒店id
- *
- * @param $id
- * @param $c_type
- *
- * @return bool|integer
- */
- public static function getSpiderId($id, $c_type)
- {
- $mapping = HotelRelation::findOne(['ChannelHotelId' => $id, 'ChannelId' => $c_type]);
- if ($mapping == null) {
- return 0;
- } else {
- return $mapping->HotelId;
- }
- }
-
- /**
- * User: wangxj
- *
- * 获取渠道酒店id
- *
- * @param $id
- * @param $c_type
- *
- * @return bool|integer
- */
- public static function getChannelId($id, $c_type)
- {
- $mapping = HotelRelation::findOne(['HotelId' => $id, 'ChannelId' => $c_type]);
- if ($mapping == null) {
- return 0;
- } else {
- return $mapping->ChannelHotelId;
- }
- }
-
- /**
- * Des:检测mapping是否已经存在
- * Name: checkIsCanMapping
- * @param $params array channelHotelId 渠道酒店ID channelId 渠道ID hotelId 酒店ID
- * @return int
- * @author 倪宗锋
- */
- public function checkIsCanMapping($params)
- {
- $select = ['count(1) as cnt'];//查询自动
- $where = [//过滤条件
- 'and',
- ['=', 'ChannelId', $params['channelId']],
- [
- 'or',
- ['=', 'ChannelHotelId', $params['channelHotelId']],
- ['=', 'HotelId', $params['hotelId']]
- ]
- ];
-
- $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 = [
- 'ChannelHotelId' => $params['channelHotelId'],
- 'HotelId' => $params['hotelId'],
- 'ChannelId' => $params['channelId'],
- ];
- $this->setAttributes($data);
- $return = $this->insert(true);
- return $return;
- }
-
- /**
- * Des:
- * Name: getInfo
- * @param $params
- * @return mixed
- * @author 倪宗锋
- */
- public function checkRoomCanMapping($params)
- {
- $select = ['b.RoomId'];//查询自动
- $where = [//过滤条件
- 'and',
- ['=', 'a.ChannelId', $params['channelId']],
- ['=', 'b.ChannelId', $params['channelId']],
- ['=', 'c.ID', $params['roomId']],
- ['=', 'b.RoomId', $params['channelRoomId']]
- ];
- $result = self::find()->select($select)
- ->from(static::tableName() . ' as a')
- ->innerJoin(ChannelRoom::tableName() . ' as b', 'b.ChannelHotelID = a.ChannelHotelId')
- ->innerJoin(OperaHotelRoom::tableName() . ' as c', 'a.HotelId=c.HOTEL_ID')
- ->where($where)
- ->asArray()
- ->one();
- if (empty($result['RoomId']) || $result['RoomId'] != $params['channelRoomId']) {
- return false;
- }
- return true;
- }
-
- /**
- * Des:解除关联
- * Name: unMapping
- * @param $params
- * @return int
- * @throws \yii\db\Exception
- * @author 倪宗锋
- */
- public function unMapping($params)
- {
- //清除当前酒店的房型数据
- $deleteWhere = [
- 'and',
- ['=', 'ChannelHotelID', $params['channelHotelId']],
- ['=', 'ChannelID', $params['channelId']]
- ];
- $flag = \Yii::$app->db->createCommand()->delete(static::tableName(), $deleteWhere)->execute();
- return $flag;
- }
-
- /**
- * 清空数据库中该酒店下所有房型数据
- *
- * 根本就没有用到对象的概念,基本全是空对象(初始化一个对象,和数据库没有关系),所以任何操作都要把数据传过来
- * 删除 ChannelRoom 和 RoomRelation 表数据
- *
- * @param $params array
- * @return bool
- * @author wangxj
- */
- public function clearRoom($params){
- //先查找出该酒店下所有房型
- $rooms = ChannelRoom::find()->where(['ChannelID'=> $params['channelId'], 'ChannelHotelID'=> $params['channelHotelId'], ])->all();
- $tran = \Yii::$app->db->beginTransaction();
- try{
- RoomRelation::deleteAll(['ChannelID'=> $params['channelId'], 'ChannelRoomId'=> ArrayHelper::map($rooms, 'RoomId', 'RoomId' )]);
- ChannelRoom::deleteAll(['ChannelID'=> $params['channelId'], 'ChannelHotelID'=> $params['channelHotelId']]);
- $tran->commit();
- return true;
- }catch (Exception $e){
- $tran->rollBack();
- return false;
- }
- }
- }
|