YiZhuLuRuServer.java 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. package thyyxxk.webserver.service.zhuyuanyisheng;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.dynamic.datasource.annotation.DS;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.commons.collections4.ListUtils;
  11. import org.jetbrains.annotations.NotNull;
  12. import org.springframework.scheduling.annotation.Async;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import thyyxxk.webserver.config.exception.BizException;
  16. import thyyxxk.webserver.config.exception.ExceptionEnum;
  17. import thyyxxk.webserver.constants.Capacity;
  18. import thyyxxk.webserver.constants.sidicts.ChargeStatus;
  19. import thyyxxk.webserver.dao.his.inpatient.XiangMuLuRuDao;
  20. import thyyxxk.webserver.dao.his.zhuyuanyisheng.YiZhuLuRuDao;
  21. import thyyxxk.webserver.entity.ResultVo;
  22. import thyyxxk.webserver.entity.RoleCode;
  23. import thyyxxk.webserver.entity.casefrontsheet.SheetOverview;
  24. import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
  25. import thyyxxk.webserver.entity.datamodify.YzActOrder;
  26. import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
  27. import thyyxxk.webserver.entity.dictionary.CodeName;
  28. import thyyxxk.webserver.entity.inpatient.patient.Overview;
  29. import thyyxxk.webserver.entity.inpatient.patient.Patient;
  30. import thyyxxk.webserver.entity.login.UserInfo;
  31. import thyyxxk.webserver.entity.zhuyuanyisheng.DoctorSOrderFee;
  32. import thyyxxk.webserver.entity.zhuyuanyisheng.ZyOrderZk;
  33. import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.*;
  34. import thyyxxk.webserver.service.PublicServer;
  35. import thyyxxk.webserver.service.casefrontsheet.CaseFrontSheetMainService;
  36. import thyyxxk.webserver.service.externalhttp.DrgWebServices;
  37. import thyyxxk.webserver.service.heliyongyao.RationalUseServer;
  38. import thyyxxk.webserver.service.redislike.RedisLikeService;
  39. import thyyxxk.webserver.utils.*;
  40. import java.math.BigDecimal;
  41. import java.math.RoundingMode;
  42. import java.util.*;
  43. import java.util.stream.Collectors;
  44. /**
  45. * <p>
  46. * 描述: 医嘱录入
  47. * </p>
  48. *
  49. * @author xc
  50. * @date 2022-01-04 16:59
  51. */
  52. @Service
  53. @Slf4j
  54. public class YiZhuLuRuServer {
  55. private final YiZhuLuRuDao dao;
  56. private final PublicServer publicServer;
  57. private final RedisLikeService redisLikeService;
  58. private final XiangMuLuRuDao xiangMuLuRuDao;
  59. private final RationalUseServer rationalUseServer;
  60. private final DrgWebServices drgWebServices;
  61. private final CaseFrontSheetMainService caseFrontSheetMainService;
  62. /**
  63. * 转科的医嘱编码
  64. */
  65. private final String ZK_CODE = "06286";
  66. private final String ONCE = "ONCE";
  67. private final int 出院带药限制天数 = 15;
  68. private final int 出院带药限制数量 = 4;
  69. private final List<String> groupList = Arrays.asList("71", "73");
  70. private final String ITEM = "00";
  71. private final String SMALL_PACKAGE = "01";
  72. private final String BIG_PACKAGE = "99";
  73. private YiZhuLuRuServer getThis() {
  74. return SpringUtil.getBean(this.getClass());
  75. }
  76. public YiZhuLuRuServer(YiZhuLuRuDao dao, PublicServer publicServer, RedisLikeService redisLikeService, XiangMuLuRuDao xiangMuLuRuDao, RationalUseServer rationalUseServer, DrgWebServices drgWebServices, CaseFrontSheetMainService caseFrontSheetMainService) {
  77. this.dao = dao;
  78. this.publicServer = publicServer;
  79. this.redisLikeService = redisLikeService;
  80. this.xiangMuLuRuDao = xiangMuLuRuDao;
  81. this.rationalUseServer = rationalUseServer;
  82. this.drgWebServices = drgWebServices;
  83. this.caseFrontSheetMainService = caseFrontSheetMainService;
  84. }
  85. public ResultVo<String> getOrderNo() {
  86. return ResultVoUtil.success(publicServer.getActOrderNo().stripTrailingZeros().toPlainString());
  87. }
  88. public ResultVo<List<Patient>> getMyPatient() {
  89. return ResultVoUtil.success(dao.getMyPatient(TokenUtil.getTokenUserId()));
  90. }
  91. /**
  92. * 获取医嘱的数据
  93. * 可以根据,医嘱时间区间,执行频率,医嘱名
  94. *
  95. * @param param 查询条件
  96. * @return 返回分页数据,同时做了树状图,这样会有一个问题,那就是开了套餐的数据查询不出来
  97. */
  98. public ResultVo<List<XinZhenYzActOrder>> huoQuYiZhuShuJu(YiZhuFeiYongChaXunTiaoJian param) {
  99. QueryWrapper<?> qw = new QueryWrapper<>();
  100. qw.eq("a.inpatient_no", param.getPatNo()).eq("a.admiss_times", param.getTimes());
  101. qw.orderByAsc("a.order_time");
  102. List<XinZhenYzActOrder> yiZhuList = dao.selectOrderNo(qw);
  103. // 还有那些没有被匹配的子级医嘱
  104. Map<BigDecimal, XinZhenYzActOrder> wuFuJiYiZhu = yiZhuList.stream().collect(Collectors.toMap(XinZhenYzActOrder::getActOrderNo, a -> a, (k1, k2) -> k1));
  105. if (ListUtil.isBlank(yiZhuList)) {
  106. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "该患者还没有开医嘱。");
  107. }
  108. // 做成树状图
  109. Map<BigDecimal, XinZhenYzActOrder> map = new HashMap<>();
  110. List<XinZhenYzActOrder> tree = new ArrayList<>();
  111. for (XinZhenYzActOrder item : yiZhuList) {
  112. if (item.getParentNo() == null) {
  113. tree.add(item);
  114. wuFuJiYiZhu.remove(item.getActOrderNo());
  115. }
  116. map.put(item.getActOrderNo(), item);
  117. EntityStringTrim.beanAttributeValueTrim(item);
  118. if ("00".equals(item.getSerial())) {
  119. item.setGroupNoName("项目");
  120. item.setSerialName("项目");
  121. } else {
  122. if ("01".equals(item.getSerial())) {
  123. item.setSerialName("小包装");
  124. } else if ("99".equals(item.getSerial())) {
  125. item.setSerialName("大包装");
  126. }
  127. }
  128. }
  129. for (XinZhenYzActOrder item : yiZhuList) {
  130. XinZhenYzActOrder actOrder = map.get(item.getParentNo());
  131. if (actOrder != null) {
  132. wuFuJiYiZhu.remove(item.getActOrderNo());
  133. if (actOrder.getChildren() == null) {
  134. actOrder.setChildren(new ArrayList<>());
  135. actOrder.setOrderGroup("┌");
  136. }
  137. item.setOrderGroup("丨");
  138. actOrder.getChildren().add(item);
  139. }
  140. }
  141. if (!wuFuJiYiZhu.isEmpty()) {
  142. tree.addAll(wuFuJiYiZhu.values());
  143. }
  144. // 删除前后空格
  145. EntityStringTrim.beanAttributeValueTrimList(tree);
  146. List<XinZhenYzActOrder> list = new ArrayList<>();
  147. for (XinZhenYzActOrder zy : tree) {
  148. list.add(zy);
  149. if (ListUtil.notBlank(zy.getChildren())) {
  150. zy.getChildren().get(zy.getChildren().size() - 1).setOrderGroup("└");
  151. list.addAll(zy.getChildren());
  152. zy.setChildren(null);
  153. }
  154. }
  155. return ResultVoUtil.success(list);
  156. }
  157. /**
  158. * 获取搜索的项目信息,如药品和项目
  159. *
  160. * @param code 拼音首字母,中文,编码来进行搜索
  161. * @return 返回项目信息
  162. */
  163. @DS("his")
  164. public ResultVo<List<YiZhuMingChen>> huoQuXiangMu(String code, String groupNo) {
  165. code = StringUtil.isContainChinese(code);
  166. // 药品
  167. List<YiZhuMingChen> list = dao.yiZhuYaoPing(code, groupNo);
  168. // 项目
  169. list.addAll(dao.yiZhuXiangMu(code));
  170. // 模板
  171. list.addAll(dao.composeOrders(code, redisLikeService.getUserInfoByToken().getDeptCode()));
  172. EntityStringTrim.beanAttributeValueTrimList(list);
  173. return ResultVoUtil.success(list);
  174. }
  175. /**
  176. * 获取父医嘱
  177. *
  178. * @param patNo 住院号
  179. * @param times 次数
  180. * @return
  181. */
  182. public ResultVo<List<XinZhenYzActOrder>> getParentOrders(String patNo, Integer times) {
  183. return ResultVoUtil.success(dao.getParentOrders(patNo, times));
  184. }
  185. /**
  186. * 把有错误的子医嘱纠正回来,子医嘱要跟随父医嘱的频率 等
  187. *
  188. * @param list 患者的数据
  189. */
  190. public void correctSubOrders(List<XinZhenYzActOrder> list) {
  191. // 父医嘱
  192. Map<BigDecimal, XinZhenYzActOrder> parentOrder = new HashMap<>(list.size());
  193. list.forEach(item -> parentOrder.put(item.getActOrderNo(), item));
  194. list.forEach(item -> {
  195. if (item.getParentNo() != null && parentOrder.containsKey(item.getParentNo())) {
  196. XinZhenYzActOrder order = parentOrder.get(item.getParentNo());
  197. // 判断是否需要更新
  198. if (updateSubOrders(order, item)) {
  199. dao.updateSubOrderStatus(item.getActOrderNo(), order);
  200. }
  201. }
  202. });
  203. }
  204. private boolean updateSubOrders(XinZhenYzActOrder parent, XinZhenYzActOrder children) {
  205. if (!parent.getOrderTime().equals(children.getOrderTime())) {
  206. return true;
  207. }
  208. if (!parent.getStartTime().equals(children.getStartTime())) {
  209. return true;
  210. }
  211. if (parent.getEndTime() != null && !parent.getEndTime().equals(children.getEndTime())) {
  212. return true;
  213. }
  214. if (!"044".equals(children.getSupplyCode())) {
  215. return true;
  216. }
  217. if (!parent.getFrequCode().equals(children.getFrequCode())) {
  218. return true;
  219. }
  220. return !parent.getGroupNo().equals(children.getGroupNo());
  221. }
  222. /**
  223. * 确认医嘱 , 这个是最新的正确的
  224. *
  225. * @param param 数据
  226. * @return 返回提示
  227. */
  228. public ResultVo<Map<String, Object>> confirmOrders(@NotNull XinZhenYiZhu param) {
  229. QueryWrapper<?> qw = new QueryWrapper<>();
  230. qw.eq("a.inpatient_no", param.getInpatientNo())
  231. .eq("a.admiss_times", param.getAdmissTimes())
  232. .eq("a.status_flag", "1")
  233. .eq("a.enter_oper", TokenUtil.getTokenUserId())
  234. // 排除出院带药的医嘱
  235. .ne("isnull(a.self_buy,'0')", "4")
  236. .orderByAsc("a.act_order_no");
  237. param.setList(dao.selectOrderNo(qw));
  238. List<XinZhenYzActOrder> yiZhuList = param.getList();
  239. if (ListUtil.isBlank(yiZhuList)) {
  240. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有需要确认的医嘱.");
  241. }
  242. correctSubOrders(param.getList());
  243. dischargeQCVerification(param);
  244. // 药品 code
  245. Set<String> yaoPingCode = new HashSet<>();
  246. // 项目的 code
  247. Set<String> xiangMuCode = new HashSet<>();
  248. for (XinZhenYzActOrder item : yiZhuList) {
  249. if (ITEM.equals(item.getSerial())) {
  250. xiangMuCode.add(item.getOrderCode());
  251. } else {
  252. yaoPingCode.add(item.getOrderCode().trim() + item.getSerial().trim() + item.getGroupNo().trim());
  253. }
  254. }
  255. String userCode = TokenUtil.getTokenUserId();
  256. // 获取医生开药品的权限
  257. Integer doctorLevel = dao.huoQuYiShenDengJi(userCode);
  258. XinZhenYiZhu patInfo = dao.queryPatientInfo(param.getInpatientNo(), param.getAdmissTimes());
  259. Map<String, XinZhenYzActOrder> drug = getDrugInformation(yaoPingCode, patInfo.getZkWard());
  260. Map<String, List<XinZhenYzActOrder>> project = getProjectInformation(xiangMuCode);
  261. // 是否可以确认
  262. boolean isThereAnyErrorMessage = false;
  263. Map<String, Object> map = new HashMap<>();
  264. List<BigDecimal> confirmOrderInformation = new ArrayList<>();
  265. Date excludeOrders = null;
  266. BigDecimal orderNo = null;
  267. for (XinZhenYzActOrder item : yiZhuList) {
  268. if (ITEM.equals(item.getSerial())) {
  269. Integer paiChiYiZhu = dao.shiFouPaiChiYiZhu(item.getOrderCode());
  270. if (excludeOrders != null) {
  271. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "一次性不能确认多条全排斥医嘱。");
  272. }
  273. if (paiChiYiZhu != null && paiChiYiZhu.equals(1)) {
  274. excludeOrders = item.getStartTime();
  275. orderNo = item.getActOrderNo();
  276. }
  277. }
  278. confirmOrderInformation.add(item.getActOrderNo());
  279. Map<String, Object> errorMessageMap = checkData(item, drug, project, patInfo, doctorLevel);
  280. if (!errorMessageMap.isEmpty()) {
  281. if (!isThereAnyErrorMessage && errorMessageMap.containsKey("error")) {
  282. isThereAnyErrorMessage = true;
  283. }
  284. errorMessageMap.put("data", item);
  285. map.put(item.getId(), errorMessageMap);
  286. }
  287. }
  288. if (isThereAnyErrorMessage) {
  289. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, "请修改错误的医嘱", map);
  290. }
  291. if (excludeOrders != null) {
  292. int count = dao.thereAreUnexecutedOrders(param.getInpatientNo(), param.getAdmissTimes(), orderNo);
  293. if (count > 0) {
  294. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "有未确认和未执行的长期医嘱,无法确认全排斥医嘱,请先确认其他医嘱。");
  295. }
  296. dao.stopOrder(param.getInpatientNo(), param.getAdmissTimes(), excludeOrders, userCode, orderNo);
  297. }
  298. for (BigDecimal decimal : confirmOrderInformation) {
  299. dao.confirmOrders(decimal, userCode, new Date());
  300. }
  301. getThis().drgOrderUpdate(patInfo.getInpatientNo() + "_" + patInfo.getAdmissTimes() + "_" + patInfo.getLedgerSn());
  302. sendAMessageToTheNurse(param, userCode, patInfo, "新增医嘱");
  303. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  304. }
  305. /**
  306. * 出院医嘱质控校验
  307. *
  308. * @param param 患者信息
  309. */
  310. private void dischargeQCVerification(XinZhenYiZhu param) {
  311. if (dao.selectDischargeOrde(param, TokenUtil.getTokenUserId()) > 0) {
  312. SheetOverview overview = new SheetOverview();
  313. overview.setBah(param.getInpatientNo());
  314. overview.setTimes(param.getAdmissTimes());
  315. overview.setInOutFlag(1);
  316. ResultVo<Map<String, List<CodeName>>> result = caseFrontSheetMainService.sheetVerification(overview);
  317. if (!result.getCode().equals(ExceptionEnum.SUCCESS.getCode())) {
  318. throw new BizException(ExceptionEnum.LOGICAL_ERROR, result.getMessage());
  319. }
  320. if (!result.getData().get("force").isEmpty()) {
  321. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "未通过病案首页质控,无法确认出院医嘱。");
  322. }
  323. }
  324. }
  325. /**
  326. * 在开医嘱时 drg 需要调用一下这个接口
  327. *
  328. * @param patientNo 患者id
  329. */
  330. @Async
  331. public void drgOrderUpdate(String patientNo) {
  332. try {
  333. JSONObject jsonDrg = new JSONObject();
  334. jsonDrg.put("visit_id", Collections.singletonList(patientNo));
  335. jsonDrg.put("scene_type", 1);
  336. JSONObject js = drgWebServices.etlClient(jsonDrg);
  337. log.info("调用drg医嘱确认接口:{}", js);
  338. } catch (Exception e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. private void sendAMessageToTheNurse(XinZhenYiZhu param, String inputCode, XinZhenYiZhu patInfo, String name) {
  343. List<String> content = new ArrayList<>();
  344. for (XinZhenYzActOrder item : param.getList()) {
  345. String sb = notificationStyle("医嘱名", "409eff", item.getOrderName()) +
  346. notificationStyle("医嘱时间", "409eff", DateUtil.formatDatetime(item.getStartTime())) +
  347. notificationStyle("床位", "409eff", patInfo.getBedNo()) +
  348. notificationStyle("患者姓名", "409eff", patInfo.getName()) +
  349. notificationStyle("频次", "409eff", item.getFrequCode());
  350. content.add(sb);
  351. }
  352. publicServer.faSongXiaoXi(patInfo, content, name, inputCode);
  353. }
  354. private String notificationStyle(String name, String color, String content) {
  355. return String.format("%s:<span style='color: #%s'>%s</span><br>", name, color, content);
  356. }
  357. /**
  358. * 录入单条医嘱 新的 下面的保存医嘱的都无效了
  359. *
  360. * @param param 参数
  361. * @return 错误信息和提示
  362. */
  363. public ResultVo<Map<String, Object>> enterOrders(XinZhenYiZhu param) {
  364. XinZhenYzActOrder oldOrderNo = dao.getActOrderNoOne(param.getActOrderNo());
  365. String userCode = TokenUtil.getTokenUserId();
  366. if (oldOrderNo != null) {
  367. if (!"1".equals(oldOrderNo.getStatusFlag().trim())) {
  368. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该医嘱不是录入状态无法保存.");
  369. }
  370. if (!oldOrderNo.getInpatientNo().trim().equals(param.getInpatientNo())) {
  371. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "两次医嘱录入时患者不一致。");
  372. }
  373. if (!oldOrderNo.getEnterOper().equals(userCode)) {
  374. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医嘱录入人不是您,无法修改。。");
  375. }
  376. }
  377. // 获取医生开药品的权限
  378. Integer doctorLevel = dao.huoQuYiShenDengJi(userCode);
  379. XinZhenYiZhu huanZheXinXi = dao.queryPatientInfo(param.getInpatientNo(), param.getAdmissTimes());
  380. XinZhenYzActOrder data = param.getData();
  381. if (ITEM.equals(data.getSerial().trim())) {
  382. data.setGroupNo("00");
  383. }
  384. // 药品的 code
  385. Set<String> yaoPingCode = new HashSet<>();
  386. // 项目的 code
  387. Set<String> xiangMuCode = new HashSet<>();
  388. if (ITEM.equals(data.getSerial())) {
  389. xiangMuCode.add(data.getOrderCode());
  390. } else {
  391. yaoPingCode.add(data.getOrderCode().trim() + data.getSerial().trim() + data.getGroupNo().trim());
  392. }
  393. Map<String, XinZhenYzActOrder> drug = getDrugInformation(yaoPingCode, huanZheXinXi.getZkWard());
  394. Map<String, List<XinZhenYzActOrder>> project = getProjectInformation(xiangMuCode);
  395. Map<String, Object> errorMessageMap = checkData(data, drug, project, huanZheXinXi, doctorLevel);
  396. if (data.getParentNo() != null) {
  397. XinZhenYzActOrder parentOrder = dao.getActOrderNoOne(data.getParentNo().stripTrailingZeros().toPlainString());
  398. if (parentOrder != null) {
  399. data.setFrequCode(parentOrder.getFrequCode());
  400. }
  401. }
  402. // 删除抗菌药物信息
  403. dao.deleteAntimicrobialInformation(param.getActOrderNo());
  404. // 先删除转科在插入
  405. dao.deleteTransferOrder(param.getActOrderNo());
  406. if (errorMessageMap.isEmpty()) {
  407. if (ZK_CODE.equals(data.getOrderCode())) {
  408. ZyOrderZk transferData = new ZyOrderZk();
  409. transferData
  410. .setActOrderNo(param.getActOrderNo())
  411. .setNewDept(data.getZkDeptCode())
  412. .setNewWard(data.getZkWardCode())
  413. .setOldWard(huanZheXinXi.getDeptCode())
  414. .setOldDept(huanZheXinXi.getSmallDept());
  415. dao.insertDoctorSOrder(transferData);
  416. }
  417. // 判断是不是抗菌药物
  418. XinZhenYzActOrder kssItem = drug.get(data.getOrderCode().trim() + data.getSerial().trim() + data.getGroupNo().trim());
  419. if (kssItem != null && kssItem.getKjywFlag() != null && kssItem.getKjywFlag() == 1) {
  420. YzActRecordKss kss = new YzActRecordKss();
  421. kss.setActOrderNo(data.getActOrderNo());
  422. kss.setChargeCode(data.getOrderCode());
  423. kss.setYyfs(data.getYyfs());
  424. kss.setSsqk(data.getSsqk());
  425. kss.setYysj(data.getYysj());
  426. dao.insertAntimicrobialInformation(param.getActOrderNo(),
  427. kss, userCode, param.getInpatientNo(), param.getAdmissTimes());
  428. }
  429. }
  430. dao.deleteOrderNo(param.getActOrderNo(), param.getInpatientNo(), param.getAdmissTimes());
  431. dao.insertEntryOrder(huanZheXinXi, param.getData(), userCode);
  432. // 01724
  433. if (!errorMessageMap.isEmpty()) {
  434. Map<String, Object> map = new HashMap<>();
  435. errorMessageMap.put("data", data);
  436. map.put(param.getActOrderNo(), errorMessageMap);
  437. if (errorMessageMap.containsKey("error")) {
  438. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, "请修改有错误的医嘱。", map);
  439. }
  440. }
  441. publicServer.orderRecord(new BigDecimal(param.getActOrderNo()), TokenUtil.getTokenUserId());
  442. log.info("医嘱录入日志:{}", JSON.toJSONString(param));
  443. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "添加成功.");
  444. }
  445. /**
  446. * 把医嘱模板中的数据插入到 患者的医嘱表中
  447. *
  448. * @param param 参数
  449. * @return
  450. */
  451. public ResultVo<String> insertTemplateOrder(XinZhenYiZhu param) {
  452. if (StringUtil.isBlank(param.getInpatientNo())) {
  453. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, "请先选择患者.");
  454. }
  455. Map<String, XinZhenYzActOrder> fatherAndSonDoctorSAdvice = new HashMap<>(param.getList().size());
  456. param.getList().forEach(item -> {
  457. fatherAndSonDoctorSAdvice.put(item.getId(), item);
  458. item.setActOrderNo(publicServer.getActOrderNo());
  459. publicServer.orderRecord(item.getActOrderNo(), TokenUtil.getTokenUserId());
  460. });
  461. XinZhenYiZhu huanZheXinXi = dao.queryPatientInfo(param.getInpatientNo(), param.getAdmissTimes());
  462. // 把模板的数据插入到医嘱
  463. getThis().templateInsertToOrder(param, fatherAndSonDoctorSAdvice, huanZheXinXi);
  464. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "插入模板数据成功。");
  465. }
  466. @Transactional(rollbackFor = Exception.class)
  467. public void templateInsertToOrder(XinZhenYiZhu param, Map<String, XinZhenYzActOrder> fatherAndSonDoctorSAdvice, XinZhenYiZhu huanZheXinXi) {
  468. String userCode = TokenUtil.getTokenUserId();
  469. param.getList().forEach(item -> {
  470. String key = item.getParentNo() != null ? item.getParentNo().stripTrailingZeros().toPlainString() : "";
  471. if (StringUtil.notBlank(key)) {
  472. if (fatherAndSonDoctorSAdvice.containsKey(key)) {
  473. item.setParentNo(fatherAndSonDoctorSAdvice.get(key).getActOrderNo());
  474. }
  475. }
  476. dao.insertEntryOrder(huanZheXinXi, item, userCode);
  477. });
  478. }
  479. /**
  480. * 删除单条医嘱
  481. *
  482. * @param orderNo 医嘱号
  483. * @return 提示
  484. */
  485. public ResultVo<String> toDeleteAnOrder(String orderNo) {
  486. XinZhenYzActOrder oldData = dao.getActOrderNoOne(orderNo);
  487. String message = judgeWhetherItCanBeDeleted(oldData);
  488. if (StringUtil.notBlank(message)) {
  489. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, message);
  490. }
  491. dao.toDeleteAnOrder(orderNo, oldData.getInpatientNo());
  492. dao.deleteGroup(orderNo);
  493. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  494. }
  495. /**
  496. * 删除多条录入的医嘱
  497. *
  498. * @param param 参数
  499. * @return 返回提示
  500. */
  501. public ResultVo<Map<String, String>> deleteMultipleOrders(XinZhenYiZhu param) {
  502. QueryWrapper<?> qw = new QueryWrapper<>();
  503. qw.eq("inpatient_no", param.getInpatientNo());
  504. qw.eq("admiss_times", param.getAdmissTimes());
  505. List<BigDecimal> list = new ArrayList<>();
  506. param.getList().forEach(item -> {
  507. list.add(item.getActOrderNo());
  508. });
  509. qw.in("act_order_no", list);
  510. List<XinZhenYzActOrder> deleteOrderList = dao.getOrdersToDelete(qw);
  511. if (ListUtil.isBlank(deleteOrderList)) {
  512. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到原医嘱可能已经被删除了");
  513. }
  514. Map<String, String> map = new HashMap<>();
  515. deleteOrderList.forEach(item -> {
  516. String error = judgeWhetherItCanBeDeleted(item);
  517. if (StringUtil.notBlank(error)) {
  518. map.put(item.getActOrderNo().stripTrailingZeros().toPlainString(), error);
  519. }
  520. });
  521. log.info("数据:{}", deleteOrderList);
  522. if (map.isEmpty()) {
  523. dao.deleteParentChildRelationship(param.getInpatientNo(), param.getAdmissTimes(), list);
  524. dao.deleteMultipleOrders(qw);
  525. return ResultVoUtil.fail(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  526. }
  527. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, "删除失败", map);
  528. }
  529. private String judgeWhetherItCanBeDeleted(XinZhenYzActOrder oldData) {
  530. StringBuilder str = new StringBuilder();
  531. if (oldData == null) {
  532. return "没有找到原医嘱可能已经被删除了";
  533. }
  534. if (!"1".equals(oldData.getStatusFlag())) {
  535. str.append("该医嘱不是录入状态,无法删除");
  536. }
  537. if (!oldData.getEnterOper().equals(TokenUtil.getTokenUserId())) {
  538. str.append("该医嘱录入人不是您,无法删除.");
  539. }
  540. return str.toString();
  541. }
  542. public ResultVo<Map<BigDecimal, String>> stopOrder(XinZhenYiZhu param) {
  543. if (ListUtil.isBlank(param.getList())) {
  544. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先选择数据");
  545. }
  546. Map<BigDecimal, String> map = new HashMap<>();
  547. param.getList().forEach(item -> {
  548. if (item.getEndTime() == null) {
  549. map.put(item.getActOrderNo(), "停止时间不能为空。");
  550. } else {
  551. // 因为前端用的是浏览器自带的 所以要删除 T
  552. if (DateUtil.shiJianDaXiao(item.getEndTime(), item.getStartTime(), "<")) {
  553. map.put(item.getActOrderNo(), "停止时间不能小于医嘱的开始时间。");
  554. }
  555. }
  556. });
  557. if (map.isEmpty()) {
  558. dao.setStopTime(param.getList(), TokenUtil.getTokenUserId(), param.getInpatientNo(), param.getAdmissTimes());
  559. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  560. }
  561. return ResultVoUtil.success(ExceptionEnum.ERROR_MESSAGE, "请修改有错误的医嘱", map);
  562. }
  563. @Transactional(rollbackFor = Exception.class)
  564. public ResultVo<String> voidOrders(String orderNo, String reasonForCancellation) {
  565. XinZhenYzActOrder yz = dao.getActOrderNoOne(orderNo);
  566. if (yz == null) {
  567. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "原医嘱找不到了。");
  568. }
  569. if ("6".equals(yz.getStatusFlag())) {
  570. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医嘱已作废了。");
  571. }
  572. UserInfo userInfo = redisLikeService.getUserInfoByToken();
  573. if (!DateUtil.within24Hours(yz.getStartTime())) {
  574. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "只能对24小时内的医嘱进行作废处理");
  575. }
  576. List<String> list = dao.getTheDoctorSDepartment(userInfo.getDeptCode());
  577. list.add(userInfo.getDeptCode());
  578. if (publicServer.needRule(1)) {
  579. list.add(yz.getWardCode());
  580. }
  581. if (!list.contains(yz.getWardCode())) {
  582. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "非本科室开的医嘱不可作废");
  583. }
  584. if (dao.getADoctorSOrderWithADefiniteFee(yz.getInpatientNo(), yz.getAdmissTimes(), orderNo) > 0) {
  585. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该医嘱已经执行,请对应的医技科室拒绝医嘱。");
  586. }
  587. if (dao.getDetailChargeFee(yz.getInstruction(), yz.getAdmissTimes(), orderNo) > 0) {
  588. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该医嘱已经产生了医技费用,请对应的医技科室进行退费处理。");
  589. }
  590. // 患者最大的 detail_sn
  591. int maxDetailSn = publicServer.getMaxDetailSn(yz.getInpatientNo(), yz.getAdmissTimes());
  592. dao.cancelMedicalTechnologyAssuranceFee(TokenUtil.getTokenUserId(), orderNo, yz.getInpatientNo(), yz.getAdmissTimes());
  593. // 如果是药品的话就需要自动退费
  594. if (!yz.getSerial().equals(ITEM)) {
  595. // 获取父子医嘱同时退费
  596. List<String> parentNoList = dao.getParentChildOrders(orderNo);
  597. parentNoList.add(orderNo);
  598. List<ZyDetailCharge> feeList = dao.getExpenseData(yz.getInpatientNo(), yz.getAdmissTimes(), parentNoList);
  599. if (ListUtil.notBlank(feeList)) {
  600. List<ZyDetailCharge> refundArray = new ArrayList<>(feeList.size());
  601. for (ZyDetailCharge item : feeList) {
  602. // 取负数的费用
  603. item.setChargeFee(item.getChargeFee().negate());
  604. item.setChargeAmount(item.getChargeAmount().negate());
  605. item.setOpIdCode(userInfo.getCode());
  606. item.setOriDetailSn(item.getDetailSn());
  607. // 设置 流水号
  608. item.setDetailSn(maxDetailSn += 1);
  609. item.setOrderNo(new BigDecimal(0));
  610. refundArray.add(item);
  611. }
  612. List<List<ZyDetailCharge>> fenGe = ListUtils.partition(refundArray, 20);
  613. fenGe.forEach(xiangMuLuRuDao::xiangMuTuiFei);
  614. dao.updateItemFlag(yz.getInpatientNo(), yz.getAdmissTimes(), parentNoList);
  615. }
  616. }
  617. dao.updateVoidedOrders(orderNo, userInfo.getCode(), reasonForCancellation);
  618. dao.updateChildOrders(orderNo, userInfo.getCode(), reasonForCancellation);
  619. log.info("作废医嘱==》操作人:{},医嘱号:{}", userInfo.getCode(), orderNo);
  620. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "撤销成功。");
  621. }
  622. /**
  623. * 获取某一个费用的详细信息
  624. *
  625. * @param code 编码
  626. * @param serial 00-项目 01-小包装的药品 99-大包装
  627. * @return 返回该费用的一些详细信息,以及一些提示信息。
  628. */
  629. @DS("his")
  630. public ResultVo<Map<String, Object>> huoQuFeiYongXinXi(String code, String serial, String deptCode, String groupNo) {
  631. Map<String, Object> map = new HashMap<>();
  632. // 一些提示
  633. List<String> prompt = new ArrayList<>();
  634. // 加载项目信息
  635. if (ITEM.equals(serial.trim())) {
  636. List<XinZhenYzActOrder> xiangMu = dao.huoQuXiangMu(code);
  637. for (XinZhenYzActOrder item : xiangMu) {
  638. if (item.getDelFlag() == 1) {
  639. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, String.format("该医嘱下的【%s】,已经被停用了,请联系物价科。", item.getOrderName()));
  640. } else if (StringUtil.isBlank(item.getNationalCode())) {
  641. prompt.add(String.format("该医嘱下面的【%s】,没有医保编码,请注意。", item.getOrderName()));
  642. } else {
  643. prompt.add(String.format("该医嘱下【%s】,医保码为:【%s】。", item.getOrderName(), item.getNationalCode()));
  644. }
  645. }
  646. if (ListUtil.isBlank(xiangMu)) {
  647. Integer paiChiYiZhu = dao.shiFouPaiChiYiZhu(code);
  648. if (paiChiYiZhu != null) {
  649. switch (paiChiYiZhu) {
  650. case 1:
  651. prompt.add("全排斥医嘱");
  652. break;
  653. case 2:
  654. prompt.add("单组排斥医嘱");
  655. break;
  656. case 3:
  657. prompt.add("多组斥医嘱");
  658. break;
  659. default:
  660. break;
  661. }
  662. }
  663. }
  664. } else {
  665. YaoPinXinXi yp = dao.huoQuYaoPin(code.trim() + serial.trim(), groupNo);
  666. if (StringUtil.notBlank(deptCode)) {
  667. if (dao.restrictedUseInTheDepartment(code.trim(), deptCode) > 0) {
  668. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "该药品禁止在患者所在的科室使用。");
  669. }
  670. }
  671. if (yp == null) {
  672. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "该医嘱下的药品,可能已经被停用了,请联系药剂科。");
  673. }
  674. yp.setDrugFlag(getDrugFlagByLargeCategories(yp.getCategoriesFlag()));
  675. if (yp.getVisibleFlagZy() == 1) {
  676. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "该药品禁止住院使用。");
  677. }
  678. Integer yiShenDengJi = dao.huoQuYiShenDengJi(TokenUtil.getTokenUserId());
  679. int yiShen = yiShenDengJi == null ? 0 : yiShenDengJi;
  680. if (yp.getYpLevel() > yiShen) {
  681. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "您没有开此药品的权限。");
  682. }
  683. List<YaoPingJiLiang> yaoPingJiLiang = new ArrayList<>();
  684. if (StringUtil.notBlank(yp.getWeighUnit())) {
  685. yaoPingJiLiang.add(new YaoPingJiLiang(yp.getWeighUnit(), yp.getWeighUnitName(), yp.getWeight()));
  686. }
  687. if (StringUtil.notBlank(yp.getVolUnit())) {
  688. yaoPingJiLiang.add(new YaoPingJiLiang(yp.getVolUnit(), yp.getVolUnitName(), yp.getVolum()));
  689. }
  690. if (StringUtil.notBlank(yp.getPackUnit())) {
  691. yaoPingJiLiang.add(new YaoPingJiLiang(yp.getPackUnit(), yp.getPackUnitName(), yp.getPackSize()));
  692. }
  693. if (yp.getKjywFlag() == 1) {
  694. prompt.add("抗菌药物,需填写抗菌药物医嘱附注");
  695. }
  696. if (yp.getSelfFlagYb() == 1) {
  697. prompt.add("该药品医保自费药品,如符合条件请填写记账,不是请填写自费");
  698. }
  699. if (StringUtil.isBlank(yp.getNationalCode())) {
  700. prompt.add("该药品医保没有匹配医保码");
  701. } else {
  702. prompt.add(String.format("药品医保码为:【%s】", yp.getNationalCode()));
  703. }
  704. if (yp.getStockAmount() != null && BigUtils.bigXiaoYu(yp.getStockAmount(), 10)) {
  705. prompt.add(String.format("该药品剩余数量为:【%s】", yp.getStockAmount().stripTrailingZeros().toPlainString()));
  706. }
  707. if (yp.getPsFlag() == 1) {
  708. prompt.add("此药品为皮试药品");
  709. map.put("piShi", dao.piShiGeiYaoFangShi());
  710. }
  711. map.put("yaoPingJiLiang", yaoPingJiLiang);
  712. map.put("data", yp);
  713. }
  714. map.put("prompt", prompt);
  715. return ResultVoUtil.success(map);
  716. }
  717. private String getDrugFlagByLargeCategories(String val) {
  718. if (StringUtil.isBlank(val)) {
  719. return "z";
  720. }
  721. switch (val) {
  722. case "0":
  723. return "d";
  724. case "1":
  725. return "i";
  726. default:
  727. return "z";
  728. }
  729. }
  730. public ResultVo<List<GetDropdownBox>> getFrequency() {
  731. return ResultVoUtil.success(dao.selectFrequency());
  732. }
  733. /**
  734. * 获取频率
  735. *
  736. * @param code 五笔,拼音,中文,编码
  737. * @return 返回对应的数据
  738. */
  739. public ResultVo<List<GetDropdownBox>> huoQuZhuYuanPinLv(String code) {
  740. return ResultVoUtil.success(dao.huoQuZhuYuanPinLv("%" + code.toUpperCase() + "%"));
  741. }
  742. /**
  743. * 获取给药方式
  744. *
  745. * @param code 五笔,拼音,中文,编码
  746. * @return 返回对应的数据
  747. */
  748. public ResultVo<List<GetDropdownBox>> huoQuGeiYaoFangShi(String code) {
  749. return ResultVoUtil.success(dao.huoQuGeiYaoFangShi(StringUtil.isContainChinese(code)));
  750. }
  751. public ResultVo<List<GetDropdownBox>> getCostFreeDosing(String code) {
  752. return ResultVoUtil.success(dao.getCostFreeDosing(StringUtil.isContainChinese(code)));
  753. }
  754. /**
  755. * 获取执行科室
  756. *
  757. * @param code 五笔,拼音,中文,编码
  758. * @return 返回对应的数据
  759. */
  760. public ResultVo<List<GetDropdownBox>> huoQuZhiXinKeShi(String code) {
  761. return ResultVoUtil.success(dao.huoQuZhiXinKeShi(StringUtil.isContainChinese(code)));
  762. }
  763. /**
  764. * 这里是校验模板的数据
  765. *
  766. * @param param 模板数据
  767. * @return 返回提示信息
  768. */
  769. public ResultVo<Map<String, Object>> singleDataCheck(XinZhenYiZhu param) {
  770. // 获取医生开药品的权限
  771. Integer doctorLevel = dao.huoQuYiShenDengJi(TokenUtil.getTokenUserId());
  772. Set<String> yaoPingCode = new HashSet<>();
  773. Set<String> xiangMuCode = new HashSet<>();
  774. for (XinZhenYzActOrder item : param.getList()) {
  775. if (ITEM.equals(item.getSerial().trim())) {
  776. xiangMuCode.add(item.getOrderCode().trim());
  777. } else {
  778. yaoPingCode.add(item.getOrderCode().trim() + item.getSerial().trim() + item.getGroupNo().trim());
  779. }
  780. }
  781. Map<String, Object> errorMessage = checkData(param.getList().get(0), getDrugInformation(yaoPingCode, null), getProjectInformation(xiangMuCode), null, doctorLevel);
  782. errorMessage.put("data", param.getList().get(0));
  783. return ResultVoUtil.success(errorMessage);
  784. }
  785. public ResultVo<String> doesTheTemplateExist(String name) {
  786. if (dao.duplicateTemplateName(name, TokenUtil.getTokenUserId()) == 0) {
  787. return ResultVoUtil.success();
  788. }
  789. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "模板名称重复");
  790. }
  791. /**
  792. * 保存模板数据
  793. *
  794. * @param param
  795. * @return
  796. */
  797. public ResultVo<String> saveTemplate(YzOrderPattern param) {
  798. // 如果名字重复了,那么就修改原来的模板
  799. YzOrderPattern yzOrderPattern = dao.yuanLaiDeMuBanBianMa(param.getPatternName(), TokenUtil.getTokenUserId());
  800. UserInfo userInfo = redisLikeService.getUserInfoByCode(TokenUtil.getTokenUserId());
  801. if (null == yzOrderPattern) {
  802. yzOrderPattern = new YzOrderPattern();
  803. }
  804. String patternCode = yzOrderPattern.getPatternCode();
  805. // 有原来的模板名称而且 还要是自己创建的才能删除,否则就只能创建新的模板了
  806. if (StringUtil.notBlank(patternCode) && userInfo.getCode().equals(yzOrderPattern.getInputId())) {
  807. // 更新的时候先删除模板 原来的模板
  808. dao.shanChuLaoMuBan(patternCode);
  809. param.setPatternCode(patternCode);
  810. } else {
  811. // 设置新的模板号
  812. param.setPatternCode(publicServer.getPatternCode());
  813. }
  814. String py = PingYinUtils.pyShouZiMuDaXie(param.getPatternName());
  815. String wb = PingYinUtils.getWBCode(param.getPatternName());
  816. param.setPyCode(py.length() > 9 ? py.substring(0, 9) : py);
  817. param.setDCode(wb.length() > 9 ? wb.substring(0, 9) : wb);
  818. param.setInputId(TokenUtil.getTokenUserId());
  819. param.setDeptCode(userInfo.getDeptCode());
  820. dao.chaRuMuBan(param);
  821. Map<String, XinZhenYzActOrder> map = param.getList().stream().collect(Collectors.toMap(XinZhenYzActOrder::getId, a -> a, (k1, k2) -> k1));
  822. for (XinZhenYzActOrder item : param.getList()) {
  823. // 设置模板号
  824. item.setActOrderNo(publicServer.getPatternOrderCode());
  825. if (item.getParentNo() != null && map.containsKey(item.getParentNo().stripTrailingZeros().toPlainString())) {
  826. item.setParentNo(map.get(item.getParentNo().stripTrailingZeros().toPlainString()).getActOrderNo());
  827. }
  828. }
  829. dao.chaRuMuBanShuJu(param.getList(), param.getPatternCode());
  830. return ResultVoUtil.success();
  831. }
  832. private void calculateDrugAmount(XinZhenYzActOrder item, XinZhenYzActOrder feiYongXinXi) {
  833. if (feiYongXinXi == null) {
  834. return;
  835. }
  836. if (StringUtil.notBlank(feiYongXinXi.getDrugWeightUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getDrugWeightUnit().trim())) {
  837. item.setDrugOcc(DecimalUtil.divide(item.getDose(), feiYongXinXi.getDrugWeight(), 2));
  838. } else if (StringUtil.notBlank(feiYongXinXi.getDrugVolUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getDrugVolUnit())) {
  839. item.setDrugOcc(DecimalUtil.divide(item.getDose(), feiYongXinXi.getDrugVolume(), 2));
  840. } else if (StringUtil.notBlank(feiYongXinXi.getPackUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getPackUnit())) {
  841. item.setDrugOcc(DecimalUtil.divide(item.getDose(), feiYongXinXi.getPackSize(), 2));
  842. }
  843. }
  844. private Map<String, Object> checkData(XinZhenYzActOrder item,
  845. Map<String, XinZhenYzActOrder> drug,
  846. Map<String, List<XinZhenYzActOrder>> project,
  847. XinZhenYiZhu huanZheXinXi,
  848. Integer doctorLevel) {
  849. boolean strictVerification = huanZheXinXi != null;
  850. List<String> errorMessage = new ArrayList<>();
  851. List<String> warningMessage = new ArrayList<>();
  852. if (StringUtil.isBlank(item.getGroupNo())) {
  853. errorMessage.add("没有药房请重新开。");
  854. }
  855. if (ITEM.equals(item.getSerial())) {
  856. List<XinZhenYzActOrder> projectDetails = project.get(item.getOrderCode());
  857. // 如果这个项目下面没有费用明细,那么这就是一条口头医,口头医嘱每次只能开一个
  858. if (ListUtil.notBlank(projectDetails)) {
  859. for (XinZhenYzActOrder detailed : projectDetails) {
  860. if (detailed.getDelFlag() == 1) {
  861. errorMessage.add(String.format("项目:【%s】,已经被物价停用了", detailed.getOrderName()));
  862. }
  863. if (StringUtil.isBlank(detailed.getNationalCode())) {
  864. warningMessage.add(String.format("项目:【%s】,没有匹配医保码", detailed.getOrderName()));
  865. }
  866. }
  867. }
  868. item.setDrugOcc(item.getDrugQuan());
  869. } else {
  870. if (!groupList.contains(item.getGroupNo())) {
  871. errorMessage.add("药房错误,请重新选择药房。");
  872. }
  873. XinZhenYzActOrder detailsOfDrugs = drug.get(item.getOrderCode().trim() + item.getSerial().trim() + item.getGroupNo().trim());
  874. if (detailsOfDrugs != null) {
  875. if (detailsOfDrugs.getDelFlag() == 1) {
  876. errorMessage.add("药品已经被停用了,请联系药剂科");
  877. }
  878. if (detailsOfDrugs.getYpLevel() > doctorLevel) {
  879. errorMessage.add("您没有开此药品的权限");
  880. }
  881. if (StringUtil.isBlank(detailsOfDrugs.getNationalCode())) {
  882. warningMessage.add("该药品没有医保编码");
  883. }
  884. if (BigUtils.bigXiaoYu(detailsOfDrugs.getStockAmount(), 10)) {
  885. warningMessage.add(String.format("该药品剩余数量为:【%s】", detailsOfDrugs.getStockAmount().stripTrailingZeros().toPlainString()));
  886. }
  887. if (BigUtils.bigDaYu(item.getDrugQuan(), detailsOfDrugs.getStockAmount())) {
  888. errorMessage.add("药品领量大于药品的库存,当前库存量" + detailsOfDrugs.getStockAmount().stripTrailingZeros().toPlainString());
  889. }
  890. if (strictVerification) {
  891. if (detailsOfDrugs.getDeptRestrictions() > 0) {
  892. errorMessage.add("该药品禁止在患者所在的科室使用。");
  893. }
  894. if (detailsOfDrugs.getVisibleFlagZy() == 1) {
  895. errorMessage.add("该药品禁止住院患者使用。");
  896. }
  897. item.setKjywFlag(detailsOfDrugs.getKjywFlag());
  898. if (detailsOfDrugs.getKjywFlag() == 1) {
  899. if (item.getYyfs() == null) {
  900. errorMessage.add("请填写抗菌药物医嘱附注信息录入");
  901. } else if (item.getYyfs() == 1 || item.getYyfs() == 2) {
  902. if (item.getSsqk() == null) {
  903. errorMessage.add("当用药方式为 1 或 2 时,手术切口和用药时间不能为空");
  904. }
  905. }
  906. }
  907. }
  908. if (item.getSelfBuy() != null && "4".equals(item.getSelfBuy())) {
  909. // 如果是临时医嘱 开了就结束了.
  910. if ("ONCE".equals(item.getFrequCode())) {
  911. item.setEndTime(item.getStartTime());
  912. }
  913. if (dao.dischargeWithMedicationAdministration(item.getSupplyCode()) == 0) {
  914. errorMessage.add("出院带药给药方式不能包含费用。");
  915. }
  916. int day = 1;
  917. if (item.getStartTime() == null) {
  918. errorMessage.add("出院带药请输入开始时间。");
  919. }
  920. if (item.getEndTime() == null) {
  921. errorMessage.add("出院带药请输入结束时间。");
  922. } else {
  923. long cha = item.getEndTime().getTime() - item.getStartTime().getTime();
  924. long nd = 1000 * 24 * 60 * 60;
  925. day = (int) (cha / nd);
  926. if (day > 出院带药限制天数) {
  927. errorMessage.add("出院带药天数不得超过 15 天,计算方式结束时间减去开始时间。");
  928. }
  929. if (day == 0) {
  930. day = 1;
  931. }
  932. }
  933. BigDecimal total = getTotal(item, day);
  934. if (StringUtil.notBlank(detailsOfDrugs.getDrugWeightUnit()) && item.getDoseUnit().trim().equals(detailsOfDrugs.getDrugWeightUnit().trim())) {
  935. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(detailsOfDrugs.getDrugWeight(), 0, RoundingMode.UP));
  936. item.setDrugOcc(item.getDrugQuan());
  937. } else if (StringUtil.notBlank(detailsOfDrugs.getDrugVolUnit()) && item.getDoseUnit().trim().equals(detailsOfDrugs.getDrugVolUnit())) {
  938. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(detailsOfDrugs.getDrugVolume(), 0, RoundingMode.UP));
  939. item.setDrugOcc(item.getDrugQuan());
  940. } else if (StringUtil.notBlank(detailsOfDrugs.getPackUnit()) && item.getDoseUnit().trim().equals(detailsOfDrugs.getPackUnit())) {
  941. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(detailsOfDrugs.getPackSize(), 0, RoundingMode.UP));
  942. item.setDrugOcc(item.getDrugQuan());
  943. }
  944. if (item.getDrugQuan().equals(BigDecimal.ZERO)) {
  945. item.setDrugQuan(BigDecimal.ONE);
  946. item.setDrugOcc(BigDecimal.ONE);
  947. }
  948. } else {
  949. // 计算普通药品的领量 durg_quan durg_occ
  950. calculateDrugAmount(item, detailsOfDrugs);
  951. }
  952. }
  953. }
  954. if (StringUtil.isBlank(item.getOrderCode())) {
  955. errorMessage.add("项目编码不能为空");
  956. }
  957. if (StringUtil.isBlank(item.getOrderName())) {
  958. errorMessage.add("项目名称不能为空");
  959. }
  960. if (StringUtil.isBlank(item.getExecUnit())) {
  961. errorMessage.add("执行科室不能为空");
  962. } else if (item.getExecUnit().startsWith("8")) {
  963. errorMessage.add("执行科室不能选择为病区");
  964. }
  965. if (ZK_CODE.equals(item.getOrderCode())) {
  966. if (StringUtil.isBlank(item.getZkWardCode())) {
  967. errorMessage.add("转科病房不能为空");
  968. }
  969. if (StringUtil.isBlank(item.getZkDeptCode())) {
  970. errorMessage.add("转科科室不能为空");
  971. }
  972. }
  973. if (StringUtil.isBlank(item.getFrequCode())) {
  974. errorMessage.add("频次不能为空");
  975. }
  976. // 药品的校验
  977. if (!ITEM.equals(item.getSerial().trim())) {
  978. if (StringUtil.isBlank(item.getDrugSpecification())) {
  979. errorMessage.add("药品规格不能为空");
  980. }
  981. if (StringUtil.isBlank(item.getSupplyCode())) {
  982. errorMessage.add("给药方式不能为空");
  983. }
  984. if (item.getDose() == null || BigUtils.dengYu(item.getDose(), 0)) {
  985. errorMessage.add("一次计量不能为空");
  986. }
  987. if (StringUtil.isBlank(item.getDoseUnit())) {
  988. errorMessage.add("计量单位不能为空");
  989. }
  990. if (StringUtil.isBlank(item.getSerial())) {
  991. errorMessage.add("包装大小不能为空");
  992. }
  993. }
  994. if (strictVerification) {
  995. if (item.getParentNo() != null && item.getActOrderNo() != null) {
  996. if (BigUtils.bigXiaoYu(item.getActOrderNo(), item.getParentNo())) {
  997. errorMessage.add("子医嘱的医嘱号,小于父医嘱号,无法成组。【原因如果子医嘱的医嘱号小于父医嘱,会导致打印乱码】");
  998. }
  999. }
  1000. // 获取患者的入院时间
  1001. if (item.getOrderTime() == null) {
  1002. errorMessage.add("医嘱时间不能为空");
  1003. } else if (item.getStartTime() == null) {
  1004. errorMessage.add("开始时间不能为空");
  1005. } else if (huanZheXinXi.getAdmissDate() == null) {
  1006. errorMessage.add("没有查询到患者的入院时间");
  1007. } else if (DateUtil.shiJianDaXiao(item.getStartTime(), huanZheXinXi.getAdmissDate(), "<")) {
  1008. errorMessage.add("开始时间不能在患者入院之前,患者入院时间" + DateUtil.formatDatetime(huanZheXinXi.getAdmissDate()));
  1009. } else if (DateUtil.shiJianDaXiao(item.getStartTime(), item.getOrderTime(), "<")) {
  1010. errorMessage.add("开始时间不能在开医嘱之前");
  1011. }
  1012. if (item.getEndTime() != null) {
  1013. if (ONCE.equals(item.getFrequCode().trim())) {
  1014. item.setEndTime(null);
  1015. } else {
  1016. if (DateUtil.shiJianDaXiao(item.getEndTime(), item.getStartTime(), "<")) {
  1017. errorMessage.add("结束时间不能在开始时间之前");
  1018. }
  1019. }
  1020. }
  1021. }
  1022. Map<String, Object> map = new HashMap<>(Capacity.TWO);
  1023. if (ListUtil.notBlank(errorMessage)) {
  1024. map.put("error", errorMessage);
  1025. }
  1026. if (ListUtil.notBlank(warningMessage)) {
  1027. map.put("warning", warningMessage);
  1028. }
  1029. return map;
  1030. }
  1031. /**
  1032. * 计算总量
  1033. *
  1034. * @param item 药品信息
  1035. * @param day 天数
  1036. * @return 返回总数
  1037. */
  1038. @NotNull
  1039. private BigDecimal getTotal(XinZhenYzActOrder item, int day) {
  1040. // 计算出院带药的领量
  1041. Map<String, Short> numberOfTimesADay = dao.numberOfTimesADay(item.getFrequCode());
  1042. int times = 0;
  1043. for (Map.Entry<String, Short> entry : numberOfTimesADay.entrySet()) {
  1044. if (entry.getValue() != null) {
  1045. times += entry.getValue();
  1046. }
  1047. }
  1048. // 最小天数一天
  1049. return new BigDecimal((day <= 0 ? 1 : day) * times);
  1050. }
  1051. /***
  1052. * 获取项目信息
  1053. * @param xiangMuCode 项目编码
  1054. * @return 返回数据
  1055. */
  1056. public Map<String, List<XinZhenYzActOrder>> getProjectInformation(Set<String> xiangMuCode) {
  1057. if (!xiangMuCode.isEmpty()) {
  1058. return dao.huoQuXiangMuXinXi(xiangMuCode).stream().collect(Collectors.groupingBy(item -> item.getOrderCode().trim()));
  1059. }
  1060. return new HashMap<>(0);
  1061. }
  1062. /**
  1063. * 获取药品信息
  1064. *
  1065. * @param yaoPingCode 药品编码
  1066. * @return 返回数据
  1067. */
  1068. public Map<String, XinZhenYzActOrder> getDrugInformation(Set<String> yaoPingCode, String dept) {
  1069. if (!yaoPingCode.isEmpty()) {
  1070. return dao.huoQuYaoPinXinXi(yaoPingCode, dept).stream().collect(Collectors.toMap(item -> item.getOrderCode().trim() + item.getSerial().trim() + item.getGroupNo(), a -> a, (k1, k2) -> k1));
  1071. }
  1072. return new HashMap<>(0);
  1073. }
  1074. /**
  1075. * 获取医嘱录入的模板1
  1076. *
  1077. * @param code 模板编码
  1078. * @param deptCode 科室编码
  1079. * @param muBanLeiXing 项目类型
  1080. * @param currentPage 当前页
  1081. * @param total 总数
  1082. * @return 返回模板
  1083. */
  1084. @DS("his")
  1085. public ResultVo<IPage<YzOrderPattern>> huoQuYiZhuMuBan(String code, String deptCode, Integer muBanLeiXing, long currentPage, long total) {
  1086. IPage<YzOrderPattern> page = new Page<>();
  1087. if (total == 0) {
  1088. page.setTotal(dao.huoQuMuBanTotal(StringUtil.isContainChinese(code), TokenUtil.getTokenUserId(), deptCode, muBanLeiXing));
  1089. }
  1090. page.setRecords(dao.huoQuMuBan(StringUtil.isContainChinese(code), TokenUtil.getTokenUserId(), deptCode, muBanLeiXing, currentPage));
  1091. return ResultVoUtil.success(page);
  1092. }
  1093. /**
  1094. * 获取 我的医嘱模板的最大医嘱码
  1095. *
  1096. * @return 最大排序码
  1097. */
  1098. public ResultVo<Integer> getDoctorSOrderTemplateMaxSortNo() {
  1099. Integer sortCode = dao.getMyTemplateMaxSortNo(TokenUtil.getTokenUserId());
  1100. return ResultVoUtil.success(sortCode == null ? 0 : sortCode);
  1101. }
  1102. private String getActOrderNo() {
  1103. Random random = new Random();
  1104. String a = String.valueOf(random.nextInt(5) + 1);
  1105. String b = String.valueOf(random.nextInt(99999));
  1106. return a + b;
  1107. }
  1108. /**
  1109. * 获取模板数据
  1110. *
  1111. * @param code 模板的编码
  1112. * @return 返回数据
  1113. */
  1114. @DS("his")
  1115. public ResultVo<List<YzActOrder>> huoQuMuBanShuJu(String code) {
  1116. List<YzActOrder> muBanShuJu = dao.huoQuMuBanShuJu(code);
  1117. Map<BigDecimal, YzActOrder> map = new HashMap<>(muBanShuJu.size());
  1118. List<YzActOrder> tree = new ArrayList<>();
  1119. for (YzActOrder item : muBanShuJu) {
  1120. item.setId(getActOrderNo());
  1121. if (item.getParentNo() == null) {
  1122. tree.add(item);
  1123. }
  1124. map.put(item.getActOrderNo(), item);
  1125. }
  1126. for (YzActOrder item : muBanShuJu) {
  1127. YzActOrder yzActOrder = map.get(item.getParentNo());
  1128. if (yzActOrder != null) {
  1129. item.setParentNo(new BigDecimal(yzActOrder.getId()));
  1130. item.setIsChildren(true);
  1131. if (yzActOrder.getChildren() == null) {
  1132. yzActOrder.setChildren(new ArrayList<>());
  1133. }
  1134. yzActOrder.getChildren().add(item);
  1135. // 没有副医嘱的就返回到最上层
  1136. } else if (!tree.contains(item)) {
  1137. item.setParentNo(null);
  1138. tree.add(item);
  1139. }
  1140. }
  1141. return ResultVoUtil.success(tree);
  1142. }
  1143. /**
  1144. * 删除医嘱模板
  1145. *
  1146. * @param patternCode 模板的编码
  1147. * @return 提示
  1148. */
  1149. public ResultVo<String> deleteADoctorSOrderTemplate(String patternCode) {
  1150. if (StringUtil.isBlank(patternCode)) {
  1151. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "模板编号为空。");
  1152. }
  1153. YzOrderPattern yzOrderPattern = dao.huoQuMuBanXinXi(patternCode);
  1154. EntityStringTrim.beanAttributeValueTrim(yzOrderPattern);
  1155. if (yzOrderPattern == null) {
  1156. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有查询到对应的模板信息。");
  1157. }
  1158. String userId = TokenUtil.getTokenUserId();
  1159. List<Integer> role = publicServer.getRoleCode().getData();
  1160. if (publicServer.needRule(role, RoleCode.PHYSICIAN_S_ORDER_TEMPLATEEDIT)) {
  1161. return startDeletingTemplates(patternCode);
  1162. }
  1163. if (userId.equals(yzOrderPattern.getInputId())) {
  1164. return startDeletingTemplates(patternCode);
  1165. }
  1166. // 主任可以删除本科室的任意模板
  1167. if (publicServer.needRule(role, RoleCode.DIRECTOR) && dao.userDeptCode(userId).equals(yzOrderPattern.getDeptCode())) {
  1168. return startDeletingTemplates(patternCode);
  1169. }
  1170. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您没有权限删除这个模板。");
  1171. }
  1172. /**
  1173. * 删除父模板以及下面的子模板 数据
  1174. *
  1175. * @param patternCode 模板编码
  1176. */
  1177. private ResultVo<String> startDeletingTemplates(String patternCode) {
  1178. dao.shanChuMuBan(patternCode);
  1179. log.info("删除医嘱模板==> 操作人:{},模板编码:{}", TokenUtil.getTokenUserId(), patternCode);
  1180. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "删除成功。");
  1181. }
  1182. /**
  1183. * 收藏医嘱模板或者取消收藏
  1184. * 如果以及收藏了就删除,没有就收藏
  1185. *
  1186. * @param patternCode 模板号
  1187. * @return 提示
  1188. */
  1189. public ResultVo<String> collectDoctorSOrderTemplate(String patternCode) {
  1190. String userId = TokenUtil.getTokenUserId();
  1191. String saveTheDoctorSOrderNumber = dao.whetherToSaveTheDoctorSOrderTemplate(patternCode, userId);
  1192. if (StringUtil.notBlank(saveTheDoctorSOrderNumber)) {
  1193. dao.shanChuMuBan(saveTheDoctorSOrderNumber);
  1194. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "已取消删除。");
  1195. } else {
  1196. YzOrderPattern yzOrderPattern = dao.huoQuMuBanXinXi(patternCode);
  1197. EntityStringTrim.beanAttributeValueTrim(yzOrderPattern);
  1198. dao.chaRuShouCang(publicServer.getPatternCode(), yzOrderPattern.getPatternName() + "(收藏)", yzOrderPattern.getPyCode(), yzOrderPattern.getDCode(), dao.userDeptCode(userId), userId, yzOrderPattern.getPatternCode());
  1199. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "收藏成功。");
  1200. }
  1201. }
  1202. /**
  1203. * 删除或修改模板
  1204. *
  1205. * @param patternCode 模板点吗
  1206. * @param patternName 模板名称
  1207. * @param sortNo 排序号
  1208. * @param flag 标志 1-修改 2- 删除 3-收藏和取消收藏
  1209. * @return 返回给前端提示
  1210. */
  1211. public ResultVo<String> muBanCaoZuo(String patternCode, String patternName, String deptCode, Integer sortNo, Integer flag) {
  1212. if (StringUtil.isBlank(patternCode)) {
  1213. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "模板编码为空。");
  1214. }
  1215. YzOrderPattern yzOrderPattern = dao.huoQuMuBanXinXi(patternCode);
  1216. if (yzOrderPattern == null) {
  1217. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有查询到对应的模板信息。");
  1218. }
  1219. EntityStringTrim.beanAttributeValueTrim(yzOrderPattern);
  1220. String inputId = TokenUtil.getTokenUserId();
  1221. List<Integer> role = publicServer.getRoleCode().getData();
  1222. // 管理员 和 医务部的无视 规则
  1223. if (!role.contains(1) && !role.contains(38) && flag != 3) {
  1224. // 只有模板在不等于 自己的时候触发
  1225. if (!inputId.equals(yzOrderPattern.getInputId())) {
  1226. if ("2".equals(yzOrderPattern.getInputType()) && !role.contains(11)) {
  1227. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该模板为科室模板您没有权限修改或删除,请联系科主任进行修改。");
  1228. } else if ("1".equals(yzOrderPattern.getInputType())) {
  1229. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该模板为全院模板,无法删除或修改。");
  1230. }
  1231. }
  1232. }
  1233. if (flag == 1) {
  1234. if (StringUtil.isBlank(patternName)) {
  1235. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "模板名称不能为空。");
  1236. }
  1237. if (patternName.trim().equals(yzOrderPattern.getPatternName().trim()) && sortNo.equals(yzOrderPattern.getSortNo())) {
  1238. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "数据没有变化,请勿点击。");
  1239. }
  1240. dao.genXingMuBan(patternName.trim(), PingYinUtils.pyShouZiMuDaXie(patternName), PingYinUtils.getWBCode(patternName), yzOrderPattern.getPatternCode(), sortNo);
  1241. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "修改成功。");
  1242. } else if (flag == 2) {
  1243. // 删除父模板以及下面的子模板 数据
  1244. dao.shanChuMuBan(yzOrderPattern.getPatternCode());
  1245. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "删除成功。");
  1246. } else if (flag == 3) {
  1247. // 收藏模板
  1248. String collectCode = dao.chongFuShouCang(inputId, patternCode);
  1249. if (collectCode == null) {
  1250. dao.chaRuShouCang(publicServer.getPatternCode(), yzOrderPattern.getPatternName() + "(收藏)", yzOrderPattern.getPyCode(), yzOrderPattern.getDCode(), deptCode, inputId, yzOrderPattern.getPatternCode());
  1251. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "收藏成功。");
  1252. } else {
  1253. dao.shanChuMuBan(yzOrderPattern.getPatternCode());
  1254. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "已取消收藏。");
  1255. }
  1256. }
  1257. return ResultVoUtil.success();
  1258. }
  1259. /**
  1260. * 获取医嘱下面产生的费用
  1261. *
  1262. * @param patNo 住院号
  1263. * @param times 住院次数
  1264. * @return 返回
  1265. */
  1266. public ResultVo<Map<String, Object>> expensesForGettingADoctorSOrder(String patNo, Integer times) {
  1267. List<DoctorSOrderFee> feeDate = dao.expensesForGettingADoctorSOrder(patNo, times);
  1268. // 这些编码的医嘱都是有问题的
  1269. List<String> problem = Arrays.asList("5", "6", "7", "9");
  1270. Map<String, String> problemDoctorSOrder = new HashMap<>();
  1271. Map<String, FeeSum> totalCost = new HashMap<>();
  1272. Map<String, List<DoctorSOrderFee>> map = new HashMap<>(feeDate.size());
  1273. for (DoctorSOrderFee fee : feeDate) {
  1274. // 如果这个没有就代表没有产生费用
  1275. if (fee.getOrderNoStr() == null) {
  1276. continue;
  1277. }
  1278. String chargeStatusName = ChargeStatus.getValue(fee.getChargeStatus());
  1279. String amount = fee.getChargeAmount().abs().stripTrailingZeros().toPlainString();
  1280. String money = fee.getChargeFee().stripTrailingZeros().toPlainString();
  1281. // 计算费用总和
  1282. if (totalCost.containsKey(fee.getOrderNoStr())) {
  1283. FeeSum sum = totalCost.get(fee.getOrderNoStr());
  1284. sum.setSum(DecimalUtil.add(money, sum.getSum()));
  1285. sum.setAmount(DecimalUtil.add(amount, sum.getAmount()));
  1286. totalCost.replace(fee.getOrderNoStr(), sum);
  1287. } else {
  1288. FeeSum sum = new FeeSum();
  1289. sum.setAmount(amount);
  1290. sum.setSum(money);
  1291. totalCost.put(fee.getOrderNoStr(), sum);
  1292. }
  1293. if (problem.contains(fee.getChargeStatus())) {
  1294. problemDoctorSOrder.put(fee.getOrderNoStr(), chargeStatusName);
  1295. }
  1296. fee.setChargeStatusName(chargeStatusName);
  1297. if (map.containsKey(fee.getOrderNoStr())) {
  1298. map.get(fee.getOrderNoStr()).add(fee);
  1299. } else {
  1300. List<DoctorSOrderFee> list = new ArrayList<>();
  1301. list.add(fee);
  1302. map.put(fee.getOrderNoStr(), list);
  1303. }
  1304. }
  1305. Map<String, Object> feeData = new HashMap<>(3);
  1306. feeData.put("data", map);
  1307. feeData.put("problem", problemDoctorSOrder);
  1308. feeData.put("totalCost", totalCost);
  1309. return ResultVoUtil.success(feeData);
  1310. }
  1311. /**
  1312. * 设置患者三级医生
  1313. *
  1314. * @param param 参数
  1315. * @return 返回值
  1316. */
  1317. public ResultVo<String> saveTheThirdLevelDoctor(Overview param) {
  1318. log.info("设置三级医生:==> 操作人{},数据:{}", TokenUtil.getTokenUserId(), JSON.toJSONString(param));
  1319. dao.updateTheThirdLevelDoctor(param);
  1320. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  1321. }
  1322. /**
  1323. * 设置父子医嘱
  1324. *
  1325. * @param param 主医嘱 和 多个子医嘱
  1326. * @return 返回数据
  1327. */
  1328. public ResultVo<String> associateOrders(XinZhenYiZhu param) {
  1329. log.info("数据:{}", JSON.toJSONString(param));
  1330. XinZhenYzActOrder order = dao.getActOrderNoOne(param.getActOrderNo());
  1331. if (order.getParentNo() != null) {
  1332. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "操作失败,父医嘱不能为子医嘱。");
  1333. }
  1334. param.getAssociatedGroup().forEach(item -> {
  1335. dao.associateOrders(item, order);
  1336. });
  1337. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "关联成功。");
  1338. }
  1339. /**
  1340. * 批量检验医嘱是否正确
  1341. *
  1342. * @param list 医嘱数据
  1343. * @param huanZheXinXi 患者信息
  1344. * @param rationalUse 是否要用合理用药
  1345. * @return
  1346. */
  1347. public Map<String, Object> batchVerification(List<XinZhenYzActOrder> list,
  1348. XinZhenYiZhu huanZheXinXi,
  1349. boolean rationalUse) {
  1350. Map<String, Object> returnMap = new HashMap<>();
  1351. // 药品 code
  1352. Set<String> yaoPingCode = new HashSet<>();
  1353. // 项目的 code
  1354. Set<String> xiangMuCode = new HashSet<>();
  1355. huanZheXinXi.setList(list);
  1356. // 合理用药校验
  1357. for (XinZhenYzActOrder item : list) {
  1358. if (ITEM.equals(item.getSerial())) {
  1359. xiangMuCode.add(item.getOrderCode());
  1360. } else {
  1361. yaoPingCode.add(item.getOrderCode().trim() + item.getSerial().trim() + item.getGroupNo().trim());
  1362. }
  1363. }
  1364. String userCode = TokenUtil.getTokenUserId();
  1365. // 获取医生开药品的权限
  1366. Integer doctorLevel = dao.huoQuYiShenDengJi(userCode);
  1367. Map<String, XinZhenYzActOrder> drug = getDrugInformation(yaoPingCode, huanZheXinXi.getZkWard());
  1368. Map<String, List<XinZhenYzActOrder>> project = getProjectInformation(xiangMuCode);
  1369. for (XinZhenYzActOrder item : list) {
  1370. String key = item.getId();
  1371. Map<String, Object> errorMessageMap = checkData(item, drug, project, huanZheXinXi, doctorLevel);
  1372. if (!errorMessageMap.isEmpty()) {
  1373. if (errorMessageMap.get("error") != null) {
  1374. errorMessageMap.put("data", item);
  1375. returnMap.put(item.getId(), errorMessageMap);
  1376. }
  1377. }
  1378. }
  1379. return returnMap;
  1380. }
  1381. /**
  1382. * 确认出院带药医嘱
  1383. *
  1384. * @param patNo 住院号
  1385. * @param times 住院次数
  1386. * @return
  1387. */
  1388. public ResultVo<Map<String, Object>> confirmTheDoctorSOrderWithMedicine(String patNo, Integer times) {
  1389. QueryWrapper<?> qw = new QueryWrapper<>();
  1390. qw.eq("a.inpatient_no", patNo)
  1391. .eq("a.admiss_times", times)
  1392. .eq("a.status_flag", "1")
  1393. .eq("a.enter_oper", TokenUtil.getTokenUserId())
  1394. .eq("isnull(a.self_buy,'0')", "4");
  1395. XinZhenYiZhu patInfo = dao.queryPatientInfo(patNo, times);
  1396. patInfo.setList(dao.selectOrderNo(qw));
  1397. if (dao.obtainTheNumberOfDischargedDrugs(patNo, times) > 出院带药限制数量) {
  1398. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "出院带药医嘱不得超过 4 种,且不得超过 15 天。");
  1399. }
  1400. String userCode = TokenUtil.getTokenUserId();
  1401. // 出院带药需要生成药单 key 是不同的药房。不同的药房就要
  1402. Map<String, List<XinZhenYzActOrder>> takeTheMedicineList = new HashMap<>(Capacity.TWO);
  1403. for (XinZhenYzActOrder item : patInfo.getList()) {
  1404. XinZhenYzActOrder feiYongXinXi = dao.getDrugOne(item.getOrderCode() + item.getSerial(), item.getGroupNo());
  1405. int day = 1;
  1406. // 如果是临时医嘱 开了就结束了.
  1407. if ("ONCE".equals(item.getFrequCode())) {
  1408. item.setEndTime(item.getStartTime());
  1409. }
  1410. if (item.getStartTime() != null && item.getEndTime() != null) {
  1411. long cha = item.getEndTime().getTime() - item.getStartTime().getTime();
  1412. long nd = 1000 * 24 * 60 * 60;
  1413. day = (int) (cha / nd);
  1414. if (day == 0) {
  1415. day = 1;
  1416. }
  1417. }
  1418. // 计算出院带药的领量
  1419. BigDecimal total = getTotal(item, day);
  1420. if (StringUtil.notBlank(feiYongXinXi.getDrugWeightUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getDrugWeightUnit().trim())) {
  1421. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(feiYongXinXi.getDrugWeight(), 0, RoundingMode.UP));
  1422. item.setDrugOcc(item.getDrugQuan());
  1423. } else if (StringUtil.notBlank(feiYongXinXi.getDrugVolUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getDrugVolUnit())) {
  1424. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(feiYongXinXi.getDrugVolume(), 0, RoundingMode.UP));
  1425. item.setDrugOcc(item.getDrugQuan());
  1426. } else if (StringUtil.notBlank(feiYongXinXi.getPackUnit()) && item.getDoseUnit().trim().equals(feiYongXinXi.getPackUnit())) {
  1427. item.setDrugQuan(DecimalUtil.multiply(item.getDose(), total).divide(feiYongXinXi.getPackSize(), 0, RoundingMode.UP));
  1428. item.setDrugOcc(item.getDrugQuan());
  1429. }
  1430. if (item.getDrugQuan().equals(BigDecimal.ZERO)) {
  1431. item.setDrugQuan(BigDecimal.ONE);
  1432. item.setDrugOcc(BigDecimal.ONE);
  1433. }
  1434. // 在这里更新出院带药领量
  1435. dao.updateThePickup(item);
  1436. // 费用标志为 4 的就是出院带药
  1437. if ("4".equals(item.getSelfBuy())) {
  1438. if (takeTheMedicineList.containsKey(item.getGroupNo())) {
  1439. takeTheMedicineList.get(item.getGroupNo()).add(item);
  1440. } else {
  1441. List<XinZhenYzActOrder> temp = new ArrayList<>();
  1442. temp.add(item);
  1443. takeTheMedicineList.put(item.getGroupNo(), temp);
  1444. }
  1445. }
  1446. }
  1447. Map<String, Object> check = batchVerification(patInfo.getList(), patInfo, true);
  1448. if (check.isEmpty()) {
  1449. List<Integer> pageNoList = new ArrayList<>();
  1450. for (int i = 0; i < takeTheMedicineList.size(); i++) {
  1451. pageNoList.add(publicServer.getTheDrugListNo());
  1452. }
  1453. sendAMessageToTheNurse(patInfo, userCode, dao.queryPatientInfo(patNo, times), "出院带药");
  1454. return getThis().getMapResultVo(patInfo, userCode, takeTheMedicineList, pageNoList);
  1455. } else {
  1456. return ResultVoUtil.fail(ExceptionEnum.ERROR_MESSAGE, "请修改有错误的医嘱。", check);
  1457. }
  1458. }
  1459. @NotNull
  1460. @Transactional(rollbackFor = Exception.class)
  1461. public ResultVo<Map<String, Object>> getMapResultVo(XinZhenYiZhu patInfo, String userCode, Map<String, List<XinZhenYzActOrder>> takeTheMedicineList, List<Integer> pageNoList) {
  1462. // 根据不同的药房,生成不同的单子.
  1463. int index = 0;
  1464. for (Map.Entry<String, List<XinZhenYzActOrder>> entry : takeTheMedicineList.entrySet()) {
  1465. Integer pageNo = pageNoList.get(index);
  1466. dao.summarySheetOfDrugs(pageNo, patInfo, userCode, entry.getKey());
  1467. dao.insertDetailedMedicineList(pageNo, patInfo, userCode, entry.getKey(), entry.getValue());
  1468. index++;
  1469. }
  1470. for (XinZhenYzActOrder order : patInfo.getList()) {
  1471. // 出院带药 确认了直接停止医嘱
  1472. dao.takeMedicineAfterDischargeStopOrder(order.getActOrderNo(), userCode, new Date());
  1473. }
  1474. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  1475. }
  1476. public ResultVo<String> copyTheDoctorSOrder(BigDecimal orderNo) {
  1477. QueryWrapper<?> qw = new QueryWrapper<>();
  1478. qw.eq("a.act_order_no", orderNo);
  1479. List<XinZhenYzActOrder> yiZhuList = dao.selectOrderNo(qw);
  1480. if (ListUtil.isBlank(yiZhuList)) {
  1481. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "原医嘱已经不存在了。");
  1482. }
  1483. XinZhenYzActOrder fatherOrder = yiZhuList.get(0);
  1484. QueryWrapper<?> childQw = new QueryWrapper<>();
  1485. childQw.eq("a.parent_no", fatherOrder.getActOrderNo());
  1486. List<XinZhenYzActOrder> childOrderList = dao.selectOrderNo(childQw);
  1487. UserInfo us = redisLikeService.getUserInfoByToken();
  1488. revertToTheDefaultState(fatherOrder, us, null);
  1489. List<XinZhenYzActOrder> addOrderList = new ArrayList<>();
  1490. addOrderList.add(fatherOrder);
  1491. if (ListUtil.notBlank(childOrderList)) {
  1492. childOrderList.forEach(item -> {
  1493. revertToTheDefaultState(item, us, fatherOrder.getActOrderNo());
  1494. addOrderList.add(item);
  1495. });
  1496. }
  1497. XinZhenYiZhu patInfo = dao.queryPatientInfo(fatherOrder.getInpatientNo(), fatherOrder.getAdmissTimes());
  1498. getThis().insertATemplate(addOrderList, patInfo);
  1499. return ResultVoUtil.success();
  1500. }
  1501. @Transactional(rollbackFor = Exception.class)
  1502. public void insertATemplate(List<XinZhenYzActOrder> list, XinZhenYiZhu patInfo) {
  1503. String userCode = TokenUtil.getTokenUserId();
  1504. list.forEach(item -> {
  1505. dao.insertEntryOrder(patInfo, item, userCode);
  1506. });
  1507. }
  1508. private void revertToTheDefaultState(XinZhenYzActOrder param, UserInfo us, BigDecimal parentNo) {
  1509. param.setActOrderNo(publicServer.getActOrderNo());
  1510. param.setStatusFlag("1");
  1511. Date newDate = new Date();
  1512. param.setOrderTime(newDate);
  1513. param.setStartTime(newDate);
  1514. param.setEndTime(null);
  1515. param.setEnterOper(us.getCode());
  1516. param.setSigner("");
  1517. param.setModifier("");
  1518. param.setParentNo(parentNo);
  1519. }
  1520. }