goods同步

This commit is contained in:
2026-05-30 08:26:47 +08:00
parent 246c5a7080
commit dbf408c51f
20 changed files with 1233 additions and 32 deletions

View File

@@ -99,13 +99,12 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
result.put("message", "merchandiseId不能为空");
return result;
}
if (this.getById(entity.getId()) != null) {
entity.setTenantId(entity.getTenantId().trim());
if (getByTenantAndId(entity.getTenantId(), entity.getId()) != null) {
result.put("success", false);
result.put("message", "该订单 id 已存在");
result.put("message", "租户下订单 id 已存在");
return result;
}
entity.setTenantId(entity.getTenantId().trim());
entity.setOrderSn(entity.getOrderSn().trim());
if (entity.getStatus() == null) {
entity.setStatus(0);
@@ -121,7 +120,7 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
result.put("success", ok);
result.put("message", ok ? "新增成功" : "新增失败");
if (ok) {
result.put("data", this.getById(entity.getId()));
result.put("data", getByTenantAndId(entity.getTenantId(), entity.getId()));
}
return result;
} catch (Exception e) {
@@ -140,16 +139,28 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
result.put("message", "id不能为空");
return result;
}
if (this.getById(entity.getId()) == null) {
String tenantId = resolveTenantId(entity.getTenantId());
if (tenantId == null) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
entity.setTenantId(tenantId);
if (getByTenantAndId(tenantId, entity.getId()) == null) {
result.put("success", false);
result.put("message", "记录不存在");
return result;
}
boolean ok = this.updateById(entity);
boolean ok =
this.update(
entity,
new LambdaQueryWrapper<LbOrderRow>()
.eq(LbOrderRow::getTenantId, tenantId)
.eq(LbOrderRow::getId, entity.getId()));
result.put("success", ok);
result.put("message", ok ? "编辑成功" : "编辑失败");
if (ok) {
result.put("data", this.getById(entity.getId()));
result.put("data", getByTenantAndId(tenantId, entity.getId()));
}
return result;
} catch (Exception e) {
@@ -160,10 +171,30 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
}
@Override
public Map<String, Object> deleteById(Long id) {
public Map<String, Object> deleteById(Long id, String tenantId) {
Map<String, Object> result = new HashMap<>();
try {
boolean ok = this.removeById(id);
if (id == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
String tid = resolveTenantId(tenantId);
if (tid == null) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
if (getByTenantAndId(tid, id) == null) {
result.put("success", false);
result.put("message", "记录不存在");
return result;
}
boolean ok =
this.remove(
new LambdaQueryWrapper<LbOrderRow>()
.eq(LbOrderRow::getTenantId, tid)
.eq(LbOrderRow::getId, id));
result.put("success", ok);
result.put("message", ok ? "删除成功" : "删除失败");
return result;
@@ -615,19 +646,25 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
}
/**
* 按外部订单 id主键插入或更新;同批重复 id 保留最后一条。
* 按复合主键 {@code (tenant_id, id)} 插入或更新;同批重复保留最后一条。
*
* @return 实际 upsert 条数;无有效 id 时返回 0失败返回 -1
* @return 实际 upsert 条数;无有效时返回 0失败返回 -1
*/
private int upsertBatch(List<LbOrderRow> entities) {
if (entities == null || entities.isEmpty()) {
return 0;
}
Map<Long, LbOrderRow> deduped = new LinkedHashMap<>();
Map<String, LbOrderRow> deduped = new LinkedHashMap<>();
for (LbOrderRow entity : entities) {
if (entity.getId() != null) {
deduped.put(entity.getId(), entity);
if (entity.getId() == null) {
continue;
}
String tenantId = trimToNull(entity.getTenantId());
if (tenantId == null) {
continue;
}
entity.setTenantId(tenantId);
deduped.put(compositeKey(tenantId, entity.getId()), entity);
}
if (deduped.isEmpty()) {
return 0;
@@ -764,4 +801,35 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
e.setUpdatedAt(row.updatedAt());
return e;
}
private LbOrderRow getByTenantAndId(String tenantId, Long id) {
if (trimToNull(tenantId) == null || id == null) {
return null;
}
return this.getOne(
new LambdaQueryWrapper<LbOrderRow>()
.eq(LbOrderRow::getTenantId, tenantId.trim())
.eq(LbOrderRow::getId, id),
false);
}
private String resolveTenantId(String tenantIdFromParam) {
String current = trimToNull(TenantContextHolder.getTenantId());
if (current != null) {
return current;
}
return trimToNull(tenantIdFromParam);
}
private static String compositeKey(String tenantId, Long id) {
return tenantId + ":" + id;
}
private static String trimToNull(String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}
}