新增业务逻辑
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.zt.plat.module.base.controller.admin.base;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.framework.common.util.object.BeanUtils;
|
||||
import com.zt.plat.module.base.controller.admin.base.vo.MasterDataSyncReqVO;
|
||||
import com.zt.plat.module.base.service.masterdatasync.MasterDataCategorySyncService;
|
||||
import com.zt.plat.module.base.service.masterdatasync.MasterDataSyncService;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncCommand;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataCategorySyncReport;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static com.zt.plat.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - 主数据同步")
|
||||
@RestController
|
||||
@RequestMapping("/base/master-data-sync")
|
||||
@Validated
|
||||
public class MasterDataSyncController {
|
||||
|
||||
@Resource
|
||||
private MasterDataSyncService masterDataSyncService;
|
||||
@Resource
|
||||
private MasterDataCategorySyncService masterDataCategorySyncService;
|
||||
|
||||
@PostMapping("/execute")
|
||||
@Operation(summary = "执行主数据同步")
|
||||
@PreAuthorize("@ss.hasPermission('base:master-data-sync:execute')")
|
||||
public CommonResult<MasterDataSyncReport> execute(@Valid @RequestBody MasterDataSyncReqVO reqVO) {
|
||||
MasterDataSyncCommand command = BeanUtils.toBean(reqVO, MasterDataSyncCommand.class);
|
||||
MasterDataSyncReport report = masterDataSyncService.sync(command);
|
||||
return success(report);
|
||||
}
|
||||
|
||||
@PostMapping("/categories")
|
||||
@Operation(summary = "同步物料分类")
|
||||
@PreAuthorize("@ss.hasPermission('base:master-data-sync:categories')")
|
||||
public CommonResult<MasterDataCategorySyncReport> syncCategories() {
|
||||
MasterDataCategorySyncReport report = masterDataCategorySyncService.syncAll();
|
||||
return success(report);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.zt.plat.module.base.controller.admin.base.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Positive;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Schema(description = "主数据同步请求参数")
|
||||
public class MasterDataSyncReqVO {
|
||||
|
||||
@Schema(description = "增量同步的起始记录时间,留空执行全量")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime since;
|
||||
|
||||
@Schema(description = "拉取批大小,不填写则使用配置默认值")
|
||||
@Positive(message = "batchSize 必须为正数")
|
||||
private Integer batchSize;
|
||||
|
||||
@Schema(description = "指定要刷新同步的物料编码列表")
|
||||
private List<String> materialCodes;
|
||||
|
||||
@Schema(description = "可选的本次同步记录数上限,未填写表示不限制")
|
||||
@Positive(message = "recordLimit 必须为正数")
|
||||
private Long recordLimit;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.zt.plat.module.base.dal.dataobject.masterdata;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 外部 MDM 分类视图的数据对象。
|
||||
*/
|
||||
@Data
|
||||
public class MdmMaterialCategoryDO {
|
||||
|
||||
/**
|
||||
* 分类主键 ID(CODEID)。
|
||||
*/
|
||||
private Long codeId;
|
||||
|
||||
/**
|
||||
* 分类编码,例如 01 / 0101 / 010101。
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 分类名称(DESC1)。
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 备注信息(DESC2)。
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 父级分类主键 ID(PARENTID)。
|
||||
*/
|
||||
private Long parentCodeId;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.zt.plat.module.base.dal.dataobject.masterdata;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 外部 MDM MySQL 物料视图的投影,用于承接同步字段。
|
||||
*/
|
||||
@Data
|
||||
public class MdmMaterialViewDO {
|
||||
|
||||
private Long codeId;
|
||||
private String materialCode;
|
||||
private String categoryCode;
|
||||
private String categoryName;
|
||||
private String materialName;
|
||||
private String baseUnitCode;
|
||||
private String baseUnitName;
|
||||
private String shortDescription;
|
||||
private String longDescription;
|
||||
private String chalcoCode;
|
||||
private String majorClassCode;
|
||||
private String majorClassName;
|
||||
private String specification;
|
||||
private String model;
|
||||
private String texture;
|
||||
private String drawingNumber;
|
||||
private String orderNumber;
|
||||
private String otherParameters;
|
||||
private String equipmentCategory;
|
||||
private String manufacturer;
|
||||
private String source;
|
||||
private LocalDateTime recordTime;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.zt.plat.module.base.dal.mysql.masterdata;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialCategoryDO;
|
||||
import com.zt.plat.module.base.framework.sync.constant.MasterDataSyncConstants;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 读取外部 MDM 分类视图的 Mapper。
|
||||
*/
|
||||
@Mapper
|
||||
@DS(MasterDataSyncConstants.DEFAULT_SOURCE_DATASOURCE)
|
||||
public interface MdmMaterialCategoryMapper {
|
||||
|
||||
/**
|
||||
* 拉取全部分类档案。
|
||||
*/
|
||||
List<MdmMaterialCategoryDO> selectAll();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.zt.plat.module.base.dal.mysql.masterdata;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialViewDO;
|
||||
import com.zt.plat.module.base.framework.sync.constant.MasterDataSyncConstants;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mapper that exposes read-only access to the external MDM material view.
|
||||
*/
|
||||
@Mapper
|
||||
@DS(MasterDataSyncConstants.DEFAULT_SOURCE_DATASOURCE)
|
||||
public interface MdmMaterialViewMapper {
|
||||
|
||||
List<MdmMaterialViewDO> selectSlice(@Param("offset") long offset,
|
||||
@Param("limit") int limit,
|
||||
@Param("since") LocalDateTime since,
|
||||
@Param("codes") Collection<String> codes);
|
||||
|
||||
Long countEligible(@Param("since") LocalDateTime since,
|
||||
@Param("codes") Collection<String> codes);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.zt.plat.module.base.framework.sync.config;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 主数据同步用到的基础配置。
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MasterDataSyncProperties.class)
|
||||
public class MasterDataSyncConfiguration {
|
||||
|
||||
@Bean
|
||||
public OkHttpClient masterDataSyncOkHttpClient(MasterDataSyncProperties properties) {
|
||||
MasterDataSyncProperties.HttpProperties http = properties.getHttp();
|
||||
return new OkHttpClient.Builder()
|
||||
.connectTimeout(http.getConnectTimeout())
|
||||
.readTimeout(http.getReadTimeout())
|
||||
.writeTimeout(http.getWriteTimeout())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.zt.plat.module.base.framework.sync.config;
|
||||
|
||||
import com.zt.plat.module.base.framework.sync.constant.MasterDataSyncConstants;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 主数据同步管道的配置项。
|
||||
*/
|
||||
@Data
|
||||
@Validated
|
||||
@ConfigurationProperties(prefix = "base.master-data-sync")
|
||||
public class MasterDataSyncProperties {
|
||||
|
||||
/**
|
||||
* 是否允许运行主数据同步。
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* 从外部数据源读取时的默认批量大小。
|
||||
*/
|
||||
@Min(1)
|
||||
private int batchSize = 500;
|
||||
|
||||
/**
|
||||
* 指向外部 MDM MySQL 的动态数据源名称。
|
||||
*/
|
||||
private String sourceDatasourceName = MasterDataSyncConstants.DEFAULT_SOURCE_DATASOURCE;
|
||||
|
||||
/**
|
||||
* 成功是否进行回调通知。
|
||||
*/
|
||||
private boolean notifyOnSuccess = true;
|
||||
|
||||
/**
|
||||
* 失败是否进行回调通知。
|
||||
*/
|
||||
private boolean notifyOnFailure = true;
|
||||
|
||||
/**
|
||||
* 可选的回调地址,用于推送同步结果。
|
||||
*/
|
||||
private String callbackUrl;
|
||||
|
||||
/**
|
||||
* 回调请求需要附加的静态请求头。
|
||||
*/
|
||||
private Map<String, String> callbackHeaders = new HashMap<>();
|
||||
|
||||
/**
|
||||
* HTTP 客户端的超时时间等参数。
|
||||
*/
|
||||
private final HttpProperties http = new HttpProperties();
|
||||
|
||||
@Data
|
||||
public static class HttpProperties {
|
||||
private Duration connectTimeout = Duration.ofSeconds(5);
|
||||
private Duration readTimeout = Duration.ofSeconds(30);
|
||||
private Duration writeTimeout = Duration.ofSeconds(30);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.zt.plat.module.base.framework.sync.constant;
|
||||
|
||||
/**
|
||||
* 主数据同步相关的通用常量。
|
||||
*/
|
||||
public final class MasterDataSyncConstants {
|
||||
|
||||
private MasterDataSyncConstants() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 指向外部 MDM MySQL 的默认数据源名称。
|
||||
*/
|
||||
public static final String DEFAULT_SOURCE_DATASOURCE = "mdm";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync;
|
||||
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataCategorySyncReport;
|
||||
|
||||
/**
|
||||
* 物料分类同步服务接口。
|
||||
*/
|
||||
public interface MasterDataCategorySyncService {
|
||||
|
||||
MasterDataCategorySyncReport syncAll();
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zt.plat.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import com.zt.plat.module.base.dal.dao.materialclasses.MaterialClassesMapper;
|
||||
import com.zt.plat.module.base.dal.dataobject.materialclasses.MaterialClassesDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialCategoryDO;
|
||||
import com.zt.plat.module.base.dal.mysql.masterdata.MdmMaterialCategoryMapper;
|
||||
import com.zt.plat.module.base.enums.ErrorCodeConstants;
|
||||
import com.zt.plat.module.base.framework.sync.config.MasterDataSyncProperties;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataCategorySyncReport;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 物料分类同步实现。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class MasterDataCategorySyncServiceImpl implements MasterDataCategorySyncService {
|
||||
|
||||
private static final long ROOT_CLASS_PARENT_ID = 0L;
|
||||
|
||||
@Resource
|
||||
private MdmMaterialCategoryMapper mdmMaterialCategoryMapper;
|
||||
@Resource
|
||||
private MaterialClassesMapper materialClassesMapper;
|
||||
@Resource
|
||||
private MasterDataSyncProperties properties;
|
||||
|
||||
@Override
|
||||
public MasterDataCategorySyncReport syncAll() {
|
||||
if (!properties.isEnabled()) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.MASTER_DATA_SYNC_DISABLED);
|
||||
}
|
||||
MasterDataCategorySyncReport report = MasterDataCategorySyncReport.start();
|
||||
try {
|
||||
List<MdmMaterialCategoryDO> categories = mdmMaterialCategoryMapper.selectAll();
|
||||
if (CollUtil.isEmpty(categories)) {
|
||||
report.markSuccess();
|
||||
return report;
|
||||
}
|
||||
categories.sort(Comparator.comparing(MdmMaterialCategoryDO::getCode));
|
||||
Map<String, MaterialClassesDO> classesByCode = loadExistingClasses(categories);
|
||||
Map<Long, MaterialClassesDO> classesById = classesByCode.values().stream()
|
||||
.filter(item -> item.getId() != null)
|
||||
.collect(Collectors.toMap(MaterialClassesDO::getId, item -> item, (a, b) -> a, LinkedHashMap::new));
|
||||
// 统一收集新增分类,批量写入提升性能
|
||||
List<MaterialClassesDO> pendingClassInserts = new ArrayList<>();
|
||||
for (MdmMaterialCategoryDO category : categories) {
|
||||
Long sourceId = category.getCodeId();
|
||||
if (sourceId == null) {
|
||||
log.warn("分类 {} 缺少主键 CODEID,已跳过", category.getCode());
|
||||
continue;
|
||||
}
|
||||
String code = normalizeCode(category.getCode());
|
||||
if (code == null) {
|
||||
continue;
|
||||
}
|
||||
report.incrementProcessed(1);
|
||||
long level = determineLevel(code);
|
||||
Long parentId = resolveParentId(category, classesByCode, classesById, report);
|
||||
if (parentId == null) {
|
||||
parentId = ROOT_CLASS_PARENT_ID;
|
||||
}
|
||||
String name = StrUtil.emptyIfNull(StrUtil.trim(category.getName()));
|
||||
String remark = StrUtil.emptyIfNull(StrUtil.trim(category.getRemark()));
|
||||
MaterialClassesDO current = classesByCode.get(code);
|
||||
if (current == null) {
|
||||
MaterialClassesDO created = MaterialClassesDO.builder()
|
||||
.id(sourceId)
|
||||
.code(code)
|
||||
.name(name)
|
||||
.level(level)
|
||||
.parentId(parentId)
|
||||
.remark(remark)
|
||||
.build();
|
||||
pendingClassInserts.add(created);
|
||||
classesByCode.put(code, created);
|
||||
if (created.getId() != null) {
|
||||
classesById.put(created.getId(), created);
|
||||
}
|
||||
report.incrementInserted();
|
||||
} else {
|
||||
if (current.getId() == null && sourceId != null) {
|
||||
current.setId(sourceId);
|
||||
classesById.put(sourceId, current);
|
||||
}
|
||||
boolean needUpdate = false;
|
||||
MaterialClassesDO update = new MaterialClassesDO();
|
||||
update.setId(current.getId());
|
||||
if (!Objects.equals(current.getParentId(), parentId)) {
|
||||
update.setParentId(parentId);
|
||||
current.setParentId(parentId);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (StrUtil.isNotBlank(name) && !StrUtil.equals(current.getName(), name)) {
|
||||
update.setName(name);
|
||||
current.setName(name);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (!Objects.equals(current.getLevel(), level)) {
|
||||
update.setLevel(level);
|
||||
current.setLevel(level);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (!Objects.equals(StrUtil.emptyIfNull(current.getRemark()), remark)) {
|
||||
update.setRemark(remark);
|
||||
current.setRemark(remark);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (needUpdate) {
|
||||
materialClassesMapper.updateById(update);
|
||||
report.incrementUpdated();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CollUtil.isNotEmpty(pendingClassInserts)) {
|
||||
materialClassesMapper.insertBatch(pendingClassInserts);
|
||||
}
|
||||
report.markSuccess();
|
||||
return report;
|
||||
} catch (Throwable ex) {
|
||||
report.markFailure(ex.getMessage());
|
||||
throw ex;
|
||||
} finally {
|
||||
report.finish();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, MaterialClassesDO> loadExistingClasses(List<MdmMaterialCategoryDO> categories) {
|
||||
Set<String> codes = categories.stream()
|
||||
.map(MdmMaterialCategoryDO::getCode)
|
||||
.map(this::normalizeCode)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
List<MaterialClassesDO> existing = materialClassesMapper.selectByCodes(codes);
|
||||
Map<String, MaterialClassesDO> result = new LinkedHashMap<>();
|
||||
if (existing != null) {
|
||||
existing.forEach(item -> {
|
||||
String key = normalizeCode(item.getCode());
|
||||
if (key != null) {
|
||||
result.put(key, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Long resolveParentId(MdmMaterialCategoryDO category,
|
||||
Map<String, MaterialClassesDO> classesByCode,
|
||||
Map<Long, MaterialClassesDO> classesById,
|
||||
MasterDataCategorySyncReport report) {
|
||||
if (category.getParentCodeId() != null && category.getParentCodeId() > 0) {
|
||||
MaterialClassesDO parent = classesById.get(category.getParentCodeId());
|
||||
if (parent != null) {
|
||||
return parent.getId();
|
||||
}
|
||||
log.warn("分类 {} 指定父级 ID {} 不存在,将尝试按编码推断", category.getCode(), category.getParentCodeId());
|
||||
}
|
||||
String parentCode = resolveParentCode(normalizeCode(category.getCode()));
|
||||
if (parentCode == null) {
|
||||
return ROOT_CLASS_PARENT_ID;
|
||||
}
|
||||
MaterialClassesDO parent = classesByCode.get(parentCode);
|
||||
if (parent == null) {
|
||||
log.warn("分类 {} 缺失父级 {},将挂载到根节点", category.getCode(), parentCode);
|
||||
if (report != null) {
|
||||
report.incrementMissingParent();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return parent.getId();
|
||||
}
|
||||
|
||||
private String resolveParentCode(String code) {
|
||||
if (code == null) {
|
||||
return null;
|
||||
}
|
||||
if (code.length() <= 2) {
|
||||
return null;
|
||||
}
|
||||
if (code.length() <= 4) {
|
||||
return code.substring(0, 2);
|
||||
}
|
||||
return code.substring(0, 4);
|
||||
}
|
||||
|
||||
private long determineLevel(String code) {
|
||||
if (code.length() <= 2) {
|
||||
return 1L;
|
||||
}
|
||||
if (code.length() <= 4) {
|
||||
return 2L;
|
||||
}
|
||||
return 3L;
|
||||
}
|
||||
|
||||
private String normalizeCode(String code) {
|
||||
return StrUtil.isBlank(code) ? null : StrUtil.trim(code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync;
|
||||
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncCommand;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
|
||||
|
||||
/**
|
||||
* 物料主数据同步的服务入口。
|
||||
*/
|
||||
public interface MasterDataSyncService {
|
||||
|
||||
MasterDataSyncReport sync(MasterDataSyncCommand command);
|
||||
}
|
||||
@@ -0,0 +1,488 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.zt.plat.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import com.zt.plat.module.base.dal.dao.materialclasses.MaterialClassesMapper;
|
||||
import com.zt.plat.module.base.dal.dao.materialhasclasses.MaterialHasClassesMapper;
|
||||
import com.zt.plat.module.base.dal.dao.materialhasproperties.MaterialHasPropertiesMapper;
|
||||
import com.zt.plat.module.base.dal.dao.materialproperties.MaterialPropertiesMapper;
|
||||
import com.zt.plat.module.base.dal.dataobject.base.MaterialInfomationDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.materialclasses.MaterialClassesDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.materialhasclasses.MaterialHasClassesDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.materialhasproperties.MaterialHasPropertiesDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.materialproperties.MaterialPropertiesDO;
|
||||
import com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialViewDO;
|
||||
import com.zt.plat.module.base.dal.mysql.base.MaterialInfomationMapper;
|
||||
import com.zt.plat.module.base.dal.mysql.masterdata.MdmMaterialViewMapper;
|
||||
import com.zt.plat.module.base.enums.ErrorCodeConstants;
|
||||
import com.zt.plat.module.base.framework.sync.config.MasterDataSyncProperties;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncCommand;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
|
||||
import com.zt.plat.module.base.service.masterdatasync.support.MasterDataPropertyDefinition;
|
||||
import com.zt.plat.module.base.service.masterdatasync.support.MasterDataSyncNotifier;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 主数据同步核心实现,负责从外部 MDM 视图读取数据并落库到本地 BSE 表。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class MasterDataSyncServiceImpl implements MasterDataSyncService {
|
||||
|
||||
private static final long ROOT_CLASS_PARENT_ID = 0L;
|
||||
|
||||
@Resource
|
||||
private MdmMaterialViewMapper mdmMaterialViewMapper;
|
||||
@Resource
|
||||
private MaterialInfomationMapper materialInfomationMapper;
|
||||
@Resource
|
||||
private MaterialClassesMapper materialClassesMapper;
|
||||
@Resource
|
||||
private MaterialHasClassesMapper materialHasClassesMapper;
|
||||
@Resource
|
||||
private MaterialPropertiesMapper materialPropertiesMapper;
|
||||
@Resource
|
||||
private MaterialHasPropertiesMapper materialHasPropertiesMapper;
|
||||
@Resource
|
||||
private TransactionTemplate transactionTemplate;
|
||||
@Resource
|
||||
private MasterDataSyncProperties properties;
|
||||
@Resource
|
||||
private MasterDataSyncNotifier notifier;
|
||||
|
||||
@Override
|
||||
public MasterDataSyncReport sync(MasterDataSyncCommand command) {
|
||||
if (!properties.isEnabled()) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.MASTER_DATA_SYNC_DISABLED);
|
||||
}
|
||||
MasterDataSyncReport report = MasterDataSyncReport.start(command);
|
||||
List<String> materialCodes = sanitizeCodes(command.getMaterialCodes());
|
||||
report.setMaterialCodes(materialCodes);
|
||||
int batchSize = resolveBatchSize(command.getBatchSize());
|
||||
report.setBatchSize(batchSize);
|
||||
Long recordLimit = resolveRecordLimit(command.getRecordLimit());
|
||||
report.setRecordLimit(recordLimit);
|
||||
LocalDateTime since = command.getSince();
|
||||
Map<MasterDataPropertyDefinition, MaterialPropertiesDO> propertyDefinitions = ensurePropertyDefinitions(report);
|
||||
// 缓存已读取的物料与分类,避免批次间重复查询数据库
|
||||
Map<Long, MaterialInfomationDO> materialCacheById = new LinkedHashMap<>();
|
||||
Map<String, MaterialInfomationDO> materialCacheByCode = new LinkedHashMap<>();
|
||||
Map<String, MaterialClassesDO> categoryCacheByCode = new LinkedHashMap<>();
|
||||
|
||||
long offset = 0L;
|
||||
Throwable failure = null;
|
||||
try {
|
||||
while (true) {
|
||||
int fetchSize = batchSize;
|
||||
if (recordLimit != null) {
|
||||
long remaining = recordLimit - report.getProcessedRecords();
|
||||
if (remaining <= 0) {
|
||||
break;
|
||||
}
|
||||
if (remaining < fetchSize) {
|
||||
fetchSize = (int) remaining;
|
||||
}
|
||||
}
|
||||
List<MdmMaterialViewDO> slice = mdmMaterialViewMapper.selectSlice(offset, fetchSize, since, materialCodes);
|
||||
if (CollUtil.isEmpty(slice)) {
|
||||
break;
|
||||
}
|
||||
offset += slice.size();
|
||||
report.incrementProcessedRecords(slice.size());
|
||||
final List<MdmMaterialViewDO> batch = slice;
|
||||
transactionTemplate.executeWithoutResult(status -> processBatch(batch, propertyDefinitions, report,
|
||||
materialCacheById, materialCacheByCode, categoryCacheByCode));
|
||||
}
|
||||
report.markSuccess();
|
||||
return report;
|
||||
} catch (Throwable ex) {
|
||||
failure = ex;
|
||||
report.markFailure(ex.getMessage());
|
||||
throw ex;
|
||||
} finally {
|
||||
report.finish();
|
||||
notifier.dispatch(report, failure);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> sanitizeCodes(List<String> codes) {
|
||||
if (CollUtil.isEmpty(codes)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return codes.stream()
|
||||
.filter(StrUtil::isNotBlank)
|
||||
.map(code -> StrUtil.trim(code).toUpperCase())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private int resolveBatchSize(Integer requested) {
|
||||
if (requested != null && requested > 0) {
|
||||
return requested;
|
||||
}
|
||||
return Math.max(1, properties.getBatchSize());
|
||||
}
|
||||
|
||||
private Long resolveRecordLimit(Long requested) {
|
||||
if (requested == null || requested <= 0) {
|
||||
return null;
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保所有需要的属性定义已经存在,若缺失则自动创建。
|
||||
*/
|
||||
private Map<MasterDataPropertyDefinition, MaterialPropertiesDO> ensurePropertyDefinitions(MasterDataSyncReport report) {
|
||||
List<String> codes = Arrays.stream(MasterDataPropertyDefinition.values())
|
||||
.map(MasterDataPropertyDefinition::getCode)
|
||||
.toList();
|
||||
List<MaterialPropertiesDO> existing = materialPropertiesMapper.selectByCodes(codes);
|
||||
Map<String, MaterialPropertiesDO> existingMap = Optional.ofNullable(existing)
|
||||
.orElse(Collections.emptyList())
|
||||
.stream()
|
||||
.collect(Collectors.toMap(MaterialPropertiesDO::getCode, Function.identity()));
|
||||
|
||||
Map<MasterDataPropertyDefinition, MaterialPropertiesDO> mapping = new EnumMap<>(MasterDataPropertyDefinition.class);
|
||||
List<MaterialPropertiesDO> pendingInsertDefinitions = new ArrayList<>();
|
||||
for (MasterDataPropertyDefinition definition : MasterDataPropertyDefinition.values()) {
|
||||
MaterialPropertiesDO current = existingMap.get(definition.getCode());
|
||||
if (current == null) {
|
||||
current = MaterialPropertiesDO.builder()
|
||||
.id(IdWorker.getId())
|
||||
.code(definition.getCode())
|
||||
.name(definition.getDisplayName())
|
||||
.dataType(definition.getDataType())
|
||||
.remark("MDM同步自动创建")
|
||||
.build();
|
||||
pendingInsertDefinitions.add(current);
|
||||
report.incrementInsertedPropertyDefinitions();
|
||||
} else {
|
||||
boolean needUpdate = false;
|
||||
if (!StrUtil.equals(current.getName(), definition.getDisplayName())) {
|
||||
current.setName(definition.getDisplayName());
|
||||
needUpdate = true;
|
||||
}
|
||||
if (!StrUtil.equals(current.getDataType(), definition.getDataType())) {
|
||||
current.setDataType(definition.getDataType());
|
||||
needUpdate = true;
|
||||
}
|
||||
if (needUpdate) {
|
||||
MaterialPropertiesDO update = new MaterialPropertiesDO();
|
||||
update.setId(current.getId());
|
||||
update.setName(current.getName());
|
||||
update.setDataType(current.getDataType());
|
||||
materialPropertiesMapper.updateById(update);
|
||||
report.incrementUpdatedPropertyDefinitions();
|
||||
}
|
||||
}
|
||||
mapping.put(definition, current);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(pendingInsertDefinitions)) {
|
||||
materialPropertiesMapper.insertBatch(pendingInsertDefinitions);
|
||||
}
|
||||
return mapping;
|
||||
}
|
||||
|
||||
private void processBatch(List<MdmMaterialViewDO> batch,
|
||||
Map<MasterDataPropertyDefinition, MaterialPropertiesDO> propertyDefinitions,
|
||||
MasterDataSyncReport report,
|
||||
Map<Long, MaterialInfomationDO> materialCacheById,
|
||||
Map<String, MaterialInfomationDO> materialCacheByCode,
|
||||
Map<String, MaterialClassesDO> categoryCacheByCode) {
|
||||
if (CollUtil.isEmpty(batch)) {
|
||||
return;
|
||||
}
|
||||
// 1. 落库物料主数据
|
||||
Map<String, MaterialInfomationDO> materials = upsertMaterials(batch, report, materialCacheById, materialCacheByCode);
|
||||
// 2. 读取已存在的分类信息
|
||||
Map<String, MaterialClassesDO> categoryClasses = loadCategoryClasses(batch, categoryCacheByCode);
|
||||
// 3. 建立物料与分类的关系
|
||||
bindMaterialClasses(batch, materials, categoryClasses, report);
|
||||
// 4. 写入物料属性及属性值
|
||||
upsertMaterialProperties(batch, materials, propertyDefinitions, report);
|
||||
}
|
||||
|
||||
private Map<String, MaterialClassesDO> loadCategoryClasses(List<MdmMaterialViewDO> batch,
|
||||
Map<String, MaterialClassesDO> categoryCacheByCode) {
|
||||
Set<String> requestedCodes = batch.stream()
|
||||
.map(MdmMaterialViewDO::getCategoryCode)
|
||||
.map(this::normalizeValue)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
if (requestedCodes.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<String, MaterialClassesDO> mapping = requestedCodes.stream()
|
||||
.map(code -> new Object[]{code, categoryCacheByCode.get(code)})
|
||||
.filter(pair -> pair[1] != null)
|
||||
.collect(Collectors.toMap(pair -> (String) pair[0], pair -> (MaterialClassesDO) pair[1], (a, b) -> a, LinkedHashMap::new));
|
||||
Set<String> missing = new LinkedHashSet<>(requestedCodes);
|
||||
missing.removeIf(mapping::containsKey);
|
||||
if (!missing.isEmpty()) {
|
||||
List<MaterialClassesDO> newlyLoaded = materialClassesMapper.selectByCodes(missing);
|
||||
Optional.ofNullable(newlyLoaded)
|
||||
.orElse(Collections.emptyList())
|
||||
.forEach(item -> {
|
||||
String code = normalizeValue(item.getCode());
|
||||
if (code != null) {
|
||||
categoryCacheByCode.put(code, item);
|
||||
mapping.put(code, item);
|
||||
}
|
||||
});
|
||||
missing.removeIf(mapping::containsKey);
|
||||
if (!missing.isEmpty()) {
|
||||
List<String> preview = missing.stream().limit(10).toList();
|
||||
log.warn("当前批次存在 {} 个分类未同步,示例: {}", missing.size(), preview);
|
||||
}
|
||||
}
|
||||
return mapping;
|
||||
}
|
||||
|
||||
private Map<String, MaterialInfomationDO> upsertMaterials(List<MdmMaterialViewDO> batch,
|
||||
MasterDataSyncReport report,
|
||||
Map<Long, MaterialInfomationDO> materialCacheById,
|
||||
Map<String, MaterialInfomationDO> materialCacheByCode) {
|
||||
Set<Long> sourceIds = batch.stream()
|
||||
.map(MdmMaterialViewDO::getCodeId)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(id -> id > 0)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
if (sourceIds.isEmpty()) {
|
||||
log.warn("本次批次缺少有效 CODEID,无法同步物料主数据");
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<Long, MaterialInfomationDO> existingById = sourceIds.stream()
|
||||
.map(materialCacheById::get)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toMap(MaterialInfomationDO::getId, Function.identity(), (a, b) -> a, LinkedHashMap::new));
|
||||
Set<Long> missingIds = new LinkedHashSet<>(sourceIds);
|
||||
missingIds.removeIf(existingById::containsKey);
|
||||
if (!missingIds.isEmpty()) {
|
||||
List<MaterialInfomationDO> fetched = materialInfomationMapper.selectBatchIds(missingIds);
|
||||
Optional.ofNullable(fetched)
|
||||
.orElse(Collections.emptyList())
|
||||
.forEach(item -> {
|
||||
if (item.getId() != null) {
|
||||
existingById.put(item.getId(), item);
|
||||
materialCacheById.put(item.getId(), item);
|
||||
String normalizedCode = normalizeValue(item.getCode());
|
||||
if (normalizedCode != null) {
|
||||
materialCacheByCode.put(normalizedCode, item);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Map<String, MaterialInfomationDO> mapping = new LinkedHashMap<>();
|
||||
List<MaterialInfomationDO> pendingMaterialInserts = new ArrayList<>();
|
||||
for (MdmMaterialViewDO item : batch) {
|
||||
Long codeId = item.getCodeId();
|
||||
if (codeId == null || codeId <= 0) {
|
||||
log.warn("物料 {} 缺少主键 CODEID,已跳过", item.getMaterialCode());
|
||||
continue;
|
||||
}
|
||||
String code = normalizeValue(item.getMaterialCode());
|
||||
if (code == null) {
|
||||
continue;
|
||||
}
|
||||
MaterialInfomationDO current = existingById.get(codeId);
|
||||
String name = normalizeValue(item.getMaterialName());
|
||||
if (current == null) {
|
||||
MaterialInfomationDO created = MaterialInfomationDO.builder()
|
||||
.id(codeId)
|
||||
.code(code)
|
||||
.name(StrUtil.emptyIfNull(name))
|
||||
.remark(null)
|
||||
.build();
|
||||
pendingMaterialInserts.add(created);
|
||||
existingById.put(codeId, created);
|
||||
materialCacheById.put(codeId, created);
|
||||
materialCacheByCode.put(code, created);
|
||||
mapping.put(code, created);
|
||||
report.incrementInsertedMaterials();
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean needUpdate = false;
|
||||
MaterialInfomationDO update = null;
|
||||
if (!StrUtil.equals(current.getCode(), code)) {
|
||||
current.setCode(code);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (name != null && !StrUtil.equals(current.getName(), name)) {
|
||||
current.setName(name);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (needUpdate) {
|
||||
update = new MaterialInfomationDO();
|
||||
update.setId(current.getId());
|
||||
update.setCode(current.getCode());
|
||||
update.setName(current.getName());
|
||||
materialInfomationMapper.updateById(update);
|
||||
report.incrementUpdatedMaterials();
|
||||
}
|
||||
mapping.put(code, current);
|
||||
materialCacheByCode.put(code, current);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(pendingMaterialInserts)) {
|
||||
materialInfomationMapper.insertBatch(pendingMaterialInserts);
|
||||
}
|
||||
return mapping;
|
||||
}
|
||||
|
||||
private void bindMaterialClasses(List<MdmMaterialViewDO> batch,
|
||||
Map<String, MaterialInfomationDO> materials,
|
||||
Map<String, MaterialClassesDO> categoryClasses,
|
||||
MasterDataSyncReport report) {
|
||||
if (materials.isEmpty() || categoryClasses.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<Long> infoIds = batch.stream()
|
||||
.map(MdmMaterialViewDO::getMaterialCode)
|
||||
.map(this::normalizeValue)
|
||||
.map(materials::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(MaterialInfomationDO::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
Map<Long, MaterialHasClassesDO> existingRelations = materialHasClassesMapper.selectByInfoIds(infoIds)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(MaterialHasClassesDO::getInfomationId, Function.identity(), (a, b) -> a));
|
||||
List<MaterialHasClassesDO> relationsToInsert = new ArrayList<>();
|
||||
for (MdmMaterialViewDO item : batch) {
|
||||
String materialCode = normalizeValue(item.getMaterialCode());
|
||||
MaterialInfomationDO info = materials.get(materialCode);
|
||||
if (info == null) {
|
||||
continue;
|
||||
}
|
||||
String categoryCode = normalizeValue(item.getCategoryCode());
|
||||
MaterialClassesDO cls = categoryClasses.get(categoryCode);
|
||||
if (cls == null) {
|
||||
continue;
|
||||
}
|
||||
MaterialHasClassesDO relation = existingRelations.get(info.getId());
|
||||
if (relation == null) {
|
||||
MaterialHasClassesDO created = MaterialHasClassesDO.builder()
|
||||
.infomationId(info.getId())
|
||||
.classesId(cls.getId())
|
||||
.build();
|
||||
relationsToInsert.add(created);
|
||||
existingRelations.put(info.getId(), created);
|
||||
report.incrementCreatedClassRelations();
|
||||
} else if (!Objects.equals(relation.getClassesId(), cls.getId())) {
|
||||
MaterialHasClassesDO update = new MaterialHasClassesDO();
|
||||
update.setId(relation.getId());
|
||||
update.setClassesId(cls.getId());
|
||||
materialHasClassesMapper.updateById(update);
|
||||
relation.setClassesId(cls.getId());
|
||||
report.incrementUpdatedClassRelations();
|
||||
}
|
||||
}
|
||||
if (CollUtil.isNotEmpty(relationsToInsert)) {
|
||||
materialHasClassesMapper.insertBatch(relationsToInsert);
|
||||
}
|
||||
}
|
||||
|
||||
private void upsertMaterialProperties(List<MdmMaterialViewDO> batch,
|
||||
Map<String, MaterialInfomationDO> materials,
|
||||
Map<MasterDataPropertyDefinition, MaterialPropertiesDO> propertyDefinitions,
|
||||
MasterDataSyncReport report) {
|
||||
if (materials.isEmpty() || propertyDefinitions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<Long> infoIds = batch.stream()
|
||||
.map(MdmMaterialViewDO::getMaterialCode)
|
||||
.map(this::normalizeValue)
|
||||
.map(materials::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(MaterialInfomationDO::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
List<Long> propertyIds = propertyDefinitions.values().stream()
|
||||
.map(MaterialPropertiesDO::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
Map<String, MaterialHasPropertiesDO> existing = materialHasPropertiesMapper
|
||||
.selectByInfoIdsAndPropertyIds(infoIds, propertyIds)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(item -> buildRelationKey(item.getInfomationId(), item.getPropertiesId()),
|
||||
Function.identity(), (a, b) -> a));
|
||||
List<MaterialHasPropertiesDO> propertyRelationsToInsert = new ArrayList<>();
|
||||
for (MdmMaterialViewDO item : batch) {
|
||||
String materialCode = normalizeValue(item.getMaterialCode());
|
||||
MaterialInfomationDO info = materials.get(materialCode);
|
||||
if (info == null || info.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
for (Map.Entry<MasterDataPropertyDefinition, MaterialPropertiesDO> entry : propertyDefinitions.entrySet()) {
|
||||
MaterialPropertiesDO property = entry.getValue();
|
||||
if (property == null || property.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
String value = normalizeValue(entry.getKey().extractValue(item));
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
String key = buildRelationKey(info.getId(), property.getId());
|
||||
MaterialHasPropertiesDO relation = existing.get(key);
|
||||
if (relation == null) {
|
||||
MaterialHasPropertiesDO created = MaterialHasPropertiesDO.builder()
|
||||
.infomationId(info.getId())
|
||||
.propertiesId(property.getId())
|
||||
.value(value)
|
||||
.sort(entry.getKey().getSort())
|
||||
.isKey(0)
|
||||
.isMetering(0)
|
||||
.build();
|
||||
propertyRelationsToInsert.add(created);
|
||||
existing.put(key, created);
|
||||
report.incrementInsertedPropertyValues();
|
||||
} else if (!StrUtil.equals(relation.getValue(), value)
|
||||
|| !Objects.equals(relation.getSort(), entry.getKey().getSort())) {
|
||||
MaterialHasPropertiesDO update = new MaterialHasPropertiesDO();
|
||||
update.setId(relation.getId());
|
||||
update.setValue(value);
|
||||
update.setSort(entry.getKey().getSort());
|
||||
materialHasPropertiesMapper.updateById(update);
|
||||
relation.setValue(value);
|
||||
relation.setSort(entry.getKey().getSort());
|
||||
report.incrementUpdatedPropertyValues();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CollUtil.isNotEmpty(propertyRelationsToInsert)) {
|
||||
materialHasPropertiesMapper.insertBatch(propertyRelationsToInsert);
|
||||
}
|
||||
}
|
||||
|
||||
private String buildRelationKey(Long infoId, Long propertyId) {
|
||||
return infoId + ":" + propertyId;
|
||||
}
|
||||
|
||||
private String normalizeValue(String value) {
|
||||
return StrUtil.isBlank(value) ? null : StrUtil.trim(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 物料分类同步执行结果。
|
||||
*/
|
||||
@Data
|
||||
public class MasterDataCategorySyncReport {
|
||||
|
||||
private long processedCategories;
|
||||
private long insertedCategories;
|
||||
private long updatedCategories;
|
||||
private long missingParentReferences;
|
||||
private boolean success;
|
||||
private String message;
|
||||
private LocalDateTime startedAt;
|
||||
private LocalDateTime finishedAt;
|
||||
|
||||
public static MasterDataCategorySyncReport start() {
|
||||
MasterDataCategorySyncReport report = new MasterDataCategorySyncReport();
|
||||
report.setStartedAt(LocalDateTime.now());
|
||||
report.setSuccess(false);
|
||||
report.setMessage("执行中");
|
||||
return report;
|
||||
}
|
||||
|
||||
public void markSuccess() {
|
||||
this.success = true;
|
||||
this.message = "执行成功";
|
||||
}
|
||||
|
||||
public void markFailure(String message) {
|
||||
this.success = false;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
this.finishedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public void incrementProcessed(long delta) {
|
||||
this.processedCategories += delta;
|
||||
}
|
||||
|
||||
public void incrementInserted() {
|
||||
this.insertedCategories++;
|
||||
}
|
||||
|
||||
public void incrementUpdated() {
|
||||
this.updatedCategories++;
|
||||
}
|
||||
|
||||
public void incrementMissingParent() {
|
||||
this.missingParentReferences++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主数据同步的入参模型。
|
||||
*/
|
||||
@Data
|
||||
public class MasterDataSyncCommand {
|
||||
|
||||
/**
|
||||
* 增量同步的起始时间,空值表示全量。
|
||||
*/
|
||||
private LocalDateTime since;
|
||||
|
||||
/**
|
||||
* 单批拉取的记录数,空值取配置默认值。
|
||||
*/
|
||||
private Integer batchSize;
|
||||
|
||||
/**
|
||||
* 需要限定同步范围的物料编码集合。
|
||||
*/
|
||||
private List<String> materialCodes;
|
||||
|
||||
/**
|
||||
* 本次同步允许处理的最大记录数,空值表示不限制。
|
||||
*/
|
||||
private Long recordLimit;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主数据同步的执行结果统计。
|
||||
*/
|
||||
@Data
|
||||
public class MasterDataSyncReport {
|
||||
|
||||
private long processedRecords;
|
||||
private long insertedMaterials;
|
||||
private long updatedMaterials;
|
||||
private long insertedClasses;
|
||||
private long updatedClasses;
|
||||
private long createdClassRelations;
|
||||
private long updatedClassRelations;
|
||||
private long insertedPropertyDefinitions;
|
||||
private long updatedPropertyDefinitions;
|
||||
private long insertedPropertyValues;
|
||||
private long updatedPropertyValues;
|
||||
private LocalDateTime startedAt;
|
||||
private LocalDateTime finishedAt;
|
||||
private boolean success;
|
||||
private String message;
|
||||
private Integer batchSize;
|
||||
private LocalDateTime since;
|
||||
private List<String> materialCodes = Collections.emptyList();
|
||||
private Long recordLimit;
|
||||
|
||||
public static MasterDataSyncReport start(MasterDataSyncCommand command) {
|
||||
MasterDataSyncReport report = new MasterDataSyncReport();
|
||||
report.setStartedAt(LocalDateTime.now());
|
||||
report.setBatchSize(command.getBatchSize());
|
||||
report.setSince(command.getSince());
|
||||
report.setMaterialCodes(command.getMaterialCodes());
|
||||
report.setRecordLimit(command.getRecordLimit());
|
||||
report.setSuccess(false);
|
||||
report.setMessage("执行中");
|
||||
return report;
|
||||
}
|
||||
|
||||
public void finish() {
|
||||
this.finishedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public void markSuccess() {
|
||||
this.success = true;
|
||||
this.message = "执行成功";
|
||||
}
|
||||
|
||||
public void markFailure(String message) {
|
||||
this.success = false;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void incrementProcessedRecords(long delta) {
|
||||
this.processedRecords += delta;
|
||||
}
|
||||
|
||||
public void incrementInsertedMaterials() {
|
||||
this.insertedMaterials++;
|
||||
}
|
||||
|
||||
public void incrementUpdatedMaterials() {
|
||||
this.updatedMaterials++;
|
||||
}
|
||||
|
||||
public void incrementInsertedClasses() {
|
||||
this.insertedClasses++;
|
||||
}
|
||||
|
||||
public void incrementUpdatedClasses() {
|
||||
this.updatedClasses++;
|
||||
}
|
||||
|
||||
public void incrementCreatedClassRelations() {
|
||||
this.createdClassRelations++;
|
||||
}
|
||||
|
||||
public void incrementUpdatedClassRelations() {
|
||||
this.updatedClassRelations++;
|
||||
}
|
||||
|
||||
public void incrementInsertedPropertyDefinitions() {
|
||||
this.insertedPropertyDefinitions++;
|
||||
}
|
||||
|
||||
public void incrementUpdatedPropertyDefinitions() {
|
||||
this.updatedPropertyDefinitions++;
|
||||
}
|
||||
|
||||
public void incrementInsertedPropertyValues() {
|
||||
this.insertedPropertyValues++;
|
||||
}
|
||||
|
||||
public void incrementUpdatedPropertyValues() {
|
||||
this.updatedPropertyValues++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.support;
|
||||
|
||||
import com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialViewDO;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 源视图字段与物料属性定义之间的标准映射关系。
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum MasterDataPropertyDefinition {
|
||||
|
||||
BASE_UNIT_CODE("MTRL_BASE_UNIT_CODE", "基本计量单位编码", 50L, MdmMaterialViewDO::getBaseUnitCode, "STRING"),
|
||||
BASE_UNIT_NAME("MTRL_BASE_UNIT_NAME", "基本计量单位描述", 60L, MdmMaterialViewDO::getBaseUnitName, "STRING"),
|
||||
SHORT_DESCRIPTION("MTRL_SHORT_DESC", "短描述", 70L, MdmMaterialViewDO::getShortDescription, "STRING"),
|
||||
LONG_DESCRIPTION("MTRL_LONG_DESC", "长描述", 80L, MdmMaterialViewDO::getLongDescription, "STRING"),
|
||||
CHALCO_CODE("MTRL_CHALCO_CODE", "中铝编码", 90L, MdmMaterialViewDO::getChalcoCode, "STRING"),
|
||||
SPECIFICATION("MTRL_SPECIFICATION", "规格", 100L, MdmMaterialViewDO::getSpecification, "STRING"),
|
||||
MODEL("MTRL_MODEL", "型号", 110L, MdmMaterialViewDO::getModel, "STRING"),
|
||||
TEXTURE("MTRL_TEXTURE", "材质", 120L, MdmMaterialViewDO::getTexture, "STRING"),
|
||||
DRAWING_NUMBER("MTRL_DRAWING_NUMBER", "图号", 130L, MdmMaterialViewDO::getDrawingNumber, "STRING"),
|
||||
ORDER_NUMBER("MTRL_ORDER_NUMBER", "订货号", 140L, MdmMaterialViewDO::getOrderNumber, "STRING"),
|
||||
OTHER_PARAMETERS("MTRL_OTHER_PARAMETERS", "其它参数", 150L, MdmMaterialViewDO::getOtherParameters, "STRING"),
|
||||
EQUIPMENT_CATEGORY("MTRL_EQUIPMENT_CATEGORY", "设备类别", 160L, MdmMaterialViewDO::getEquipmentCategory, "STRING"),
|
||||
MANUFACTURER("MTRL_MANUFACTURER", "主机生产商", 170L, MdmMaterialViewDO::getManufacturer, "STRING"),
|
||||
SOURCE("MTRL_SOURCE", "来源", 180L, MdmMaterialViewDO::getSource, "STRING"),
|
||||
RECORD_TIME("MTRL_RECORD_TIME", "记录时间", 190L,
|
||||
source -> source.getRecordTime() == null ? null : source.getRecordTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
|
||||
"DATETIME");
|
||||
|
||||
private final String code;
|
||||
private final String displayName;
|
||||
private final long sort;
|
||||
private final Function<MdmMaterialViewDO, String> extractor;
|
||||
private final String dataType;
|
||||
|
||||
public String extractValue(MdmMaterialViewDO source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
String value = extractor.apply(source);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
value = value.trim();
|
||||
return value.isEmpty() ? null : value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.support;
|
||||
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 同步结束后推送给下游监听方的 JSON 载荷。
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MasterDataSyncNotificationPayload {
|
||||
|
||||
private MasterDataSyncReport report;
|
||||
private String errorMessage;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.zt.plat.module.base.service.masterdatasync.support;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zt.plat.framework.common.util.json.JsonUtils;
|
||||
import com.zt.plat.module.base.framework.sync.config.MasterDataSyncProperties;
|
||||
import com.zt.plat.module.base.service.masterdatasync.dto.MasterDataSyncReport;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 在同步结束后发送回调通知。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MasterDataSyncNotifier {
|
||||
|
||||
private static final MediaType JSON = MediaType.get("application/json; charset=UTF-8");
|
||||
|
||||
private final OkHttpClient httpClient;
|
||||
private final MasterDataSyncProperties properties;
|
||||
|
||||
public MasterDataSyncNotifier(@Qualifier("masterDataSyncOkHttpClient") OkHttpClient httpClient,
|
||||
MasterDataSyncProperties properties) {
|
||||
this.httpClient = httpClient;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public void dispatch(MasterDataSyncReport report, Throwable failure) {
|
||||
if (report == null || StrUtil.isBlank(properties.getCallbackUrl())) {
|
||||
return;
|
||||
}
|
||||
if (report.isSuccess() && !properties.isNotifyOnSuccess()) {
|
||||
return;
|
||||
}
|
||||
if (!report.isSuccess() && !properties.isNotifyOnFailure()) {
|
||||
return;
|
||||
}
|
||||
MasterDataSyncNotificationPayload payload = new MasterDataSyncNotificationPayload(report,
|
||||
failure == null ? null : failure.getMessage());
|
||||
RequestBody body = RequestBody.create(JSON, JsonUtils.toJsonString(payload));
|
||||
Request.Builder builder = new Request.Builder()
|
||||
.url(properties.getCallbackUrl())
|
||||
.post(body);
|
||||
properties.getCallbackHeaders().forEach(builder::addHeader);
|
||||
try (Response response = httpClient.newCall(builder.build()).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
log.warn("主数据同步回调响应非成功状态: {}", response.code());
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
log.warn("调用主数据同步回调接口失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.zt.plat.module.base.dal.mysql.masterdata.MdmMaterialCategoryMapper">
|
||||
|
||||
<select id="selectAll" resultType="com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialCategoryDO">
|
||||
SELECT
|
||||
CODEID AS codeId,
|
||||
CODE AS code,
|
||||
DESC1 AS name,
|
||||
DESC2 AS remark,
|
||||
PARENTID AS parentCodeId
|
||||
FROM mdm_wlfl_code
|
||||
ORDER BY CODE ASC
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.zt.plat.module.base.dal.mysql.masterdata.MdmMaterialViewMapper">
|
||||
|
||||
<sql id="baseColumns">
|
||||
a.codeid AS code_id,
|
||||
a.code AS material_code,
|
||||
a.categorycode AS category_code,
|
||||
a.categoryname AS category_name,
|
||||
a.desc1 AS material_name,
|
||||
a.desc6 AS base_unit_code,
|
||||
a.desc19 AS base_unit_name,
|
||||
a.descshort AS short_description,
|
||||
a.desclong AS long_description,
|
||||
a.desc86 AS chalco_code,
|
||||
b.pur_class AS major_class_code,
|
||||
c.desc1 AS major_class_name,
|
||||
a.desc9 AS specification,
|
||||
a.desc10 AS model,
|
||||
a.desc11 AS texture,
|
||||
a.desc12 AS drawing_number,
|
||||
a.desc13 AS order_number,
|
||||
a.desc14 AS other_parameters,
|
||||
a.desc15 AS equipment_category,
|
||||
a.desc16 AS manufacturer,
|
||||
a.desc17 AS source,
|
||||
a.recordtime AS record_time
|
||||
</sql>
|
||||
|
||||
<sql id="fromClause">
|
||||
mdm_wlzsj_code a
|
||||
INNER JOIN z_pur_class b ON a.codeid = b.codeid
|
||||
INNER JOIN mdm_wlfl_code c ON b.pur_class = c.code
|
||||
</sql>
|
||||
|
||||
<select id="selectSlice" resultType="com.zt.plat.module.base.dal.dataobject.masterdata.MdmMaterialViewDO">
|
||||
SELECT
|
||||
<include refid="baseColumns"/>
|
||||
FROM
|
||||
<include refid="fromClause"/>
|
||||
WHERE a.auditflag = 2
|
||||
<if test="since != null">
|
||||
AND a.recordtime >= #{since}
|
||||
</if>
|
||||
<if test="codes != null and codes.size > 0">
|
||||
AND a.code IN
|
||||
<foreach collection="codes" item="code" open="(" separator="," close=")">
|
||||
#{code}
|
||||
</foreach>
|
||||
</if>
|
||||
ORDER BY a.CODEID
|
||||
LIMIT #{limit} OFFSET #{offset}
|
||||
</select>
|
||||
|
||||
<select id="countEligible" resultType="long">
|
||||
SELECT COUNT(1)
|
||||
FROM
|
||||
<include refid="fromClause"/>
|
||||
WHERE a.auditflag = 2
|
||||
<if test="since != null">
|
||||
AND a.recordtime >= #{since}
|
||||
</if>
|
||||
<if test="codes != null and codes.size > 0">
|
||||
AND a.code IN
|
||||
<foreach collection="codes" item="code" open="(" separator="," close=")">
|
||||
#{code}
|
||||
</foreach>
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user