PrizeListController.class.php
4.09 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
<?php
/**
* 导出中奖列表
* User: daijun
* Date: 18/3/24
* Time: 下午3:44
*/
namespace Apicp\Controller\Export;
use Common\Common\Constant;
use Com\PythonExcel;
use Common\Service\ActivityService;
use Common\Common\ExportDownload;
use Common\Service\RecordService;
class PrizeListController extends \Apicp\Controller\AbstractController
{
public function Index()
{
$params = I('post.');
if (empty($params['ac_id'])) {
// 活动ID不能为空
E('_EMPTY_ACTIVITY_ID');
}
$activity_serv = new ActivityService();
$detail = $activity_serv->get($params['ac_id']);
if (empty($detail)) {
// 活动信息不存在
E('_ERR_ACTIVITY_DATA');
}
if (!isset($params['type']) || !in_array($params['type'], [Constant::HAVE_PRIZE, Constant::NO_PRIZE])) {
// 如果type为空或者不是特定的数值【未中奖,已中奖】
return true;
}
$record_serv = new RecordService();
// 组织查询条件
$where = $record_serv->get_where($params);
// 时间倒序
$order_option = ['lr_id' => 'DESC'];
// 定义查询字段
$fields = 'lr_id,lp_id,lp_name,is_prize,username,dp_name,mobile,role,job,remark,created';
// 查询列表数据
$list = $record_serv->list_by_conds($where, [], $order_option, $fields);
// 生成下载Excel
$this->_download($list, $params['type'], $detail['title']);
return true;
}
/**
* 导出模板
*
* @param array $list 导出数据
* @param int $type 是否中奖(0:中奖,1:未中奖)
* @param string $ac_title 活动标题
*
* @return bool
*/
protected function _download($list = [], $type = 0, $ac_title = '')
{
$rows = [];
$rand_str = substr(md5(QY_DOMAIN), 0, 1) . substr(md5(QY_DOMAIN), -1);
if ($type == Constant::NO_PRIZE) {
// 未中奖
$file_name = $ac_title . '未中奖记录_' . rgmdate(strval(MILLI_TIME), 'YmdHis') . $rand_str; // 导出文件名称
// Excel 表头字段
$title = [
'抽奖时间',
'姓名',
'组织',
'手机号',
'抽奖结果',
];
// 回帖数据处理
foreach ($list as $v) {
$rows[] = [
rgmdate(strval($v['created']), 'Y/m/d H:i'),
$v['username'],
$v['dp_name'],
" " . $v['mobile'],
'谢谢参与',
];
}
} else {
// 中奖
$file_name = $ac_title . '中奖记录_' . rgmdate(strval(MILLI_TIME), 'YmdHis') . $rand_str; // 导出文件名称
// Excel 表头字段
$title = [
'抽奖时间',
'姓名',
'组织',
'手机号',
'抽奖结果',
'备注信息',
];
// 回帖数据处理
foreach ($list as $v) {
$rows[] = [
rgmdate(strval($v['created']), 'Y/m/d H:i'),
$v['username'],
$v['dp_name'],
" " . $v['mobile'],
$v['lp_name'],
$v['remark'],
];
}
}
unset($list);
$dir = DATA_PATH;
// 生成Excel 下载
$real_path = $dir . D_S . $file_name . '.xls';
$ret = PythonExcel::instance()->write($real_path, $title, $rows);
if ($ret) {
$data = [
'title' => $file_name,
'ea_id' => $this->_login->user['eaId'],
'type' => ExportDownload::EXCEL_TYPE,
'username' => $this->_login->user['eaRealname'],
'url' => $real_path,
'app_dir' => APP_DIR
];
ExportDownload::insert_down_load($data);
}
return true;
}
}