AddController.class.php
6.85 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
<?php
/**
* 【调研中心-后台】08_新增调研
* AddController.class.php
* CreateBy:dj
* Date:2017-03-03
*/
namespace Apicp\Controller\Questionnaire;
use Common\Common\AttachOperation;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RightService;
class AddController extends \Apicp\Controller\AbstractController
{
/** @var BaseinfoService */
protected $_baseinfo_s;
/** @var QuestionService */
protected $_question_s;
/** @var RightService */
protected $_right_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 调研基本信息表
$this->_baseinfo_s = new BaseinfoService();
// 调研问题信息表
$this->_question_s = new QuestionService();
// 可见范围信息表
$this->_right_s = new RightService();
return true;
}
/**
* @return bool
* @throws \VcySDK\Exception
*/
public function Index_post()
{
// 获取参数
$data = I('post.');
// 调研类型:常规类型、任务类、线下培训、其他类型
$qu_types = [
BaseinfoService::ROUTINE_TYPE,
BaseinfoService::TASK_TYPE,
BaseinfoService::OFF_LINE_TYPE,
BaseinfoService::OTHER_TYPE
];
// 验证调研类型
if (!in_array($data['qu_type'], $qu_types)) {
// 调研类型不符合规范
E('_ERR_QU_TYPE');
}
// 非常规类型:任务类型或者线下培训类型
if (BaseinfoService::ROUTINE_TYPE != $data['qu_type']) {
// 跳转新增类型
$this->new_type($data);
return true;
}
$base_data = $this->_baseinfo_s->checkData($data);
// 验证并组装基本信息
if (!$base_data) {
return false;
}
// 验证问题信息
if (!$this->_question_s->checkData($data['q_list'])) {
return false;
}
// 获取全部可填写人数
$involved_total = $this->_right_s->user_count($data);
if (empty($involved_total)) {
E('_ERR_INVOLVED_TOTAL_GET_FAIL');
}
// 全部人数
$base_data['involved_total'] = $involved_total;
// 已填写人数
$base_data['involved_num'] = 0;
$qu_id = 0;
try {
// 开始事务
$this->_baseinfo_s->start_trans();
// 插入基本信息
$qu_id = $this->_baseinfo_s->insert($base_data);
// 组装问题信息表入库数据
list($qc_data, $attach_ids) = $this->_question_s->create_data($qu_id, $data['q_list']);
// 插入问题信息表
$this->_question_s->insert_all($qc_data);
// 如果是指定人可见,则组装插入权限数据
if (is_numeric($data['is_all']) && BaseinfoService::IS_ALL_NO == $data['is_all']) {
// 验证并组装权限表入库数据
$qx_data = $this->_right_s->create_data($qu_id, $data['right']);
// 插入权限表信息
$this->_right_s->insert_all($qx_data);
}
// 调研状态:预发布、发布
if (BaseinfoService::PRE_PUBLISH_STATUS == $base_data['release_status']
|| BaseinfoService::PUBLISH_STATUS == $base_data['release_status']
) {
$cron_params = [
'type' => 0, // 0:添加 1:修改
'qu_id' => $qu_id,
];
// 添加定时任务
$this->_baseinfo_s->cron_add($cron_params);
}
if (BaseinfoService::PUBLISH_STATUS == $base_data['release_status'] &&
BaseinfoService::NOCTICE_YES == $base_data['is_notice']) {
// 如果为发布状态,且开启发布时发消息则推送消息给用户
$data = $this->_baseinfo_s->get($qu_id);
$this->_baseinfo_s->sendmsg_release($data);
}
// 提交事务
$this->_baseinfo_s->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->_baseinfo_s->rollback();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->_baseinfo_s->rollback();
}
// 附件处理
if (!empty($attach_ids)) {
$attach_serv = new AttachOperation();
$attach_serv->insert_attach(
APP_DIR,
'baseinfo',
$qu_id,
['attach_ids' => $attach_ids]
);
}
$this->_result = ['qu_id' => $qu_id];
return true;
}
/**
* 新增类型处理
*
* @param array $data 调研基本信息
*
* @return array|bool
*
* @throws \VcySDK\Exception
*/
protected function new_type($data = [])
{
// 验证并组装基本信息
$base_data = $this->_baseinfo_s->check_BaseInfoData($data);
// 验证问题信息
$this->_question_s->checkData($data['q_list']);
try {
// 开始事务
$this->_baseinfo_s->start_trans();
// 插入基本信息
$qu_id = $this->_baseinfo_s->insert($base_data);
// 组装问题信息表入库数据
list($qc_data, $attach_ids) = $this->_question_s->create_data($qu_id, $data['q_list']);
// 插入问题信息表
$this->_question_s->insert_all($qc_data);
// 提交事务
$this->_baseinfo_s->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->_baseinfo_s->rollback();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->_baseinfo_s->rollback();
}
// 附件处理
if (!empty($attach_ids)) {
$attach_serv = new AttachOperation();
$attach_serv->update_attach(
APP_DIR,
'baseinfo',
$qu_id,
['attach_ids' => $attach_ids]
);
}
$this->_result = ['qu_id' => $qu_id];
return true;
}
}