ExecService.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package thyyxxk.simzfeeoprnsystm.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import thyyxxk.simzfeeoprnsystm.dao.StaffDao;
  4. import thyyxxk.simzfeeoprnsystm.dicts.SiFunction;
  5. import thyyxxk.simzfeeoprnsystm.utils.DateUtil;
  6. import thyyxxk.simzfeeoprnsystm.utils.SiUtil;
  7. import thyyxxk.simzfeeoprnsystm.utils.StringUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.http.HttpEntity;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.web.client.RestTemplate;
  14. /**
  15. * @description: 签到签退
  16. * @author: DingJie
  17. * @create: 2021-05-28 15:57:26
  18. **/
  19. @Slf4j
  20. @Service
  21. public class ExecService {
  22. @Value("${si-api-url}")
  23. private String siApiUrl;
  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. private final StaffDao dao;
  29. private volatile String signNo;
  30. @Autowired
  31. public ExecService(StaffDao dao) {
  32. this.dao = dao;
  33. }
  34. private synchronized String getSignNo() {
  35. if (StringUtil.isBlank(signNo)) {
  36. signIn();
  37. }
  38. return signNo;
  39. }
  40. public synchronized void signIn() {
  41. JSONObject input = makeSignHeader(SiFunction.SIGN_IN);
  42. JSONObject signIn = new JSONObject();
  43. signIn.put("opter_no", "99999");
  44. signIn.put("mac", MAC_ADDR);
  45. signIn.put("ip", IP_ADDR);
  46. input.getJSONObject("input").put("signIn", signIn);
  47. JSONObject result = executeTrade(input, SiFunction.SIGN_IN);
  48. log.info("医保签到:\n参数:{},\n结果:{}", input, result);
  49. if (null != result && result.getIntValue(RESULT_CODE) == 0) {
  50. try {
  51. JSONObject output = result.getJSONObject(OUTPUT);
  52. signNo = output.getJSONObject("signinoutb").getString("sign_no");
  53. log.info("签到成功,签到号:{}", signNo);
  54. } catch (Exception e) {
  55. log.error("签到出错", e);
  56. }
  57. }
  58. }
  59. public void signOut() {
  60. if (StringUtil.notBlank(signNo)) {
  61. JSONObject input = makeSignHeader(SiFunction.SIGN_OUT);
  62. JSONObject signOut = new JSONObject();
  63. signOut.put("sign_no", signNo);
  64. signOut.put("opter_no", "99999");
  65. input.getJSONObject("input").put("signOut", signOut);
  66. JSONObject result = executeTrade(input, SiFunction.SIGN_OUT);
  67. log.info("医保签退:\n参数:{},\n结果:{}", input, result);
  68. if (null != result && result.getIntValue(RESULT_CODE) == 0) {
  69. signNo = null;
  70. }
  71. }
  72. }
  73. private JSONObject makeSignHeader(SiFunction function) {
  74. JSONObject header = makePublicHeader("");
  75. header.put("infno", function.getCode());
  76. header.put("opter", "99999");
  77. header.put("opter_name", "全院");
  78. return header;
  79. }
  80. public JSONObject makeTradeHeaderWithInsureArea(SiFunction function, String insureArea, String staffId) {
  81. String staffName = dao.selectStaffName(staffId);
  82. JSONObject header = makePublicHeader(insureArea);
  83. header.put("infno", function.getCode());
  84. header.put("opter", staffId);
  85. header.put("opter_name", staffName);
  86. header.replace("sign_no", getSignNo());
  87. return header;
  88. }
  89. public JSONObject makePublicHeader(String insureArea) {
  90. if (null == insureArea) {
  91. insureArea = "";
  92. }
  93. JSONObject header = new JSONObject();
  94. JSONObject input = new JSONObject();
  95. header.put("msgid", SiUtil.makeMsgId());
  96. header.put("mdtrtarea_admvs", SiUtil.getInstitutionArea(insureArea));
  97. header.put("insuplc_admdvs", insureArea);
  98. header.put("recer_sys_code", SiUtil.INSTITUTION_AREA);
  99. header.put("dev_no", "");
  100. header.put("dev_safe_info", "");
  101. header.put("cainfo", "");
  102. header.put("signtype", "");
  103. header.put("infver", SiUtil.API_VERSION);
  104. header.put("opter_type", "1");
  105. header.put("inf_time", DateUtil.now());
  106. header.put("fixmedins_code", SiUtil.INSTITUTION_ID);
  107. header.put("fixmedins_name", SiUtil.INSTITUTION_NAME);
  108. header.put("sign_no", "");
  109. header.put("input", input);
  110. return header;
  111. }
  112. public JSONObject executeTrade(JSONObject input, SiFunction function) {
  113. RestTemplate template = new RestTemplate();
  114. String result = template.postForObject(siApiUrl,
  115. new HttpEntity<>(input, SiUtil.getHttpHeaders(function)), String.class);
  116. return JSONObject.parseObject(result);
  117. }
  118. }