zjk
2026-07-31 dc2872d6d346a7de629b3b0686057ab54f216f09
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
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysEmployee;
import com.ruoyi.system.dto.SysEmployeeAddRequest;
import com.ruoyi.system.dto.SysEmployeeDeleteRequest;
import com.ruoyi.system.dto.SysEmployeeDetailRequest;
import com.ruoyi.system.dto.SysEmployeeEditRequest;
import com.ruoyi.system.dto.SysEmployeePageRequest;
import com.ruoyi.system.dto.SysEmployeeStatusRequest;
import com.ruoyi.system.dto.SysEmployeeUnbindRequest;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysEmployeeMapper;
import com.ruoyi.system.service.ISysEmployeeService;
import com.ruoyi.system.utils.EmployeePhoneCrypto;
import com.ruoyi.system.vo.SysEmployeeVO;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
 
/**
 * 员工管理服务实现
 *
 * @author ruoyi
 */
@Service
public class SysEmployeeServiceImpl implements ISysEmployeeService
{
    private static final Pattern MOBILE_PATTERN = Pattern.compile("^1\\d{10}$");
 
    @Autowired
    private SysEmployeeMapper employeeMapper;
 
    @Autowired
    private SysDeptMapper deptMapper;
 
    @Override
    public PageInfo<SysEmployeeVO> pageList(SysEmployeePageRequest request)
    {
        SysEmployeePageRequest query = request == null ? new SysEmployeePageRequest() : request;
        query.setEmployeeName(StringUtils.trim(query.getEmployeeName()));
        query.setEmployeeNo(StringUtils.trim(query.getEmployeeNo()));
        query.setContactPhone(EmployeePhoneCrypto.normalize(query.getContactPhone()));
        query.setPosition(StringUtils.trim(query.getPosition()));
        String phoneHash = StringUtils.isEmpty(query.getContactPhone()) ? null : EmployeePhoneCrypto.hash(query.getContactPhone());
        PageInfo<SysEmployeeVO> pageInfo = new PageInfo<SysEmployeeVO>(query.getPageNum(), query.getPageSize());
        List<SysEmployeeVO> list = employeeMapper.pageList(query, phoneHash, pageInfo);
        for (SysEmployeeVO employee : list)
        {
            fillDisplayFields(employee, false);
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public SysEmployeeVO detail(SysEmployeeDetailRequest request)
    {
        Long employeeId = getRequiredEmployeeId(request == null ? null : request.getEmployeeId());
        SysEmployeeVO detail = employeeMapper.selectVoById(employeeId);
        if (detail == null)
        {
            throw new ServiceException("员工不存在");
        }
        fillDisplayFields(detail, true);
        return detail;
    }
 
    @Override
    public int add(SysEmployeeAddRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("员工内容不能为空");
        }
        SysEmployee employee = buildEmployee(request.getDeptId(), request.getEmployeeName(), request.getEmployeeNo(),
                request.getContactPhone(), request.getPosition(), request.getCardNo(), request.getFaceImage(),
                request.getEnabled(), null);
        employee.setCreateBy(username);
        return employeeMapper.insertEmployee(employee);
    }
 
    @Override
    public int edit(SysEmployeeEditRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("员工内容不能为空");
        }
        Long employeeId = getRequiredEmployeeId(request.getEmployeeId());
        if (employeeMapper.selectById(employeeId) == null)
        {
            throw new ServiceException("员工不存在");
        }
        SysEmployee employee = buildEmployee(request.getDeptId(), request.getEmployeeName(), request.getEmployeeNo(),
                request.getContactPhone(), request.getPosition(), request.getCardNo(), request.getFaceImage(),
                request.getEnabled(), employeeId);
        employee.setEmployeeId(employeeId);
        employee.setUpdateBy(username);
        return employeeMapper.updateEmployee(employee);
    }
 
    @Override
    public int changeStatus(SysEmployeeStatusRequest request, String username)
    {
        List<Long> employeeIds = getRequiredEmployeeIds(request == null ? null : request.getEmployeeIds());
        if (request.getEnabled() == null)
        {
            throw new ServiceException("启用状态不能为空");
        }
        return employeeMapper.updateEnabled(employeeIds, request.getEnabled(), username);
    }
 
