123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- 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.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import thyyxxk.webserver.config.exception.ExceptionEnum;
- import thyyxxk.webserver.constants.Capacity;
- import thyyxxk.webserver.constants.sidicts.Insutype;
- import thyyxxk.webserver.constants.sidicts.MdtrtCertType;
- import thyyxxk.webserver.constants.sidicts.MedType;
- 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.InsuInfo;
- import thyyxxk.webserver.entity.medicalinsurance.query.PsnBaseInfo;
- import thyyxxk.webserver.entity.medicalinsurance.query.QryPsnBsInfo;
- 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 final SiQueryService qryService;
- private static final String RESULT_CODE = "infcode";
- private static final String ERROR_MESSAGE = "err_msg";
- private static final String OUTPUT = "output";
- @Value("${si-mz-fee-url}")
- private String siMzFeeUrl;
- @Value("${thmz-api-url}")
- private String thmzApiUrl;
- @Autowired
- public SiMzService(SiMzDao dao, ExecService exec, ThmzSystem thmz, SiMzSrvc mzSrvc, SiQueryService qryService) {
- this.dao = dao;
- this.exec = exec;
- this.thmz = thmz;
- this.mzSrvc = mzSrvc;
- this.qryService = qryService;
- }
- public ResultVo<String> outpatientRegistration(MzPatientInfo p) {
- p.setStaffId(TokenUtil.getTokenUserId());
- return mzSrvc.outpatientRegistration(siMzFeeUrl, p);
- }
- public ResultVo<String> revokeOutpatientRegistration(MzPatientInfo p) {
- p.setStaffId(TokenUtil.getTokenUserId());
- return mzSrvc.revokeOutpatientRegistration(siMzFeeUrl, p);
- }
- public ResultVo<List<Map<String, Object>>> getMzReceipts(MzPatientInfo p) {
- String patNo = p.getPatNo();
- JSONObject queryMzChargeListParam = new JSONObject();
- queryMzChargeListParam.put("patCardType", 21);
- queryMzChargeListParam.put("patCardNo", patNo);
- queryMzChargeListParam.put("hisOrdNum", "");
- queryMzChargeListParam.put("ybRequest", 1);
- Map<String, Object> mzChargeListMap = thmz.getMzChargeDetailForUnPaid(thmzApiUrl, 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<Map<String, Object>> finalResult = new ArrayList<>();
- List<Map<String, String>> mzChargeList = FilterUtil.cast(mzChargeListMap.get("data"));
- for (Map<String, String> 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<String, Object> 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<String, Object> mzChargeDetailMap = thmz.unPaidToFullChargeDetail(thmzApiUrl, queryMzChargeDetailParam);
- if (null != mzChargeDetailMap && 0 == (int) mzChargeDetailMap.get("resultCode")) {
- List<Map<String, Object>> 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<Integer, List<MzReceipt>> 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<MzReceipt> list = new ArrayList<>();
- list.add(receipt);
- orderReceiptsMap.put(receipt.getOrderNo(), list);
- } else {
- orderReceiptsMap.get(receipt.getOrderNo()).add(receipt);
- }
- });
- List<OrderNo> orderNos = getOrderNos(patNo, tempTimes, mzVisit.getReceiptNo(), orderReceiptsMap);
- childResult.put("orderNos", orderNos);
- childResult.put("mzReceipts", orderReceiptsMap);
- finalResult.add(childResult);
- }
- }
- }
- return ResultVoUtil.success(finalResult);
- }
- public ResultVo<List<MzDepositFile>> getHistoryMzReceipts(String patNo, String start, String end) {
- List<MzDepositFile> mzChargeList = dao.selectMzDepositFiles(patNo, start, end);
- if (null == mzChargeList || mzChargeList.isEmpty()) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到历史处方!");
- }
- return ResultVoUtil.success(mzChargeList);
- }
- public ResultVo<Map<String, Object>> getHistoryReceiptDetail(MzDepositFile mzDepositFile) {
- MzVisit mzVisit = dao.selectMzVisit(mzDepositFile.getPatNo(), mzDepositFile.getTimes());
- List<MzReceipt> mzReceipts = dao.selectMzCharge(mzDepositFile.getPatNo(),
- mzDepositFile.getTimes(), mzDepositFile.getReceiptNo());
- mzReceipts.forEach(itm -> itm.setChecked(StringUtil.notBlank(itm.getNationalCode())));
- Map<String, Object> map = new HashMap<>();
- map.put("mzVisit", mzVisit);
- map.put("mzReceipts", mzReceipts);
- return ResultVoUtil.success(map);
- }
- private MzReceipt fillMzReceipt(Map<String, Object> 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.setGroupNo(detail.get("groupNo").toString());
- receipt.setChargeDate(detail.get("priceTime").toString().split("\\+")[0].replace("T", " "));
- 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("serialNo")) {
- receipt.setSerialNo(detail.get("serialNo").toString());
- }
- 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<OrderNo> getOrderNos(String patientId, int times, int receiptNo, Map<Integer, List<MzReceipt>> orderReceiptsMap) {
- List<OrderNo> orderNos = new ArrayList<>();
- for (Map.Entry<Integer, List<MzReceipt>> 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<String> insertSiMzFees(List<MzReceipt> receipts) {
- receipts.removeIf(item -> !item.getChecked() || item.getChargeItemCode().equals("019110")
- || item.getChargeItemCode().equals("019180"));
- if (receipts.size() == 0) {
- return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者没有可以报销的项目!");
- }
- receipts.forEach(dao::insertBatchedMtFeeInfo);
- return ResultVoUtil.success();
- }
- public ResultVo<String> insertSiMzFeesForSupplement(List<MzReceipt> receipts) {
- receipts.removeIf(item -> !item.getChecked() || item.getChargeItemCode().equals("019110")
- || item.getChargeItemCode().equals("019180"));
- if (receipts.size() == 0) {
- return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者没有可以报销的项目!");
- }
- MzReceipt receipt = receipts.get(0);
- List<Integer> receiptNos = dao.selectGeneratedReceiptNos(receipt.getPatientId(), receipt.getTimes());
- for (int receiptNo : receiptNos) {
- if (dao.isReceiptPayed(receipt.getPatientId(), receipt.getTimes(), receiptNo) == 0) {
- dao.deleteCertainReceipt(receipt.getPatientId(), receipt.getTimes(), receiptNo);
- }
- }
- receipts.forEach(dao::insertBatchedMtFeeInfo);
- return ResultVoUtil.success();
- }
- public ResultVo<String> deleteMzReceipt(OrderNo param) {
- dao.deleteMzReceipt(param);
- return ResultVoUtil.success();
- }
- public ResultVo<String> 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<SiPatInfo> uploadOutpatientFeeDetails(SpcChrDiseAcct p) {
- p.setStaffId(TokenUtil.getTokenUserId());
- return mzSrvc.uploadOutpatientFeeDetails(siMzFeeUrl, p);
- }
- public ResultVo<String> revokeOutpatientFeeDetails(MzPatientInfo p) {
- p.setStaffId(TokenUtil.getTokenUserId());
- return mzSrvc.revokeOutpatientFeeDetails(siMzFeeUrl, p);
- }
- public ResultVo<FundDetail> outpatientPreSettlement(MzPatientInfo p) {
- if (null == p.getStaffId()) {
- p.setStaffId(TokenUtil.getTokenUserId());
- }
- return mzSrvc.outpatientPreSettlement(siMzFeeUrl, p);
- }
- public ResultVo<FundDetail> outpatientSettlement(MzPatientInfo p) {
- if (null == p.getStaffId()) {
- p.setStaffId(TokenUtil.getTokenUserId());
- }
- return mzSrvc.outpatientSettlement(siMzFeeUrl, p);
- }
- public ResultVo<FundDetail> revokeOutpatientSettlement(MzPatientInfo p) {
- if (null == p.getStaffId()) {
- p.setStaffId(TokenUtil.getTokenUserId());
- }
- return mzSrvc.revokeOutpatientSettlement(siMzFeeUrl, p);
- }
- public void insertSiPatInfo(MzPatientInfo mz) {
- PsnBaseInfo patinfo = dao.selectPsnBaseinfo(mz.getPatNo(), mz.getTimes());
- patinfo.setTimes(-1);
- dao.insertSiMzInfoPsnNo(patinfo);
- }
- public ResultVo<String> saveSiMzDiags(List<SiMzDiag> 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 ResultVo<List<SpcChrDiseAcct>> 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<SpcChrDiseAcct> 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, "此患者没有有效的慢特病备案,或慢特病备案已过期!");
- }
- public Map<String, Object> queryInsuinfo(SiMzBusinessParams param) {
- Map<String, Object> result = new HashMap<>();
- QryPsnBsInfo mzptnt = new QryPsnBsInfo();
- mzptnt.setStaffId(param.getStaffId());
- mzptnt.setPatNo(param.getPatientId());
- mzptnt.setTimes(param.getTimes());
- mzptnt.setMedType(MedType.GENERAL_CLINIC.getCode());
- mzptnt.setOnlyQry(1);
- mzptnt.setTimes(-1);
- ResultVo<PsnBaseInfo> baseInfo = qryService.obtainBasicPersonInfo(mzptnt);
- if (null == baseInfo) {
- result.put("code", -2);
- result.put("msg", "网络异常。");
- return result;
- }
- if (baseInfo.getCode() != ExceptionEnum.SUCCESS.getCode()) {
- result.put("code", -2);
- result.put("msg", baseInfo.getMessage());
- return result;
- }
- // 如果有职工保险,就入库
- PsnBaseInfo psninfo = baseInfo.getData();
- InsuInfo insuInfo = getEmployeeInsuInfo(psninfo.getInsuinfo());
- if (null == insuInfo) {
- result.put("code", -1);
- result.put("msg", "患者没有职工医保,如需报销请走医保科流程。");
- return result;
- }
- if (!isInsuplcAdmdvsEligible(insuInfo.getInsuplcAdmdvs())) {
- result.put("code", -1);
- result.put("msg", "患者参保地不满足门诊统筹要求。");
- return result;
- }
- psninfo.setInsutype(Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES.getCode());
- int count = dao.selectSiMzInfoCount(mzptnt.getPatNo(), mzptnt.getTimes());
- if (count == 0) {
- psninfo.setMedType(MedType.GENERAL_CLINIC.getCode());
- psninfo.setInsuplc(insuInfo.getInsuplcAdmdvs());
- psninfo.setPsnType(insuInfo.getPsnType());
- psninfo.setEmpName(insuInfo.getEmpName());
- psninfo.setBalc(insuInfo.getBalc());
- dao.insertSiMzInfoPsnNo(psninfo);
- } else {
- dao.updateSiZyInfoPsnNo(mzptnt.getPatNo(), mzptnt.getTimes(), psninfo.getInsutype(), insuInfo.getBalc());
- }
- result.put("code", 0);
- result.put("expContentRequired", expContentRequired(insuInfo.getInsuplcAdmdvs()));
- result.put("msg", "查询职工参保信息成功。");
- return result;
- }
- private InsuInfo getEmployeeInsuInfo(List<InsuInfo> list) {
- for (InsuInfo info : list) {
- if (info.getInsutype().equals("310")) {
- return info;
- }
- }
- return null;
- }
- private boolean isInsuplcAdmdvsEligible(String insuplc) {
- return !insuplc.startsWith("4304") && !insuplc.startsWith("4307");
- }
- private int expContentRequired(String insuplc) {
- return (insuplc.startsWith("4301") || insuplc.equals("439900")) ? 0 : 1;
- }
- public ResultVo<FundDetail> directRegistration(SiMzBusinessParams param) {
- return mzSrvc.directRegistration(siMzFeeUrl, param);
- }
- public ResultVo<Integer> isPatientDuringSiSettle(String patientId) {
- return mzSrvc.isPatientDuringSiSettle(siMzFeeUrl, patientId);
- }
- }
|