PrizeAddController.class.php
5.04 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
<?php
/**
* Created by IntelliJ IDEA.
* 奖品添加
* User: zhoutao
* Date: 2016-12-06
* Time: 16:48:56
*/
namespace Apicp\Controller\Mall;
use Common\Common\AttachOperation;
use Common\Model\PrizeModel;
use Common\Service\PrizeService;
class PrizeAddController extends AbstractController
{
public function before_action($action = '')
{
// 开启自动获取
$this->autoGetData = true;
// 获取的数据规则
$this->field = [
'ia_id' => [
'require' => false,
'verify' => 'intval',
'cn' => '要编辑的奖品ID',
],
'name' => [
'require' => true,
'verify' => 'strval',
'cn' => '名称',
'maxLength' => PrizeModel::MAX_NAME_COUNT
],
'sequence' => [
'require' => true,
'verify' => 'intval',
'cn' => '序号',
],
'on_sale' => [
'require' => false,
'verify' => 'intval',
'cn' => '状态',
],
'reserve' => [
'require' => true,
'verify' => 'intval',
'cn' => '库存',
],
'integral' => [
'require' => true,
'verify' => 'intval',
'cn' => '所需积分',
'maxLength' => PrizeModel::MAX_INTEGRAL_LEN
],
'times' => [
'require' => false,
'verify' => 'intval',
'cn' => '每人限兑次数',
],
'picture' => [
'require' => true,
'cn' => '图片',
'maxLength' => PrizeModel::MAX_PICTURE_NUMBER
],
'desc' => [
'require' => true,
'verify' => 'strval',
'cn' => '介绍',
],
'range_mem' => [
'require' => false,
'cn' => '人员范围',
],
'range_dep' => [
'require' => false,
'cn' => '部门范围',
],
];
return parent::before_action($action);
}
public function Index()
{
// 处理
$this->dealData();
if (!empty($this->data['ia_id'])) {
// 更新
$prizeServ = new PrizeService();
$iaId = $this->data['ia_id'];
unset($this->data['ia_id']);
$prizeServ->update($iaId, $this->data);
// 附件操作
$this->attach_operation($iaId, $this->data, 'edit');
} else {
// 排序兼容
$this->data['updated'] = MILLI_TIME;
// 入库
$prizeServ = new PrizeService();
$iaId = $prizeServ->insert($this->data);
// 附件操作
$this->attach_operation($iaId, $this->data);
}
return true;
}
/**
* 处理数据
* @return bool
*/
protected function dealData()
{
$this->data['picture'] = implode(',', $this->data['picture']);
$this->data['range_mem'] = implode(',', $this->data['range_mem']);
$this->data['range_dep'] = implode(',', $this->data['range_dep']);
// 如果没有设置范围 则全公司
if (empty($this->data['range_mem']) && empty($this->data['range_dep'])) {
$this->data['range_mem'] = '';
$this->data['range_dep'] = '';
$this->data['is_all'] = self::MEAN_TRUE;
} else {
$this->data['is_all'] = self::MEAN_FALSE;
}
return true;
}
/**
* 附件操作
*
* @param int $prize_id 奖品id
* @param array $data 奖品数据
* @param string $type 操作类型(add:添加,edit:编辑)
*
* @return bool
*/
private function attach_operation($prize_id, $data, $type = 'add')
{
/**
* 附件操作处理 start
*/
$attach_serv = new AttachOperation();
$attach_ids = [];
if (!empty($data['picture'])) {
// 奖品图片
$attach_ids = explode(',', $data['picture']);
}
if (!empty($data['desc'])) {
// 内容中的附件
$content_at_ids = $attach_serv->match_at_ids($data['desc']);
$attach_ids = array_merge($attach_ids, $content_at_ids);
}
if (!empty($attach_ids)) {
// 添加
if ($type == 'add') {
$attach_serv->insert_attach(
APP_DIR,
'prize',
$prize_id,
['attach_ids' => $attach_ids]
);
}
// 编辑
if ($type == 'edit') {
$attach_serv->update_attach(
APP_DIR,
'prize',
$prize_id,
['attach_ids' => $attach_ids]
);
}
}
return true;
}
}