SiManageService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package thyyxxk.webserver.service.medicalinsurance;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import thyyxxk.webserver.config.exception.ExceptionEnum;
  9. import thyyxxk.webserver.constants.Capacity;
  10. import thyyxxk.webserver.constants.YesOrNo;
  11. import thyyxxk.webserver.constants.sidicts.*;
  12. import thyyxxk.webserver.dao.his.medicalinsurance.SiLogDao;
  13. import thyyxxk.webserver.dao.his.medicalinsurance.SiManageDao;
  14. import thyyxxk.webserver.dao.his.medicalinsurance.SiSetlinfoDao;
  15. import thyyxxk.webserver.entity.ResultVo;
  16. import thyyxxk.webserver.entity.dictionary.PureCodeName;
  17. import thyyxxk.webserver.entity.medicalinsurance.SiLog;
  18. import thyyxxk.webserver.entity.medicalinsurance.manage.*;
  19. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
  20. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SlctSetlPrm;
  21. import thyyxxk.webserver.utils.*;
  22. import java.io.IOException;
  23. import java.math.BigDecimal;
  24. import java.nio.file.Files;
  25. import java.nio.file.Paths;
  26. import java.util.*;
  27. /**
  28. * @description: 医保管理服务,包涵项目对照等
  29. * @author: DingJie
  30. * @create: 2021/6/2914:44
  31. */
  32. @Service
  33. @Slf4j
  34. public class SiManageService {
  35. private static final String RESULT_CODE = "infcode";
  36. private static final String ERROR_MESSAGE = "err_msg";
  37. private static final String OUTPUT = "output";
  38. private final ExecService exec;
  39. private final SiUploadService upldServcie;
  40. private final SiDownloadService dldService;
  41. private final SiManageDao dao;
  42. private final SiSetlinfoDao setlinfoDao;
  43. private final SiLogDao logDao;
  44. @Autowired
  45. public SiManageService(ExecService exec, SiUploadService upldServcie,
  46. SiDownloadService dldService, SiManageDao dao, SiSetlinfoDao setlinfoDao, SiLogDao logDao) {
  47. this.exec = exec;
  48. this.upldServcie = upldServcie;
  49. this.dldService = dldService;
  50. this.dao = dao;
  51. this.setlinfoDao = setlinfoDao;
  52. this.logDao = logDao;
  53. }
  54. public ResultVo<String> uploadCatalogueContrast(CatalogueContrast prm) {
  55. JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_CATALOGUE_CONTRAST);
  56. JSONArray data = new JSONArray();
  57. setListType(prm);
  58. List<String> codes = new ArrayList<>();
  59. prm.getList().forEach(item -> {
  60. codes.add(item.getCode());
  61. JSONObject catalogue = new JSONObject();
  62. catalogue.put("fixmedins_hilist_id", item.getCode());
  63. catalogue.put("fixmedins_hilist_name", item.getName());
  64. catalogue.put("list_type", prm.getListType());
  65. catalogue.put("med_list_codg", item.getNationalCode());
  66. catalogue.put("begndate", DateUtil.formatDatetime(prm.getBeginDate()));
  67. catalogue.put("enddate", DateUtil.formatDatetime(prm.getEndDate()));
  68. data.add(catalogue);
  69. });
  70. input.getJSONObject("input").put("data", data);
  71. JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_CATALOGUE_CONTRAST);
  72. log.info("【操作员:{}】,上传目录对照:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  73. logDao.insert(new SiLog(input, result, null, null, null));
  74. if (null == result) {
  75. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  76. }
  77. if (null == result.getInteger(RESULT_CODE)) {
  78. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  79. }
  80. if (result.getIntValue(RESULT_CODE) == 0) {
  81. dao.updateUploadedFlag(prm.getTable(), YesOrNo.YES.getCode(), codes);
  82. return ResultVoUtil.success("上传目录对照成功。");
  83. }
  84. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  85. }
  86. public ResultVo<String> revokeCatalogueContrast(CatalogueContrast prm) {
  87. setListType(prm);
  88. JSONObject input = exec.makeTradeHeader(SiFunction.REVOKE_CATALOGUE_CONTRAST);
  89. JSONObject data = new JSONObject();
  90. data.put("fixmedins_code", SiUtil.INSTITUTION_ID);
  91. data.put("fixmedins_hilist_id", prm.getChargeCode());
  92. data.put("list_type", prm.getListType());
  93. data.put("med_list_codg", prm.getNationalCode());
  94. input.getJSONObject("input").put("data", data);
  95. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_CATALOGUE_CONTRAST);
  96. log.info("【操作员:{}】,撤销目录:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  97. logDao.insert(new SiLog(input, result, null, null, null));
  98. if (null == result) {
  99. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  100. }
  101. if (null == result.getInteger(RESULT_CODE)) {
  102. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  103. }
  104. if (result.getIntValue(RESULT_CODE) == 0) {
  105. dao.updateUploadedFlag2(prm.getTable(), prm.getChargeCode());
  106. return ResultVoUtil.success("撤销目录对照成功。");
  107. }
  108. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  109. }
  110. private void setListType(CatalogueContrast prm) {
  111. String table = "yp_zd_dict";
  112. switch (prm.getType()) {
  113. case 1:
  114. prm.setListType(ListType.WEST_PATENT.getCode());
  115. break;
  116. case 2:
  117. prm.setListType(ListType.HERBAL.getCode());
  118. break;
  119. case 3:
  120. prm.setListType(ListType.SERVICE.getCode());
  121. table = "zd_charge_item";
  122. break;
  123. case 4:
  124. prm.setListType(ListType.SUPPLY.getCode());
  125. table = "zd_charge_item";
  126. break;
  127. }
  128. prm.setTable(table);
  129. }
  130. public ResultVo<String> autoRecoveryTrade(AtoRcvTrd atoRcvTrd) {
  131. JSONObject input = exec.makeTradeHeader(SiFunction.AUTO_RECOVERY_TRADE);
  132. String ref = JSONObject.toJSONString(atoRcvTrd);
  133. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  134. JSONObject result = exec.executeTrade(input, SiFunction.AUTO_RECOVERY_TRADE);
  135. logDao.insert(new SiLog(input, result, null, null, null));
  136. log.info("【操作员:{}】,冲正交易:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  137. return SiUtil.makeReturnWithoutOutput(result, "冲正成功。");
  138. }
  139. public ResultVo<String> institutionSettlementLedgerCheck(InstStlLdgChk instStlLdgChk) {
  140. String startdate = DateUtil.getDayStartTime(instStlLdgChk.getStmtBegndate());
  141. String enddate = DateUtil.getDayEndTime(instStlLdgChk.getStmtEnddate());
  142. List<InstSetlLdgChkBrf> brfs = dao.selectSetlChkBrfsWithInsutype(instStlLdgChk.getClrType(), instStlLdgChk.getInsutype(),
  143. instStlLdgChk.getSetlOptins(), startdate, enddate);
  144. BigDecimal medfeesum = new BigDecimal("0");
  145. BigDecimal acctpaysum = new BigDecimal("0");
  146. BigDecimal fundpaysum = new BigDecimal("0");
  147. for (InstSetlLdgChkBrf brf : brfs) {
  148. medfeesum = DecimalUtil.add(medfeesum, brf.getMedfeeSumamt());
  149. acctpaysum = DecimalUtil.add(acctpaysum, brf.getAcctPay());
  150. fundpaysum = DecimalUtil.add(fundpaysum, brf.getFundPaySumamt());
  151. if (null != brf.getHospPay()) {
  152. fundpaysum = DecimalUtil.minus(fundpaysum, brf.getHospPay());
  153. }
  154. }
  155. instStlLdgChk.setMedfeeSumamt(medfeesum);
  156. instStlLdgChk.setAcctPay(acctpaysum);
  157. instStlLdgChk.setFundPaySumamt(fundpaysum);
  158. instStlLdgChk.setFixmedinsSetlCnt(brfs.size());
  159. JSONObject input = exec.makeTradeHeader(SiFunction.INSTITUTION_SETTLEMENT_LEDGER_CHECK);
  160. String ref = JSONObject.toJSONStringWithDateFormat(instStlLdgChk, "yyyy-MM-dd");
  161. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  162. JSONObject result = exec.executeTrade(input, SiFunction.INSTITUTION_SETTLEMENT_LEDGER_CHECK);
  163. log.info("【操作员:{}】,医药机构费用结算对总账:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  164. if (null == result) {
  165. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  166. }
  167. if (null == result.getInteger(RESULT_CODE)) {
  168. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  169. }
  170. if (result.getIntValue(RESULT_CODE) == 0) {
  171. JSONObject output = result.getJSONObject(OUTPUT);
  172. JSONObject info = output.getJSONObject("stmtinfo");
  173. String setlOptins = info.getString("setl_optins");
  174. Admdvs admdvs = Admdvs.get(setlOptins);
  175. String optins;
  176. if (null == admdvs) {
  177. optins = dao.selectRegion(setlOptins);
  178. } else {
  179. optins = admdvs.getName();
  180. }
  181. String rslt;
  182. if (info.getString("stmt_rslt").equals(StmtRslt.CORRECT.getCode())) {
  183. rslt = StmtRslt.CORRECT.getName();
  184. } else {
  185. rslt = StmtRslt.WRONG.getName();
  186. }
  187. String dscr = info.getString("stmt_rslt_dscr");
  188. if (StringUtil.isBlank(dscr)) {
  189. dscr = "结算笔数:" + brfs.size() +
  190. ",医疗费总额:" + medfeesum + ",基金支付总额:" + fundpaysum;
  191. }
  192. String data = "【清算机构:" + optins + "】对账结果:" + rslt + "。对账结果说明:" + dscr;
  193. if (rslt.equals(StmtRslt.CORRECT.getName())) {
  194. return ResultVoUtil.success(data);
  195. }
  196. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, data);
  197. }
  198. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  199. }
  200. public ResultVo<List<InsSetlDetlChkRslt>> institutionSettlementDetailCheck(InsSetlDetlChk prm) {
  201. String snowId = SnowFlakeId.instance().nextId();
  202. String filename = snowId + ".txt";
  203. StringBuilder fsIn = new StringBuilder();
  204. String startdate = DateUtil.getDayStartTime(prm.getStmtBegndate());
  205. String enddate = DateUtil.getDayEndTime(prm.getStmtEnddate());
  206. List<InstSetlLdgChkBrf> brfs = dao.selectSetlChkBrfs(prm.getClrType(),
  207. prm.getSetlOptins(), startdate, enddate);
  208. BigDecimal medfeesum = new BigDecimal("0");
  209. BigDecimal psnCashPay = new BigDecimal("0");
  210. BigDecimal fundpaysum = new BigDecimal("0");
  211. for (InstSetlLdgChkBrf brf : brfs) {
  212. medfeesum = DecimalUtil.add(medfeesum, brf.getMedfeeSumamt());
  213. psnCashPay = DecimalUtil.add(psnCashPay, brf.getPsnCashPay());
  214. fundpaysum = DecimalUtil.add(fundpaysum, brf.getFundPaySumamt());
  215. if (null != brf.getHospPay()) {
  216. fundpaysum = DecimalUtil.minus(fundpaysum, brf.getHospPay());
  217. brf.setFundPaySumamt(DecimalUtil.minus(brf.getFundPaySumamt(), brf.getHospPay()));
  218. }
  219. }
  220. prm.setMedfeeSumamt(medfeesum);
  221. prm.setCashPayamt(psnCashPay);
  222. prm.setFundPaySumamt(fundpaysum);
  223. prm.setFixmedinsSetlCnt(brfs.size());
  224. for (InstSetlLdgChkBrf item : brfs) {
  225. fsIn.append(item.getSetlId()).append("\t")
  226. .append(item.getMdtrtId()).append("\t")
  227. .append(item.getPsnNo()).append("\t")
  228. .append(item.getMedfeeSumamt()).append("\t")
  229. .append(item.getFundPaySumamt()).append("\t")
  230. .append(item.getAcctPay()).append("\t")
  231. .append(prm.getRefdSetlFlag()).append("\n");
  232. }
  233. try {
  234. Files.write(Paths.get(filename), fsIn.toString().getBytes());
  235. String zipFile = ZipUtil.zip(filename);
  236. ZipUtil.deleteFile(filename);
  237. String fsUploadIn = "{\"filename\": \"" + filename + ".zip\", \"fixmedins_code\": \"" +
  238. SiUtil.INSTITUTION_ID + "\", \"in\": " + Arrays.toString(ZipUtil.zipFileToBytes(zipFile)) + "}";
  239. ResultVo<String> uplRes = upldServcie.uploadFile(JSONObject.parseObject(fsUploadIn), filename);
  240. ZipUtil.deleteFile(zipFile);
  241. if (uplRes.getCode().equals(ExceptionEnum.SUCCESS.getCode())) {
  242. prm.setFileQuryNo(uplRes.getData());
  243. }
  244. } catch (IOException e) {
  245. log.error("医药机构费用结算对明细账出错", e);
  246. }
  247. JSONObject input = exec.makeTradeHeader(SiFunction.INSTITUTION_SETTLEMENT_DETAIL_CHECK);
  248. String ref = JSONObject.toJSONStringWithDateFormat(prm, "yyyy-MM-dd");
  249. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  250. JSONObject result = exec.executeTrade(input, SiFunction.INSTITUTION_SETTLEMENT_DETAIL_CHECK);
  251. log.info("【操作员:{}】,医药机构费用结算对明细账:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  252. if (null == result) {
  253. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  254. }
  255. if (null == result.getInteger(RESULT_CODE)) {
  256. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  257. }
  258. if (result.getIntValue(RESULT_CODE) == 0) {
  259. JSONObject output = result.getJSONObject(OUTPUT);
  260. JSONObject fileinfo = output.getJSONObject("fileinfo");
  261. String filePath = dldService.downloadFile(fileinfo.getString("file_qury_no"), fileinfo.getString("filename"));
  262. Queue<String> queue = SiUtil.readTxtFile(filePath);
  263. if (null == queue || queue.size() == 0) {
  264. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未能成功获取到对账结果。");
  265. }
  266. List<InsSetlDetlChkRslt> list = new ArrayList<>();
  267. while (queue.size() > 0) {
  268. String item = queue.poll();
  269. String[] arr = item.split("\t");
  270. InsSetlDetlChkRslt rslt = new InsSetlDetlChkRslt(arr);
  271. list.add(rslt);
  272. }
  273. return ResultVoUtil.success(list);
  274. }
  275. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  276. }
  277. public ResultVo<Map<String, Object>> selectSetlinfos(SlctSetlPrm prm) {
  278. QueryWrapper<SiSetlinfo> wrapper = new QueryWrapper<>();
  279. if (StringUtil.notBlank(prm.getInsutype())) {
  280. wrapper.eq("insutype", prm.getInsutype());
  281. }
  282. String begndate = DateUtil.formatDatetime(prm.getStmtBegndate(), "yyyy-MM-dd") + " 00:00:00";
  283. String enddate = DateUtil.formatDatetime(prm.getStmtEnddate(), "yyyy-MM-dd") + " 23:59:59";
  284. wrapper.eq("setl_type", prm.getClrType());
  285. wrapper.eq("revoked", YesOrNo.NO.getCode());
  286. wrapper.apply("fund_pay_sumamt!=0");
  287. wrapper.apply("setl_time>='" + begndate + "'");
  288. wrapper.apply("setl_time<='" + enddate + "'");
  289. wrapper.apply("(insuplc_admdvs='439900' or insuplc_admdvs like '4301%') ");
  290. List<SiSetlinfo> list = setlinfoDao.selectList(wrapper);
  291. if (null == list || list.isEmpty()) {
  292. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有查询到符合条件的数据。");
  293. }
  294. Map<String, Object> map = new HashMap<>(Capacity.TWO);
  295. Map<String, String> optinsMap = new HashMap<>();
  296. List<PureCodeName> optinList = new ArrayList<>();
  297. list.forEach(item -> {
  298. Admdvs admdvs = Admdvs.get(item.getClrOptins());
  299. if (null == admdvs) {
  300. item.setClrOptinsName(dao.selectRegion(item.getClrOptins()));
  301. } else {
  302. item.setClrOptinsName(admdvs.getName());
  303. }
  304. if (!optinsMap.containsKey(item.getClrOptins())) {
  305. optinsMap.put(item.getClrOptins(), item.getClrOptinsName());
  306. }
  307. item.setGendName(Gend.get(item.getGend()).getName());
  308. Insutype insutype = Insutype.get(item.getInsutype());
  309. if (null != insutype) {
  310. item.setInsutypeName(insutype.getName());
  311. }
  312. PsnType psnType = PsnType.get(item.getPsnType());
  313. if (null != psnType) {
  314. item.setPsnTypeName(psnType.getName());
  315. }
  316. MedType medType = MedType.get(item.getMedType());
  317. if (null != medType) {
  318. item.setMedTypeName(medType.getName());
  319. }
  320. });
  321. map.put("list", list);
  322. for (Map.Entry<String, String> entry : optinsMap.entrySet()) {
  323. optinList.add(new PureCodeName(entry.getKey(), entry.getValue()));
  324. }
  325. map.put("optins", optinList);
  326. return ResultVoUtil.success(map);
  327. }
  328. }