LymsHisInfoServiceImpl.java 12.1 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
 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
package com.lyms.talkonlineweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.*;
import com.lyms.talkonlineweb.mapper.LymsHisInfoMapper;
import com.lyms.talkonlineweb.util.Constant;
import com.lyms.talkonlineweb.util.StringUtil;
import com.lyms.talkonlineweb.util.WeiXinUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
*
*/
@Service
@Log4j2
public class LymsHisInfoServiceImpl extends ServiceImpl<LymsHisInfoMapper, LymsHisInfo>
implements LymsHisInfoService{

@Autowired
private LymsDictService lymsDictService;
@Autowired
private LymsPatientService lymsPatientService;//患者
@Autowired
private LymsHdepartService lymsHdepartService;//科室
@Autowired
private LymsDoctorService lymsDoctorService;//医生
@Autowired
private LymsPcaseService lymsPcaseService;//病例
@Autowired
private LymsIllnessService lymsIllnessService;//疾病
@Autowired
private LymsTcardService lymsTcardService;//问诊卡信息
@Autowired
private LymsHisInfoService lymsHisInfoService;
@Autowired
private LymsDictCodeOrmService lymsDictCodeOrmService;


@Override
@Transactional(rollbackFor = Exception.class)
public String upHisInfo(LymsHisInfo lymsHisInfo) {
String result=collateData(lymsHisInfo);
if(StringUtil.isEmpty(result)){
lymsHisInfo.setUpType(1);
lymsHisInfo.setUpTime(new Date());
boolean b = lymsHisInfoService.updateById(lymsHisInfo);
if(!b){
//这里想触发事务回滚需抛运行时异常
throw new RuntimeException("更新HIS患者信息返回值失败状态!");
}
}
return result;
}

/**
* 处理数据,执行
// * @param lymsHisInfo
*/
private String collateData(LymsHisInfo lymsHisInfo){
if(null!=lymsHisInfo) {
//姓名
String name = lymsHisInfo.getName();
//性别
Integer sex = null;
if("男".equals(lymsHisInfo.getSex())){
sex=1;
}else if("女".equals(lymsHisInfo.getSex())){
sex=2;
}
//生日
String birthday = lymsHisInfo.getBirthday();
//电话
String phone = lymsHisInfo.getPhone();
//科室
Integer deptId = null;//科室id
String deptName = lymsHisInfo.getDept();
LymsHdepart depart = new LymsHdepart();
depart.setDname(deptName);
depart.setHid(Integer.valueOf(lymsHisInfo.getHospitalId()));
LymsHdepart lymsHdeparts = lymsHdepartService.getOne(Wrappers.query(depart));
if (null!=lymsHdeparts) {
deptId = lymsHdeparts.getDid();
}else {
return "医院科室不存在,请增加"+deptName;
}
//医生
Integer doctorId = null;//医生id
String doctorName = lymsHisInfo.getDoctor();
LymsDoctor doctor = new LymsDoctor();
doctor.setHid(Integer.valueOf(lymsHisInfo.getHospitalId()));
doctor.setDpid(deptId);
doctor.setDname(doctorName);
List<LymsDoctor> lymsDoctors = lymsDoctorService.list(Wrappers.query(doctor));
if (CollectionUtils.isNotEmpty(lymsDoctors)) {
doctorId = lymsDoctors.get(0).getDid();
}else {
return "医生不存在,请在"+deptName+"下面增加"+doctorName+"医生!";
}

//疾病
List<String> diagnoses = Arrays.asList(lymsHisInfo.getDiagnose().split(","));
List<String> diagnoseIds = new ArrayList<>();//疾病ids
List<String> diagnoseOrmIds = new ArrayList<>();//疾病映射ids
if (null != diagnoses) {
for (String diagnose : diagnoses) {
LymsDict dict = new LymsDict();
dict.setVtype(3);
dict.setValue(diagnose);
List<LymsDict> dictList = lymsDictService.list(Wrappers.query(dict));
if (CollectionUtils.isNotEmpty(dictList)) {
for (LymsDict lymsDict : dictList) {
diagnoseIds.add(lymsDict.getCode().toString());
}
}
//去疾病映射库做对比
LymsDictCodeOrm dictCodeOrm=new LymsDictCodeOrm();
dictCodeOrm.setHisIllness(diagnose);
List<LymsDictCodeOrm> dictCodeOrms = lymsDictCodeOrmService.list(Wrappers.query(dictCodeOrm));
for (LymsDictCodeOrm codeOrm : dictCodeOrms) {
if(null != codeOrm.getIllId()){
diagnoseOrmIds.add(codeOrm.getIllId().toString());
}
}

}
}
//如果有疾病在疾病库中不存在,则都不予上传,如果在映射库中存在可以上传
if(diagnoses.size()!=diagnoseIds.size()){
if(diagnoses.size()==diagnoseOrmIds.size()+diagnoseIds.size()){
//去重赋值
diagnoseIds.addAll(diagnoseOrmIds.stream().distinct().collect(Collectors.toList()));
}else {
return "疾病不存在,请在设置-字典管理中添加疾病或在疾病映射中添加映射关系。";
}
}
//患者(身份证)
String idCard = lymsHisInfo.getIdcard();
if (StringUtil.isEmpty(idCard)) {
idCard=lymsHisInfo.getVccardno();
}
//判断患者是否存在
LymsPatient patientQuery = new LymsPatient();
patientQuery.setIdno(idCard.toLowerCase());
LymsPatient patient = lymsPatientService.getOne(Wrappers.query(patientQuery));
if (null != patient) {
QueryWrapper<LymsPcase> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("pid", patient.getId());
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 00);//时
calendar.set(Calendar.MINUTE, 00);//分
calendar.set(Calendar.SECOND, 00);//秒
queryWrapper.ge("createdtime", calendar.getTime());
List<LymsPcase> pcaseList = lymsPcaseService.list(queryWrapper);
for (LymsPcase lymsPcase : pcaseList) {
QueryWrapper<LymsIllness> illnessQueryWrapper = new QueryWrapper<>();
illnessQueryWrapper.eq("pcid", lymsPcase.getPcid());
illnessQueryWrapper.in("iid", diagnoseIds);
illnessQueryWrapper.ge("createdtime", calendar.getTime());
int count = lymsIllnessService.count(illnessQueryWrapper);
//如果今天有这个患者病例不用添加(这里防止同一天平台添加后。his有该患者病例。重复添加)
if (count != 0) {
return null;
}
}
}
//添加患者信息及病例
LymsPatient patient2 = new LymsPatient();
LymsPcase pcase = new LymsPcase();
patient2.setPname(name);
patient2.setIdno(idCard);
patient2.setBirth(birthday);
patient2.setSex(sex);
patient2.setCcnt(0);
patient2.setCreatedby(1);
pcase.setMobile(phone);
pcase.setHid(Integer.valueOf(lymsHisInfo.getHospitalId()));
pcase.setHname(lymsHdeparts.getHname());
pcase.setDid(deptId);
pcase.setDname(deptName);
pcase.setDtid(doctorId);
pcase.setDtname(doctorName);
pcase.setCreatedby(1);
String illness = StringUtils.join(Arrays.asList(diagnoseIds.toArray()), ",");//疾病ids
BaseResponse baseResponse = saveOrUpdatePatient(patient2, pcase, illness);
if (null != baseResponse && 0 == baseResponse.getErrorcode()) {
log.info(">>>>>>>>>>>>>>>患者添加成功!");
}else {
throw new RuntimeException("添加患者信息/病例/病例中疾病,返回值失败状态!");
}
}
return null;
}

