package com.lyms.talkonlineweb.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.*;
import com.lyms.talkonlineweb.util.Constant;
import com.lyms.talkonlineweb.util.HXService;
import com.lyms.talkonlineweb.util.JwtUtils;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.Collectors;
/**
* 患者管理
*/
@RestController
@RequestMapping("pat")
@Slf4j
public class PatientController {
@Autowired
private LymsPatientService lymsPatientService;//患者
@Autowired
private LymsPcaseService lymsPcaseService;//病例
@Autowired
private LymsIllnessService lymsIllnessService;//疾病
@Autowired
private PatientInfoService patientInfoService;//患者视图
@Autowired
private LymsTcardService lymsTcardService;//问诊卡信息
@Autowired
private LymsLogsService lymsLogsService;//日志记录
@Autowired
private HXService hxService;
/**
* 获取患者列表
* @param patientInfo
* @param current
* @param size
* @return
*/
@GetMapping("sltPatientLst")
public BaseResponse sltPatientLst(PatientInfo patientInfo, int current, int size,@RequestParam(required = false) Integer qtype){
BaseResponse baseResponse=new BaseResponse();
Page<PatientInfo> page=new Page<>(current,size);
QueryWrapper<PatientInfo> queryWrapper = Wrappers.query(patientInfo);
if (Objects.nonNull(qtype) &&qtype==1){//从小程序购买
queryWrapper.inSql("id","SELECT t.`pid` FROM lyms_tcard t WHERE t.`fid`=1");
}
if (Objects.nonNull(qtype) && qtype==2){//从医院购买
queryWrapper.inSql("id","SELECT t.`pid` FROM lyms_tcard t WHERE t.`fid`=2");
}
Page<PatientInfo> patientPagePage=patientInfoService.page(page, queryWrapper);
List<PatientInfo> pLst=patientInfoService.list(queryWrapper);
int daiCnt=pLst.parallelStream().mapToInt(PatientInfo::getCcnt).sum();//待使用数量
List<Integer> pids=pLst.parallelStream().map(PatientInfo::getId).collect(Collectors.toList());
LymsTcard tcard=new LymsTcard();
QueryWrapper<LymsTcard> tWrapper = Wrappers.query(tcard);
if(pids.size()==0){
pids.add(-1);
}
tWrapper.in("pid",pids);
int sum=lymsTcardService.list(tWrapper).parallelStream().mapToInt(LymsTcard::getCnt).sum();
log.error("统计需要改动数据结构:daiCnt:{} sum:{}",daiCnt,sum);
baseResponse.setObject(patientPagePage);
return baseResponse;
}
/**
* 保存或更新患者信息
* @param patient
* @return
*/
@PostMapping("savePatient")
public BaseResponse savePatient(@Validated LymsPatient patient, BindingResult result, LymsPcase pcase, String illness){
BaseResponse baseResponse=new BaseResponse();
log.info(">>>>>>>>>>>>>>>登记病例");
baseResponse.setErrormsg("");
LymsPatient patient2=lymsPatientService.getOne(Wrappers.query(patient).eq("idno",patient.getIdno()));
if(patient2==null){
patient.setCreatedtime(new Date());
}else{
patient.setId(patient2.getId());
patient.setUpdatedtime(new Date());
}
if(result.hasErrors()){
baseResponse.setErrorcode(1);
result.getAllErrors().forEach(e->{
baseResponse.setErrormsg(baseResponse.getErrormsg()+e.getDefaultMessage());
});
baseResponse.setErrorcode(1);
return baseResponse;
}
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(",");
Map param=new HashMap();
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());
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());
}
//需要添加医院购买卡记录
LymsTcard tcard=new LymsTcard();
tcard.setCnt(patient.getCcnt());
tcard.setPcid(pcase.getPcid());
tcard.setPid(patient.getId());
tcard.setFid(2);
param.clear();
param.put("pcid",pcase.getPcid());
List<LymsTcard> tLst=lymsTcardService.listByMap(param);
if(tLst.size()>0){
tcard.setId(tLst.get(0).getId());
tcard.setUpdatedby(pcase.getUpdatedby());
tcard.setUpdatedTime(new Date());
}else{
tcard.setCreatedby(patient.getCreatedby());
tcard.setCreatedtime(new Date());
}
lymsTcardService.saveOrUpdate(tcard);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}
/**
* 删除患者
* @param id
* @return
*/
@GetMapping("delPatient")
public BaseResponse delPatient(int id){
BaseResponse baseResponse=new BaseResponse();
boolean f=lymsPatientService.removeById(id);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}
/**
* 删除患者病例ID
* @param pcid
* @return
*/
@GetMapping("delPcase")
public BaseResponse delPcase(int pcid){
BaseResponse baseResponse=new BaseResponse();
boolean f=lymsPcaseService.removeById(pcid);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}
/**
* 患者登录
* @param patient
* @return
*/
@PostMapping("loginPatient")
public BaseResponse loginPatient(@RequestBody LymsPatient patient){
BaseResponse baseResponse=new BaseResponse();
List<LymsPatient> dLst=lymsPatientService.list(Wrappers.query(patient));
baseResponse.setErrorcode(1);
if (dLst.size()>0) {
patient=dLst.get(0);
patient.setPpasswd(null);
baseResponse.setObject(patient);
String jwt = JwtUtils.createJWT("1", patient.getIdno(), Constant.JWT_TTL);
Map<String,Object> map=new HashMap<>();
map.put("patient",patient);
map.put("token",jwt);
patient.setIslogin(1);
if(patient.getHxid()==null ){
JSONObject json=hxService.addUser(patient.getIdno(),Constant.COMMON_PASSWD);
JSONArray rArr=json.getJSONArray("entities");
if(rArr.size()>0){
patient.setHxid(rArr.getJSONObject(0).getString("uuid"));
}
}
lymsPatientService.update().update(patient);
baseResponse.setErrorcode(0);
baseResponse.setObject(map);
LymsLogs logEntity=new LymsLogs();
logEntity.setFunc("loginPatient");
logEntity.setLogDate(new Date());
logEntity.setMessage(String.format("login:%s name:%s",patient.getIdno(),patient.getPname()));
lymsLogsService.save(logEntity);
}
return baseResponse;
}
/**
* 查询单个患者信息
* @param patientInfo
* @return
*/
@GetMapping("queryPatient")
public BaseResponse queryPatient(PatientInfo patientInfo){
BaseResponse baseResponse=new BaseResponse();
PatientInfo patientInfo2=patientInfoService.getOne(Wrappers.query(patientInfo));
baseResponse.setObject(patientInfo2);
return baseResponse;
}
}