    @Override
    public int unbindCard(SysEmployeeUnbindRequest request, String username)
    {
        return employeeMapper.clearCard(getRequiredEmployeeIds(request == null ? null : request.getEmployeeIds()), username);
    }
 
    @Override
    public int unbindFace(SysEmployeeUnbindRequest request, String username)
    {
        return employeeMapper.clearFace(getRequiredEmployeeIds(request == null ? null : request.getEmployeeIds()), username);
    }
 
    @Override
    public int delete(SysEmployeeDeleteRequest request, String username)
    {
        return employeeMapper.deleteByIds(getRequiredEmployeeIds(request == null ? null : request.getEmployeeIds()), username);
    }
 
    @Override
    public String importData(InputStream inputStream, String username)
    {
        int successCount = 0;
        List<String> errorMessages = new ArrayList<String>();
        Map<String, Long> deptNameMap = buildDeptNameMap(errorMessages);
        DataFormatter formatter = new DataFormatter();
 
        try (Workbook workbook = WorkbookFactory.create(inputStream))
        {
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet == null || sheet.getLastRowNum() < 1)
            {
                throw new ServiceException("导入文件没有员工数据");
            }
            for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++)
            {
                Row row = sheet.getRow(rowIndex);
                if (isImportRowEmpty(row, formatter))
                {
                    continue;
                }
                try
                {
                    SysEmployeeAddRequest request = buildImportRequest(row, formatter, deptNameMap);
                    add(request, username);
                    successCount++;
                }
                catch (Exception e)
                {
                    errorMessages.add("第" + (rowIndex + 1) + "行:" + e.getMessage());
                }
            }
        }
        catch (ServiceException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new ServiceException("员工导入失败:" + e.getMessage());
        }
 
        if (successCount == 0 && errorMessages.isEmpty())
        {
            throw new ServiceException("导入文件没有有效员工数据");
        }
        if (errorMessages.isEmpty())
        {
            return "导入成功,共导入" + successCount + "条员工数据";
        }
        return "导入完成,成功" + successCount + "条,失败" + errorMessages.size() + "条;" + String.join(";", errorMessages);
    }
 
    private SysEmployee buildEmployee(Long deptId, String employeeName, String employeeNo, String contactPhone,
                                      String position, String cardNo, String faceImage, Boolean enabled,
                                      Long excludeEmployeeId)
    {
        Long requiredDeptId = getRequiredDeptId(deptId);
        String requiredName = getRequiredText(employeeName, "员工名称不能为空");
        String requiredNo = getRequiredText(employeeNo, "员工编号不能为空");
        String requiredPhone = getRequiredPhone(contactPhone);
        String normalizedPosition = normalizePosition(position);
        String phoneHash = EmployeePhoneCrypto.hash(requiredPhone);
        String normalizedCardNo = StringUtils.trim(cardNo);
 
        if (employeeMapper.checkEmployeeNoUnique(requiredNo, excludeEmployeeId) != null)
        {
            throw new ServiceException("员工编号已存在");
        }
        if (employeeMapper.checkPhoneUnique(phoneHash, excludeEmployeeId) != null)
        {
            throw new ServiceException("联系电话已存在");
        }
        if (StringUtils.isNotEmpty(normalizedCardNo) && employeeMapper.checkCardNoUnique(normalizedCardNo, excludeEmployeeId) != null)
        {
            throw new ServiceException("卡号已绑定其他员工");
        }
 
        SysEmployee employee = new SysEmployee();
        employee.setDeptId(requiredDeptId);
        employee.setEmployeeName(requiredName);
        employee.setEmployeeNo(requiredNo);
        employee.setContactPhoneCipher(EmployeePhoneCrypto.encrypt(requiredPhone));
        employee.setContactPhoneHash(phoneHash);
        employee.setPosition(normalizedPosition);
        employee.setCardNo(normalizedCardNo);
        employee.setFaceImage(StringUtils.trim(faceImage));
        employee.setEnabled(enabled == null ? Boolean.TRUE : enabled);
        return employee;
    }
 
    private Map<String, Long> buildDeptNameMap(List<String> errorMessages)
    {
        Map<String, Long> deptNameMap = new HashMap<String, Long>();
        Map<String, Integer> countMap = new HashMap<String, Integer>();
        List<SysDept> depts = deptMapper.selectDeptList(new SysDept());
        for (SysDept dept : depts)
        {
            String deptName = StringUtils.trim(dept.getDeptName());
            if (StringUtils.isEmpty(deptName))
            {
                continue;
            }
            countMap.put(deptName, countMap.containsKey(deptName) ? countMap.get(deptName) + 1 : 1);
            deptNameMap.put(deptName, dept.getDeptId());
        }
        for (Map.Entry<String, Integer> entry : countMap.entrySet())
        {
            if (entry.getValue() > 1)
            {
                errorMessages.add("部门名称重复:" + entry.getKey());
            }
        }
        return deptNameMap;
    }
 
    private SysEmployeeAddRequest buildImportRequest(Row row, DataFormatter formatter, Map<String, Long> deptNameMap)
    {
        String employeeName = getImportCell(row, 0, formatter);
        String employeeNo = getImportCell(row, 1, formatter);
        String contactPhone = getImportCell(row, 2, formatter);
        String deptName = getImportCell(row, 3, formatter);
        String position = normalizePosition(getImportCell(row, 4, formatter));
        String cardNo = getImportCell(row, 5, formatter);
        Long deptId = deptNameMap.get(deptName);
        if (deptId == null)
        {
            throw new ServiceException("所属部门不存在:" + deptName);
        }
 
        SysEmployeeAddRequest request = new SysEmployeeAddRequest();
        request.setEmployeeName(employeeName);
        request.setEmployeeNo(employeeNo);
        request.setContactPhone(contactPhone);
        request.setDeptId(deptId);
        request.setPosition(position);
        request.setCardNo(cardNo);
        request.setEnabled(Boolean.TRUE);
        return request;
    }
 
    private boolean isImportRowEmpty(Row row, DataFormatter formatter)
    {
        if (row == null)
        {
            return true;
        }
        for (int index = 0; index <= 5; index++)
        {
            if (StringUtils.isNotEmpty(getImportCell(row, index, formatter)))
            {
                return false;
            }
        }
        return true;
    }
 
    private String getImportCell(Row row, int index, DataFormatter formatter)
    {
        return StringUtils.trim(formatter.formatCellValue(row.getCell(index)));
    }
 
    private Long getRequiredDeptId(Long deptId)
    {
        if (deptId == null || deptId <= 0)
        {
            throw new ServiceException("所属部门不能为空");
        }
        SysDept dept = deptMapper.selectDeptById(deptId);
        if (dept == null)
        {
            throw new ServiceException("所属部门不存在");
        }
        return deptId;
    }
 
    private Long getRequiredEmployeeId(Long employeeId)
    {
        if (employeeId == null || employeeId <= 0)
        {
            throw new ServiceException("员工主键不能为空");
        }
        return employeeId;
    }
 
    private List<Long> getRequiredEmployeeIds(List<Long> employeeIds)
    {
        if (CollectionUtils.isEmpty(employeeIds))
        {
            throw new ServiceException("请选择员工");
        }
        for (Long employeeId : employeeIds)
        {
            getRequiredEmployeeId(employeeId);
        }
        return employeeIds;
    }
 
    private String getRequiredText(String value, String message)
    {
        String result = StringUtils.trim(value);
        if (StringUtils.isEmpty(result))
        {
            throw new ServiceException(message);
        }
        return result;
    }
 
    private String getRequiredPhone(String value)
    {
        String phone = EmployeePhoneCrypto.normalize(value);
        if (!MOBILE_PATTERN.matcher(phone).matches())
        {
            throw new ServiceException("联系电话必须为11位手机号");
        }
        return phone;
    }
 
    private String normalizePosition(String value)
    {
        return StringUtils.trim(value);
    }
 
    private void fillDisplayFields(SysEmployeeVO employee, boolean fullPhone)
    {
        String phone = EmployeePhoneCrypto.decrypt(employee.getContactPhone());
        employee.setContactPhone(fullPhone ? phone : maskPhone(phone));
        employee.setPositionName(employee.getPosition());
    }
 
    private String maskPhone(String phone)
    {
        if (phone == null || phone.length() < 7)
        {
            return "";
        }
        return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4);
    }
}