EditController.class.php
11 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* 【销售活动-后台】活动编辑保存接口
*
* User: WJY
* Date: 2017-11-02
*/
namespace Apicp\Controller\Activity;
use Common\Common\AttachOperation;
use Common\Common\Helper;
use Common\Common\User;
use Common\Model\ActivityModel;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\PacketrecordService;
use Common\Service\PacketService;
use Common\Service\RightService;
use Think\Exception;
class EditController extends \Apicp\Controller\AbstractController
{
/** @var ActivityService */
protected $activity_s;
/** @var PacketService */
protected $packet_s;
/** @var CommentService */
protected $comment_s;
/** @var PacketrecordService */
protected $packetrecord_s;
/** @var RightService */
protected $right_s;
/** @var User */
protected $user_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化活动信息表
$this->activity_s = new ActivityService();
// 实例化红包表
$this->packet_s = new PacketService();
// 实例化评论表
$this->comment_s = new CommentService();
// 实例化红包记录表
$this->packetrecord_s = new PacketrecordService();
// 实例化权限表
$this->right_s = new RightService();
// 实例化人员公共类
$this->user_s = new User();
return true;
}
public function Index_post()
{
$params = I('post.');
// 开始编辑保存活动信息
$ac_id = $this->update_activity($params);
$this->_result = [
'ac_id' => $ac_id,
];
return true;
}
/**
* 编辑活动(后端)
*
* @param array $reqData 请求数据
*
* @return bool
*/
public function update_activity($reqData)
{
$ac_id = intval($reqData['ac_id']);
// 活动ID不能为空
if (empty($ac_id)) {
E('_ERR_AC_ID_EMPTY');
}
// 活动不存在
if (!$old_activity = $this->activity_s->get($ac_id)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
// 获取用户提交的活动数据
$activity = $this->activity_s->fetch_activity($reqData);
// 验证数据
$this->activity_s->validate_for_add($activity);
// 已发布的不能编辑保存成草稿
if (ActivityModel::ACTIVITY_PUBLISH == $old_activity['activity_status'] &&
ActivityModel::ACTIVITY_DRAFT == $activity['activity_status']) {
E('_ERR_ACTIVITY_STATUS');
}
// 已发布的活动只能延长结束时间,不能减短
if (ActivityModel::ACTIVITY_PUBLISH == $old_activity['activity_status']) {
if ($old_activity['end_time'] > $activity['end_time'] || $activity['end_time'] < MILLI_TIME) {
E('_ERR_ACTIVITY_ENDTIME');
}
}
// 更新最后更新时间
$activity['last_time'] = MILLI_TIME;
// 红包是否开启放到外层
$activity['is_show_red'] = $activity['red_content']['is_show_red'];
// 用户提交的新的权限数据
$right = $activity['right'];
$right['is_all'] = $activity['is_all'];
unset($activity['right']);
// 获取已有权限
$old_right = $this->right_s->list_by_conds(['ac_id' => $ac_id]);
try {
$this->activity_s->start_trans();
// 组装活动入库数组
$activityList = $activity;
// 去除不需要的元素
unset($activityList['red_content']);
unset($activityList['right']);
// 这里是指草稿状态变为发布状态
// 草稿不生成红包记录数据,已发布的活动不能修改红包,已发布的不能保存为草稿
if (ActivityService::ACTIVITY_DRAFT != $activity['activity_status'] &&
ActivityModel::ACTIVITY_PUBLISH != $old_activity['activity_status']) {
// 草稿发布的时候,加入发布时间
if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {
$activityList['publish_time'] = MILLI_TIME;
}
// 是否开启红包
if (ActivityService::RED_OPENED == $activity['is_red_open']) {
// 是否为随机比例红包
if (ActivityService::RAND_PRO_RED == $activity['red_content']['red_type']) {
$packetdate = [
'ac_id' => $ac_id,
'type' => $activity['red_content']['red_type'],
'red_base_num' => $activity['red_content']['red_base_num'],
'max_total' => $activity['red_content']['red_get_num'],
'red_content' => serialize($activity['red_content']['rule_list']),
'warn_mobile' => $activity['red_content']['warn_mobile'],
];
// 生成红包
$pid = $this->packet_s->insert($packetdate);
// 生成随机红包
$this->packetrecord_s->create_packet_record($ac_id, $pid, $activity['red_content']['rule_list'],
$activity['red_content']['red_base_num']);
}
} else {
// 干掉红包
$this->packet_s->delete_by_conds(['ac_id' => $ac_id]);
// 若不开启红包,删除红包祝福语
$activityList['packet_bless'] = '';
}
// 草稿编辑为草稿
} elseif (ActivityModel::ACTIVITY_DRAFT == $activity['activity_status'] &&
ActivityModel::ACTIVITY_PUBLISH != $old_activity['activity_status']) {
// 是否开启红包
if (ActivityService::RED_OPENED == $activity['is_red_open']) {
// 是否为随机比例红包
if (ActivityService::RAND_PRO_RED == $activity['red_content']['red_type']) {
$packetdate = [
'ac_id' => $ac_id,
'type' => $activity['red_content']['red_type'],
'red_base_num' => $activity['red_content']['red_base_num'],
'max_total' => $activity['red_content']['red_get_num'],
'red_content' => serialize($activity['red_content']['rule_list']),
'warn_mobile' => $activity['red_content']['warn_mobile'],
];
// 修改红包信息
$this->packet_s->update_by_conds(['ac_id' => $ac_id], $packetdate);
}
} else {
// 干掉红包
$this->packet_s->delete_by_conds(['ac_id' => $ac_id]);
// 若不开启红包,删除红包祝福语
$activityList['packet_bless'] = '';
}
}
// 活动数据入库
$this->activity_s->update($ac_id, $activityList);
// 权限与参与人数入库
if (ActivityService::IS_ALL != $activity['is_all']) {
// 如果权限不是全公司
$this->right_s->save_data(['ac_id' => $ac_id], $right);
// 获取活动参与权限总数入库
$unjoin_num = $this->comment_s->count_unjoin(['ac_id' => $ac_id], []);
$this->activity_s->update($ac_id, ['join_total' => $unjoin_num['unjoin_num']]);
} else {
// 如果权限是全公司
$should_join_num = $this->user_s->listByConds();
$this->activity_s->update($ac_id, ['join_total' => $should_join_num['total']]);
}
// 如果是全公司则删除数据库已有的权限
if (ActivityService::IS_ALL == $activity['is_all'] && !empty($old_right)) {
$this->right_s->delete_by_conds(['ac_id' => $ac_id]);
}
$this->activity_s->commit();
} catch (Exception $e) {
$this->activity_s->rollback();
E('_ERR_ADD_FAILED');
}
// 发送消息
if (ActivityModel::NOTICE_ON == $activity['is_notice'] &&
ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {
//【1】如果是由草稿 改为发布
if (ActivityModel::ACTIVITY_DRAFT == $old_activity['activity_status']) {
// 组装发送消息的数据
$activity['right'] = $right;
$params = $this->activity_s->assemble_msg_params($ac_id, $activity);
// 发送消息
$this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);
// 给审核人发送消息
$send_params = $this->activity_s->send_check_msg_params($ac_id, $activity);
// 如果开启移动端首页推荐
if (Helper::SALE_INDEX_RECOMMEND == $activity['is_recommend']) {
$this->activity_s->send_recommend($ac_id, $activity);
}
} else {
//【2】如果是由已发布改为发布
// 获取需要发送消息的所有用户
$msg_users = $this->right_s->right_to_all($old_right, $right);
// 发送新消息
if (!empty($msg_users['add'])) {
$activity['right']['uids'] = $msg_users['add'];
// 组装发送消息的数据
$params = $this->activity_s->assemble_msg_params($ac_id, $activity, 'edit');
// 发送发布活动消息
$this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);
}
// 给审核人发送消息
$send_params = $this->activity_s->send_check_msg_params($ac_id, $activity, $old_activity);
}
// 如果开启审核
if ($activity['is_check_open'] == ActivityService::IS_CHECK_OPEN) {
// 如果开启审核且有审核人变更
if (!empty($send_params['uids'])) {
// 给审核人发送消息
$this->activity_s->send_msg($send_params, ActivityService::MSG_ACTIVITY_PUBLISH_CHECK_USER);
}
}
}
// 附件操作
$attach_serv = new AttachOperation();
// 封面图
$attach_ids[] = $activity['cover_id'];
// 内容中的封面图
$content_at_ids = $attach_serv->match_at_ids($activity['content']);
$attach_ids = array_merge($attach_ids, $content_at_ids);
$attach_serv->update_attach(
APP_DIR,
'activity',
$ac_id,
['attach_ids' => $attach_ids]
);
return intval($ac_id);
}
}