|
|
@@ -1,156 +0,0 @@
|
|
|
-package com.xuekairui.user.service;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.xuekairui.user.config.ThirdPartySyncProperties;
|
|
|
-import com.xuekairui.user.dto.SyncShopReportRequest;
|
|
|
-import com.xuekairui.user.dto.ThirdPartySyncResponse;
|
|
|
-import com.xuekairui.user.entity.BusinessLicense;
|
|
|
-import com.xuekairui.user.entity.User;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.client.RestClient;
|
|
|
-import org.springframework.web.util.UriComponentsBuilder;
|
|
|
-
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * 我方 → 第三方 同步客户端
|
|
|
- * <p>调用第三方 POST /api/sync_shop_resource/report_user(query 参数,snake_case)
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-@RequiredArgsConstructor
|
|
|
-public class ThirdPartySyncClient {
|
|
|
-
|
|
|
- private final ThirdPartySyncProperties properties;
|
|
|
- private final RestClient restClient;
|
|
|
- private final ObjectMapper objectMapper;
|
|
|
-
|
|
|
- /**
|
|
|
- * 同步用户店铺信息到第三方
|
|
|
- *
|
|
|
- * @return 第三方响应(含 token、user_id 等);失败返回 null
|
|
|
- */
|
|
|
- public ThirdPartySyncResponse syncToThirdParty(SyncShopReportRequest request) {
|
|
|
- try {
|
|
|
- Map<String, String> queryParams = toQueryParams(request);
|
|
|
- String uri = UriComponentsBuilder.fromPath(properties.getSyncPath())
|
|
|
- .queryParams(toMultiValueMap(queryParams))
|
|
|
- .build()
|
|
|
- .toUriString();
|
|
|
-
|
|
|
- ThirdPartySyncResponse response = restClient.post()
|
|
|
- .uri(uri)
|
|
|
- .retrieve()
|
|
|
- .body(ThirdPartySyncResponse.class);
|
|
|
-
|
|
|
- if (response != null && "success".equals(response.getCode())) {
|
|
|
- log.info("同步到第三方成功: phone={}, thirdUserId={}",
|
|
|
- request.getPhone(),
|
|
|
- response.getData() != null ? response.getData().getUserId() : null);
|
|
|
- } else {
|
|
|
- log.warn("同步到第三方返回非成功: phone={}, code={}, msg={}",
|
|
|
- request.getPhone(),
|
|
|
- response != null ? response.getCode() : null,
|
|
|
- response != null ? response.getMsg() : null);
|
|
|
- }
|
|
|
- return response;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("同步到第三方失败: phone={}, error={}", request.getPhone(), e.getMessage(), e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 使用 Jackson @JsonProperty 将 DTO 序列化为 snake_case 的 query 参数 Map
|
|
|
- */
|
|
|
- private Map<String, String> toQueryParams(SyncShopReportRequest req) {
|
|
|
- Map<String, String> params = new LinkedHashMap<>();
|
|
|
- JsonNode node = objectMapper.valueToTree(req);
|
|
|
- node.fields().forEachRemaining(entry -> {
|
|
|
- if (!entry.getValue().isNull()) {
|
|
|
- params.put(entry.getKey(), entry.getValue().asText());
|
|
|
- }
|
|
|
- });
|
|
|
- return params;
|
|
|
- }
|
|
|
-
|
|
|
- private static org.springframework.util.MultiValueMap<String, String> toMultiValueMap(
|
|
|
- Map<String, String> map) {
|
|
|
- org.springframework.util.LinkedMultiValueMap<String, String> multiMap =
|
|
|
- new org.springframework.util.LinkedMultiValueMap<>();
|
|
|
- map.forEach(multiMap::add);
|
|
|
- return multiMap;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据我方 User 实体构建同步请求
|
|
|
- */
|
|
|
- public SyncShopReportRequest buildRequest(User user) {
|
|
|
- SyncShopReportRequest req = new SyncShopReportRequest();
|
|
|
- req.setPhone(user.getPhone());
|
|
|
- req.setContactName(user.getNickname());
|
|
|
- req.setContactPhone(user.getContactPhone());
|
|
|
- req.setContactShop(user.getPharmacyName());
|
|
|
- req.setContactProvince(user.getProvince());
|
|
|
- req.setContactCity(user.getCity());
|
|
|
- req.setContactArea(user.getDistrict());
|
|
|
- req.setContactAddr(user.getPharmacyAddress());
|
|
|
- req.setAppId(properties.getAppId());
|
|
|
- req.setUserType("1");
|
|
|
- req.setOperatorUserid("0");
|
|
|
- return req;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据我方 User + BusinessLicense 实体构建完整同步请求
|
|
|
- */
|
|
|
- public SyncShopReportRequest buildRequest(User user, BusinessLicense license) {
|
|
|
- SyncShopReportRequest req = buildRequest(user);
|
|
|
- if (license != null) {
|
|
|
- req.setContactShop(license.getStoreName() != null ? license.getStoreName() : req.getContactShop());
|
|
|
- req.setContactProvince(license.getProvince() != null ? license.getProvince() : req.getContactProvince());
|
|
|
- req.setContactCity(license.getCity() != null ? license.getCity() : req.getContactCity());
|
|
|
- req.setContactArea(license.getDistrict() != null ? license.getDistrict() : req.getContactArea());
|
|
|
- req.setContactAddr(license.getStoreAddress() != null ? license.getStoreAddress() : req.getContactAddr());
|
|
|
- req.setContactName(license.getContactPerson() != null ? license.getContactPerson() : req.getContactName());
|
|
|
- req.setContactPhone(license.getContactPhone() != null ? license.getContactPhone() : req.getContactPhone());
|
|
|
- req.setBusinessLicenseImage(license.getLicenseImageUrl());
|
|
|
- req.setDrugBusinessLicenseImage(license.getDrugLicenseUrl());
|
|
|
- req.setTwoMedicalDeviceRegistration(license.getMedicalDeviceClass2Url());
|
|
|
- req.setThreeMedicalDeviceBusinessLicense(license.getMedicalDeviceClass3Url());
|
|
|
- // shop_type: 反向映射 terminalType → 第三方编码
|
|
|
- req.setShopType(mapShopTypeToThirdParty(license.getTerminalType()));
|
|
|
- // status: 反向映射 reviewStatus → 第三方编码
|
|
|
- req.setStatus(mapStatusToThirdParty(license.getReviewStatus()));
|
|
|
- }
|
|
|
- req.setAppId(properties.getAppId());
|
|
|
- req.setUserType("1");
|
|
|
- req.setOperatorUserid("0");
|
|
|
- return req;
|
|
|
- }
|
|
|
-
|
|
|
- /** terminalType → 第三方编码 */
|
|
|
- private static String mapShopTypeToThirdParty(String terminalType) {
|
|
|
- if (terminalType == null) return null;
|
|
|
- return switch (terminalType) {
|
|
|
- case "SINGLE" -> "1";
|
|
|
- case "CHAIN" -> "2";
|
|
|
- case "CLINIC", "COMMUNITY_HEALTH" -> "3";
|
|
|
- default -> terminalType;
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- /** reviewStatus → 第三方编码 */
|
|
|
- private static String mapStatusToThirdParty(String reviewStatus) {
|
|
|
- if (reviewStatus == null) return "0";
|
|
|
- return switch (reviewStatus) {
|
|
|
- case "APPROVED" -> "1";
|
|
|
- case "REJECTED" -> "2";
|
|
|
- default -> "0";
|
|
|
- };
|
|
|
- }
|
|
|
-}
|