package com.lxzn.base.controller;
|
|
import com.lxzn.base.service.impl.DataImportService;
|
import com.lxzn.framework.domain.nc.*;
|
import com.lxzn.framework.domain.ucenter.User;
|
import com.lxzn.framework.model.response.CommonCode;
|
import com.lxzn.framework.model.response.ResponseResult;
|
import com.lxzn.nc.service.*;
|
import com.lxzn.ucenter.service.IUserService;
|
import org.apache.poi.ss.formula.functions.T;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.client.RestTemplate;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
@RestController
|
@RequestMapping("/receiver")
|
public class DataReceiverController {
|
|
private static final Logger logger = LoggerFactory.getLogger(DataReceiverController.class);
|
|
// 服务地址配置
|
@Value("${dnc.server.url}")
|
private String serverUrl;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@Autowired
|
private DataImportService dataImportService;
|
|
@Autowired
|
private IProductInfoService iProductInfoService;
|
@Autowired
|
private IComponentInfoService iComponentInfoService;
|
@Autowired
|
private IPartsInfoService iPartsInfoService;
|
@Autowired
|
private IProcessInfoService iProcessInfoService;
|
@Autowired
|
private IProcessStreamService iProcessStreamService;
|
@Autowired
|
private IDocInfoService iDocInfoService;
|
@Autowired
|
private IDocFileService iDocFileService;
|
@Autowired
|
private IProductPermissionService productPermissionService;
|
@Autowired
|
private IComponentPermissionService componentPermissionService;
|
@Autowired
|
private IPartsPermissionService partsPermissionService;
|
@Autowired
|
private IPermissionStreamService permissionStreamService;
|
@Autowired
|
private IUserService iUserService;
|
// 数据类型映射
|
private static final Map<String, Class<?>> DATA_TYPE_MAPPING = new HashMap<>();
|
static {
|
DATA_TYPE_MAPPING.put("productInfoList", ProductInfo.class);
|
DATA_TYPE_MAPPING.put("componentInfoList", ComponentInfo.class);
|
DATA_TYPE_MAPPING.put("partsInfoList", PartsInfo.class);
|
DATA_TYPE_MAPPING.put("processStreamList", ProcessStream.class);
|
DATA_TYPE_MAPPING.put("processInfoList", ProcessInfo.class);
|
DATA_TYPE_MAPPING.put("docInfoList", DocInfo.class);
|
DATA_TYPE_MAPPING.put("docFileList", DocFile.class);
|
}
|
|
@GetMapping("/syncData")
|
public ResponseResult syncData() {
|
try {
|
// 1. 获取原始数据
|
Map<String, List<?>> rawData = fetchDataFromServer();
|
User user=iUserService.findByUsername("admin");
|
// 2. 并行处理每个数据集
|
rawData.forEach((dataType, rawList) -> {
|
Class<?> targetType = DATA_TYPE_MAPPING.get(dataType);
|
if (targetType != null) {
|
try {
|
List<?> convertedList = TypeConverter.convertList(rawList, targetType);
|
switch (dataType){
|
case "productInfoList":
|
List<ProductInfo> list = (List<ProductInfo>) convertedList;
|
//添加用户权限
|
for (ProductInfo productInfo : list) {
|
ProductPermission productPermission = new ProductPermission();
|
productPermission.setUserId(user.getUserId());
|
productPermission.setProductId(productInfo.getProductId());
|
productPermissionService.save(productPermission);
|
PermissionStream stream = new PermissionStream();
|
stream.setProductId(productInfo.getProductId());
|
stream.setUserId(user.getUserId());
|
permissionStreamService.save(stream);
|
}
|
iProductInfoService.saveBatch(list);
|
break;
|
case "componentInfoList":
|
List<ComponentInfo> list1 = (List<ComponentInfo>) convertedList;
|
iComponentInfoService.saveBatch(list1);
|
//添加用户权限
|
for (ComponentInfo componentInfo : list1) {
|
ComponentPermission componentPermission = new ComponentPermission();
|
componentPermission.setUserId(user.getUserId());
|
componentPermission.setComponentId(componentInfo.getComponentId());
|
componentPermissionService.save(componentPermission);
|
PermissionStream stream = new PermissionStream();
|
stream.setProductId(componentInfo.getProductId());
|
stream.setComponentId(componentInfo.getComponentId());
|
stream.setUserId(user.getUserId());
|
permissionStreamService.save(stream);
|
}
|
break;
|
case "partsInfoList":
|
List<PartsInfo> list2 = (List<PartsInfo>) convertedList;
|
//添加用户权限
|
for (PartsInfo partsInfo : list2) {
|
PartsPermission partsPermission = new PartsPermission();
|
partsPermission.setUserId(user.getUserId());
|
partsPermission.setPartsId(partsInfo.getPartsId());
|
partsPermissionService.save(partsPermission);
|
PermissionStream stream = new PermissionStream();
|
stream.setProductId(partsInfo.getProductId());
|
stream.setComponentId(partsInfo.getComponentId());
|
stream.setPartsId(partsInfo.getPartsId());
|
stream.setUserId(user.getUserId());
|
permissionStreamService.save(stream);
|
}
|
iPartsInfoService.saveBatch(list2);
|
break;
|
case "processStreamList":
|
List<ProcessStream> list3 = (List<ProcessStream>) convertedList;
|
iProcessStreamService.saveBatch(list3);
|
break;
|
case "processInfoList":
|
List<ProcessInfo> list4 = (List<ProcessInfo>) convertedList;
|
iProcessInfoService.saveBatch(list4);
|
break;
|
case "docInfoList":
|
List<DocInfo> list5 = (List<DocInfo>) convertedList;
|
iDocInfoService.saveBatch(list5);
|
break;
|
case "docFileList":
|
List<DocFile> list6 = (List<DocFile>) convertedList;
|
iDocFileService.saveBatch(list6);
|
break;
|
}
|
logger.info("成功同步 {} 条 {} 数据", convertedList.size(), dataType);
|
} catch (TypeConverter.TypeConversionException e) {
|
logger.error("数据类型 {} 转换失败: {}", dataType, e.getMessage());
|
}
|
}
|
});
|
|
} catch (Exception e) {
|
logger.error("数据同步失败: {}", e.getMessage(), e);
|
throw new RuntimeException("数据同步失败", e);
|
}
|
return new ResponseResult(CommonCode.SUCCESS);
|
}
|
|
|
private Map<String, List<?>> fetchDataFromServer() {
|
String apiUrl = serverUrl + "/outer/fileNcSendData";
|
ParameterizedTypeReference<Map<String, List<?>>> typeRef =
|
new ParameterizedTypeReference<Map<String, List<?>>>() {};
|
|
ResponseEntity<Map<String, List<?>>> response =
|
restTemplate.exchange(apiUrl, HttpMethod.GET, null, typeRef);
|
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
throw new RuntimeException("调用服务端接口失败");
|
}
|
return response.getBody();
|
}
|
|
private ImportResult processDataSet(String dataType, List<?> rawList) {
|
try {
|
Class<T> targetClass = (Class<T>) DATA_TYPE_MAPPING.get(dataType);
|
|
if (targetClass == null) {
|
return ImportResult.error("未知数据类型");
|
}
|
// 2. 类型安全转换
|
List<T> entities = convertList(rawList, targetClass);
|
// 3. 执行批量插入
|
int count = dataImportService.batchInsert(entities, targetClass);
|
return ImportResult.success(count);
|
} catch (Exception e) {
|
logger.error("{} 数据处理失败", dataType, e);
|
return ImportResult.error(e.getMessage());
|
}
|
}
|
|
@SuppressWarnings("unchecked")
|
private <T> List<T> convertList(List<?> rawList, Class<T> targetClass) {
|
return rawList.stream()
|
.map(item -> {
|
if (targetClass.isInstance(item)) {
|
return targetClass.cast(item);
|
}
|
throw new ClassCastException("类型转换失败: "
|
+ item.getClass() + " -> " + targetClass);
|
})
|
.collect(Collectors.toList());
|
}
|
|
// 导入结果封装
|
public static class ImportResult {
|
private boolean success;
|
private String message;
|
private int count;
|
|
public static ImportResult success(int count) {
|
ImportResult result = new ImportResult();
|
result.success = true;
|
result.count = count;
|
result.message = "成功插入 " + count + " 条记录";
|
return result;
|
}
|
|
public static ImportResult error(String message) {
|
ImportResult result = new ImportResult();
|
result.success = false;
|
result.message = message;
|
return result;
|
}
|
}
|
}
|