liusheng
2023-06-15 77d7257c02fc811a53b8d9207e4239f69c8a600c
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
package com.smartor.service.impl;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.mapper.BaseTagMapper;
import com.smartor.mapper.PatArchivetagMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.formula.functions.Now;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.smartor.mapper.PatArchiveMapper;
import com.smartor.service.IPatArchiveService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * 患者档案Service业务层处理
 *
 * @author smartor
 * @date 2023-03-04
 */
@Service
public class PatArchiveServiceImpl implements IPatArchiveService {
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
    @Autowired
    private BaseTagMapper baseTagMapper;
 
    @Autowired
    private PatArchivetagMapper patArchivetagMapper;
 
 
    /**
     * 查询患者档案
     *
     * @param patid 患者档案主键
     * @return 患者档案
     */
    @Override
    public PatArchive selectPatArchiveByPatid(Long patid) {
        return patArchiveMapper.selectPatArchiveByPatid(patid);
    }
 
    /**
     * 查询患者档案列表
     *
     * @param patArchive 患者档案
     * @return 患者档案
     */
    @Override
    public List<PatArchive> selectPatArchiveList(PatArchive patArchive) {
        return patArchiveMapper.selectPatArchiveList(patArchive);
    }
 
    /**
     * 新增患者档案
     *
     * @param patArchive 患者档案
     * @return 结果
     */
    @Override
    public int insertPatArchive(PatArchive patArchive) {
        patArchive.setCreateTime(DateUtils.getNowDate());
        return patArchiveMapper.insertPatArchive(patArchive);
    }
 
    /**
     * 修改患者档案
     *
     * @param patArchive 患者档案
     * @return 结果
     */
    @Override
    public int updatePatArchive(PatArchive patArchive) {
        patArchive.setUpdateTime(DateUtils.getNowDate());
        return patArchiveMapper.updatePatArchive(patArchive);
    }
 
    /**
     * 批量删除患者档案
     *
     * @param patids 需要删除的患者档案主键
     * @return 结果
     */
    @Override
    public int deletePatArchiveByPatids(Long[] patids) {
        return patArchiveMapper.deletePatArchiveByPatids(patids);
    }
 
    /**
     * 删除患者档案信息
     *
     * @param patid 患者档案主键
     * @return 结果
     */
    @Override
    public int deletePatArchiveByPatid(Long patid) {
        return patArchiveMapper.deletePatArchiveByPatid(patid);
    }
 
