package thyyxxk.webserver.service.medicalinsurance; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; 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.constants.Capacity; import thyyxxk.webserver.constants.sidicts.MdtrtCertType; import thyyxxk.webserver.constants.sidicts.SiFunction; import thyyxxk.webserver.dao.his.medicalinsurance.SiMzDao; import thyyxxk.webserver.entity.ResultVo; import thyyxxk.webserver.entity.markmtfees.*; import thyyxxk.webserver.entity.medicalinsurance.outpatient.*; import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo; import thyyxxk.webserver.entity.medicalinsurance.setlinfo.FundDetail; import thyyxxk.webserver.service.externalhttp.SiMzSrvc; import thyyxxk.webserver.service.externalhttp.ThmzSystem; import thyyxxk.webserver.utils.*; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; /** * @description: 门诊医保交易 * @author: DingJie * @create: 2021-05-28 16:11:19 **/ @Slf4j @Service public class SiMzService { private final SiMzDao dao; private final ExecService exec; private final ThmzSystem thmz; private final SiMzSrvc mzSrvc; private static final String RESULT_CODE = "infcode"; private static final String ERROR_MESSAGE = "err_msg"; private static final String OUTPUT = "output"; @Autowired public SiMzService(SiMzDao dao, ExecService exec, ThmzSystem thmz, SiMzSrvc mzSrvc) { this.dao = dao; this.exec = exec; this.thmz = thmz; this.mzSrvc = mzSrvc; } public ResultVo outpatientRegistration(MzPatientInfo p) { p.setStaffId(TokenUtil.getTokenUserId()); return mzSrvc.outpatientRegistration(p); } public ResultVo revokeOutpatientRegistration(MzPatientInfo p) { p.setStaffId(TokenUtil.getTokenUserId()); return mzSrvc.revokeOutpatientRegistration(p); } public ResultVo>> getMzReceipts(MzPatientInfo p) { String patNo = p.getPatNo(); JSONObject queryMzChargeListParam = new JSONObject(); queryMzChargeListParam.put("patCardType", 21); queryMzChargeListParam.put("patCardNo", patNo); queryMzChargeListParam.put("hisOrdNum", ""); Map mzChargeListMap = thmz.getMzChargeDetailForUnPaid(queryMzChargeListParam); if (null == mzChargeListMap) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (0 != (int) mzChargeListMap.get("resultCode")) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, mzChargeListMap.get("resultMessage").toString()); } List> finalResult = new ArrayList<>(); List> mzChargeList = FilterUtil.cast(mzChargeListMap.get("data")); for (Map item : mzChargeList) { String hisOrdNum = item.get("hisOrdNum"); if (null != hisOrdNum) { String[] hisOrdNumParts = hisOrdNum.split("_"); int tempTimes = Integer.parseInt(hisOrdNumParts[1]); MzVisit mzVisit = dao.selectMzVisit(patNo, tempTimes); if (null == mzVisit) { continue; } Map childResult = new HashMap<>(Capacity.FIVE); childResult.put("times", tempTimes); childResult.put("mzVisit", mzVisit); JSONObject queryMzChargeDetailParam = new JSONObject(); queryMzChargeDetailParam.put("patientId", patNo); queryMzChargeDetailParam.put("times", tempTimes); queryMzChargeDetailParam.put("receiptNo", hisOrdNumParts[2]); mzVisit.setReceiptNo(Integer.parseInt(hisOrdNumParts[2])); Map mzChargeDetailMap = thmz.unPaidToFullChargeDetail(queryMzChargeDetailParam); if (null != mzChargeDetailMap && 0 == (int) mzChargeDetailMap.get("resultCode")) { List> mzChargeDetailList = FilterUtil.cast(mzChargeDetailMap.get("data")); mzChargeDetailList.removeIf(detail -> "TC".equals(detail.get("billItemCode")) || !"5".equals(detail.get("payMark")) || "BILL99".equals(detail.get("chargeItemCode")) || "四舍五入".equals(detail.get("tcName"))); if (mzChargeDetailList.isEmpty()) { continue; } Map> orderReceiptsMap = new HashMap<>(Capacity.DEFAULT); String doctorName = dao.selectDoctorName(mzChargeDetailList.get(0).get("doctorCode").toString()); mzChargeDetailList.forEach(detail -> { MzReceipt receipt = fillMzReceipt(detail); receipt.setPatientId(patNo); receipt.setTimes(tempTimes); receipt.setDoctorName(doctorName); if (!orderReceiptsMap.containsKey(receipt.getOrderNo())) { List list = new ArrayList<>(); list.add(receipt); orderReceiptsMap.put(receipt.getOrderNo(), list); } else { orderReceiptsMap.get(receipt.getOrderNo()).add(receipt); } }); List orderNos = getOrderNos(patNo, tempTimes, mzVisit.getReceiptNo(), orderReceiptsMap); childResult.put("orderNos", orderNos); childResult.put("mzReceipts", orderReceiptsMap); finalResult.add(childResult); } } } return ResultVoUtil.success(finalResult); } public ResultVo> getHistoryMzReceipts(String patNo, String start, String end) { List mzChargeList = dao.selectMzDepositFiles(patNo, start, end); if (null == mzChargeList || mzChargeList.isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到历史处方!"); } return ResultVoUtil.success(mzChargeList); } public ResultVo> getHistoryReceiptDetail(MzDepositFile mzDepositFile) { List mzReceipts = dao.selectMzCharge(mzDepositFile.getPatNo(), mzDepositFile.getTimes(), mzDepositFile.getReceiptNo()); mzReceipts.forEach(itm -> itm.setChecked(StringUtil.notBlank(itm.getNationalCode()))); return ResultVoUtil.success(mzReceipts); } private MzReceipt fillMzReceipt(Map detail) { MzReceipt receipt = new MzReceipt(); receipt.setReceiptNo((int) detail.get("receiptNo")); receipt.setOrderNo((int) detail.get("orderNo")); receipt.setItemNo((int) detail.get("itemNo")); receipt.setDrugName(detail.get("tcName").toString()); receipt.setChargeItemCode(detail.get("chargeItemCode").toString()); receipt.setPriceTime(DateUtil.formatPriceTime(detail.get("priceTime").toString())); receipt.setUnitPrice(new BigDecimal(detail.get("unitPrice").toString()).setScale(4, RoundingMode.HALF_UP)); receipt.setQuantity(Double.valueOf(detail.get("quantity").toString())); receipt.setDrugWin((int) detail.get("drugWin")); receipt.setDoctorCode(detail.get("doctorCode").toString()); receipt.setSerialNo(detail.get("serialNo").toString()); receipt.setGroupNo(detail.get("groupNo").toString()); receipt.setBillItemCode(detail.get("billItemCode").toString()); receipt.setChargeBillCode(detail.get("chargeBillCode").toString()); String table = receipt.getGroupNo().trim().equals("00") ? "zd_charge_item" : "yp_zd_dict"; receipt.setNationalCode(dao.selectNationalCode(receipt.getChargeItemCode(), table)); receipt.setChecked(StringUtil.notBlank(receipt.getNationalCode())); if (null != detail.get("instructionText")) { receipt.setInstructionText(detail.get("instructionText").toString()); } if (null != detail.get("specification")) { receipt.setSpecification(detail.get("specification").toString()); } if (null != detail.get("frequency")) { receipt.setFrequency(detail.get("frequency").toString()); } if (null != detail.get("drugQuan")) { receipt.setDrugQuan(Double.valueOf(detail.get("drugQuan").toString())); } if (null != detail.get("orderDays")) { receipt.setOrderDays((Integer) detail.get("orderDays")); } // groupNo:00-项目;其他-药品 if ("00".equals(receipt.getGroupNo())) { receipt.setDrugUnit(dao.selectXmChargeUnit(receipt.getChargeItemCode())); } else { if (null != detail.get("serial")) { receipt.setSerial(detail.get("serial").toString()); receipt.setSpecification(dao.selectSpecification(receipt.getChargeItemCode(), receipt.getSerial())); } if (null != detail.get("supplyCode")) { receipt.setSupplyCode(dao.selectSupplyName(detail.get("supplyCode").toString())); } if (null != detail.get("drugUnit")) { receipt.setDrugUnit(dao.selectDrugUnit(detail.get("drugUnit").toString())); } } return receipt; } private List getOrderNos(String patientId, int times, int receiptNo, Map> orderReceiptsMap) { List orderNos = new ArrayList<>(); for (Map.Entry> entry : orderReceiptsMap.entrySet()) { OrderNo orderNo = new OrderNo(); orderNo.setPatientId(patientId); orderNo.setTimes(times); orderNo.setReceiptNo(entry.getValue().get(0).getReceiptNo()); orderNo.setOrderNo(entry.getKey()); BigDecimal total = new BigDecimal("0.00"); for (MzReceipt receipt : entry.getValue()) { total = total.add(receipt.getChargeFee()); } orderNo.setTotalFee(total); int count = dao.selectFeeCount(patientId, times, receiptNo, entry.getKey()); orderNo.setStatus(count > 0); orderNos.add(orderNo); } return orderNos; } public ResultVo insertSiMzFees(List receipts) { receipts.removeIf(itm -> !itm.getChecked()); if (receipts.size() == 0) { return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请选择至少一条处方明细!"); } MzReceipt receipt = receipts.get(0); int count = dao.selectFeeCount(receipt.getPatientId(), receipt.getTimes(), receipt.getReceiptNo(), receipt.getOrderNo()); if (count > 0) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该处方已生成过门特费用,请勿重复操作。"); } receipts.forEach(item -> { if (item.getChecked()) { dao.insertBatchedMtFeeInfo(item); } }); return ResultVoUtil.success(); } public ResultVo deleteMzReceipt(OrderNo param) { dao.deleteMzReceipt(param); return ResultVoUtil.success(); } public ResultVo deleteAllMzReceipts(MzPatientInfo p) { if (null == p.getTimes()) { p.setTimes(dao.selectMaxTimes(p.getPatNo())); } log.info("【操作员:{}】删除所有门特处方:门诊号:{},门诊次数:{}", TokenUtil.getTokenUserId(), p.getPatNo(), p.getTimes()); dao.deleteAllReceipts(p.getPatNo(), p.getTimes()); return ResultVoUtil.success("删除成功。"); } public ResultVo uploadOutpatientFeeDetails(SpcChrDiseAcct p) { p.setStaffId(TokenUtil.getTokenUserId()); return mzSrvc.uploadOutpatientFeeDetails(p); } public ResultVo revokeOutpatientFeeDetails(MzPatientInfo p) { p.setStaffId(TokenUtil.getTokenUserId()); return mzSrvc.revokeOutpatientFeeDetails(p); } public ResultVo outpatientPreSettlement(MzPatientInfo p) { if (null == p.getStaffId()) { p.setStaffId(TokenUtil.getTokenUserId()); } return mzSrvc.outpatientPreSettlement(p); } public ResultVo outpatientSettlement(MzPatientInfo p) { if (null == p.getStaffId()) { p.setStaffId(TokenUtil.getTokenUserId()); } return mzSrvc.outpatientSettlement(p); } public ResultVo revokeOutpatientSettlement(MzPatientInfo p) { if (null == p.getStaffId()) { p.setStaffId(TokenUtil.getTokenUserId()); } return mzSrvc.revokeOutpatientSettlement(p); } public ResultVo saveSiMzDiags(List diags) { if (ListUtil.isBlank(diags)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有要保存的诊断!"); } SiMzDiag diag = diags.get(0); dao.deleteMzDiags(diag.getPatNo(), diag.getTimes()); String staffId = TokenUtil.getTokenUserId(); for (SiMzDiag itm : diags) { itm.setRealOpter(staffId); dao.insertNewMzDiag(itm); } return ResultVoUtil.success("门诊诊断保存成功。"); } public void updateMzSaved(String patientId, int times, int val) { dao.updateMzSaved(patientId, times, val); } public ResultVo> fetchSpcSlwinfo(String socialNo) { if (StringUtil.isBlank(socialNo)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者身份证号不能为空,请补充!"); } JSONObject input = exec.makeTradeHeader(SiFunction.OBTAIN_BASIC_PERSON_INFO); JSONObject data = new JSONObject(); data.put("mdtrt_cert_type", MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode()); data.put("psn_cert_type", "01"); data.put("mdtrt_cert_no", socialNo); data.put("certno", socialNo); input.getJSONObject("input").put("data", data); JSONObject result = exec.executeTrade(input, SiFunction.OBTAIN_BASIC_PERSON_INFO); if (null == result) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (null == result.getInteger(RESULT_CODE)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的慢特病备案,或慢特病备案已过期!"); } if (result.getIntValue(RESULT_CODE) == 0) { JSONObject output = result.getJSONObject(OUTPUT); JSONObject baseinfo = output.getJSONObject("baseinfo"); String psnNo = baseinfo.getString("psn_no"); JSONArray insuinfo = output.getJSONArray("insuinfo"); Date now = new Date(); List list = new ArrayList<>(); for (int i = 0; i < insuinfo.size(); i++) { JSONObject item = insuinfo.getJSONObject(i); String admdvs = item.getString("insuplc_admdvs"); input = exec.makeTradeHeaderWithInsureArea(SiFunction.QUERY_SPECIAL_CHRONIC_DISEASES_ACCREDITATION, admdvs); data = new JSONObject(); data.put("psn_no", psnNo); input.getJSONObject("input").put("data", data); result = exec.executeTrade(input, SiFunction.QUERY_SPECIAL_CHRONIC_DISEASES_ACCREDITATION); if (null == result) { continue; } if (null == result.getInteger(RESULT_CODE)) { continue; } if (result.getIntValue(RESULT_CODE) == 0) { output = result.getJSONObject(OUTPUT); JSONArray details = output.getJSONArray("feedetail"); if (null == details || details.size() == 0) { continue; } for (int j = 0; j < details.size(); j++) { JSONObject detail = details.getJSONObject(j); SpcChrDiseAcct spcChrDiseAcct = JSONObject.parseObject(detail.toJSONString(), SpcChrDiseAcct.class); if (now.before(spcChrDiseAcct.getEnddate())) { list.add(spcChrDiseAcct); } } if (list.size() > 0) { return ResultVoUtil.success(list); } } } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的慢特病备案,或慢特病备案已过期!"); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的慢特病备案,或慢特病备案已过期!"); } }