Browse Source

使用RestTemplate的bean

lighter 7 months ago
parent
commit
2f86aa80f8

+ 8 - 8
thyy-scheduled/src/main/java/org/thyy/scheduled/service/CorpwxHttp.java

@@ -16,12 +16,12 @@ import org.thyy.utils.exception.ExceptionEnum;
 public class CorpwxHttp {
     private final Corpwx corpwx;
     private final String tokenApiUrl;
-    private final RestTemplate restTemplate;
+    private final RestTemplate template;
 
     @Autowired
-    public CorpwxHttp(Corpwx corpwx, RestTemplate restTemplate) {
+    public CorpwxHttp(Corpwx corpwx, RestTemplate template) {
         this.corpwx = corpwx;
-        this.restTemplate = restTemplate;
+        this.template = template;
         this.tokenApiUrl = corpwx.getApiUrl()
                 + "/gettoken?corpid="
                 + corpwx.getCorpIdSecret()
@@ -33,7 +33,7 @@ public class CorpwxHttp {
             throw new BizException(ExceptionEnum.NO_CONFIGURATION);
         }
         String url = tokenApiUrl + corpwx.getAddressBookSecret();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (json == null) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }
@@ -47,7 +47,7 @@ public class CorpwxHttp {
             throw new BizException(ExceptionEnum.NO_CONFIGURATION);
         }
         String url = tokenApiUrl + corpwx.getHospAppsSecret();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (json == null) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }
@@ -61,7 +61,7 @@ public class CorpwxHttp {
             throw new BizException(ExceptionEnum.NO_CONFIGURATION);
         }
         String url = tokenApiUrl + corpwx.getClockinSecret();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (json == null) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }
@@ -75,7 +75,7 @@ public class CorpwxHttp {
             throw new BizException(ExceptionEnum.NO_CONFIGURATION);
         }
         String url = tokenApiUrl + corpwx.getUrgentMsgSecret();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (json == null) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }
@@ -89,7 +89,7 @@ public class CorpwxHttp {
             throw new BizException(ExceptionEnum.NO_CONFIGURATION);
         }
         String url = tokenApiUrl + corpwx.getFilePushSecret();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (json == null) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }

+ 16 - 8
thyy-scheduled/src/main/java/org/thyy/scheduled/service/DpccService.java

