yz
2021-04-06 8f27785e23d7551483041f6481b7054e4d91656f
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package cc.mrbird.febs.server.hr.service.impl;
 
import cc.mrbird.febs.common.core.constant.ModuleCode;
import cc.mrbird.febs.common.core.entity.QueryRequest;
import cc.mrbird.febs.common.core.exception.FebsException;
import cc.mrbird.febs.common.core.utils.FebsUtil;
import cc.mrbird.febs.common.core.utils.MyUtil;
import cc.mrbird.febs.common.core.utils.SequenceUtil;
import cc.mrbird.febs.server.hr.entity.EmpAccessory;
import cc.mrbird.febs.server.hr.entity.EmpBaseInfo;
import cc.mrbird.febs.server.hr.entity.Label;
import cc.mrbird.febs.server.hr.feign.IRemoteDeptService;
import cc.mrbird.febs.server.hr.mapper.EmpAccessoryMapper;
import cc.mrbird.febs.server.hr.properties.FebsServerHrProperties;
import cc.mrbird.febs.server.hr.service.IEmpAccessoryService;
import cc.mrbird.febs.server.hr.service.IEmpBaseInfoService;
import cc.mrbird.febs.server.hr.service.ILabelService;
import cc.mrbird.febs.server.hr.vo.EmpAccessoryVO;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
 
