Video.php 6.63 KB
<?php

namespace common\models;

use Yii;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\lib\QcloudApi\QcloudApi as Qapi;

/**
 * This is the model class for table "{{%video}}".
 *
 * @property string  $id
 * @property string  $title
 * @property string  $pic
 * @property string  $url
 * @property integer $app
 * @property integer $task_id
 * @property string  $live_title
 * @property string  $live_start_time
 * @property string  $live_end_time
 * @property string  $master_id
 * @property string  $room_id
 * @property integer $status
 * @property integer $create_time
 * @property string  $update_time
 */
class Video extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%video}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['app', 'task_id', 'live_start_time', 'live_end_time', 'master_id', 'room_id', 'status', 'create_time'], 'integer'],
            [['update_time'], 'safe'],
            [['title', 'live_title'], 'string', 'max' => 100],
            [['pic', 'url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'              => 'ID',
            'title'           => 'Title',
            'pic'             => 'Pic',
            'url'             => 'Url',
            'app'             => 'App',
            'task_id'         => 'Task ID',
            'live_title'      => 'Live Title',
            'live_start_time' => 'Live Start Time',
            'live_end_time'   => 'Live End Time',
            'master_id'       => 'Master ID',
            'room_id'         => 'Room ID',
            'status'          => 'Status',
            'create_time'     => 'Create Time',
            'update_time'     => 'Update Time',
        ];
    }
    
    public function behaviors() {
        return [
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'update_time',
            ]
        ];
    }

    /**
     * 关联表-主播
     * @return type
     */
    public function getMaster()
    {
        return $this->hasOne(Master::className(), ['master_id' => 'master_id']);
    }

    /**
     * 关联表-主播
     * @return type
     */
    public function getRoom()
    {
        return $this->hasOne(Room::className(), ['room_id' => 'room_id']);
    }

    /**
     * 关联表-计数
     * @return type
     */
    public function getRoomNums()
    {
        return $this->hasOne(RoomNums::className(), ['room_id' => 'room_id']);
    }

    public function setVideoStart($room_id)
    {
        $transaction = $this->getDb()->beginTransaction();
        try {
            $qchannel = (new \yii\db\Query())
                ->select('channel_id')
                ->from('tbl_room_qchannel')
                ->where(['room_id' => $room_id])
                ->one();
            $room = (new \yii\db\Query())
                ->select('app,title,master_id')
                ->from('tbl_room')
                ->where(['room_id' => $room_id])
                ->one();
            //$qchannel = $this->find()->select('id,channel_id,task_id')->where(['room_id'=>$room_id])->one();
            $time = time();
            $ret = QcloudApiClient::self()->CreateRecord($qchannel['channel_id'], date('Y-m-d H:i:s'), '');
            file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|start ".var_export($ret,true),FILE_APPEND);
            if ($ret['code'] == 0) {
                $this->setAttributes([
                    'app'             => $room['app'],
                    'task_id'         => $ret['task_id'],
                    'live_title'      => $room['title'],
                    'live_start_time' => $time,
                    'live_end_time'   => 0,
                    'master_id'       => $room['master_id'],
                    'room_id'         => $room_id,
                ]);
                //插入视频表
                if (!$this->insert()) {
                    throw new \Exception(current($this->getFirstErrors()));
                }
                //更新房间主表直播状态
                $result = Yii::$app->db->createCommand()
                    ->update('{{%room}}', ['living' => 1,'update_time'=>time()], ['room_id' => $room_id])
                    ->execute();
                if (!$result) {
                    throw new \Exception('tbl_room update error');
                }
                $transaction->commit();
                return true;
            } else {
                throw new \Exception(QcloudApiClient::self()->error());
            }
        } catch (\Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }

    public function setVideoStop($room_id)
    {
        $transaction = $this->getDb()->beginTransaction();
        try {
            $qchannel = (new \yii\db\Query())
                ->select('channel_id')
                ->from('tbl_room_qchannel')
                ->where(['room_id' => $room_id])
                ->one();
            $video = $this->find()
                ->where(['room_id' => $room_id, 'live_end_time' => 0])
                ->orderBy(['live_start_time' => SORT_DESC])
                ->one();
            if ($video) {
                $ret = QcloudApiClient::self()->StopRecord($qchannel['channel_id'], $video['task_id']);
                file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|stop ".var_export($ret,true),FILE_APPEND);
                if ($ret['code'] == 0) {
                    $this->setAttributes([
                        'live_end_time' => time(),
                    ]);
                    if (!$this->update()) {
                        throw new \Exception(current($this->getFirstErrors()));
                    }
                    //更新房间主表直播状态
                    $result = Yii::$app->db->createCommand()
                        ->update('{{%room}}', ['living' => 2,'update_time'=>time()], ['room_id' => $room_id])
                        ->execute();
                    if (!$result) {
                        throw new \Exception('tbl_room update error');
                    }
                    $transaction->commit();
                    return true;
                } else {
                    throw new \Exception(QcloudApiClient::self()->error());
                }
            }
            return false;
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage());
            return false;
        }
    }
}