@@ -6,8 +6,10 @@ import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.ParameterizedTypeReference;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 import org.thyy.scheduled.config.constant.Dpcc;
@@ -39,13 +41,13 @@ import java.util.List;
 public class DpccService {
     private final Dpcc dpcc;
     private final DpccDao dao;
-    private final RestTemplate restTemplate;
+    private final RestTemplate template;
 
     @Autowired
-    public DpccService(Dpcc dpcc, DpccDao dao, RestTemplate restTemplate) {
+    public DpccService(Dpcc dpcc, DpccDao dao, RestTemplate template) {
         this.dpcc = dpcc;
         this.dao = dao;
-        this.restTemplate = restTemplate;
+        this.template = template;
     }
 
     public void getToken() {
@@ -58,7 +60,7 @@ public class DpccService {
                 + "?grant_type=access_client&"
                 + "username=" + dpcc.getClientName() + "&"
                 + "password=" + dpcc.getPassword();
-        JSONObject response = restTemplate.postForObject(url,
+        JSONObject response = template.postForObject(url,
                 new HttpEntity<>(null, headers), JSONObject.class);
         log.info("DPCC获取TOKEN:{}", response);
         if (null == response) {
@@ -130,8 +132,11 @@ public class DpccService {
                 .patientNumType(inquiry.getPatType()).patientNum(inquiry.getPatNo())
                 .startDate(inquiry.getReqStartTime()).endDate(inquiry.getReqEndTime()).build();
         JSONObject json = JSONObject.from(request);
-        ResultVo<List<JyIndexResponse>> resultVo =
-                restTemplate.postForObject(dpcc.getJyApi() + "/queryJyIndex", json, ResultVo.class);
+        String url = dpcc.getJyApi() + "/queryJyIndex";
+        ResultVo<List<JyIndexResponse>> resultVo = template.exchange(
+                url, HttpMethod.POST, new HttpEntity<>(json),
+                new ParameterizedTypeReference<ResultVo<List<JyIndexResponse>>>() {}
+        ).getBody();
         if (null == resultVo) {
             return new ArrayList<>();
         }
@@ -142,8 +147,11 @@ public class DpccService {
         JSONObject json = new JSONObject();
         json.put("reportId", reportId);
         json.put("responseMode", "Json");
-        ResultVo<JyDetailResponse> resultVo =
-                restTemplate.postForObject(dpcc.getJyApi() + "/queryJyDetail", json, ResultVo.class);
+        String url = dpcc.getJyApi() + "/queryJyDetail";
+        ResultVo<JyDetailResponse> resultVo = template.exchange(
+                url, HttpMethod.POST, new HttpEntity<>(json),
+                new ParameterizedTypeReference<ResultVo<JyDetailResponse>>() {}
+        ).getBody();
         if (null == resultVo) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }

+ 4 - 3
thyy-scheduled/src/main/java/org/thyy/scheduled/service/FetchClockinData.java

@@ -24,13 +24,15 @@ import java.util.List;
 public class FetchClockinData {
     private final Corpwx corpwx;
     private final ClockinDao dao;
+    private final RestTemplate template;
     private final String api;
 
     @Autowired
-    public FetchClockinData(Corpwx corpwx, ClockinDao dao) {
+    public FetchClockinData(Corpwx corpwx, ClockinDao dao, RestTemplate template) {
         this.corpwx = corpwx;
         this.dao = dao;
         this.api = corpwx.getApiUrl() + "/checkin/getcheckindata?access_token=";
+        this.template = template;
     }
 
     public void start() {
@@ -72,8 +74,7 @@ public class FetchClockinData {
         postStr.put("endtime", DateUtil.getTimestamp(param.getEndtime() + " 23:59:00") / 1000);
         postStr.put("useridlist", param.getUseridlist());
 
-        RestTemplate restTemplate = new RestTemplate();
-        String httpRes = restTemplate.postForObject(requestUrl, postStr, String.class);
+        String httpRes = template.postForObject(requestUrl, postStr, String.class);
         JSONObject res = JSONObject.parseObject(httpRes);
         assert res != null;
         if (res.getIntValue("errcode") == 0) {

+ 4 - 4
thyy-scheduled/src/main/java/org/thyy/scheduled/service/ThmzHttp.java

@@ -15,12 +15,12 @@ import org.thyy.utils.exception.ExceptionEnum;
 @Service
 public class ThmzHttp {
     private final Thmz thmz;
-    private final RestTemplate restTemplate;
+    private final RestTemplate template;
 
     @Autowired
-    public ThmzHttp(Thmz thmz, RestTemplate restTemplate) {
+    public ThmzHttp(Thmz thmz, RestTemplate template) {
         this.thmz = thmz;
-        this.restTemplate = restTemplate;
+        this.template = template;
     }
 
     public void getToken() {
@@ -31,7 +31,7 @@ public class ThmzHttp {
         String url = thmz.getApiUrl() +
                 "/api/v1/getToken?userCode=" +
                 thmz.getTokenUser();
-        JSONObject json = restTemplate.getForObject(url, JSONObject.class);
+        JSONObject json = template.getForObject(url, JSONObject.class);
         if (null == json) {
             throw new BizException(ExceptionEnum.API_ERROR);
         }

+ 4 - 4
thyy-scheduled/src/main/java/org/thyy/scheduled/task/EmrTask.java

@@ -12,19 +12,19 @@ import org.thyy.scheduled.config.constant.Emr;
 @Component
 public class EmrTask {
     private final Emr emr;
-    private final RestTemplate restTemplate;
+    private final RestTemplate template;
 
     @Autowired
-    public EmrTask(Emr emr, RestTemplate restTemplate) {
+    public EmrTask(Emr emr, RestTemplate template) {
         this.emr = emr;
-        this.restTemplate = restTemplate;
+        this.template = template;
     }
 
     @Scheduled(cron = "0 30 23 * * ?")
     public void historyDelete() {
         if (StrUtil.isNotBlank(emr.getApi())) {
             String url = emr.getApi() + "/emr/runtime/api/v1/document/history/destroy/7";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
             log.info("删除电子病历历史记录");
         }
     }

+ 8 - 8
thyy-scheduled/src/main/java/org/thyy/scheduled/task/MainBusinessTask.java

@@ -14,21 +14,21 @@ public class MainBusinessTask {
     private final Thyy thyy;
     private final MainBusinessDo dao;
     private final ReceiveCost receiveCost;
-    private final RestTemplate restTemplate;
+    private final RestTemplate template;
 
     @Autowired
-    public MainBusinessTask(MainBusinessDo dao, Thyy thyy, ReceiveCost receiveCost, RestTemplate restTemplate) {
+    public MainBusinessTask(MainBusinessDo dao, Thyy thyy, ReceiveCost receiveCost, RestTemplate template) {
         this.dao = dao;
         this.thyy = thyy;
         this.receiveCost = receiveCost;
-        this.restTemplate = restTemplate;
+        this.template = template;
     }
 
     @Scheduled(cron = "0 40 3 * * ?")
     public void executeAutoSign() {
         if (StrUtil.isNotBlank(thyy.getMainAddress())) {
             String url = thyy.getMainAddress() + "/caseFrontSheet/autoSign";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
         }
     }
 
@@ -36,7 +36,7 @@ public class MainBusinessTask {
     public void analyzeSetlData() {
         if (StrUtil.isNotBlank(thyy.getMainAddress())) {
             String url = thyy.getMainAddress() + "/analyzeSiPatientCharges/analyzeSetlData";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
         }
     }
 
@@ -44,7 +44,7 @@ public class MainBusinessTask {
     public void uploadBillingList() {
         if (StrUtil.isNotBlank(thyy.getMainAddress())) {
             String url = thyy.getMainAddress() + "/setlListUpld/tasks";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
         }
     }
 
@@ -52,7 +52,7 @@ public class MainBusinessTask {
     private void uploadFrontSheet() {
         if (StrUtil.isNotBlank(thyy.getMainAddress())) {
             String url = thyy.getMainAddress() + "/uploadFrontSheet/startUploadStatistics";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
         }
     }
 
@@ -60,7 +60,7 @@ public class MainBusinessTask {
     public void notifyDailyCount() {
         if (StrUtil.isNotBlank(thyy.getMainAddress())) {
             String url = thyy.getMainAddress() + "/dailyPatientCount/notifyDailyCount";
-            restTemplate.getForObject(url, String.class);
+            template.getForObject(url, String.class);
         }
     }
 

+ 4 - 4
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/inspection/InspectionController.java

@@ -40,12 +40,12 @@ public class InspectionController {
     }
 
     @PostMapping("/queryJcIndex")
-    public ResultVo<List<JcIndexResponse>> checkTestIndex(@RequestBody ReportIndexInquiry request) {
+    public ResultVo<List<JcIndexResponse>> queryJcIndex(@RequestBody ReportIndexInquiry request) {
         return R.ok(service.queryJcIndex(request));
     }
 
     @PostMapping("/queryJcDetail")
-    public ResultVo<JcIndexResponse> checkTestDetail(@RequestBody ReportDetailInquiry request) {
+    public ResultVo<JcIndexResponse> queryJcDetail(@RequestBody ReportDetailInquiry request) {
         if (StrUtil.isBlank(request.getReportId())) {
             return R.fail(ExceptionEnum.INVALID_PARAM, "报告ID不能为空!");
         }
@@ -53,14 +53,14 @@ public class InspectionController {
     }
 
     @PostMapping("/queryBlIndex")
-    public ResultVo<List<BlIndexResponse>> checkPathologyIndex(@RequestBody ReportIndexInquiry request) {
+    public ResultVo<List<BlIndexResponse>> queryBlIndex(@RequestBody ReportIndexInquiry request) {
         request.setReqStartTime(request.getReqStartTime().replaceAll("-", ""));
         request.setReqEndTime(request.getReqEndTime().replaceAll("-", ""));
         return R.ok(service.queryBlIndex(request));
     }
 
     @PostMapping("/queryXdIndex")
-    public ResultVo<List<XdIndexResponse>> checkElectroIndex(@RequestBody ReportIndexInquiry request) {
+    public ResultVo<List<XdIndexResponse>> queryXdIndex(@RequestBody ReportIndexInquiry request) {
         request.setReqStartTime(request.getReqStartTime().replaceAll("-", ""));
         request.setReqEndTime(request.getReqEndTime().replaceAll("-", ""));
         return R.ok(service.queryXdIndex(request));

+ 2 - 2
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/inspection/inspectionImpl/ThyyInspectionImpl.java

@@ -62,7 +62,7 @@ public class ThyyInspectionImpl implements InspectionInterface {
         if (StrUtil.isBlank(apiConfig.getJy())) {
             return null;
         }
-        JSONObject response = new RestTemplate().postForObject(
+        JSONObject response = template.postForObject(
                 apiConfig.getJy() + "/detail", request, JSONObject.class);
         JSONObject data = getJyJsonData(response);
         return JSON.parseObject(JSON.toJSONString(data), JyDetailResponse.class);
@@ -128,7 +128,7 @@ public class ThyyInspectionImpl implements InspectionInterface {
                 "&endTime=" + request.getReqEndTime();
 
         List<XdIndexResponse> xdIndexList = new ArrayList<>();
-        JSONObject response = new RestTemplate().getForObject(url, JSONObject.class);
+        JSONObject response = template.getForObject(url, JSONObject.class);
         JSONArray rows = getIndexRows(response);
         if (null != rows && !rows.isEmpty()) {
             for (int i = 0; i < rows.size(); i++) {