AddController.class.php
9.84 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
315
316
317
318
319
320
<?php
/**
* 【调研中心-手机端】05_填写调研
* AddController.class.php
* CreateBy:daijun
* Date:2017-03-13
*/
namespace Api\Controller\Record;
use Common\Common\AttachOperation;
use Common\Common\StudyMap;
use Common\Common\Train;
use Common\Common\TaskCenter;
use Common\Common\Integral;
use Common\Service\AnswerService;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RecordService;
class AddController extends \Api\Controller\AbstractController
{
// 免登录
protected $_require_login = false;
/**
* @return bool
* @throws \VcySDK\Exception
* @throws \Exception
*/
public function Index()
{
// 调研id
$qu_id = I('post.qu_id', 0, 'intval');
// 调研id为空:调研ID不能为空
if (empty($qu_id)) {
E('_EMPTY_BASEINFO_ID');
}
// 调研问题答案列表
$q_list = I('post.q_list');
// 答案列表为空:调研问题列表信息不能为空
if (empty($q_list)) {
E('_EMPTY_BASEINFO_LIST');
}
// 实例化调研基本信息Service
$base_info_s = new BaseinfoService();
// 调研基本信息
$data = $base_info_s->get($qu_id);
// 查询的调研基本信息为空:调研信息不存在
if (empty($data)) {
E('_ERR_BASEINFOID_NO_EXISTENT');
}
// 常规类型并且调研已截止:调研已截止
if (BaseinfoService::ROUTINE_TYPE == $data['qu_type'] && $data['deadline'] <= MILLI_TIME) {
E('_ERR_BASEINFO_DEADLINE');
}
$openid = '';
$uid = $this->uid;
if (empty($uid)) {
$openid = $this->_login->openid;
}
// 任务id
$task_id = I('post.customtask_id', 0, 'intval');
// 培训计划id
$plan_id = I('post.plan_id', 0, 'intval');
// 学习地图
$map_id = I('post.map_id', 0, 'intval');
$path_id = I('post.path_id', 0, 'intval');
$obj_id = 0;
if (BaseinfoService::TASK_TYPE == $data['qu_type'] && $task_id) {
$obj_id = $task_id;
} elseif (BaseinfoService::OFF_LINE_TYPE == $data['qu_type'] && $plan_id) {
$obj_id = $plan_id;
}
if (BaseinfoService::OTHER_TYPE == $data['qu_type'] && $map_id && $path_id) {
$obj_id = $map_id;
}
// 实例化调研回答记录信息Service
$answer_s = new AnswerService();
// 组装查询条件
$condition = [
'uid' => $uid,
'openid' => $openid,
'qu_id' => $qu_id,
'data_type' => $data['qu_type'],
'obj_id' => rintval($obj_id)
];
// 查询之前的回答记录
$answer_list = $answer_s->list_by_conds($condition);
// 回答记录不为空:你已填写过该调研不可重复填写
if (!empty($answer_list)) {
E('_ERR_CANNOT_FILL_IN');
}
// 积分策略ID
$businessid = $qu_id;
// 积分策略返回值
$score_list = [];
// 实例化调研回复详情记录表Service
$record_s = new RecordService();
// 实例化调研问题信息表Service
$question_s = new QuestionService();
try {
// 开始事务
$base_info_s->start_trans();
// 更新调研主表参与人数
$involved_num = rintval($data['involved_num']) + 1;
$base_info_s->update($qu_id, ['involved_num' => $involved_num]);
// 调研外部用户参与人数
$outside_num = $answer_s->count_by_conds(['qu_id' => $qu_id, 'uid' => '']);
// 外部用户微信昵称
$username = '';
// 用户uid不为空
if ($uid) {
// 调用积分策略
$score_list = $this->get_score_action($data['integral_action_type'], $uid, $businessid, $data['credit_action_type']);
} else {
$username = '外部用户' . ($outside_num + 1);
}
// 组装回答表入库数据
$answer_user = [
'qu_id' => $qu_id,
'uid' => $uid,
'openid' => $openid,
'username' => $username,
'businessid' => $businessid,
'data_type' => intval($data['qu_type']),
'obj_id' => rintval($obj_id)
];
// 插入用户回答信息表
$a_id = $answer_s->insert($answer_user);
list($answer_data, $attach_ids) = $question_s->check_answer($qu_id, $a_id, $q_list);
// 验证问题及答案信息并组装入库数据
if (!$answer_data) {
return false;
}
// 插入答题记录表
$record_s->insert_all($answer_data);
// 提交事务
$base_info_s->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$base_info_s->rollback();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$base_info_s->rollback();
}
// 格式化返回的积分策略数据
$integral = [];
if (!empty($score_list)) {
foreach ($score_list as $v) {
if ('SUCCESS' == $v['result']) {
$integral[] = [
'content' => '积分',
'number' => intval($v['score'])
];
}
}
}
// 常规任务埋点:参与调研
if (BaseinfoService::TASK_TYPE == $data['qu_type'] && $task_id) {
$params = [
'uid' => $uid,
'customtask_id' => $task_id,
'app_data_id' => $qu_id,
'action_key' => 'questionnaire_join',
'description' => '参与调研'
];
$taskCenter = &TaskCenter::instance();
$taskCenter->triggerCustomtask($params);
}
// 同步培训完成状态
if (BaseinfoService::OFF_LINE_TYPE == $data['qu_type'] && $plan_id) {
$Train = &Train::instance();
$Train->syncTrainStatus($plan_id, $qu_id);
}
// 通知学习地图,应用数据已完成
if (BaseinfoService::OTHER_TYPE == $data['qu_type'] && $map_id && $path_id) {
$mapService = new StudyMap($map_id);
// 验证权限
$mapService->checkRight($path_id, $qu_id, $this->_login->user);
// 通知为已完成
$mapService->studyContent($path_id, $qu_id, $this->_login->user);
}
// 附件处理
if (!empty($attach_ids)) {
$attach_serv = new AttachOperation();
$attach_serv->insert_attach(
APP_DIR,
'baseinfo',
$qu_id,
['attach_ids' => $attach_ids]
);
}
$this->_result = ['score_list' => $integral];
return true;
}
/**
* 调用积分策略
*
* @param int $integral_action_type 启用状态 1:启用默认,2:不启用
* @param string $uid 用户ID
* @param int $businessid 积分策略ID
* @param int $credit_action_type 启用状态 1:启用默认,2:不启用
* @return array|mixed
*/
private function get_score_action($integral_action_type = 1, $uid = '', $businessid = 0, $credit_action_type = 1)
{
// 积分策略返回值
$score_list = [];
// 初始化积分策略
$Integral = new Integral();
// 用户uid不为空
if (!empty($uid)) {
// 组装策略请求参数
$param = [
'memUid' => $uid,
'msgIdentifier' => APP_IDENTIFIER,
'businessKey' => 'research_center',
'businessAct' => 'join',
'businessId' => $businessid,
'triggers' => [],
];
// 积分策略启用
if (BaseinfoService::INTEGRAL_ACTION_TYPE_NO != $integral_action_type) {
// 组装积分策略请求参数
$param['triggers'][] = [
'miType' => 'mi_type0',
'businessStrategyId' => '',
'triggerTypes' => [
[
'triggerKey' => 'number',
'value' => 1,
'remark' => '参加调研'
]
],
];
}
// 学分策略启用
if (BaseinfoService::CREDIT_ACTION_TYPE_NO != $credit_action_type) {
// 组装学分策略请求参数
$param['triggers'][] = [
'miType' => 'mi_type1',
'businessStrategyId' => '',
'triggerTypes' => [
[
'triggerKey' => 'number',
'value' => 1,
'remark' => '参加调研'
]
],
];
}
// 触发学分策略
if (!empty($param['triggers'])) {
$score_list = $Integral->changeScore($param);
}
}
return $score_list;
}
}