123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package thyyxxk.webserver.service.inpatient.nursemodule;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import thyyxxk.webserver.config.exception.ExceptionEnum;
- import thyyxxk.webserver.dao.his.inpatient.nursemodule.PrintInfusionCardDao;
- import thyyxxk.webserver.entity.ResultVo;
- import thyyxxk.webserver.entity.inpatient.nursemodule.request.CardType;
- import thyyxxk.webserver.entity.inpatient.nursemodule.request.InfusionInsert;
- import thyyxxk.webserver.entity.inpatient.nursemodule.request.UpdatePrintStatus;
- import thyyxxk.webserver.entity.inpatient.nursemodule.response.*;
- import thyyxxk.webserver.entity.inpatient.nursemodule.request.QueryInfusionCard;
- import thyyxxk.webserver.utils.DateUtil;
- import thyyxxk.webserver.utils.ResultVoUtil;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Service
- public class PrintInfusionCardService {
- private final PrintInfusionCardDao dao;
- @Autowired
- public PrintInfusionCardService(PrintInfusionCardDao dao) {
- this.dao = dao;
- }
- public ResultVo<List<PatientBed>> getPatientBeds(String wardCode) {
- return ResultVoUtil.success(dao.selectPatientBedList(wardCode));
- }
- public ResultVo<List<PatientInfusionInfo>> queryInfusionCardOrBottleCard(QueryInfusionCard params) {
- String bedNos = makeBedNos(params.getBedNos());
- InfusionInsert insert = getInfusionInsert(params, params.getWardCode(), bedNos);
- if (params.getCardType() == CardType.INFUSION_CARD) {
- return queryInfusionCard(insert);
- }
- return queryBottleCard(insert);
- }
- private ResultVo<List<PatientInfusionInfo>> queryInfusionCard(InfusionInsert insert) {
- String wardCode = insert.getWardCode();
- List<PatientBriefInfo> targetPatients = dao.selectTargetPatients(wardCode, insert.getBedNos());
- String prefix = "▲";
- dao.deleteOldInfusion(wardCode);
- dao.insertNewInfusion(insert);
- dao.insertInfusionAgain(wardCode);
- List<YzActOccInfusionNew> mainDrugs = dao.selectMainDrugs(wardCode);
- if (mainDrugs.isEmpty()) {
- return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有当天的输液卡。");
- }
- List<YzActOccInfusionNew> sideDrugs = dao.selectSideDrugs(wardCode);
- Map<String, List<YzActOccInfusionNew>> sideDrugMap = new HashMap<>();
- sideDrugs.forEach(drug -> {
- drug.setDrugName(prefix + drug.getDrugName());
- String key = drug.getParentNo();
- if (sideDrugMap.containsKey(key)) {
- sideDrugMap.get(key).add(drug);
- } else {
- List<YzActOccInfusionNew> tempList = new ArrayList<>();
- tempList.add(drug);
- sideDrugMap.put(key, tempList);
- }
- });
- List<InfusionCard> list = new ArrayList<>();
- for (YzActOccInfusionNew item : mainDrugs) {
- InfusionCard card = new InfusionCard();
- card.setMainDrug(item);
- List<YzActOccInfusionNew> sideDrugList = sideDrugMap.get(item.getActOrderNo());
- if (null == sideDrugList) {
- list.add(card);
- continue;
- }
- card.setSideDrugs(sideDrugList);
- list.add(card);
- }
- Map<String, PatientInfusionInfo> tempMap = new HashMap<>();
- for (InfusionCard card : list) {
- String key = card.getMainDrug().getInpatientNo();
- if (tempMap.containsKey(key)) {
- tempMap.get(key).getDrugGroups().add(card);
- } else {
- PatientInfusionInfo infusionInfo = new PatientInfusionInfo();
- List<InfusionCard> tempList = new ArrayList<>();
- tempList.add(card);
- infusionInfo.setDrugGroups(tempList);
- tempMap.put(key, infusionInfo);
- }
- }
- List<PatientInfusionInfo> response = new ArrayList<>();
- targetPatients.forEach(target -> {
- PatientInfusionInfo infusionInfo = tempMap.get(target.getPatNo());
- if (null != infusionInfo) {
- target.setAgeLabel(DateUtil.calculateAgeLabelByDays(target.getAgeDays()));
- infusionInfo.setPatInfo(target);
- response.add(infusionInfo);
- }
- });
- return ResultVoUtil.success(response);
- }
- private ResultVo<List<PatientInfusionInfo>> queryBottleCard(InfusionInsert insert) {
- String wardCode = insert.getWardCode();
- List<PatientBriefInfo> targetPatients = dao.selectTargetPatients(wardCode, insert.getBedNos());
- Map<String, PatientBriefInfo> patInfoMap = new HashMap<>();
- for (PatientBriefInfo info : targetPatients) {
- info.setAgeLabel(DateUtil.calculateAgeLabelByDays(info.getAgeDays()));
- patInfoMap.put(info.getPatNo(), info);
- }
- dao.deleteOldInfusion(wardCode);
- dao.insertNewBottleCard(insert);
- List<YzActOccInfusionNew> mainDrugs = dao.selectMainDrugsForBottleCard(insert);
- List<YzActOccInfusionNew> sideDrugs = dao.selectSideDrugsForBottleCard(insert);
- List<PatientInfusionInfo> response = new ArrayList<>();
- for (YzActOccInfusionNew main : mainDrugs) {
- main.setStartHour(DateUtil.formatDatetime(main.getStartTime(), "HH:mm"));
- PatientInfusionInfo info = new PatientInfusionInfo();
- info.setPatInfo(patInfoMap.get(main.getInpatientNo()));
- info.setBottleCardDrugs(fetchDrugsForBottleCard(main, sideDrugs));
- response.add(info);
- }
- // 按bed_no去掉最后一个字符后转为数字排序
- response.sort((o1, o2) -> {
- // 获取并处理bedNo(去掉最后一个字符)
- String bedNo1 = getTrimmedBedNo(o1.getPatInfo());
- String bedNo2 = getTrimmedBedNo(o2.getPatInfo());
- // 转为数字比较
- try {
- Integer b1 = bedNo1.isEmpty() ? 0 : Integer.parseInt(bedNo1);
- Integer b2 = bedNo2.isEmpty() ? 0 : Integer.parseInt(bedNo2);
- return b1.compareTo(b2);
- } catch (NumberFormatException e) {
- // 非数字格式按处理后的字符串字典序排序
- return bedNo1.compareTo(bedNo2);
- }
- });
- return ResultVoUtil.success(response);
- }
- // 辅助方法:获取去掉最后一个字符的bedNo
- private String getTrimmedBedNo(PatientBriefInfo patInfo) {
- if (patInfo == null || patInfo.getBedNo() == null || patInfo.getBedNo().length() == 0) {
- return "";
- }
- // 去掉最后一个字符(若长度为1则返回空字符串)
- return patInfo.getBedNo().substring(0, patInfo.getBedNo().length() - 1);
- }
- private List<YzActOccInfusionNew> fetchDrugsForBottleCard(YzActOccInfusionNew main, List<YzActOccInfusionNew> sideDrugs) {
- List<YzActOccInfusionNew> list = new ArrayList<>();
- list.add(main);
- String prefix = "▲";
- for (YzActOccInfusionNew drug : sideDrugs) {
- if (drug.getParentNo().equals(main.getActOrderNo())) {
- drug.setDrugName(prefix + drug.getOrderName());
- drug.setStartHour(DateUtil.formatDatetime(drug.getStartTime(), "HH:mm"));
- list.add(drug);
- }
- }
- return list;
- }
- private InfusionInsert getInfusionInsert(QueryInfusionCard params, String wardCode, String bedNos) {
- InfusionInsert insert = new InfusionInsert();
- insert.setStartTime(params.getExecuteDate() + " 00:00:00.000");
- insert.setEndTime(params.getExecuteDate() + " 23:59:59.999");
- insert.setWardCode(wardCode);
- insert.setBedNos(bedNos);
- insert.setAlwaysFlag(params.getFrequency().getLabel());
- String printComment = params.getCardType() == CardType.INFUSION_CARD ?
- params.getPrintRange().getInfusionLabel() :
- params.getPrintRange().getBottleLabel();
- insert.setPrintComment(printComment);
- insert.setBottleCardType(params.getBottleCardType());
- return insert;
- }
- private String makeBedNos(List<String> bedNos) {
- if (null == bedNos || bedNos.isEmpty()) {
- return "''";
- }
- List<String> temp = new ArrayList<>();
- StringBuilder sb = new StringBuilder();
- for (String bedNo : bedNos) {
- if (temp.contains(bedNo)) {
- continue;
- }
- temp.add(bedNo);
- sb.append(",'").append(bedNo).append("'");
- }
- return sb.substring(1);
- }
- public String updatePrintStatus(UpdatePrintStatus request) {
- if (null == request.getOrderNos() || request.getOrderNos().isEmpty()) {
- return "empty orderNos";
- }
- String status = request.getCardType() == CardType.INFUSION_CARD ? "S" : "J";
- StringBuilder sb = new StringBuilder();
- for (String orderNo : request.getOrderNos()) {
- sb.append(",'").append(orderNo).append("'");
- }
- String orderNos = sb.substring(1);
- dao.updateYzActOccPrintStatus(status, orderNos);
- return status;
- }
- }
|