CommentListController.class.php
15 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* Created by PhpStorm.
* User: wanghuan
* Date: 2017/11/17
* Time: 11:28
*/
namespace Apicp\Controller\Export;
use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Common\Helper;
use Common\Common\User;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\PacketrecordService;
class CommentListController extends \Apicp\Controller\AbstractController
{
public $success_money_total = 0; // 领取成功的总金额
public $success_money_system = 0; // 领取成功的随机红包的总金额
public $success_money_admin = 0; // 领取成功的定向红包的总金额
public $success_money_admin_num = 0; // 领取成功的定向红包的次数
public $ing_money_total = 0; // 待领取的总金额
public $ing_money_system = 0; // 待领取的随机红包的总金额
public $ing_money_admin = 0; // 待领取的定向红包的总金额
public $ing_money_admin_num = 0; // 待领取的定向红包的次数
/** @var ActivityService */
protected $activity_s;
public $comment_id = 0; // 帖子id
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化活动信息表
$this->activity_s = new ActivityService();
return true;
}
// 导出回帖列表接口
public function Index()
{
// 活动id
$ac_id = I('post.ac_id');
$username = I('post.username');
$start_time = I('post.start_time');
$end_time = I('post.end_time');
if (empty($ac_id)) {
// 活动id为空
E('_EMPTY_ACTIVITY_ID');
}
$activity = $this->activity_s->get($ac_id);
if (empty($activity)) {
E('_ERR_ACTIVITY_DATA');
}
// 获取活动状态
$status = $this->activity_s->activity_status($activity['activity_status'], $activity['begin_time'],
$activity['end_time']);
if (intval($status) < 3) {
// 如果是草稿和未开始
return true;
}
// 导出列表导出
$this->download_answers($activity, $username, $start_time, $end_time);
}
/**
* 导出发帖列表导出
*
* @param array $activity 活动信息
* @param string $user_name 搜索的用户名
* @param string $start_time 开始时间
* @param string $end_time 结束时间
* @return bool
*/
protected function download_answers($activity = [], $user_name = '', $start_time = '', $end_time = '')
{
// 查询条件
$condition = [
'ac_id' => $activity['ac_id'],
];
// 如果查询人员
if (!empty($user_name)) {
$uids = $this->activity_s->get_username_uid($user_name);
// 组装用户查询条件
$condition['uid'] = $uids;
}
// 如果开始时间和结束时间存在
if (!empty($start_time) && !empty($end_time)) {
$condition['created >=?'] = $start_time;
$condition['created <=?'] = $end_time;
}
// 排序:发布时间倒序
$order_option = [
'comment_id' => 'DESC',
];
// 查询字段
$fields = 'comment_id,ac_id,uid,check_status,ext_fields,created,ext_fields';
$comment_s = new CommentService();
// 发帖列表
$comment_list = $comment_s->list_by_conds($condition, null, $order_option, $fields);
$ext_fields = [];
$list = [];
if (!empty($comment_list)) {
// 发帖人id集合
$user_ids = array_unique(array_column($comment_list, 'uid'));
sort($user_ids);
// 获取发帖人信息
$user_s = User::instance();
$user_list = $user_s->listByUid($user_ids);
// 实例化红包记录表
$packet_records = new PacketrecordService();
// 红包记录查询条件
$reords_conds = [
'ac_id' => $activity['ac_id'],
'uid<>?' => '',
];
// 排序:发布时间倒序
$reords_order_option = [
'cid' => 'DESC',
];
// 查询参数
$reords_field = 'rid,p_type,cid,rand_money,packet_status,parent_rid,regive_status';
// 获取红包记录
$reords_list = $packet_records->list_by_conds($reords_conds, null, $reords_order_option, $reords_field);
// 数据格式化
foreach ($comment_list as $comment) {
// 部门名称
$dp_names = '';
// 用户信息
$user_info = $user_list[$comment['uid']];
if (empty($user_info)) {
// 如果用户被删除
$user_info = $user_s->getByUid($comment['uid']);
}
// 姓名
$username = $user_info['memUsername'];
// 电话
$mobile = $user_info['memMobile'];
// 部门名称
$dpName = array_column($user_info['dpName'], 'dpName');
if (!empty($dpName)) {
$dp_names = implode(',', $dpName);
}
$this->success_money_total = 0;
$this->success_money_system = 0;
$this->success_money_admin = 0;
$this->success_money_admin_num = 0;
$this->ing_money_total = 0;
$this->ing_money_system = 0;
$this->ing_money_admin = 0;
$this->ing_money_admin_num = 0;
$this->comment_id = $comment['comment_id'];
if ($comment['check_status'] == 1) {
// 审核通过的才会有红包记录
array_filter($reords_list, function ($v) {
if ($v['packet_status'] == 0 && $v['cid'] == $this->comment_id) {
// 当前帖子待领取总金额
$this->ing_money_total += $v['rand_money'];
if ($v['p_type'] == 1) {
// 随机红包
$this->ing_money_system += $v['rand_money'];
} elseif ($v['p_type'] == 4) {
// 定向红包
$this->ing_money_admin += $v['rand_money'];
$this->ing_money_admin_num += 1;
}
} elseif ($v['packet_status'] == 1 && $v['cid'] == $this->comment_id) {
// 当前帖子领取成功金额
$this->success_money_total += $v['rand_money'];
if ($v['p_type'] == 1) {
// 随机红包
$this->success_money_system += $v['rand_money'];
} elseif ($v['p_type'] == 4) {
// 定向红包
$this->success_money_admin += $v['rand_money'];
$this->success_money_admin_num += 1;
}
}
});
}
// 如果有扩展字段
if (!empty($comment['ext_fields'])) {
$ext_fields[] = $this->get_ext_fields(unserialize($comment['ext_fields']));
}
$list[] = [
'username' => $username,
'dp_names' => $dp_names,
'mobile' => $mobile,
'created' => rgmdate(strval($comment['created']), 'Y/m/d H:i'),
'check_status' => $this->get_check_status($comment['check_status']),
'success_money_total' => empty($this->success_money_total) ? '' : sprintf("%.2f",
$this->success_money_total / 100) . '元',
// 领取成功总金额
'success_money_system' => empty($this->success_money_system) ? '' : sprintf("%.2f",
$this->success_money_system / 100) . '元',
// 领取成功随机红包金额
'success_money_admin' => empty($this->success_money_admin) ? '' : sprintf("%.2f",
$this->success_money_admin / 100) . '元',
// 领取成功定向红包金额
'success_money_admin_num' => empty($this->success_money_admin_num) ? '' : $this->success_money_admin_num,
// 领取成功定向红包次数
'ing_money_total' => empty($this->ing_money_total) ? '' : sprintf("%.2f",
$this->ing_money_total / 100) . '元',
// 待领取红包总金额
'ing_money_system' => empty($this->ing_money_system) ? '' : sprintf("%.2f",
$this->ing_money_system / 100) . '元',
// 待领取随机红包金额
'ing_money_admin' => empty($this->ing_money_admin) ? '' : sprintf("%.2f",
$this->ing_money_admin / 100) . '元',
// 待领取定向红包金额
'ing_money_admin_num' => empty($this->ing_money_admin_num) ? '' : $this->ing_money_admin_num,
// 待领取定向红包次数
];
}
}
unset($comment_list);
unset($user_list);
// 生成下载Excel
$this->_download($list, $ext_fields, $activity, rgmdate(strval(MILLI_TIME), 'YmdHis'));
}
/**
* 获取扩展字段从手机端获得的值
* @param array $data 扩展字段数据
* @return array
*/
protected function get_ext_fields($data = [])
{
$list = [];
// 遍历数据
foreach ($data as $key => $v) {
// 如果是文本类型或者时间类型
if (Helper::EXT_FIELD_TEXT_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_HI_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_YMD_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_YMD_HI_TYPE == $v['type']
) {
$list[] = $v['text'];
}
// 数字类型
if (Helper::EXT_FIELD_NUMBER_TYPE == $v['type']) {
$list[] = $v['text'] . $v['unit'];
}
// 单选类型或者多选类型
if (Helper::EXT_FIELD_RADIO_TYPE == $v['type'] || Helper::EXT_FIELD_MULTI_SELECT_TYPE == $v['type']) {
// 默认选中项
$check = [];
foreach ($v['options'] as $item) {
// 如果被选中
if (Helper::EXT_FIELD_CHECK == $item['value']) {
$check[] = $item['key'];
}
}
// 获取选中的值
$list[] = implode(',', $check);
}
}
return $list;
}
/**
* 获取审核状态
*
* @param int $check_status
*
* @return string
*/
protected function get_check_status($check_status = 0)
{
if ($check_status == 0) {
return '待审核';
} elseif ($check_status == 1) {
return '已通过';
} elseif ($check_status == 2) {
return '未通过';
}
return '';
}
/**
* 回答导出模板
*
* @param array $list 导出数据
* @param array $ext_fields 扩展字段数据
* @param array $activity 活动详情
* @param string $file_name 文件名称
*
* @return bool
*/
protected function _download($list = [], $ext_fields = [], $activity = [], $file_name)
{
// Excel 表头字段
$title = [
'姓名',
'组织',
'手机号',
'发表时间'
];
// 如果存在扩展字段
if (!empty($activity['ext_fields'])) {
// 扩展字段名称
$ext_name = array_column(unserialize($activity['ext_fields']), 'name');
$title = array_merge($title, $ext_name);
}
// 扩展字段后的部分
$tail = [
'审核状态',
'已领取红包总金额',
'已领取系统随机红包金额',
'已领取手动定向红包金额',
'已领取手动定向红包次数',
'未领取红包总金额',
'未领取系统随机红包金额',
'未领取手动定向红包金额',
'未领取手动定向红包次数',
];
$title = array_merge($title, $tail);
$rows = [];
// 回帖数据处理
foreach ($list as $key => $v) {
// 将数组分割两个部门
$rows[$key] = [
$v['username'],
$v['dp_names'],
$v['mobile'],
$v['created']
];
// 扩展字段后的部分
$tail_info = [
$v['check_status'],
$v['success_money_total'],
$v['success_money_system'],
$v['success_money_admin'],
$v['success_money_admin_num'],
$v['ing_money_total'],
$v['ing_money_system'],
$v['ing_money_admin'],
$v['ing_money_admin_num'],
];
// 如果存在扩展字段部分
if (!empty($ext_fields)) {
$rows[$key] = array_merge($rows[$key], $ext_fields[$key]);
}
$rows[$key] = array_merge($rows[$key], $tail_info);
}
unset($list);
$file_title=$activity ['subject'] . '_评论列表_' . $file_name;
// 生成Excel 下载
$real_path = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true)) .$file_title . '.xls';
$ret = PythonExcel::instance()->write($real_path, $title, $rows);
if ($ret) {
// header('Content-Description: File Transfer');
// header('Content-Type: application/octet-stream');
// header('Content-Disposition: attachment; filename=' . $file_name . '.xls');
// header('Content-Transfer-Encoding: binary');
// header('Expires: 0');
// header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
// header('Pragma: public');
// header('Content-Length: ' . filesize($real_path));
// readfile($real_path);
// 写入数据到下载中心
$params = [
'title' => $file_title,
'ea_id' => $this->_login->user['eaId'],
'username' => $this->_login->user['eaRealname'],
'type' => ExportDownload::EXCEL_TYPE,
'url' => $real_path,
];
ExportDownload::insert_down_load($params);
}
return true;
}
}