Commit ba1dc654 authored by matianhao's avatar matianhao

[卫健接口] <feat> 接口接入:门诊信息和住院信息

parent 90f1d102
......@@ -20,6 +20,14 @@ public class Constants {
* 招必得接口
*/
public static final String ZBD_REQUEST_SECRET_PREFIX = "zbd.request.secret";
/**
* 卫健申请码
*/
public static final String HEALTH_SQM = "health.sqm";
/**
* 申请码存入redis时间
*/
public static final String HEALTH_SQM_CURRENT_TIME = "health.sqm.current.time";
// 数据局接口返回响应码
......@@ -39,4 +47,11 @@ public class Constants {
* redis分布式锁的key
*/
public static final String SJJ_DISTRIBUTED_LOCK = "sjj.distributed.lock";
// 错误信息
//-----------------------------------
/**
* 卫健申请码错误
*/
public static final String ERROR_MSG_HEALTH_SQM = "权限码错误";
}
package com.mth.requestsecret.controller;
import com.alibaba.fastjson.JSONObject;
import com.mth.requestsecret.service.RestTemplateService;
import com.mth.requestsecret.vo.health.ZyDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 健康档案接口控制器
*
* @author MaTianHao
* @date 2021/10/11
*/
@RestController
public class HealthRecordController {
@Autowired
private RestTemplateService restTemplateService;
/**
* 住院详情
*/
@PostMapping("zydetail")
public ResponseEntity<String> zydetail(ZyDetailVO param) {
String apiMethod = "zydetail";
JSONObject jsonParam = new JSONObject();
jsonParam.put("startDate", param.getStartDate());
jsonParam.put("endDate", param.getEndDate());
jsonParam.put("idcard", param.getIdcard());
return restTemplateService.healthRecordSendRequest(jsonParam, apiMethod);
}
/**
* 门诊详情
*/
@PostMapping("mzdetail")
public ResponseEntity<String> mzdetail(ZyDetailVO param) {
String apiMethod = "mzdetail";
JSONObject jsonParam = new JSONObject();
jsonParam.put("startDate", param.getStartDate());
jsonParam.put("endDate", param.getEndDate());
jsonParam.put("idcard", param.getIdcard());
return restTemplateService.healthRecordSendRequest(jsonParam, apiMethod);
}
}
......@@ -3,8 +3,10 @@ package com.mth.requestsecret.service;
import com.alibaba.fastjson.JSONObject;
import com.mth.requestsecret.scheduler.RequestSecretSchedulerTask;
import com.mth.requestsecret.util.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
......@@ -85,6 +87,16 @@ public class RestTemplateService {
@Value("${hongcheng.priviteKey}")
private String hongchengPriviteKey;
/**
* 卫健相关
*/
@Value("${health.address}")
private String healthAddress;
@Value("${health.ipip}")
private String healthSqmIp;
@Value("${health.yhm}")
private String healthSqmYhm;
/**
* 瑞成平台接口发送请求
*
......@@ -452,4 +464,102 @@ public class RestTemplateService {
return responseEntity;
}
/**
* 卫健 发送请求
*
* @param jsonParam
* @param apiMethod
* @return
*/
public ResponseEntity<String> healthRecordSendRequest(JSONObject jsonParam, String apiMethod) {
// 请求地址
String url = healthAddress + apiMethod;
// 获取授权码
String sqm = getHealthSqm(false);
jsonParam.put("sqm", sqm);
// 构建请求体
String paramStr = jsonParam.toJSONString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(paramStr, headers);
log.info("卫健api:{}", apiMethod);
log.info("api url:{}", url);
log.info("api params:{}", jsonParam);
// 发送请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
log.info("api response:{}", responseEntity);
// 如果返回权限码错误,则重新请求一次
String msg = JSONObject.parseObject(responseEntity.getBody()).getString("msg");
if (ERROR_MSG_HEALTH_SQM.equals(msg)) {
// 重新获取权限码
String healthSqm = getHealthSqm(true);
jsonParam.put("sqm", healthSqm);
String paramStr1 = jsonParam.toJSONString();
HttpEntity<String> httpEntity1 = new HttpEntity<>(paramStr1, headers);
// 重新发送请求
responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity1, String.class);
log.info("{} 重新请求 api params:{}", apiMethod, jsonParam);
log.info("{} 重新请求 api response:{}", apiMethod, responseEntity);
return responseEntity;
}
return responseEntity;
}
/**
* 获取权限申请码
*
* @param forceRefreshFlag 是否强制刷新申请码,true强制刷新
* @return
*/
@SneakyThrows
private String getHealthSqm(boolean forceRefreshFlag) {
String redisSqm = redisUtil.get(HEALTH_SQM);
String redisSqmCurrentTime = redisUtil.get(HEALTH_SQM_CURRENT_TIME);
boolean refreshTimeFalg = StringUtils.isNotBlank(redisSqmCurrentTime) && System.currentTimeMillis() - Long.parseLong(redisSqmCurrentTime) < 10 * 1000L;
// 不强制刷新:redis存在直接返回,不存在请求后返回
if (!forceRefreshFlag && StringUtils.isNotBlank(redisSqm)) {
return redisSqm;
}
// 强制刷新:10s内获取过不再请求,否则重新请求
if (forceRefreshFlag && refreshTimeFalg) {
return redisSqm;
}
// 请求地址
String url = healthAddress + "organVerify";
// 构建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject jsonObject = new JSONObject();
jsonObject.put("ipip", healthSqmIp);
jsonObject.put("yhm", healthSqmYhm);
String jsonString = jsonObject.toJSONString();
HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, headers);
log.info("卫健api:获取申请码");
log.info("api url:{}", url);
// 发送请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
log.info("api response:{}", responseEntity);
// 接口返回,存入redis
String sqm = JSONObject.parseObject(responseEntity.getBody()).getString("msg");
redisUtil.set(HEALTH_SQM, sqm);
redisUtil.set(HEALTH_SQM_CURRENT_TIME, String.valueOf(System.currentTimeMillis()));
// 等待1s,否则使用新的权限码也无法获得数据
Thread.sleep(1000);
return sqm;
}
}
\ No newline at end of file
package com.mth.requestsecret.vo.health;
import lombok.Data;
/**
* 住院详情、门诊详情vo
* @author MaTianHao
* @date 2021/10/11
*/
@Data
public class ZyDetailVO {
/**
* 开始日期
*/
private String startDate;
/**
* 结束日期
*/
private String endDate;
/**
* 权限申请码
*/
private String sqm;
/**
* 身份证号码,多个以“;”区分
*/
private String idcard;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment