|
|
@@ -1,5 +1,6 @@
|
|
|
package org.springblade.u9cloud.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
@@ -9,8 +10,11 @@ import org.springblade.u9cloud.entity.ViewApiResponse;
|
|
|
import org.springblade.u9cloud.entity.ZcrmViewCustomerSel;
|
|
|
import org.springblade.u9cloud.mapper.ZcrmViewCustomerSelMapper;
|
|
|
import org.springblade.u9cloud.service.ZcrmViewCustomerSelService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
import org.springframework.http.*;
|
|
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
@@ -18,9 +22,8 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springblade.u9cloud.config.U9cloudConfig;
|
|
|
|
|
|
@@ -41,6 +44,9 @@ public class ZcrmViewCustomerSelServiceImpl extends BaseServiceImpl<ZcrmViewCust
|
|
|
|
|
|
private final U9cloudGetTokenUtil u9cloudGetTokenUtil;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@@ -54,85 +60,139 @@ public class ZcrmViewCustomerSelServiceImpl extends BaseServiceImpl<ZcrmViewCust
|
|
|
return this.saveBatch(customers);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
private final IUserClient userClient;
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean insertBatchCustomers() {
|
|
|
- // 1. 构建 URL
|
|
|
+ // 1. 构建URL和请求参数(保持不变)
|
|
|
String url = UriComponentsBuilder.fromHttpUrl(U9cloudConfig.BASE_URL + U9cloudConfig.PATH_API)
|
|
|
.encode(StandardCharsets.UTF_8)
|
|
|
.toUriString();
|
|
|
|
|
|
- // 2. 请求体
|
|
|
Map<String, String> requestBody = new HashMap<>();
|
|
|
requestBody.put("SqlString", "select * from ZCRM_View_Customer_Sel");
|
|
|
|
|
|
- // 3. 请求头
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
headers.set("token", u9cloudGetTokenUtil.getToken());
|
|
|
-
|
|
|
- HttpEntity<Map<String, Object>> requestEntity = new HttpEntity(requestBody, headers);
|
|
|
+ HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
|
|
|
|
|
|
|
|
- // 4. 发送 POST
|
|
|
+ // 2. 发送请求并获取响应(保持不变)
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
ParameterizedTypeReference<ViewApiResponse<ZcrmViewCustomerSel>> typeRef =
|
|
|
new ParameterizedTypeReference<ViewApiResponse<ZcrmViewCustomerSel>>() {};
|
|
|
-
|
|
|
ResponseEntity<ViewApiResponse<ZcrmViewCustomerSel>> response =
|
|
|
restTemplate.exchange(url, HttpMethod.POST, requestEntity, typeRef);
|
|
|
-
|
|
|
- // 5. 处理结果
|
|
|
ViewApiResponse<ZcrmViewCustomerSel> apiResponse = response.getBody();
|
|
|
|
|
|
- // 将 ViewApiResponse<ZcrmViewCustomerSel> 转成 List<ZcrmViewCustomerSel>
|
|
|
+
|
|
|
+ // 3. 处理用户同步(保持不变)
|
|
|
try {
|
|
|
if (apiResponse != null && apiResponse.isSuccess()) {
|
|
|
List<ZcrmViewCustomerSel> customerList = apiResponse.getData();
|
|
|
- // 检查数据列表不为空再调用接口
|
|
|
if (customerList != null && !customerList.isEmpty()) {
|
|
|
R<String> result = userClient.insertOrUpdateDealerUser(customerList);
|
|
|
- System.out.println(result); // R(code=400, success=false, data=null, msg=缺少经销商角色,请先维护角色)
|
|
|
- // 将R对象转换为Map
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
- // 通过反射获取R对象的属性值
|
|
|
Class<?> rClass = result.getClass();
|
|
|
resultMap.put("code", rClass.getMethod("getCode").invoke(result));
|
|
|
resultMap.put("success", rClass.getMethod("isSuccess").invoke(result));
|
|
|
resultMap.put("data", rClass.getMethod("getData").invoke(result));
|
|
|
resultMap.put("msg", rClass.getMethod("getMsg").invoke(result));
|
|
|
- // 根据Map中的值进行判断
|
|
|
- if (!(boolean) resultMap.get("success")) {
|
|
|
- String errorMsg = (String) resultMap.get("msg");
|
|
|
- int code = (int) resultMap.get("code");
|
|
|
-
|
|
|
- // 处理缺少经销商角色的错误
|
|
|
- if (code == 400) {
|
|
|
- System.err.println("返回数据:" + resultMap.get("msg"));
|
|
|
- return false;
|
|
|
- }
|
|
|
- } else {
|
|
|
- System.err.println("同步用户数据成功,返回数据:" + resultMap.get("data"));
|
|
|
+
|
|
|
+ if (!(boolean) resultMap.get("success") && (int) resultMap.get("code") == 400) {
|
|
|
+ System.err.println("用户同步失败:" + resultMap.get("msg"));
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- // 可以考虑在这里抛出异常让事务回滚,根据业务需求决定
|
|
|
- throw new RuntimeException("同步用户数据失败", e);
|
|
|
+ throw new RuntimeException("用户同步异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 4. 核心逻辑:按 customerId + customerCode 去重(改用JdbcTemplate查询)
|
|
|
+ if (apiResponse == null || !apiResponse.isSuccess()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<ZcrmViewCustomerSel> newCustomers = apiResponse.getData();
|
|
|
+ if (newCustomers == null || newCustomers.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 4.1 提取新数据中的 customerId 和 customerCode(过滤空值)
|
|
|
+ List<Long> customerIds = newCustomers.stream()
|
|
|
+ .map(ZcrmViewCustomerSel::getCustomerId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<String> customerCodes = newCustomers.stream()
|
|
|
+ .map(ZcrmViewCustomerSel::getCustomerCode)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (customerIds.isEmpty() || customerCodes.isEmpty()) {
|
|
|
+ System.err.println("存在 customerId 或 customerCode 为空的记录,跳过处理");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 4.2 关键修改:用JdbcTemplate查询已存在记录(绕开MyBatis拦截器)
|
|
|
+ List<ZcrmViewCustomerSel> existingCustomers = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ // 构建SQL(注意表名和字段名与数据库一致)
|
|
|
+ String sql = "SELECT * FROM zcrm_view_customer_sel " +
|
|
|
+ "WHERE Customer_ID IN (" + String.join(",", customerIds.stream().map(String::valueOf).collect(Collectors.toList())) + ") " +
|
|
|
+ "AND Customer_CODE IN ('" + String.join("','", customerCodes) + "')";
|
|
|
+
|
|
|
+ // 执行查询(用BeanPropertyRowMapper映射实体)
|
|
|
+ existingCustomers = jdbcTemplate.query(
|
|
|
+ sql,
|
|
|
+ new BeanPropertyRowMapper<>(ZcrmViewCustomerSel.class)
|
|
|
+ );
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("查询已存在客户记录失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 4.3 用 "customerId-customerCode" 作为唯一键,构建已存在记录的映射表(保持不变)
|
|
|
+ Map<String, ZcrmViewCustomerSel> existingMap = new HashMap<>();
|
|
|
+ for (ZcrmViewCustomerSel existing : existingCustomers) {
|
|
|
+ String key = existing.getCustomerId() + "-" + existing.getCustomerCode();
|
|
|
+ existingMap.put(key, existing);
|
|
|
}
|
|
|
|
|
|
- boolean res = false;
|
|
|
- if (apiResponse != null && apiResponse.isSuccess()) {
|
|
|
- List<ZcrmViewCustomerSel> customers1 = apiResponse.getData();
|
|
|
- if (customers1 != null && !customers1.isEmpty()) {
|
|
|
- res = this.saveBatch(customers1);
|
|
|
+
|
|
|
+ // 4.4 分离新增和更新列表(保持不变)
|
|
|
+ List<ZcrmViewCustomerSel> insertList = new ArrayList<>();
|
|
|
+ List<ZcrmViewCustomerSel> updateList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (ZcrmViewCustomerSel newCust : newCustomers) {
|
|
|
+ Long id = newCust.getCustomerId();
|
|
|
+ String code = newCust.getCustomerCode();
|
|
|
+
|
|
|
+ if (id == null || code == null) {
|
|
|
+ System.err.println("跳过 customerId 或 customerCode 为空的记录");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = id + "-" + code;
|
|
|
+ if (existingMap.containsKey(key)) {
|
|
|
+ ZcrmViewCustomerSel existing = existingMap.get(key);
|
|
|
+ newCust.setId(existing.getId()); // 拷贝主键
|
|
|
+ updateList.add(newCust);
|
|
|
+ } else {
|
|
|
+ insertList.add(newCust);
|
|
|
}
|
|
|
}
|
|
|
- return res;
|
|
|
+
|
|
|
+
|
|
|
+ // 5. 执行批量操作(保持不变)
|
|
|
+ boolean insertSuccess = insertList.isEmpty() || this.saveBatch(insertList);
|
|
|
+ boolean updateSuccess = updateList.isEmpty() || this.updateBatchById(updateList);
|
|
|
+
|
|
|
+ return insertSuccess && updateSuccess;
|
|
|
}
|
|
|
|
|
|
/**
|