AddController.class.php
7.37 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
<?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 AddController extends \Apicp\Controller\AbstractController
{
/** @var ActivityService */
protected $activity_s;
/** @var PacketService */
protected $packet_s;
/** @var CommentService */
protected $comment_s;
/** @var RightService */
protected $right_s;
/** @var PacketrecordService */
protected $packetrecord_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->right_s = new RightService();
// 实例化红包记录表
$this->packetrecord_s = new PacketrecordService();
// 实例化用户表
$this->user_s = new User();
return true;
}
public function Index_post()
{
$params = I('post.');
// 进行活动添加操作
$ac_id = $this->add_activity($params);
$this->_result = [
'ac_id' => $ac_id,
];
return true;
}
/**
* 新增活动(后端)
*
* @param array $reqData 请求数据
*
* @return mixed
*/
protected function add_activity($reqData)
{
$ac_id = 0;
// 获取活动数据,类型为添加
$activity = $this->activity_s->fetch_activity($reqData);
// 验证数据
$this->activity_s->validate_for_add($activity);
// 开始时间不能小于当前时间
if ($activity['begin_time'] <= MILLI_TIME) {
E('_ERR_BEGINTIME_LT_NOWTIME');
}
// 结束时间不能小于当前时间
if ($activity['end_time'] > 0 && $activity['end_time'] <= MILLI_TIME) {
E('_ERR_ENDTIME_LT_NOWTIME');
}
// 是否是立即发布活动
if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {
$activity['publish_time'] = MILLI_TIME;
}
// 将最后更新时间设为当前时间
$activity['last_time'] = MILLI_TIME;
// 将是否开启红包放在外层
$activity['is_show_red'] = $activity['red_content']['is_show_red'];
// 重新定义权限数组
$right = $activity['right'];
unset($activity['right']);
try {
$this->activity_s->start_trans();
// 活动数据入库
$ac_id = $this->activity_s->insert($activity);
// 是否开启红包
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'],
'is_show_red' => $activity['red_content']['is_show_red']
];
// 存储红包配置数据
$pid = $this->packet_s->insert($packetdate);
// 已发布的活动
if (ActivityService::DRAFT_STATUS != $activity['activity_status']) {
// 生成红包记录
$this->packetrecord_s->create_packet_record($ac_id, $pid, $activity['red_content']['rule_list'],
$activity['red_content']['red_base_num']);
}
}
} else {
// 若不开启红包,删除红包祝福语
$activity['packet_bless'] = '';
// 干掉抢红包图标信息
$activity['is_show_red'] = ActivityService::SHOW_RED_CLOSE;
}
// 权限入库
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']]);
}
$this->activity_s->commit();
} catch (Exception $e) {
$this->activity_s->rollback();
E('_ERR_ADD_FAILED');
}
// 如果是已发布状态
if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {
$activity['right'] = $right;
//【发送消息】状态为发布状态并且开启发送消息时则发送消息
if (ActivityModel::NOTICE_ON == $activity['is_notice'] && $ac_id) {
// 组装发送消息的数据
$params = $this->activity_s->assemble_msg_params($ac_id, $activity);
// 发送消息
$this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);
}
// 如果开启审核且是已发布状态
if ($activity['is_check_open'] == ActivityService::IS_CHECK_OPEN) {
// 给审核人发送消息
$send_params = $this->activity_s->send_check_msg_params($ac_id, $activity);
// 给审核人发送消息
$this->activity_s->send_msg($send_params, ActivityService::MSG_ACTIVITY_PUBLISH_CHECK_USER);
}
// 如果开启移动端首页推荐
if (Helper::SALE_INDEX_RECOMMEND == $activity['is_recommend']) {
$this->activity_s->send_recommend($ac_id, $activity);
}
}
// 附件操作
$attach_serv = new AttachOperation();
$attach_ids = [];
// 封面图
if (!empty($activity['cover_id'])) {
$attach_ids[] = $activity['cover_id'];
}
// 内容中的封面图
if (!empty($activity['content'])) {
$content_at_ids = $attach_serv->match_at_ids($activity['content']);
$attach_ids = array_merge($attach_ids, $content_at_ids);
}
$attach_serv->insert_attach(
APP_DIR,
'activity',
$ac_id,
['attach_ids' => $attach_ids]
);
return $ac_id;
}
}