ProductController.php
3.88 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
<?php
namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use yii\helpers\ArrayHelper;
/**
* Live controller
*/
class ProductController extends BaseController
{
public function init()
{
parent::init();
$this->main_id = 'live';
$this->sub_id = 'room';
$this->sub_title = '商品管理';
}
/**
* 房间商品列表
* @return type
*/
public function actionList()
{
if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = \app\models\RoomProduct::find()->where(['room_id'=>$room_id]);
$count = clone $model;
$pagination = new Pagination(['totalCount' =>$count->count()]);
$list = $model
->offset($pagination->offset)->limit($pagination->limit)
->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
->all();
return $this->render('list',[
'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
'pagination'=>$pagination,
'list'=>$list,
]);
}
/**
* 添加商品
* @return type
*/
public function actionAdd()
{
if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = new \app\models\RoomProduct;
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
$model->room_id = $room_id;
if ($model->load($post,'') && $model->save()){
Yii::$app->session->setFlash('success', '创建成功。');
return $this->redirect($this->_refer);
}else{
Yii::$app->session->setFlash('error', '创建失败。');
$model->setAttributes($post);
}
}
return $this->render('edit',[
'model'=>$model,
]);
}
/**
* 编辑商品
* @return type
*/
public function actionEdit()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$model = \app\models\RoomProduct::findOne($id)){
Yii::$app->session->setFlash('error', '找不到该商品');
return $this->redirect($this->_refer);
}
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
if ($model->load($post,'') && $model->save()){
Yii::$app->session->setFlash('success', '编辑成功。');
$model->refresh();
}else{
Yii::$app->session->setFlash('error', '编辑失败。');
}
}
return $this->render('edit',[
'model'=>$model,
]);
}
/**
* 商品操作
* @return type
*/
public function actionOperate()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
return $this->redirect($this->_refer);
}
if (!$model = \app\models\RoomProduct::findOne($id)){
Yii::$app->session->setFlash('error', '找不到该商品');
return $this->redirect($this->_refer);
}
switch ($type){
case 'del'://删除
$r = $model->delete();
break;
default:
break;
}
if (!empty($r)){
Yii::$app->session->setFlash('success', '操作成功');
}else{
Yii::$app->session->setFlash('error', '操作失败');
}
return $this->redirect($this->_refer);
}
}