    @Override
    @Transactional
    public PatUpInfoVO importFilehandle(SysUser user, String tags, MultipartFile file) {
        PatUpInfoVO patUpInfoVO = new PatUpInfoVO();
        Integer successNum = 0;
        Integer failNum = 0;
 
        List<PatArchive> errorpatArchiveList = new ArrayList<>();
        try {
            Workbook workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(0);
 
            for (int i = sheet.getFirstRowNum() + 1; i < sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                //如果行为空,进行下一次循环
                if (ObjectUtils.isEmpty(row)) {
                    continue;
                }
                PatArchive patArchive = new PatArchive();
                if (ObjectUtils.isEmpty(row.getCell(3)) || StringUtils.isEmpty(row.getCell(3).toString())) {
                    addRemark("身份证号为空", patArchive);
                } else {
                    patArchive.setIccardno(row.getCell(3).toString());
                    //根据身份证,先去患者管理表里看看有没有这个人,如果有这个人,也不需要插入患者表
                    List<PatArchive> patArchiveList1 = patArchiveMapper.selectPatArchiveList(patArchive);
                    if (patArchiveList1.size() > 0) {
                        patArchive.setRemark("该患者已存在");
                    }
                }
 
                //判断姓名是否为空
                if (Objects.isNull(row.getCell(0)) || StringUtils.isEmpty(row.getCell(0).toString())) {
                    addRemark("姓名为空", patArchive);
                } else {
                    patArchive.setName(row.getCell(0).toString());
                }
 
                //判断性别是否为空
 
                if (ObjectUtils.isEmpty(row.getCell(1)) || StringUtils.isEmpty(row.getCell(1).toString())) {
                    addRemark("性别为空", patArchive);
                } else {
                    patArchive.setSex(row.getCell(1).toString().equals("男") ? 1L : 2L);
                }
 
                //判断证件类型是否为空
                if (ObjectUtils.isEmpty(row.getCell(2)) || StringUtils.isEmpty(row.getCell(2).toString())) {
                    addRemark("证件类型为空", patArchive);
                } else {
                    patArchive.setIccardtype(row.getCell(2).toString());
                }
 
                //判断出生日期是否为空
                if (ObjectUtils.isEmpty(row.getCell(4)) || StringUtils.isEmpty(row.getCell(4).toString())) {
                    addRemark("出生日期为空", patArchive);
                } else {
                    //格式转换,转成日期
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    Date javaDate = new Date();
                    if (row.getCell(4).getCellType().toString().equals("NUMERIC")) {
                        javaDate = HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue());
                    } else {
                        javaDate = new Date(row.getCell(4).toString());
                    }
                    dateFormat.format(javaDate);
                    patArchive.setBirthdate(javaDate);
                }
 
                //判断本人联系是否为空,和长度是否正确
                if (ObjectUtils.isEmpty(row.getCell(5)) || StringUtils.isEmpty(row.getCell(5).toString())) {
                    addRemark("本人联系电话为空", patArchive);
                } else {
                    //格式转换,转成文本
                    if (row.getCell(5).getCellType().toString().equals("NUMERIC")) {
                        String cellData = String.valueOf((long) row.getCell(5).getNumericCellValue());
                        patArchive.setTelcode(cellData);
                    } else {
                        DataFormatter dataFormatter = new DataFormatter();
                        String cellValue = dataFormatter.formatCellValue(row.getCell(5));
                        patArchive.setTelcode(cellValue);
                    }
                }
 
                //判断亲属联系方式是否为空,长度是否正确
                if (ObjectUtils.isEmpty(row.getCell(6)) || StringUtils.isEmpty(row.getCell(6).toString())) {
                    addRemark("亲属联系方式为空", patArchive);
                } else {
                    //格式转换,转成文本
                    if (row.getCell(6).getCellType().toString().equals("NUMERIC")) {
                        String cellData = String.valueOf((long) row.getCell(6).getNumericCellValue());
                        patArchive.setRelativetelcode(cellData);
                    } else {
                        DataFormatter dataFormatter = new DataFormatter();
                        String cellValue = dataFormatter.formatCellValue(row.getCell(6));
                        patArchive.setRelativetelcode(cellValue);
                    }
                }
 
                //患都标签是否为空
                if (ObjectUtils.isEmpty(row.getCell(7)) || StringUtils.isEmpty(row.getCell(7).toString())) {
                    if (StringUtils.isEmpty(tags)) {
                        addRemark("患者标签为空", patArchive);
                    } else {
                        patArchive.setTag(tags);
 
                    }
                } else {
                    if (StringUtils.isNotEmpty(tags)) {
                        patArchive.setTag(row.getCell(7).toString() + "," + tags);
                    } else {
                        patArchive.setTag(row.getCell(7).toString());
                    }
                }
 
                //判断备注是否为空
                if (!StringUtils.isEmpty(patArchive.getRemark())) {
                    //如果备注字段不为空,说有该患者填写有问题
                    errorpatArchiveList.add(patArchive);
                    failNum = failNum + 1;
                    continue;
                }
 
                //往患者表里新增,并获取到新增ID
                patArchiveMapper.insertPatArchive(patArchive);
 
                //根据标签名查询出标签信息
                String s = patArchive.getTag();
                String[] split = s.split(",");
                for (String tagName : split) {
                    BaseTag baseTag = new BaseTag();
                    baseTag.setTagname(tagName);
                    List<BaseTag> baseTags = baseTagMapper.selectBaseTagList(baseTag);
 
                    //如果该标签为空,现标签管理没有出现过的新标签时,自动将标签添加到"标签管理"未分类“中。编辑人为导入账号
                    if (CollectionUtils.isEmpty(baseTags)) {
                        baseTag = new BaseTag();
                        baseTag.setTagcategoryid(1L);
                        baseTag.setTagname(tagName);
                        baseTag.setOrgid(user.getDeptId().toString());
                        baseTag.setDelFlag("0");
                        baseTag.setCreateBy(user.getUserName());
                        baseTag.setCreateTime(new Date());
                        baseTag.setCreateBy(user.getUserName());
                        baseTag.setCreateTime(new Date());
                        baseTagMapper.insertBaseTag(baseTag);
                    } else {
                        baseTag = baseTags.get(0);
                    }
 
                    // 新增患者档案标签
                    PatArchivetag patArchivetag = DtoConversionUtils.sourceToTarget(baseTag, PatArchivetag.class);
                    patArchivetag.setUpdateBy(user.getUserName());
                    patArchivetag.setCreateTime(new Date());
                    patArchivetag.setPatid(patArchive.getPatid());
                    patArchivetagMapper.insertPatArchivetag(patArchivetag);
                }
                successNum = successNum + 1;
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        patUpInfoVO.setFailNum(failNum);
        patUpInfoVO.setSuccessNum(successNum);
        patUpInfoVO.setPatArchiveList(errorpatArchiveList);
        return patUpInfoVO;
    }
 
    @Override
    public String exportErrPatInfo(List<PatArchive> patArchiveList) {
        // 创建工作簿和工作表
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");
        // 创建行和单元格,并设置单元格的值
        Row row = sheet.createRow(0);
        getdata(row, null, true);
 
        for (int i = 0; i < patArchiveList.size(); i++) {
            row = sheet.createRow(i + 1);
            getdata(row, patArchiveList.get(i), false);
        }
        // 保存工作簿到文件
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("患者错误信息.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
            return "导出失败";
        }
        return "导出成功";
    }
 
    @Override
    public List<PatArchive> patInfoByContion(PatArchiveReq patArchive) {
        List<PatArchive> patArchives = new ArrayList<>();
 
        //根据条件获取患者信息
        List<PatArchive> patArchiveList = patArchiveMapper.patInfoByContion(patArchive);
        //根据患者ID进行分组
        Map<Long, List<PatArchive>> listMap = patArchiveList.stream().collect(Collectors.groupingBy(PatArchive::getPatid));
 
        //对数据进行封装
        for (List<PatArchive> list : listMap.values()) {
            PatArchive patArchive1 = new PatArchive();
            List<String> stringList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    patArchive1 = DtoConversionUtils.sourceToTarget(list.get(0), PatArchive.class);
                    //将标签置空,不空也没有问题
                    patArchive1.setTag("");
                }
                //将查出的tag,放到patArchive1里的TagList中
                stringList.add(list.get(i).getTag());
            }
            patArchive1.setTagList(stringList);
            patArchives.add(patArchive1);
        }
 
