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;
}
}