package thyyxxk.webserver.service.zhuyuanyisheng;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.YiZhuLuRuDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.yibao.ZyActpatient;
import thyyxxk.webserver.entity.yibao.patient.Patient;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.*;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.service.yibao.PatientService;
import thyyxxk.webserver.utils.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
*
* 描述: 医嘱录入
*
*
* @author xc
* @date 2022-01-04 16:59
*/
@Service
@Slf4j
public class YiZhuLuRuServer {
private final YiZhuLuRuDao dao;
private final PatientService patientService;
private final PublicServer publicServer;
public YiZhuLuRuServer(YiZhuLuRuDao dao, PatientService service, PublicServer publicServer) {
this.dao = dao;
this.patientService = service;
this.publicServer = publicServer;
}
/**
* 获取患者列表
*
* @param wardCode 根据病区来进行搜索
* @return 返回患者信息
*/
public ResultVo> huoQuHuanZheLieBiao(String wardCode) {
List list = dao.huoQuHuanZheLieBiao(wardCode);
if (ListUtil.isBlank(list)) {
return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
}
list.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getBedNo())));
return ResultVoUtil.success(list);
}
/**
* 获取患者的具体信息
* 同时要计算一些数据信息
*
* @param inpatientNo 住院号
* @return 返回
*/
public ResultVo huoQuHuanZheXinXi(String inpatientNo) {
ResultVo resPpatient = patientService.getPatientInfo(inpatientNo);
if (resPpatient.getCode() != 200) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, resPpatient.getMessage());
}
PatientTemp patient = EntityCopy.Copy(resPpatient.getData(), PatientTemp.class);
HuanZheFeiYong feiYong = dao.feiYongXinXi(patient.getInpatientNo(), patient.getAdmissTimes(), patient.getLedgerSn());
if (feiYong != null) {
// 余额
patient.setBalance(feiYong.getBalance());
// 总费用
patient.setTotalCharge(feiYong.getTotalCharge());
// 药品 百分比
patient.setYp(DecimalUtil.getPercent(Double.parseDouble(feiYong.getYp()), Double.parseDouble(feiYong.getTotalCharge())));
// 检验检查 百分比
patient.setJyjc(DecimalUtil.getPercent(Double.parseDouble(feiYong.getJyjc()), Double.parseDouble(feiYong.getTotalCharge())));
// 医保 费用
patient.setChargeYb(feiYong.getChargeYb());
// 医保百分比
patient.setYb(DecimalUtil.getPercent(Double.parseDouble(feiYong.getChargeYb()), Double.parseDouble(feiYong.getTotalCharge())));
}
// 年龄
patient.setAge(DateUtil.calculateAge(patient.getBirthDate()));
patient.setTimesBilled(dao.huoQuHuanZheXinXi(patient.getInpatientNo(), patient.getAdmissTimes()).getTimesBilled());
return ResultVoUtil.success(patient);
}
/**
* 获取医嘱的名称 这里为什么不用 医嘱的code和医嘱号呢,是因为药品编码和项目编码有些一样而医嘱号,又每次只能查询一个
*
* @param inpatientNo 住院号
* @param admissTimes 住院次数
* @param orderName 前端搜索的医嘱名称
* @return 返回医嘱名称去重了
*/
public ResultVo> huoQuYiZhuMingCheng(String inpatientNo, Integer admissTimes, String orderName) {
return ResultVoUtil.success(dao.huoQuYiZhuMingZi(inpatientNo, admissTimes, StringUtil.isContainChinese(orderName)));
}
/**
* 获取患者个人的频率
*
* @param patNo 住院号
* @param times 住院次数
* @return 返回自己的医嘱频率
*/
public ResultVo> huoQuGeRenPinLv(String patNo, Integer times) {
return ResultVoUtil.success(dao.huoQuGeRenPinLv(patNo, times));
}
/**
* 获取医嘱的数据
* 可以根据,医嘱时间区间,执行频率,医嘱名
*
* @param param 查询条件
* @return 返回分页数据,同时做了树状图,这样会有一个问题,那就是开了套餐的数据查询不出来
*/
public ResultVo> huoQuYiZhuShuJu(YiZhuFeiYongChaXunTiaoJian param) {
QueryWrapper> qw = new QueryWrapper<>();
qw.eq("inpatient_no", param.getPatNo())
.eq("admiss_times", param.getTimes());
if (StringUtil.notBlank(param.getOrderName())) {
qw.eq("order_name", param.getOrderName());
}
if (StringUtil.notBlank(param.getStartTime())) {
qw.ge("order_time", param.getStartTime())
.le("order_time", param.getEndTime());
}
if (StringUtil.notBlank(param.getFrequCode())) {
qw.eq("frequ_code", param.getFrequCode());
}
List zhaungTai = new ArrayList() {
private static final long serialVersionUID = 1194053908853933514L;
{
add(1);
add(2);
add(5);
}
};
if (zhaungTai.contains(param.getZhuangTai())) {
qw.eq("status_flag", param.getZhuangTai());
} else if (param.getZhuangTai() == 3) {
qw.in("status_flag", 3, 4);
} else if (param.getZhuangTai() == 6) {
qw.ne("frequ_code", "ONCE")
.isNull("end_time")
.eq("doctor_flag", "1")
.ne("status_flag", "5");
} else if (param.getZhuangTai() == 7) {
qw.eq("doctor_flag", "1")
.ge("status_flag", "2")
.ge("start_time", DateUtil.formatDatetime(DateUtil.addDateMinut(new Date(), -24)));
} else if (param.getZhuangTai() == 8) {
qw.eq("frequ_code", "ONCE");
} else if (param.getZhuangTai() == 9) {
qw.ne("frequ_code", "ONCE");
}
IPage page = new Page<>();
if (param.getTotal() == 0) {
page.setTotal(dao.huoQuYiZhuShuJuTotal(qw));
} else {
page.setTotal(param.getTotal());
}
List yiZhuList = dao.huoQuYiZhuShuJu(param.getCurrentPage(), param.getPageSize(), qw);
// 还有那些没有被匹配的子级医嘱
Map wuFuJiYiZhu = yiZhuList.stream().collect(
Collectors.toMap(YzActOrder::getActOrderNo, a -> a, (k1, k2) -> k1));
if (ListUtil.isBlank(yiZhuList)) {
return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
}
// 做成树状图
Map map = new HashMap<>();
List tree = new ArrayList<>();
for (YzActOrder item : yiZhuList) {
if (item.getParentNo() == null) {
tree.add(item);
wuFuJiYiZhu.remove(item.getActOrderNo());
}
map.put(item.getActOrderNo(), item);
EntityStringTrim.beanAttributeValueTrim(item);
if (item.getSerial().equals("00")) {
item.setGroupNoName("医技科室");
item.setSerialName("项目");
} else {
if (item.getSerial().equals("01")) {
item.setSerialName("小包装");
} else if (item.getSerial().equals("99")) {
item.setSerialName("大包装");
}
}
}
for (YzActOrder item : yiZhuList) {
YzActOrder actOrder = map.get(item.getParentNo());
if (actOrder != null) {
wuFuJiYiZhu.remove(item.getActOrderNo());
if (actOrder.getChildren() == null) {
actOrder.setChildren(new ArrayList<>());
}
item.setIsChildren(true);
actOrder.getChildren().add(item);
}
}
if (!wuFuJiYiZhu.isEmpty()) {
tree.addAll(wuFuJiYiZhu.values());
}
// 删除前后空格
EntityStringTrim.beanAttributeValueTrimList(tree);
page.setRecords(tree);
return ResultVoUtil.success(page);
}
/**
* 获取搜索的项目信息,如药品和项目
*
* @param code 拼音首字母,中文,编码来进行搜索
* @return 返回项目信息
*/
public ResultVo> huoQuXiangMu(String code) {
code = StringUtil.isContainChinese(code);
List list = dao.yiZhuYaoPing(code, publicServer.getGroupNo());
list.addAll(dao.yiZhuXiangMu(code));
EntityStringTrim.beanAttributeValueTrimList(list);
return ResultVoUtil.success(list);
}
/**
* 获取某一个费用的详细信息
*
* @param code 编码
* @param serial 00-项目 01-小包装的药品 99-大包装
* @return 返回该费用的一些详细信息,以及一些提示信息。
*/
public ResultVo