ExportController.class.php
5.07 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
<?php
/**
* Created by IntelliJ IDEA.
* 勋章列表
* User: zhoutao
* Reader: zhoutao 2017-05-31 10:07:35
* Date: 2017-05-24 15:43:49
*/
namespace Apicp\Controller\Medal;
use Com\PackageValidate;
use Com\PythonExcel;
use Common\Common\Constant;
use Common\Common\ExportDownload;
use Common\Common\Integral;
use Common\Common\User;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;
class ExportController extends AbstractController
{
/**
* Export
* @author tangxingguo
* @desc 导出接口
* @param Array dp_ids 组织ID
* @param Array job_ids 岗位ID
* @param Array role_ids 角色ID
* @param String username 用户名
* @return Array
*/
public function index()
{
$rules = [
'dp_ids' => 'array',
'job_ids' => 'array',
'role_ids' => 'array',
'username' => 'max:64',
];
// 验证请求数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 条件
$conds = [];
if (isset($postData['username'])) {
$conds['mem_username like ?'] = '%' . $postData['username'] . '%';
}
// 组织、岗位、角色转UID
$condUids = &Integral::instance()->getUids($postData);
if (!empty($postData['dp_ids']) || !empty($postData['job_ids']) || !empty($postData['role_ids'])) {
if (empty($condUids)) {
return true;
}
}
$conds['mem_uid'] = $condUids;
$medalServ = new MemberMedalService();
$list = $medalServ->list_by_conds($conds,null);
$all_uids = array_unique(array_filter(array_column($list, 'mem_uid')));
// 去重列表
$unList = [];
$columns = ['姓名', '组织', '岗位', '角色', '勋章'];
$rows = [];
if (!empty($list)) {
// 用户信息
$uids = array_unique($all_uids);
$users = &User::instance()->listAll(['memUids' => $uids]);
$users = array_combine_by_key($users, 'memUid');
// 所有勋章
$medalModel = new MedalService();
$medalList = $medalModel->list_all();
$medalList = array_combine_by_key($medalList, 'im_id');
// 组织、岗位、角色、勋章
foreach ($list as $v) {
// 勋章
if (isset($medalList[$v['im_id']])) {
if (isset($unList[$v['mem_uid']])) {
$unList[$v['mem_uid']]['madel'][] = [
'name' => $medalList[$v['im_id']]['name'],
'num' => $v['im_total']
];
} else {
// 组织、岗位、角色
if (isset($users[$v['mem_uid']])) {
$dpInfo = $users[$v['mem_uid']]['dpName'];
$v['dp_name'] = array_column($dpInfo, 'dpName');
$v['job_name'] = $users[$v['mem_uid']]['memJob'];
$v['role_name'] = $users[$v['mem_uid']]['memRole'];
}
$v['madel'][] = [
'name' => $medalList[$v['im_id']]['name'],
'num' => $v['im_total']
];
$unList[$v['mem_uid']] = $v;
}
}
}
// 行数据格式化
foreach ($all_uids as $uid) {
$madelStr = '';
foreach ($unList[$uid]['madel'] as $madel) {
if ($madel['num'] > 1) {
$madelStr .= $madel['name'] . "({$madel['num']})";
} else {
$madelStr .= $madel['name'];
}
if (end($unList[$uid]['madel']) != $madel) {
$madelStr .= ';';
}
}
$rows[] = [
$unList[$uid]['mem_username'],
implode(';', $unList[$uid]['dp_name']),
$unList[$uid]['job_name'],
$unList[$uid]['role_name'],
$madelStr,
];
}
}
$title = '员工勋章列表_' . rgmdate(MILLI_TIME, 'YmdHis') . '.xls';
$filename = ExportDownload::get_down_dir($this->_login->user['eaId']) . $title . '.xls';
$result = PythonExcel::instance()->write($filename, $columns, $rows);
if ($result === true) {
// 写入数据到下载中心
$conditon = [
'title' => $title,
'ea_id' => $this->_login->user['eaId'],
'type' => ExportDownload::EXCEL_TYPE,
'size' => filesize($filename),
'username' => $this->_login->user['eaRealname'],
'url' => $filename,
'app_dir' => APP_DIR
];
ExportDownload::insert_down_load($conditon);
}
return true;
}
}