ExportController.class.php
8.52 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 16/9/17
* Time: 22:47
*/
namespace Apicp\Controller\User;
use Com\PythonExcel;
use Common\Common\Constant;
use Common\Common\ExportDownload;
use Common\Common\User;
use Common\Common\Department;
use Common\Model\AttrModel;
use Common\Service\AttrService;
use Common\Service\UserService;
class ExportController extends AbstractController
{
/**
* 【通讯录】批量导出
* @author zhonglei
*/
public function Index_post()
{
$role = $this->_login->role;
// 接收定义参数
$dpId = I('post.department_id', '', 'trim');
$keyword = I('post.keyword', '', 'trim');
$status = I('post.status', '', 'trim');
$active = I('post.active', '', 'trim');
$mobile = I('post.mobile', '', 'trim');
$email = I('post.email', '', 'trim');
$jobIds = I('post.job_ids', '', 'trim');
$roleIds = I('post.role_ids', '', 'trim');
// UC查询条件
$conds = [
'departmentChildrenFlag' => UserService::DEPT_CHILDREN_FLAG,
'memUsername' => $keyword,
'memSubscribeStatus' => $status,
'memMobile' => $mobile,
'memEmail' => $email,
'memActive' => $active
];
if (!empty($dpId)) {
$conds['dpIdList'] = (array)$dpId;
} elseif (!empty($role['readDpIdList'])) {
$conds['dpIdList'] = $role['readDpIdList'];
}
if (!empty($jobIds)) {
$conds['jobIdList'] = (array)$jobIds;
}
if (!empty($roleIds)) {
$conds['roleIdList'] = (array)$roleIds;
}
$newUser = new User();
$userlist = $newUser->listAll($conds);
// Excel单元格标题
$attrServ = new AttrService();
$attrs = $attrServ->getAttrList(true, array(), true);
$titles = array();
foreach ($attrs as $_attr) {
if (1 == $_attr['is_required_cp'] || 1 == $_attr['is_required']) {
$titles[] = array(
'value' => $_attr['attr_name'], // 单元格内容
'pattern' => array( // 单元格样式, 关于样式, 请参考: http://www.python-excel.org
'pattern' => 'solid',
'fore_colour' => 'red'
)
);
} else {
$titles[] = $_attr['attr_name'];
}
}
$titles[] = '加入状态';
$titles[] = '微信插件';
// 初始化标题简介数据
$merge_titles = Constant::STAFF_LIST_TITLES_DESC;
$merge_titles[] = array_values($titles);
$merge_titles[] = Constant::STAFF_LIST_DATA;
$title_xls[] = '填写说明';
// 初始化默认值数据
$deptServ = new Department();
$rows = [];
// 企业微信加入状态
$joinStatus = [
UserService::USER_STATUS_ALL => '全部',
UserService::USER_STATUS_FOLLOW => '已加入',
UserService::USER_STATUS_DISABLE => '已禁用',
UserService::USER_STATUS_UNFOLLOW => '未加入'
];
// 微信插件状态
$wxPluginStatus = [
1 => '已关注',
0 => '未关注'
];
foreach ($userlist as $v) {
$row = [];
foreach ($attrs as $attr) {
$field_name = $attr['field_name'];
$value = isset($v[$field_name]) ? $v[$field_name] : '';
if (isset($v[$field_name])) {
// 根据属性类型格式化数据
switch ($attr['type']) {
// 日期
case AttrModel::ATTR_TYPE_DATE:
$value = rgmdate($value, 'Y-m-d');
break;
// 日期时间
case AttrModel::ATTR_TYPE_DATE_TIME:
$value = rgmdate($value, 'Y-m-d H:i:s');
break;
// 单选
case AttrModel::ATTR_TYPE_RADIO:
$default = '';
foreach ($attr['option'] as $val) {
if ($value == $val['value']) {
$default = $val['name'];
break;
}
}
$value = $default;
break;
// 下拉框单选
case AttrModel::ATTR_TYPE_DROPBOX:
$default = '';
foreach ($attr['option'] as $val) {
if ($value == $val['value']) {
$default = $val['name'];
break;
}
}
$value = $default;
break;
// 多选
case AttrModel::ATTR_TYPE_CHECKBOX:
$data = unserialize($value);
if (is_array($data)) {
$nameArr = array_column($data, 'name');
$value = implode(';', $nameArr);
}
break;
// 图片
case AttrModel::ATTR_TYPE_PICTURE:
$images = $attrServ->formatValueByType(AttrModel::ATTR_TYPE_PICTURE, $value);
if (!empty($images)) {
$urls = array_column($images, 'url');
if (is_array($urls)) {
$value = implode(';', $urls);
}
}
break;
// 直属上级 V1.2.0版本迭代
case AttrModel::ATTR_TYPE_LEADER:
$leaders = $attrServ->formatValueByType(AttrModel::ATTR_TYPE_LEADER, $value, $userlist);
$value = '';
if (!empty($leaders)) {
$leaderName = '';
foreach ($leaders as $leader) {
$leaderName .= $leader['name'] . ';';
}
$value = substr($leaderName, 0, -1);
}
break;
}
}
// 部门
if ($field_name === 'dpName' && $v['dpName']) {
$dpIds = array_column($v['dpName'], 'dpId');
$dpNames = [];
foreach ($dpIds as $dpId) {
$dpNames[] = $deptServ->getCdNames($dpId);
}
$value = implode(';', $dpNames);
}
$row[] = $value;
}
// 企业微信加入状态
$row[] = isset($joinStatus[$v['memSubscribeStatus']]) ? $joinStatus[$v['memSubscribeStatus']] : $v['memSubscribeStatus'];
// 微信插件关注状态
$row[] = isset($wxPluginStatus[$v['memWxpluginStatus']]) ? $wxPluginStatus[$v['memWxpluginStatus']] : $v['memWxpluginStatus'];
if ($row) {
$rows[] = $row;
}
}
// 合并标题到数据中
$data = array_merge($merge_titles, $rows);
// 生成 Excel 并输出
$filename = rgmdate(NOW_TIME, 'Y') . rgmdate(NOW_TIME, 'm') . rgmdate(NOW_TIME, 'd') . '_员工列表';
// 生成文件路径
$site_dir = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true));
$ret = PythonExcel::instance()->write($site_dir . $filename . '.xls', $title_xls, $data);
if ($ret) {
// 数据写入到下载中心
$conditon = [
'title' => $filename,
'ea_id' => $this->_login->user['eaId'],
'type' => ExportDownload::EXCEL_TYPE,
'size' => filesize($site_dir . $filename . '.xls'),
'url' => $site_dir . $filename . '.xls',
'username' => $this->_login->user['eaRealname'],
'app_dir' => APP_DIR
];
ExportDownload::insert_down_load($conditon);
}
return true;
}
}