zhs
2025-04-22 ad5f737d5fa1265e956317bce7e6ed69a5973241
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
package com.smartor.service.impl;
 
import java.util.*;
 
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUserDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smartor.service.IPatMedOuthospService;
import org.springframework.util.CollectionUtils;
 
/**
 * 患者门诊记录Service业务层处理
 *
 * @author smartor
 * @date 2023-03-04
 */
@Slf4j
@Service
public class PatMedOuthospServiceImpl implements IPatMedOuthospService {
    @Autowired
    private PatMedOuthospMapper patMedOuthospMapper;
 
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
 
    @Autowired
    private ServiceSubtaskMapper serviceSubtaskMapper;
 
    @Autowired
    private ServiceTaskMapper serviceTaskMapper;
 
    @Autowired
    private ServiceTaskdiagMapper serviceTaskdiagMapper;
 
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
 
    /**
     * 查询患者门诊记录
     *
     * @param id 患者门诊记录主键
     * @return 患者门诊记录
     */
    @Override
    public PatMedOuthosp selectPatMedOuthospById(Long id) {
        return patMedOuthospMapper.selectPatMedOuthospById(id);
    }
 
    /**
     * 查询患者门诊记录列表
     *
     * @param patMedOuthosp 患者门诊记录
     * @return 患者门诊记录
     */
    @Override
    public List<PatMedOuthosp> selectPatMedOuthospList(PatMedOuthosp patMedOuthosp) {
        List<PatMedOuthosp> patMedOuthosps = patMedOuthospMapper.selectPatMedOuthospList(patMedOuthosp);
        return patMedOuthosps;
    }
 