        return patArchives;
    }
 
    @Override
    public List<PatArchivetagAndPatientInfo> patTagByContion(List<Long> tagids) {
        List<PatArchivetagAndPatientInfo> patArchivetagAndPatientInfos = new ArrayList<>();
        if (tagids.size() > 0) {
            for (int i = 0; i < tagids.size(); i++) {
                PatArchivetag patArchivetag = new PatArchivetag();
                patArchivetag.setTagid(tagids.get(i));
                //获取患者patid
                List<PatArchivetag> patArchivetags1 = patArchivetagMapper.selectPatArchivetagAndBaseTagList(patArchivetag);
                for (int j = 0; j < patArchivetags1.size(); j++) {
                    PatArchivetagAndPatientInfo patArchivetagAndPatientInfo = DtoConversionUtils.sourceToTarget(patArchivetags1.get(j), PatArchivetagAndPatientInfo.class);
                    PatArchive patArchive = new PatArchive();
                    patArchive.setPatid(patArchivetagAndPatientInfo.getPatid());
                    List<PatArchive> patArchiveList = patArchiveMapper.selectPatArchiveList(patArchive);
                    patArchivetagAndPatientInfo.setPatNum(patArchiveList.size());
                    // patArchivetagAndPatientInfo.setPatArchives(patArchiveList);
                    patArchivetagAndPatientInfos.add(patArchivetagAndPatientInfo);
                }
            }
        } else {
            PatArchivetag patArchivetag = new PatArchivetag();
            //获取患者patid
            List<PatArchivetag> patArchivetags1 = patArchivetagMapper.selectPatArchivetagAndBaseTagList(patArchivetag);
            for (int j = 0; j < patArchivetags1.size(); j++) {
                PatArchivetagAndPatientInfo patArchivetagAndPatientInfo = DtoConversionUtils.sourceToTarget(patArchivetags1.get(j), PatArchivetagAndPatientInfo.class);
                PatArchive patArchive = new PatArchive();
                patArchive.setPatid(patArchivetagAndPatientInfo.getPatid());
                List<PatArchive> patArchiveList = patArchiveMapper.selectPatArchiveList(patArchive);
                //  patArchivetagAndPatientInfo.setPatArchives(patArchiveList);
                patArchivetagAndPatientInfo.setPatNum(patArchiveList.size());
                patArchivetagAndPatientInfos.add(patArchivetagAndPatientInfo);
            }
        }
        return patArchivetagAndPatientInfos;
    }
 
    @Override
    public List<PatArchivetagAndPatientInfo> patInfoByTag(List<Long> tagids) {
 
 
 
 
 
        return null;
    }
 
    private void getdata(Row row, PatArchive patArchive, Boolean head) {
        if (head) {
            row.createCell(0).setCellValue("姓名");
            row.createCell(1).setCellValue("性别");
            row.createCell(2).setCellValue("证件类型");
            row.createCell(3).setCellValue("证件号码");
            row.createCell(4).setCellValue("出生日期");
            row.createCell(5).setCellValue("本人联系方式");
            row.createCell(6).setCellValue("亲属联系方式");
            row.createCell(7).setCellValue("患者标签");
            row.createCell(8).setCellValue("错误原因");
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            row.createCell(0).setCellValue(patArchive.getName());
            row.createCell(1).setCellValue(patArchive.getSex() == 1 ? "男" : "女");
            row.createCell(2).setCellValue(patArchive.getIccardtype());
            row.createCell(3).setCellValue(patArchive.getIccardno());
            if (ObjectUtils.isNotEmpty(patArchive.getBirthdate())) {
                row.createCell(4).setCellValue(simpleDateFormat.format(patArchive.getBirthdate()).toString());
            }
            row.createCell(5).setCellValue(patArchive.getTelcode());
            row.createCell(6).setCellValue(patArchive.getRelativetelcode());
            row.createCell(7).setCellValue(patArchive.getTag());
            row.createCell(8).setCellValue(patArchive.getRemark());
 
 
        }
 
    }
 
    public void addRemark(String info, PatArchive patArchive) {
        if (StringUtils.isNotEmpty(patArchive.getRemark()) == true) {
            patArchive.setRemark(patArchive.getRemark() + "," + info);
        } else {
            patArchive.setRemark(info);
        }
    }
 
}