ExecService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package thyyxxk.webserver.service.medicalinsurance;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.http.HttpEntity;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.web.client.RestTemplate;
  9. import thyyxxk.webserver.config.exception.BizException;
  10. import thyyxxk.webserver.constants.sidicts.SiFunction;
  11. import thyyxxk.webserver.dao.his.medicalinsurance.SiZyDao;
  12. import thyyxxk.webserver.utils.DateUtil;
  13. import thyyxxk.webserver.utils.SiUtil;
  14. import thyyxxk.webserver.utils.StringUtil;
  15. import thyyxxk.webserver.utils.TokenUtil;
  16. /**
  17. * @description: 医保交易执行
  18. * @author: DingJie
  19. * @create: 2021-05-28 15:57:26
  20. **/
  21. @Slf4j
  22. @Service
  23. public class ExecService {
  24. private static final String RESULT_CODE = "infcode";
  25. private static final String OUTPUT = "output";
  26. private static final String MAC_ADDR = "FE-FC-FE-35-35-DE";
  27. private static final String IP_ADDR = "218.104.151.243";
  28. @Value("${si-api-url}")
  29. private String siApiUrl;
  30. private final SiZyDao dao;
  31. private volatile String signNo;
  32. @Autowired
  33. public ExecService(SiZyDao dao) {
  34. this.dao = dao;
  35. }
  36. private synchronized String getSignNo() {
  37. if (StringUtil.isBlank(signNo)) {
  38. signIn();
  39. }
  40. return signNo;
  41. }
  42. public synchronized void signIn() {
  43. JSONObject input = makeSignHeader(SiFunction.SIGN_IN);
  44. JSONObject signIn = new JSONObject();
  45. signIn.put("opter_no", "99999");
  46. signIn.put("mac", MAC_ADDR);
  47. signIn.put("ip", IP_ADDR);
  48. input.getJSONObject("input").put("signIn", signIn);
  49. JSONObject result = executeTrade(input, SiFunction.SIGN_IN);
  50. log.info("医保签到:\n参数:{},\n结果:{}", input, result);
  51. if (null != result && result.getInteger(RESULT_CODE) == 0) {
  52. try {
  53. JSONObject output = result.getJSONObject(OUTPUT);
  54. signNo = output.getJSONObject("signinoutb").getString("sign_no");
  55. log.info("签到成功,签到号:{}", signNo);
  56. } catch (Exception e) {
  57. throw new BizException();
  58. }
  59. }
  60. }
  61. public void signOut() {
  62. if (StringUtil.notBlank(signNo)) {
  63. JSONObject input = makeSignHeader(SiFunction.SIGN_OUT);
  64. JSONObject signOut = new JSONObject();
  65. signOut.put("sign_no", signNo);
  66. signOut.put("opter_no", "99999");
  67. input.getJSONObject("input").put("signOut", signOut);
  68. JSONObject result = executeTrade(input, SiFunction.SIGN_OUT);
  69. log.info("医保签退:\n参数:{},\n结果:{}", input, result);
  70. if (null != result && result.getIntValue(RESULT_CODE) == 0) {
  71. signNo = null;
  72. }
  73. }
  74. }
  75. private JSONObject makeSignHeader(SiFunction function) {
  76. JSONObject header = makePublicHeader("");
  77. header.put("infno", function.getCode());
  78. header.put("opter", "99999");
  79. header.put("opter_name", "全院");
  80. return header;
  81. }
  82. public JSONObject makeTradeHeader(SiFunction function) {
  83. String staffId = TokenUtil.getTokenUserId();
  84. String staffName = dao.selectStaffName(staffId);
  85. JSONObject header = makePublicHeader(SiUtil.INSTITUTION_AREA);
  86. header.put("infno", function.getCode());
  87. header.put("opter", staffId);
  88. header.put("opter_name", staffName);
  89. header.replace("sign_no", getSignNo());
  90. return header;
  91. }
  92. public JSONObject makeTradeHeaderWithInsureArea(SiFunction function, String insureArea) {
  93. String staffId = TokenUtil.getTokenUserId();
  94. String staffName = dao.selectStaffName(staffId);
  95. JSONObject header = makePublicHeader(insureArea);
  96. header.put("infno", function.getCode());
  97. header.put("opter", staffId);
  98. header.put("opter_name", staffName);
  99. header.replace("sign_no", getSignNo());
  100. return header;
  101. }
  102. public JSONObject makePublicHeader(String insureArea) {
  103. if (null == insureArea) {
  104. insureArea = "";
  105. }
  106. JSONObject header = new JSONObject();
  107. JSONObject input = new JSONObject();
  108. header.put("msgid", SiUtil.makeMsgId());
  109. header.put("mdtrtarea_admvs", SiUtil.getInstitutionArea(insureArea));
  110. header.put("insuplc_admdvs", insureArea);
  111. header.put("recer_sys_code", "医院");
  112. header.put("dev_no", "");
  113. header.put("dev_safe_info", "");
  114. header.put("cainfo", "");
  115. header.put("signtype", "");
  116. header.put("infver", SiUtil.API_VERSION);
  117. header.put("opter_type", "1");
  118. header.put("inf_time", DateUtil.now());
  119. header.put("fixmedins_code", SiUtil.INSTITUTION_ID);
  120. header.put("fixmedins_name", SiUtil.INSTITUTION_NAME);
  121. header.put("sign_no", "");
  122. header.put("input", input);
  123. return header;
  124. }
  125. public JSONObject executeTrade(JSONObject input, SiFunction function) {
  126. RestTemplate template = new RestTemplate();
  127. try {
  128. String result = template.postForObject(siApiUrl,
  129. new HttpEntity<>(input, SiUtil.getHttpHeaders(function)), String.class);
  130. return JSONObject.parseObject(result);
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. log.error("医保网络出错", e);
  134. JSONObject object = new JSONObject();
  135. object.put("infcode", -1);
  136. if (e.getMessage().contains("Connection timed out")) {
  137. object.put("err_msg", "医保中心网络故障,连接超时。");
  138. } else {
  139. object.put("err_msg", e.getMessage());
  140. }
  141. return object;
  142. }
  143. }
  144. }