//添加修改患者方法
public BaseResponse saveOrUpdatePatient(LymsPatient patient, LymsPcase pcase, String illness) {
BaseResponse baseResponse = new BaseResponse();
log.info(">>>>>>>>>>>>>>>登记病例");
baseResponse.setErrormsg("");
patient.setIdno(patient.getIdno().toLowerCase());
LymsPatient tmpP=new LymsPatient();
tmpP.setIdno(patient.getIdno().toLowerCase());
LymsPatient patient2 = lymsPatientService.getOne(Wrappers.query(tmpP).eq("idno", patient.getIdno()));
if (patient2 == null) {
patient.setCreatedtime(new Date());
patient.setPpasswd(DigestUtils.md5DigestAsHex(Constant.COMMON_PASSWD.getBytes()));
} else {
patient.setId(patient2.getId());
patient.setUpdatedtime(new Date());
patient.setCcnt(patient2.getCcnt()+ (null== patient.getCcnt()?0:patient.getCcnt()));
}


if (!StringUtil.isEmpty(patient.getCode())) {
patient.setOpenid(WeiXinUtil.getWxOpenId(patient.getCode()));
}

boolean f = lymsPatientService.saveOrUpdate(patient);

// 添加病例
pcase.setCreatedby(patient.getCreatedby());
pcase.setPid(patient.getId());
pcase.setCreatedtime(new Date());
f = lymsPcaseService.saveOrUpdate(pcase);
String[] iArr = illness.split(",");

log.info(patient);
log.info(pcase);

Map param = new HashMap();
param.put("vtype",3);
List<LymsDict> dLst=lymsDictService.listByMap(param);
for (int i = 0; i < iArr.length; i++) {
LymsIllness ness = new LymsIllness();
ness.setCreatedby(patient.getCreatedby());
ness.setPcid(pcase.getPcid());
ness.setIid(Integer.parseInt(iArr[i]));
ness.setCreatedtime(new Date());

dLst.forEach(e->{
if(e.getCode().intValue()==ness.getIid().intValue()){
ness.setIname(e.getValue());
}
});

param.clear();
param.put("pcid", pcase.getPcid());
param.put("iid", iArr[i]);
List<LymsIllness> iLst = lymsIllnessService.listByMap(param);
if (iLst.size() > 0) {
ness.setId(iLst.get(0).getId());
}
f = lymsIllnessService.saveOrUpdate(ness);
log.info(ness.toString());
}

Date instDate = new Date();
//需要添加医院购买卡记录(his患者上传默认给10张问诊卡)
patient.setCcnt(10);
for (int i = 0; i < patient.getCcnt() && Objects.nonNull(patient.getCcnt()); i++) {
LymsTcard tcard = new LymsTcard();
tcard.setCnt(1);
tcard.setPcid(pcase.getPcid());
tcard.setPid(patient.getId());
tcard.setFid(2);
tcard.setCreatedby(patient.getCreatedby());
tcard.setCreatedtime(instDate);

lymsTcardService.saveOrUpdate(tcard);
}
baseResponse.setObject(patient);
baseResponse.setErrorcode(f == true ? 0 : 1);
return baseResponse;
}


}