ImportController.class.php
8.11 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
<?php
/**
* 用户导入操作
* User: liyifei2012it
* Date: 16/9/17
* Time: 22:45
*/
namespace Apicp\Controller\User;
use Common\Common\Department;
use Common\Model\AttrModel;
use Common\Service\AttrService;
use Common\Service\DepartmentService;
use Common\Service\ImportDataService;
use Common\Service\UserService;
use VcySDK\Member;
use VcySDK\Service;
class ImportController extends AbstractController
{
/**
* 【通讯录】人员导入
* @author liyifei
*/
public function Index_post()
{
try {
// 保存失败原因
$errMsg = '';
$title_id = (int)I('post.title_id');
$index = (int)I('post.index');
// 读取 title 和待导入数据
$importDataService = new ImportDataService();
$dataList = $importDataService->list_by_pks(array($title_id, $index));
$dataList = array_combine_by_key($dataList, 'cid_id');
// 如果数据不存在
if (empty($dataList[$title_id]) || empty($dataList[$index])) {
E(L('_ERR_EXCEL_IMPORT', array('msg' => '数据错误')));
}
// 获取组织类型
$titleData = json_decode($dataList[$title_id]['data']);
$importData = json_decode($dataList[$index]['data']);
// 新增导入失败原因
$titleData[] = '失败原因';
$this->_result['title'] = $titleData;
$this->_result['data'] = $importData;
// 判断管理权限
if (!$this->_managePower()) {
E(L('_ERR_EXCEL_IMPORT', array('msg' => '没有管理权限')));
}
$attrServ = new AttrService();
$attrs = $attrServ->getAttrList(true, array(), true);
$attrs = array_combine_by_key($attrs, 'attr_name');
// 导入数据
$member = array();
$ignoreAttrs = array(
AttrModel::ATTR_TYPE_PICTURE
);
foreach ($titleData as $_k => $_title) {
$attr = $attrs[$_title];
if (empty($attr) || in_array($attr['type'], $ignoreAttrs)) {
continue;
}
// 如果是多选
if (AttrModel::ATTR_TYPE_CHECKBOX == $attr['type']) {
$explodes = explode(';', $importData[$_k]);
$selected = array();
foreach ($attr['option'] as $_attr) {
if (in_array($_attr['name'], $explodes)) {
$selected[] = $_attr;
}
}
$member[$attr['field_name']] = serialize($selected);
continue;
} elseif (AttrModel::ATTR_TYPE_RADIO == $attr['type'] || AttrModel::ATTR_TYPE_DROPBOX == $attr['type']) {
$value = '';
foreach ($attr['option'] as $_attr) {
if ($_attr['name'] == $importData[$_k]) {
$value = $_attr['value'];
break;
}
}
$importData[$_k] = $value;
} elseif (AttrModel::ATTR_TYPE_DATE == $attr['type']) {
// 没有提交日期时间时 跳过 不然会变成 1970-01-01
if (is_string($importData[$_k]) && empty($importData[$_k])) {
continue;
}
$originalData = $importData[$_k];
// 入库需要转换成毫秒
$importData[$_k] = rstrtotime($importData[$_k], 1);
// 显示需要还原成年月日格式
$this->_result['data'][$_k] = $originalData;
}
$member[$attr['field_name']] = $importData[$_k];
}
// 调用验证接口,验证参数传值是否符合规范
$errors = $attrServ->checkValue($member);
if (!empty($errors)) {
if (count($errors) > 1) {
$errMsg = implode(',', $errors);
} else {
$errMsg = $errors[0];
}
E($errMsg);
}
// 获取部门信息
$dpIds = $this->_getDpIdByName($member['dpName']);
if (empty($dpIds)) {
if (!empty($errMsg)) {
$errMsg .= ',';
}
$errMsg .= '组织不存在';
E(L('_ERR_EXCEL_IMPORT', array('msg' => $errMsg)));
// 判断部门范围是否在管理范围内
} elseif (!empty($this->_login->role['writeDpIdList'])) {
$dpServ = new Department();
$allow_dp_ids = $dpServ->list_childrens_by_cdid($this->_login->role['writeDpIdList'], true);
if (array_diff($dpIds, $allow_dp_ids)) {
E(L('_ERR_EXCEL_IMPORT', array('msg' => '组织不在管理权限内')));
}
}
$member['dpIdList'] = $dpIds;
unset($member['dpName']);
// 先搜索用户
$memberSDK = new Member(Service::instance());
$memberResult = array();
if (!empty($member['memMobile'])) {
$condition = array('memMobile' => $member['memMobile']);
$memberResult = $memberSDK->listAll($condition);
}
if (empty($memberResult) && !empty($member['memWeixin'])) {
$condition = array('memWeixin' => $member['memWeixin']);
$memberResult = $memberSDK->listAll($condition);
}
if (empty($memberResult) && !empty($member['memEmail'])) {
$condition = array('memEmail' => $member['memEmail']);
$memberResult = $memberSDK->listAll($condition);
}
// 如果用户存在, 则
if (!empty($memberResult['list']) && 1 == count($memberResult['list'])) {
$uid = $memberResult['list'][0]['memUid'];
} else {
$uid = 0;
}
if (0 == $uid) {
// 邀请记录信息
$member['memJoinType'] = UserService::ADMIN_ADD_JOIN;
if (!empty($this->_login->user['eaRealname'])) {
$member['memJoinInviter'] = $this->_login->user['eaRealname'];
} elseif (!empty($this->_login->user['eaMobile'])) {
$member['memJoinInviter'] = $this->_login->user['eaMobile'];
} elseif (!empty($this->_login->user['eaEmail'])) {
$member['memJoinInviter'] = $this->_login->user['eaEmail'];
} else {
$member['memJoinInviter'] = '';
}
}
// 更新用户信息
$userService = new UserService();
$this->_result = $userService->saveUser($uid, $member);
$this->clearUserCache();
} catch (\Exception $e) {
// 如果读取数据正常, 则抛错
if (!empty($this->_result['data'])) {
if (empty($errMsg)) {
$errMsg = $e->getMessage();
}
$importDataService = new ImportDataService();
$importDataService->update($index, array('is_error' => 1, 'fail_message' => $errMsg));
// 错误列表新增失败原因
$this->_result['data'][] = $errMsg;
}
E($e->getCode() . ":" . $errMsg);
}
return true;
}
/**
* 通过部门名称获取部门ID
* @param $dpName
* @return array
*/
protected function _getDpIdByName($dpName)
{
$dpIds = array();
$dpNames = explode(';', $dpName);
$departmentService = new DepartmentService();
foreach ($dpNames as $_name) {
$department = array();
$departmentService->getDepartmentByPath($department, $_name, '', false);
if (empty($department['dpId'])) {
continue;
}
$dpIds[] = $department['dpId'];
}
return $dpIds;
}
}