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> 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> 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 list = (List) 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 list1 = (List) 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 list2 = (List) 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 list3 = (List) convertedList; iProcessStreamService.saveBatch(list3); break; case "processInfoList": List list4 = (List) convertedList; iProcessInfoService.saveBatch(list4); break; case "docInfoList": List list5 = (List) convertedList; iDocInfoService.saveBatch(list5); break; case "docFileList": List list6 = (List) 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> fetchDataFromServer() { String apiUrl = serverUrl + "/outer/fileNcSendData"; ParameterizedTypeReference>> typeRef = new ParameterizedTypeReference>>() {}; ResponseEntity>> 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 targetClass = (Class) DATA_TYPE_MAPPING.get(dataType); if (targetClass == null) { return ImportResult.error("未知数据类型"); } // 2. 类型安全转换 List 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 List convertList(List rawList, Class 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; } } }