/**
 * 附件管理 Service实现
 *
 * @author yz
 * @date 2021-02-15 17:07:19
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Slf4j
public class EmpAccessoryServiceImpl extends ServiceImpl<EmpAccessoryMapper, EmpAccessory> implements IEmpAccessoryService {
    private final ILabelService labelService;
    private final EmpAccessoryMapper empAccessoryMapper;
    private final FebsServerHrProperties properties;
    private final IRemoteDeptService remoteDeptService;
 
    private final IEmpBaseInfoService empBaseInfoService;
    /**
     * 参数2为数据中心ID 参数1为终端ID
     */
    private Snowflake snowflake = IdUtil.getSnowflake(ModuleCode.HR_FIlE, 1);
 
    @Override
    public IPage<Map<String, Object>> findEmpAccessorys(QueryRequest request, EmpAccessoryVO vo) {
        QueryWrapper<EmpAccessoryVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.delFlag", 0);
        if (StrUtil.isNotBlank(vo.getBasic())) {
            queryWrapper.and(p -> {
                p.like("a.empNumb", vo.getBasic())
                        .or().like("a.allDeptName", vo.getBasic())
                        .or().like("a.certificateNumb", vo.getBasic())
                        .or().like("a.empName", vo.getBasic())
                        .or().like("a.jobName", vo.getBasic());
            });
        }
        if (StrUtil.isNotBlank(vo.getEmpName())) {
            queryWrapper.like("a.empName", vo.getEmpName());
        }
        if (StrUtil.isNotBlank(vo.getEmpNumb())) {
            queryWrapper.like("a.empNumb", vo.getEmpNumb());
        }
        if (StrUtil.isNotBlank(vo.getCertificateNumb())) {
            queryWrapper.like("a.certificateNumb", vo.getCertificateNumb());
        }
        if (StrUtil.isNotBlank(vo.getDeptName())) {
            queryWrapper.like("a.allDeptName", vo.getDeptName());
        }
 
        if (StrUtil.isNotBlank(vo.getEntryDate())) {
            queryWrapper.between("a.entryDate", vo.getEntryDate().split(",")[0], vo.getEntryDate().split(",")[1]);
        }
        if (StrUtil.isNotBlank(vo.getAge())) {
            String[] arr = vo.getAge().split(",");
            for (String s : arr) {
                if (StrUtil.equals(s, "18-29")) {
                    queryWrapper.or().between("a.age", 18, 29);
                }
                if (StrUtil.equals(s, "30-39")) {
                    queryWrapper.or().between("a.age", 30, 39);
                }
                if (StrUtil.equals(s, "40-49")) {
                    queryWrapper.or().between("a.age", 40, 49);
                }
                if (StrUtil.equals(s, "50-59")) {
                    queryWrapper.or().between("a.age", 50, 59);
                }
                if (StrUtil.equals(s, "60-69")) {
                    queryWrapper.or().between("a.age", 60, 69);
                }
                if (StrUtil.equals(s, "69-999")) {
                    queryWrapper.or().ge("a.age", 69);
                }
            }
 
        }
        if (StrUtil.isNotBlank(vo.getArchivesStatus())) {
            queryWrapper.in("a.archivesStatus", vo.getArchivesStatus().split(","));
        }
        if (StrUtil.isNotBlank(vo.getEducation())) {
            queryWrapper.in("a.education", vo.getEducation().split(","));
        }
        if (StrUtil.isNotBlank(vo.getSex())) {
            queryWrapper.in("a.sex", vo.getSex().split(","));
        }
        if (StrUtil.isNotBlank(vo.getInsuranceType())) {
            queryWrapper.in("a.insuranceType", vo.getInsuranceType().split(","));
        }
        if (StrUtil.isNotBlank(vo.getPolitics())) {
            queryWrapper.in("a.politics", vo.getPolitics().split(","));
        }
        if (StrUtil.isNotBlank(vo.getEmpStatus())) {
            queryWrapper.in("a.empStatus", vo.getEmpStatus().split(","));
        }
        if (StrUtil.isNotBlank(vo.getPolitics())) {
            queryWrapper.in("a.politics", vo.getPolitics().split(","));
        }
        if (StringUtils.isNotBlank(vo.getHandbookStatus())) {
            queryWrapper.in("a.HandbookStatus", vo.getHandbookStatus().split(","));
        }
        if (StringUtils.isNotBlank(vo.getEmpCardStatus())) {
            queryWrapper.in("a.EmpCardStatus", vo.getEmpCardStatus().split(","));
        }
        if (StringUtils.isNotBlank(vo.getEmpType())) {
            queryWrapper.in("a.empType", vo.getEmpType().split(","));
        }
        if (StringUtils.isNotBlank(vo.getEntryType())) {
            queryWrapper.in("a.entryType", vo.getEntryType().split(","));
        }
        queryWrapper.inSql("c.dept_Id", remoteDeptService.userRightDepts());
        if (StrUtil.isNotBlank(vo.getFileName())){
            queryWrapper.like("acc.filesName", vo.getFileName());
        }
        if (StrUtil.equals(vo.getField(),"empName")){
            if (StrUtil.equals(vo.getOrder(),"descending")){
                queryWrapper.orderByDesc("a.empName");
            }else{
                queryWrapper.orderByAsc("a.empName");
            }
        }else if(StrUtil.equals(vo.getField(),"empNumb")){
            if (StrUtil.equals(vo.getOrder(),"descending")){
                queryWrapper.orderByDesc("a.empNumb");
            }else{
                queryWrapper.orderByAsc("a.empNumb");
            }
        }else if(StrUtil.equals(vo.getField(),"deptName")){
            if (StrUtil.equals(vo.getOrder(),"descending")){
                queryWrapper.orderByDesc("deptName");
            }else{
                queryWrapper.orderByAsc("deptName");
            }
        }else if(StrUtil.equals(vo.getField(),"jobName")){
            if (StrUtil.equals(vo.getOrder(),"descending")){
                queryWrapper.orderByDesc("a.jobName");
            }else{
                queryWrapper.orderByAsc("a.jobName");
            }
        }else if(StrUtil.equals(vo.getField(),"certificateNumb")){
            if (StrUtil.equals(vo.getOrder(),"descending")){
                queryWrapper.orderByDesc("a.certificateNumb");
            }else{
                queryWrapper.orderByAsc("a.certificateNumb");
            }
        }
 
        queryWrapper.groupBy(" a.delFlag,a.empName,a.empStatus,a.empId,a.certificateNumb,a.empNumb,deptName,a.jobName");
        Page<EmpAccessory> page = new Page<>(request.getPageNum(), request.getPageSize());
       // String querySql = empAccessoryMapper.getQuerySql();
       //List<Label> labelList = labelService.findLabels(new Label());
        IPage<Map<String, Object>> mapIPage = empAccessoryMapper.selectPageVo(page, queryWrapper);
        mapIPage.getRecords().parallelStream().forEach(i->{
            List<Map<String, Object>> mapList = this.baseMapper.getEmpAccessoryNumber((Long) i.get("empId"));
            mapList.parallelStream().forEach(j->i.put(j.get("labelCode").toString(),Integer.parseInt(j.get("number").toString())));
        });
        return mapIPage;
    }
 
    @Override
    public List<EmpAccessory> findEmpAccessorys(EmpAccessory empAccessory) {
        LambdaQueryWrapper<EmpAccessory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpAccessory::getDelFlag, 0);
        if (null != empAccessory.getLabelid()) {
            queryWrapper.eq(EmpAccessory::getLabelid, empAccessory.getLabelid());
        }
        if (StrUtil.isNotBlank(empAccessory.getFilesname())) {
            queryWrapper.like(EmpAccessory::getFilesname, empAccessory.getFilesname());
        }
        if (null != empAccessory.getEmpid()) {
            queryWrapper.eq(EmpAccessory::getEmpid, empAccessory.getEmpid());
        }
        return this.baseMapper.selectList(queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createEmpAccessory(Long labelid, MultipartFile file, Long empId) throws FebsException, IOException {
        EmpBaseInfo empBaseInfo = empBaseInfoService.getById(empId);
        String nameAndCertificateNumb = empBaseInfo.getEmpName() + "_" + empBaseInfo.getCertificateNumb() + "/";
        Dict dict = MyUtil.filesUpload(file, properties.getUploadSinglePath() + nameAndCertificateNumb, snowflake.nextIdStr());
        EmpAccessory accessory = new EmpAccessory();
        accessory.setCreatetime(new Date());
        accessory.setCreator(FebsUtil.getUserId());
        accessory.setDelFlag(0);
        accessory.setEmpid(empId);
        accessory.setFilesaddress(dict.getStr("newName"));
        accessory.setFilesformat(StrUtil.isBlank(dict.getStr("suffix")) ? dict.getStr("suffix") : dict.getStr("suffix").substring(1));
        accessory.setAccessoryid(SequenceUtil.generateId(0L, ModuleCode.HR_FIlE));
        accessory.setFilesname(dict.getStr("fileName"));
        accessory.setLabelid(labelid);
        accessory.setModifier(FebsUtil.getUserId());
        accessory.setModifytime(new Date());
        accessory.setVersion(0);
        this.save(accessory);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmpAccessory(EmpAccessory empAccessory) throws FebsException {
        if ( empAccessory.getFilesname().lastIndexOf(".")<=0){
            throw  new FebsException("文件名格式错误");
        }
        LambdaUpdateWrapper<EmpAccessory> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EmpAccessory::getAccessoryid,empAccessory.getAccessoryid())
                .set(EmpAccessory::getFilesname,empAccessory.getFilesname()).set(EmpAccessory::getFilesformat,  empAccessory.getFilesname().substring( empAccessory.getFilesname().lastIndexOf(".")+1));
        this.update(wrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmpAccessory(String accessoryids) {
        String[] arr = accessoryids.split(",");
        LambdaUpdateWrapper<EmpAccessory> wapper = new LambdaUpdateWrapper<>();
        wapper.in(EmpAccessory::getAccessoryid, arr);
        wapper.set(EmpAccessory::getDelFlag, 1);
        wapper.set(EmpAccessory::getModifier, FebsUtil.getUserId());
        wapper.set(EmpAccessory::getModifytime, new Date());
        this.update(wapper);
 
        LambdaQueryWrapper<EmpAccessory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EmpAccessory::getAccessoryid, arr);
        List<EmpAccessory> list = this.list(queryWrapper);
        list.parallelStream().forEach(i -> {
            EmpBaseInfo empBaseInfo = empBaseInfoService.getById(i.getEmpid());
            String nameAndCertificateNumb = empBaseInfo.getEmpName() + "_" + empBaseInfo.getCertificateNumb() + "/";
            FileUtil.del(properties.getUploadSinglePath() + nameAndCertificateNumb + "/" + i.getFilesaddress());
        });
    }
 
 
    @Override
    public void download(String accessoryids, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = accessoryids.split(",");
        List<File> files = new ArrayList<>();
        List<String> fileName = new ArrayList<>();
        Arrays.stream(arr).forEach(i -> {
            EmpAccessory accessory = this.getById(i);
            EmpBaseInfo empBaseInfo = empBaseInfoService.getById(accessory.getEmpid());
            String nameAndCertificateNumb = empBaseInfo.getEmpName() + "_" + empBaseInfo.getCertificateNumb() + "/";
            files.add(new File(properties.getUploadSinglePath() + nameAndCertificateNumb + accessory.getFilesaddress()));
            if (fileName.parallelStream().filter(filter -> StrUtil.equals(accessory.getFilesname(), filter)).count() == 0) {
                fileName.add(accessory.getFilesname());
            } else {
                fileName.add(accessory.getFilesname().substring(0,accessory.getFilesname().lastIndexOf(".")) + fileName.parallelStream().filter(filter -> StrUtil.equals(accessory.getFilesname(), filter)).count()+"."+accessory.getFilesformat());
            }
        });
        MyUtil.download(request, response, files, fileName);
 
    }
 
    @Override
    public void mvFiles(String accessoryids, Long labelid, Long empId) {
        String[] arr = accessoryids.split(",");
        LambdaUpdateWrapper<EmpAccessory> wapper = new LambdaUpdateWrapper<>();
        wapper.in(EmpAccessory::getAccessoryid, arr);
        wapper.set(EmpAccessory::getModifier, FebsUtil.getUserId());
        wapper.set(EmpAccessory::getModifytime, new Date());
        wapper.set(EmpAccessory::getLabelid, labelid);
        this.update(wapper);
 
    }
 
    @Override
    public void singledownload(String empIds, Long labelid, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] arr = empIds.split(",");
        //主文件夹
        String mianFileStr = properties.getUploadSinglePath() + snowflake.nextIdStr() + "/";
        FileUtil.mkdir(mianFileStr);
        Arrays.stream(arr).forEach(i -> {
            LambdaQueryWrapper<EmpAccessory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(EmpAccessory::getEmpid, i);
            lambdaQueryWrapper.eq(EmpAccessory::getDelFlag, 0);
            if (!labelid.equals(-1L)) {
                lambdaQueryWrapper.eq(EmpAccessory::getLabelid, labelid);
            }
            List<EmpAccessory> accessory = this.list(lambdaQueryWrapper);
            EmpBaseInfo empBaseInfo = empBaseInfoService.getById(i);
            String nameAndCertificateNumb = empBaseInfo.getEmpName() + "_" + empBaseInfo.getCertificateNumb() + "/";
            //创建个人文件夹
            String singlefileStr = mianFileStr + nameAndCertificateNumb;
            FileUtil.mkdir(singlefileStr);
            accessory.stream().forEach(a -> {
                Label label = labelService.getById(a.getLabelid());
                FileUtil.copy(properties.getUploadSinglePath() + nameAndCertificateNumb + a.getFilesaddress(), singlefileStr + label.getLabelname() + "/" + a.getFilesname(), true);
            });
        });
        ZipUtil.zip(mianFileStr);
        try {
            MyUtil.downloadFile(new File(new StringBuilder().append(mianFileStr, 0, mianFileStr.length() - 1).append(".zip").toString()), response);
        } catch (IOException e) {
            log.error("下载文件异常", e);
        }
        FileUtil.del(new StringBuilder().append(mianFileStr, 0, mianFileStr.length() - 1).append(".zip").toString());
        FileUtil.del(mianFileStr);
    }
 
 
    @Override
    public Integer getLabelCount(Long labelId) {
        LambdaQueryWrapper<EmpAccessory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpAccessory::getDelFlag, 0);
        wrapper.eq(EmpAccessory::getLabelid, labelId);
        return this.count(wrapper);
    }
 
    @Override
    public void autoInstall(String fileids) {
        File[] list = FileUtil.ls(fileids);
        for (File file : list) {
            String[] fileName = file.getName().split("_");
            if (fileName.length<2){
                log.error(file.getName());
                continue;
            }
            List<EmpBaseInfo> empBaseInfo = empBaseInfoService.list(new LambdaQueryWrapper<EmpBaseInfo>().eq(EmpBaseInfo::getCertificateNumb, fileName[1]).ne(EmpBaseInfo::getDelFlag, 1));
            if (null == empBaseInfo ||0 == empBaseInfo.size()) {
                continue;
            }
            File[] childlist = FileUtil.ls(fileids+"/"+file.getName());
            for (File file1 : childlist) {
                EmpAccessory accessory = new EmpAccessory();
                if (file1.getName().indexOf("外皮")>=0||file1.getName().indexOf("外批")>=0){
                    accessory.setLabelid(142445285230593L);
                }else if (file1.getName().indexOf("心电图")>=0){
                    accessory.setLabelid(142445218121729L);
                }else if (file1.getName().indexOf("体检")>=0){
                    accessory.setLabelid(142445218121729L);
                }else if (file1.getName().indexOf("合同")>=0){
                    accessory.setLabelid(142445285230593L);
                }else if (file1.getName().indexOf("入职")>=0||file1.getName().indexOf("务工")>=0){
                    accessory.setLabelid(143073554221057L);
                }else if (file1.getName().indexOf("身份")>=0){
                    accessory.setLabelid(143007818991617L);
                }else if (file1.getName().indexOf("银行卡")>=0){
                    accessory.setLabelid(143073768130561L);
                }else if (file1.getName().indexOf("回执")>=0){
                    accessory.setLabelid(143073617135617L);
                }else if (file1.getName().indexOf("证")>=0){
                    accessory.setLabelid(143073812170753L);
                }else{
                    accessory.setLabelid(143073866696705L);
                }
 
                accessory.setCreatetime(new Date());
                accessory.setCreator(FebsUtil.getUserId());
                accessory.setDelFlag(0);
                accessory.setEmpid(empBaseInfo.get(0).getEmpId());
                accessory.setFilesaddress(file1.getName());
                if (file1.getName().lastIndexOf(".") > 0) {
                    accessory.setFilesformat( file1.getName().substring(file1.getName().lastIndexOf(".")+1));
                }
                accessory.setAccessoryid(SequenceUtil.generateId(0L, ModuleCode.HR_FIlE));
                accessory.setFilesname(file1.getName());
                accessory.setModifier(FebsUtil.getUserId());
                accessory.setModifytime(new Date());
                accessory.setVersion(0);
                this.save(accessory);
            }
 
        }
    }
 
    @Override
    public void getImage(String accessoryid, HttpServletResponse response) throws Exception {
        EmpAccessory empAccessory = this.getById(accessoryid);
        if (StrUtil.isBlank(empAccessory.getFilesaddress())) {
            return;
        }
        EmpBaseInfo empBaseInfo = empBaseInfoService.getById(empAccessory.getEmpid());
        String nameAndCertificateNumb = empBaseInfo.getEmpName() + "_" + empBaseInfo.getCertificateNumb() + "/";
        String path = properties.getUploadSinglePath() + nameAndCertificateNumb + empAccessory.getFilesaddress();
        try (InputStream inputStream = new FileInputStream(path); OutputStream out = response.getOutputStream()) {
            //byte数组用于存放图片字节数据
            byte[] buff = new byte[inputStream.available()];
            inputStream.read(buff);
            inputStream.close();
            if (empAccessory.getFilesformat().equals("tif")){
                //设置发送到客户端的响应内容类型
                response.setContentType("image/tiff");
            }else if (empAccessory.getFilesformat().equals("bmp")){
                response.setContentType("application/x-bmp");
            }else if (empAccessory.getFilesformat().equals("jpg")){
                response.setContentType("image/jpeg");
            }else if (empAccessory.getFilesformat().equals("png")){
                response.setContentType("image/png");
            }else if (empAccessory.getFilesformat().equals("gif")){
                response.setContentType("image/gif");
            }
            out.write(buff);
        }
    }
}