package thyyxxk.webserver.service.medicalinsurance; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.YesOrNo; import thyyxxk.webserver.constants.sidicts.*; import thyyxxk.webserver.dao.his.medicalinsurance.SiLogDao; import thyyxxk.webserver.dao.his.medicalinsurance.SiManageDao; import thyyxxk.webserver.dao.his.medicalinsurance.SiSetlinfoDao; import thyyxxk.webserver.entity.ResultVo; import thyyxxk.webserver.entity.dictionary.PureCodeName; import thyyxxk.webserver.entity.medicalinsurance.SiLog; import thyyxxk.webserver.entity.medicalinsurance.manage.*; import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo; import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SlctSetlPrm; import thyyxxk.webserver.utils.*; import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Paths; import java.util.*; /** * @description: 医保管理服务,包涵项目对照等 * @author: DingJie * @create: 2021/6/2914:44 */ @Service @Slf4j public class SiManageService { private static final String RESULT_CODE = "infcode"; private static final String ERROR_MESSAGE = "err_msg"; private static final String OUTPUT = "output"; private final ExecService exec; private final SiUploadService upldServcie; private final SiDownloadService dldService; private final SiManageDao dao; private final SiSetlinfoDao setlinfoDao; private final SiLogDao logDao; @Autowired public SiManageService(ExecService exec, SiUploadService upldServcie, SiDownloadService dldService, SiManageDao dao, SiSetlinfoDao setlinfoDao, SiLogDao logDao) { this.exec = exec; this.upldServcie = upldServcie; this.dldService = dldService; this.dao = dao; this.setlinfoDao = setlinfoDao; this.logDao = logDao; } public ResultVo uploadCatalogueContrast(CatalogueContrast prm) { JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_CATALOGUE_CONTRAST); JSONArray data = new JSONArray(); setListType(prm); List codes = new ArrayList<>(); prm.getList().forEach(item -> { codes.add(item.getCode()); JSONObject catalogue = new JSONObject(); catalogue.put("fixmedins_hilist_id", item.getCode()); catalogue.put("fixmedins_hilist_name", item.getName()); catalogue.put("list_type", prm.getListType()); catalogue.put("med_list_codg", item.getNationalCode()); catalogue.put("begndate", DateUtil.formatDatetime(prm.getBeginDate())); catalogue.put("enddate", DateUtil.formatDatetime(prm.getEndDate())); data.add(catalogue); }); input.getJSONObject("input").put("data", data); JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_CATALOGUE_CONTRAST); log.info("【操作员:{}】,上传目录对照:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result); logDao.insert(new SiLog(input, result, null, null, null)); if (null == result) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (null == result.getInteger(RESULT_CODE)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message")); } if (result.getIntValue(RESULT_CODE) == 0) { dao.updateUploadedFlag(prm.getTable(), YesOrNo.YES.getCode(), codes); return ResultVoUtil.success("上传目录对照成功。"); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE)); } public ResultVo revokeCatalogueContrast(CatalogueContrast prm) { setListType(prm); JSONObject input = exec.makeTradeHeader(SiFunction.REVOKE_CATALOGUE_CONTRAST); JSONObject data = new JSONObject(); data.put("fixmedins_code", SiUtil.INSTITUTION_ID); data.put("fixmedins_hilist_id", prm.getChargeCode()); data.put("list_type", prm.getListType()); data.put("med_list_codg", prm.getNationalCode()); input.getJSONObject("input").put("data", data); JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_CATALOGUE_CONTRAST); log.info("【操作员:{}】,撤销目录:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result); logDao.insert(new SiLog(input, result, null, null, null)); if (null == result) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (null == result.getInteger(RESULT_CODE)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message")); } if (result.getIntValue(RESULT_CODE) == 0) { dao.updateUploadedFlag2(prm.getTable(), prm.getChargeCode()); return ResultVoUtil.success("撤销目录对照成功。"); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE)); } private void setListType(CatalogueContrast prm) { String table = "yp_zd_dict"; switch (prm.getType()) { case 1: prm.setListType(ListType.WEST_PATENT.getCode()); break; case 2: prm.setListType(ListType.HERBAL.getCode()); break; case 3: prm.setListType(ListType.SERVICE.getCode()); table = "zd_charge_item"; break; case 4: prm.setListType(ListType.SUPPLY.getCode()); table = "zd_charge_item"; break; } prm.setTable(table); } public ResultVo autoRecoveryTrade(AtoRcvTrd atoRcvTrd) { JSONObject input = exec.makeTradeHeader(SiFunction.AUTO_RECOVERY_TRADE); String ref = JSONObject.toJSONString(atoRcvTrd); input.getJSONObject("input").put("data", JSONObject.parseObject(ref)); JSONObject result = exec.executeTrade(input, SiFunction.AUTO_RECOVERY_TRADE); logDao.insert(new SiLog(input, result, null, null, null)); log.info("【操作员:{}】,冲正交易:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result); return SiUtil.makeReturnWithoutOutput(result, "冲正成功。"); } public ResultVo institutionSettlementLedgerCheck(InstStlLdgChk instStlLdgChk) { String startdate = DateUtil.getDayStartTime(instStlLdgChk.getStmtBegndate()); String enddate = DateUtil.getDayEndTime(instStlLdgChk.getStmtEnddate()); List brfs = dao.selectSetlChkBrfsWithInsutype(instStlLdgChk.getClrType(), instStlLdgChk.getInsutype(), instStlLdgChk.getSetlOptins(), startdate, enddate); BigDecimal medfeesum = new BigDecimal("0"); BigDecimal acctpaysum = new BigDecimal("0"); BigDecimal fundpaysum = new BigDecimal("0"); for (InstSetlLdgChkBrf brf : brfs) { medfeesum = DecimalUtil.add(medfeesum, brf.getMedfeeSumamt()); acctpaysum = DecimalUtil.add(acctpaysum, brf.getAcctPay()); fundpaysum = DecimalUtil.add(fundpaysum, brf.getFundPaySumamt()); if (null != brf.getHospPay()) { fundpaysum = DecimalUtil.minus(fundpaysum, brf.getHospPay()); } } instStlLdgChk.setMedfeeSumamt(medfeesum); instStlLdgChk.setAcctPay(acctpaysum); instStlLdgChk.setFundPaySumamt(fundpaysum); instStlLdgChk.setFixmedinsSetlCnt(brfs.size()); JSONObject input = exec.makeTradeHeader(SiFunction.INSTITUTION_SETTLEMENT_LEDGER_CHECK); String ref = JSONObject.toJSONStringWithDateFormat(instStlLdgChk, "yyyy-MM-dd"); input.getJSONObject("input").put("data", JSONObject.parseObject(ref)); JSONObject result = exec.executeTrade(input, SiFunction.INSTITUTION_SETTLEMENT_LEDGER_CHECK); log.info("【操作员:{}】,医药机构费用结算对总账:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result); if (null == result) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (null == result.getInteger(RESULT_CODE)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message")); } if (result.getIntValue(RESULT_CODE) == 0) { JSONObject output = result.getJSONObject(OUTPUT); JSONObject info = output.getJSONObject("stmtinfo"); String setlOptins = info.getString("setl_optins"); Admdvs admdvs = Admdvs.get(setlOptins); String optins; if (null == admdvs) { optins = dao.selectRegion(setlOptins); } else { optins = admdvs.getName(); } String rslt; if (info.getString("stmt_rslt").equals(StmtRslt.CORRECT.getCode())) { rslt = StmtRslt.CORRECT.getName(); } else { rslt = StmtRslt.WRONG.getName(); } String dscr = info.getString("stmt_rslt_dscr"); if (StringUtil.isBlank(dscr)) { dscr = "结算笔数:" + brfs.size() + ",医疗费总额:" + medfeesum + ",基金支付总额:" + fundpaysum; } String data = "【清算机构:" + optins + "】对账结果:" + rslt + "。对账结果说明:" + dscr; if (rslt.equals(StmtRslt.CORRECT.getName())) { return ResultVoUtil.success(data); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, data); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE)); } public ResultVo> institutionSettlementDetailCheck(InsSetlDetlChk prm) { String snowId = SnowFlakeId.instance().nextId(); String filename = snowId + ".txt"; StringBuilder fsIn = new StringBuilder(); String startdate = DateUtil.getDayStartTime(prm.getStmtBegndate()); String enddate = DateUtil.getDayEndTime(prm.getStmtEnddate()); List brfs = dao.selectSetlChkBrfs(prm.getClrType(), prm.getSetlOptins(), startdate, enddate); BigDecimal medfeesum = new BigDecimal("0"); BigDecimal psnCashPay = new BigDecimal("0"); BigDecimal fundpaysum = new BigDecimal("0"); for (InstSetlLdgChkBrf brf : brfs) { medfeesum = DecimalUtil.add(medfeesum, brf.getMedfeeSumamt()); psnCashPay = DecimalUtil.add(psnCashPay, brf.getPsnCashPay()); fundpaysum = DecimalUtil.add(fundpaysum, brf.getFundPaySumamt()); if (null != brf.getHospPay()) { fundpaysum = DecimalUtil.minus(fundpaysum, brf.getHospPay()); brf.setFundPaySumamt(DecimalUtil.minus(brf.getFundPaySumamt(), brf.getHospPay())); } } prm.setMedfeeSumamt(medfeesum); prm.setCashPayamt(psnCashPay); prm.setFundPaySumamt(fundpaysum); prm.setFixmedinsSetlCnt(brfs.size()); for (InstSetlLdgChkBrf item : brfs) { fsIn.append(item.getSetlId()).append("\t") .append(item.getMdtrtId()).append("\t") .append(item.getPsnNo()).append("\t") .append(item.getMedfeeSumamt()).append("\t") .append(item.getFundPaySumamt()).append("\t") .append(item.getAcctPay()).append("\t") .append(prm.getRefdSetlFlag()).append("\n"); } try { Files.write(Paths.get(filename), fsIn.toString().getBytes()); String zipFile = ZipUtil.zip(filename); ZipUtil.deleteFile(filename); String fsUploadIn = "{\"filename\": \"" + filename + ".zip\", \"fixmedins_code\": \"" + SiUtil.INSTITUTION_ID + "\", \"in\": " + Arrays.toString(ZipUtil.zipFileToBytes(zipFile)) + "}"; ResultVo uplRes = upldServcie.uploadFile(JSONObject.parseObject(fsUploadIn), filename); ZipUtil.deleteFile(zipFile); if (uplRes.getCode().equals(ExceptionEnum.SUCCESS.getCode())) { prm.setFileQuryNo(uplRes.getData()); } } catch (IOException e) { log.error("医药机构费用结算对明细账出错", e); } JSONObject input = exec.makeTradeHeader(SiFunction.INSTITUTION_SETTLEMENT_DETAIL_CHECK); String ref = JSONObject.toJSONStringWithDateFormat(prm, "yyyy-MM-dd"); input.getJSONObject("input").put("data", JSONObject.parseObject(ref)); JSONObject result = exec.executeTrade(input, SiFunction.INSTITUTION_SETTLEMENT_DETAIL_CHECK); log.info("【操作员:{}】,医药机构费用结算对明细账:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result); if (null == result) { return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR); } if (null == result.getInteger(RESULT_CODE)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message")); } if (result.getIntValue(RESULT_CODE) == 0) { JSONObject output = result.getJSONObject(OUTPUT); JSONObject fileinfo = output.getJSONObject("fileinfo"); String filePath = dldService.downloadFile(fileinfo.getString("file_qury_no"), fileinfo.getString("filename")); Queue queue = SiUtil.readTxtFile(filePath); if (null == queue || queue.size() == 0) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未能成功获取到对账结果。"); } List list = new ArrayList<>(); while (queue.size() > 0) { String item = queue.poll(); String[] arr = item.split("\t"); InsSetlDetlChkRslt rslt = new InsSetlDetlChkRslt(arr); list.add(rslt); } return ResultVoUtil.success(list); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE)); } public ResultVo> selectSetlinfos(SlctSetlPrm prm) { QueryWrapper wrapper = new QueryWrapper<>(); if (StringUtil.notBlank(prm.getInsutype())) { wrapper.eq("insutype", prm.getInsutype()); } String begndate = DateUtil.formatDatetime(prm.getStmtBegndate(), "yyyy-MM-dd") + " 00:00:00"; String enddate = DateUtil.formatDatetime(prm.getStmtEnddate(), "yyyy-MM-dd") + " 23:59:59"; wrapper.eq("setl_type", prm.getClrType()); wrapper.eq("revoked", YesOrNo.NO.getCode()); wrapper.apply("fund_pay_sumamt!=0"); wrapper.apply("setl_time>='" + begndate + "'"); wrapper.apply("setl_time<='" + enddate + "'"); wrapper.apply("(insuplc_admdvs='439900' or insuplc_admdvs like '4301%') "); List list = setlinfoDao.selectList(wrapper); if (null == list || list.isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有查询到符合条件的数据。"); } Map map = new HashMap<>(Capacity.TWO); Map optinsMap = new HashMap<>(); List optinList = new ArrayList<>(); list.forEach(item -> { Admdvs admdvs = Admdvs.get(item.getClrOptins()); if (null == admdvs) { item.setClrOptinsName(dao.selectRegion(item.getClrOptins())); } else { item.setClrOptinsName(admdvs.getName()); } if (!optinsMap.containsKey(item.getClrOptins())) { optinsMap.put(item.getClrOptins(), item.getClrOptinsName()); } item.setGendName(Gend.get(item.getGend()).getName()); Insutype insutype = Insutype.get(item.getInsutype()); if (null != insutype) { item.setInsutypeName(insutype.getName()); } PsnType psnType = PsnType.get(item.getPsnType()); if (null != psnType) { item.setPsnTypeName(psnType.getName()); } MedType medType = MedType.get(item.getMedType()); if (null != medType) { item.setMedTypeName(medType.getName()); } }); map.put("list", list); for (Map.Entry entry : optinsMap.entrySet()) { optinList.add(new PureCodeName(entry.getKey(), entry.getValue())); } map.put("optins", optinList); return ResultVoUtil.success(map); } }