    /**
     * 新增患者门诊记录
     *
     * @param patMedOuthosp 患者门诊记录
     * @return 结果
     */
    @Override
    public int insertPatMedOuthosp(PatMedOuthosp patMedOuthosp) {
        patMedOuthosp.setCreateTime(DateUtils.getNowDate());
        patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
        return patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp);
    }
 
    /**
     * 修改患者门诊记录
     *
     * @param patMedOuthosp 患者门诊记录
     * @return 结果
     */
    @Override
    public int updatePatMedOuthosp(PatMedOuthosp patMedOuthosp) {
        patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
        return patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp);
    }
 
    /**
     * 批量删除患者门诊记录
     *
     * @param ids 需要删除的患者门诊记录主键
     * @return 结果
     */
    @Override
    public int deletePatMedOuthospByIds(Long[] ids) {
        return patMedOuthospMapper.deletePatMedOuthospByIds(ids);
    }
 
    /**
     * 删除患者门诊记录信息
     *
     * @param id 患者门诊记录主键
     * @return 结果
     */
    @Override
    public int deletePatMedOuthospById(Long id) {
        return patMedOuthospMapper.deletePatMedOuthospById(id);
    }
 
    @Override
    public PatMedRes selectPatMedOuthospCount(PatMedReq patMedReq) {
        // 获取当前登陆人的部门权限
        if (CollectionUtils.isEmpty(patMedReq.getDeptcodeList())) {
            Long userId = SecurityUtils.getUserId();
            List<SysDept> sysDepts = sysUserDeptMapper.selectDeptListByUserId(userId);
            List<String> deptCode = new ArrayList<>();
            for (SysDept sysDept : sysDepts) {
                deptCode.add(sysDept.getDeptId().toString());
            }
            patMedReq.setDeptcodeList(deptCode);
        }
        return patMedOuthospMapper.selectPatMedOuthospCount(patMedReq);
    }
 
    @Override
    public PatMedOuthosp getDeptCodeByPatId(PatMedOuthosp patMedOuthosp) {
        List<PatMedOuthosp> patMedOuthosps = selectPatMedOuthospList(patMedOuthosp);
        if (!CollectionUtils.isEmpty(patMedOuthosps)) {
            Collections.sort(patMedOuthosps, Comparator.comparing(PatMedOuthosp::getAdmitdate).reversed());
        }
        return patMedOuthosps.get(0);
    }
 
    /**
     * 门诊病人信息处理
     *
     * @return
     */
    @Override
    public Integer dealOutpatientInfo() {
        PatMedOuthosp patMedOuthosp = new PatMedOuthosp();
        patMedOuthosp.setDiagcheckFlag("0");
        List<PatMedOuthosp> patMedOuthosps = selectPatMedOuthospList(patMedOuthosp);
        for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) {
            // 根据患者的疾病,获取该疾病的长期任务
            ServiceTaskdiag serviceTaskdiag = new ServiceTaskdiag();
            serviceTaskdiag.setLongtask(1L);
            serviceTaskdiag.setIcd10code(patMedOuthosp1.getIcd10code());
            List<ServiceTaskdiag> serviceTaskdiags = serviceTaskdiagMapper.selectServiceTaskdiagList(serviceTaskdiag);
            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patMedOuthosp1.getPatid());
            //如果部门模板为空(将deptIsNull设置为true)
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTaskdiags) || serviceTaskdiags.size() == 0) {
                patMedOuthosp1.setDiagcheckFlag("2");
                patMedOuthosp1.setRemark("通过icd10,没有找到随访任务ID");
                patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
            } else {
                for (ServiceTaskdiag serviceTaskdept1 : serviceTaskdiags) {
                    writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedOuthosp1, patArchive);
                }
            }
 
        }
        return 1;
    }
 
    private void writeInSubTask(Long taskid, Boolean check, PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
 
        ServiceTask st = new ServiceTask();
        st.setTaskid(taskid);
        st.setSendState(2L);
        List<ServiceTask> serviceTasks = serviceTaskMapper.selectServiceTaskList(st);
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTasks)) {
            log.error("该患者疾病随访长期任务不存在,任务ID为:{}", taskid);
            patMedOuthosp.setDiagcheckFlag("2");
            patMedOuthosp.setRemark("该患者疾病随访长期任务不存在,任务ID为:" + taskid);
            patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp);
            return;
        }
        ServiceTask serviceTask = serviceTasks.get(0);
        //封装serviceSubtask
        ServiceSubtask serviceSubtask = boxedServiceSubtask(serviceTask, patMedOuthosp, patArchive);
        Integer i = 0;
        //先判断一下,是否需要校验
        if (check) {
            //在新增之前,先通过患者ID,sendstate=2查询一下,在所有长期任务中,是不是还有该患者待执行的任务,有的话,比较之前的endtime是否小于当前的endtaime,如果之前的小于现在的,则直接将之前的停掉(原因再入院)
            ServiceSubtaskVO subtask = new ServiceSubtaskVO();
            subtask.setPatid(patArchive.getId());
            subtask.setSendstate(2L);
            subtask.setTaskid(taskid);
            List<ServiceSubtask> selectServiceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(subtask);
            log.error("该患者待执行的任务:{}", selectServiceSubtaskList);
            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(selectServiceSubtaskList) && selectServiceSubtaskList.size() > 0) {
                for (ServiceSubtask serviceSubtask1 : selectServiceSubtaskList) {
                    if (Objects.isNull(serviceSubtask1.getLongSendTime())) {
                        //不是长期任务,不处理
                        continue;
                    }
 
                    //将之前的停掉
                    serviceSubtask1.setSendstate(4L);
                    serviceSubtask1.setRemark("疾病患者再入院");
                    serviceSubtask1.setResult("error");
                    serviceSubtask1.setFinishtime(new Date());
                    serviceSubtask1.setUpdateBy(serviceTask.getUpdateBy());
                    serviceSubtaskMapper.updateServiceSubtask(serviceSubtask1);
                    //重新新增子任务
                    i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
 
                }
            } else {
                if (StringUtils.isEmpty(serviceSubtask.getPhone())) {
                    serviceSubtask.setRemark("手机号为空");
                    serviceSubtask.setSendstate(4L);
                    serviceSubtask.setResult("error");
                    serviceSubtask.setFinishtime(new Date());
                }
                serviceSubtask.setCreateBy(serviceTask.getCreateBy());
                serviceSubtask.setCreateTime(new Date());
                i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
            }
        } else {
            if (StringUtils.isEmpty(serviceSubtask.getPhone())) {
                serviceSubtask.setRemark("手机号为空");
                serviceSubtask.setSendstate(4L);
                serviceSubtask.setResult("error");
                serviceSubtask.setFinishtime(new Date());
 
            }
            serviceSubtask.setCreateBy(serviceTask.getCreateBy());
            serviceSubtask.setCreateTime(new Date());
            i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
        }
        if (i == 1) {
            //将check_flag改成1(已处理)
            PatMedOuthosp patMedOuthosp1 = new PatMedOuthosp();
            patMedOuthosp1.setId(patMedOuthosp.getId());
            patMedOuthosp1.setDiagcheckFlag("1");
            patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
        } else {
            //生成子任务失败,
            log.error("生成子任务失败serviceSubtask为:{}", serviceSubtask);
            PatMedOuthosp pmo = new PatMedOuthosp();
            pmo.setId(patMedOuthosp.getId());
            pmo.setDiagcheckFlag("2");
            pmo.setRemark("生成子任务失败");
            patMedOuthospMapper.updatePatMedOuthosp(pmo);
        }
    }
 
    //封装serviceSubtask
    private ServiceSubtask boxedServiceSubtask(ServiceTask serviceTask, PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
        ServiceSubtask serviceSubtask = DtoConversionUtils.sourceToTarget(serviceTask, ServiceSubtask.class);
        serviceSubtask.setTaskid(serviceTask.getTaskid());
        if (StringUtils.isNotEmpty(serviceTask.getLibtemplateid()))
            serviceSubtask.setLibtemplateid(Long.valueOf(serviceTask.getLibtemplateid()));
        serviceSubtask.setDrcode(patMedOuthosp.getDrcode());
        serviceSubtask.setDrname(patMedOuthosp.getDrname());
        serviceSubtask.setDeptcode(patMedOuthosp.getDeptcode());
        serviceSubtask.setDeptname(patMedOuthosp.getDeptname());
        serviceSubtask.setTemplateid(serviceTask.getTemplateid());
        serviceSubtask.setTemplatename(serviceTask.getTemplatename());
        serviceSubtask.setPatid(patArchive.getId());
        serviceSubtask.setSendname(patArchive.getName());
        serviceSubtask.setSfzh(patArchive.getIdcardno());
        serviceSubtask.setPhone(patArchive.getTelcode());
        if (StringUtils.isBlank(patArchive.getTelcode())) serviceSubtask.setPhone(patArchive.getRelativetelcode());
        serviceSubtask.setSex(patArchive.getSex());
        serviceSubtask.setAge(patArchive.getAge());
        serviceSubtask.setSendstate(2L);
        serviceSubtask.setServiceType("3");
        serviceSubtask.setPreachform(serviceTask.getPreachform());
        serviceSubtask.setHospType("1");
        serviceSubtask.setCreateTime(new Date());
        serviceSubtask.setUpdateTime(new Date());
        serviceSubtask.setUpdateBy(serviceTask.getUpdateBy());
        serviceSubtask.setUpdateTime(new Date());
        //设置发送时间
        if (serviceTask.getSendDay() == null) serviceTask.setSendDay(1L);
        Date newDate = addDays(patMedOuthosp.getAdmitdate(), serviceTask.getSendDay().intValue());
        serviceSubtask.setLongSendTime(newDate);
        //患者发送时间
        if (StringUtils.isNotEmpty(patArchive.getNotrequiredFlag()) && patArchive.getNotrequiredFlag().equals("1")) {
            String remark = patArchive.getNotrequiredreason();
            serviceSubtask.setRemark(remark);
            serviceSubtask.setResult("error");
            serviceSubtask.setFinishtime(new Date());
            //不执行
            serviceSubtask.setSendstate(4L);
        }
        return serviceSubtask;
    }
 
    private Date addDays(Date date, Integer days) {
        if (days == null) {
            days = 1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }
}