Video.php
5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace app\models;
use Yii;
use common\config\Params;
/**
* 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 [
[['task_id', 'live_start_time', 'live_end_time', 'master_id', 'room_id', 'status', 'like_num', 'audience_num', 'replay_num', 'sort', 'create_time', 'update_time'], 'integer'],
[['title', 'live_title'], 'string', 'max' => 100],
[['pic', 'url'], 'string', 'max' => 255],
[['app'], 'string', 'max' => 50],
];
}
/**
* @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',
'like_num' => 'Like Num',
'audience_num' => 'Audience Num',
'replay_num' => 'Replay Num',
'sort' => 'Sort',
'create_time' => 'Create Time',
'update_time' => 'Update Time',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
public function afterSave($insert, $changedAttributes)
{
if (!empty($changedAttributes)){
$this->room->touch('update_time');
}
parent::afterSave($insert, $changedAttributes);
}
/**
* 关联表-主播
* @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']);
}
/**
* 编辑
* @param array $data
* @author yaoxiaofeng
* @return bool
*/
public function edit(array $data)
{
$transaction = $this->getDb()->beginTransaction();
try {
$this->setAttributes([
'pic' => $data['pic'],
'url' => $data['url'],
'title' => $data['title'],
'sort' => $data['sort']
]);
if (!$this->save()){
throw new \Exception(current($this->getFirstErrors()));
}
//编辑分享内容
if (!$share = Share::findOne(['type' => Params::VIDEO_SHARE_TYPE, 'obj_id' => $this->id])) {
//编辑以前的视频时,没有相应的分享记录,那么这时候要重新创建一个对象来做插入
$share = new Share();
}
$share->setAttributes([
'obj_id' => $this->id,
'type' => Params::VIDEO_SHARE_TYPE,
'pic' => $data['share_pic'],
'title' => $data['share_title'],
'content' => $data['share_content'],
]);
if (!$share->save()) {
throw new \Exception(current($share->getFirstErrors()));
}
$transaction->commit();
return true;
} catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('warning', $e->getMessage());
return false;
}
}
/**
* 根据条件
* @param array $condition
*/
public static function condition(array $condition){
$model = parent::find()->alias('v');
foreach ($condition as $field=> $value) {
if ($field == 'title') {
if (!empty($value)){
$model->joinWith('master m')->andWhere(['or',['like','v.title',$value],['like','v.live_title', $value], ['like','m.name',$value]]);
}
}
else if($field == 'backState') {
switch ($value) {
case 1://已上传回看视频
$sql = "v.url <>''";
break;
case 0://未上传回看视频
$sql = "v.url ='' OR v.url IS NULL";
break;
case -1:
default:
$sql = "1";
break;
}
$model->andWhere($sql);
}
}
$model->orderBy(['v.sort'=>SORT_DESC,'v.create_time' => SORT_DESC]);
return $model;
}
}