103 lines
4.6 KiB
Java
103 lines
4.6 KiB
Java
package com.baiyue.firstleg.service;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
import com.baiyue.common.core.api.ResultCode;
|
|
import com.baiyue.common.core.exception.BizException;
|
|
import com.baiyue.firstleg.dto.CustomerDimensionConfigDtos.InitializeResponse;
|
|
import com.baiyue.firstleg.dto.CustomerDimensionConfigDtos.Response;
|
|
import com.baiyue.firstleg.dto.CustomerDimensionConfigDtos.UpdateRequest;
|
|
import com.baiyue.firstleg.entity.CustomerDimensionConfigEntity;
|
|
import com.baiyue.firstleg.repository.CustomerDimensionConfigRepository;
|
|
import jakarta.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
/** 管理每个客户唯一的尺寸加边配置,不参与当前订单或计费计算。 */
|
|
@Service
|
|
public class CustomerDimensionConfigService {
|
|
public static final BigDecimal DEFAULT_EDGE_ADDITION_CM = new BigDecimal("2.000");
|
|
private static final String DEFAULT_REMARK = "默认每侧加宽 2cm";
|
|
|
|
@Resource
|
|
private CustomerDimensionConfigRepository repository;
|
|
|
|
public Response get(long tenantId) {
|
|
CustomerDimensionConfigEntity value = repository.find(tenantId);
|
|
if (value == null) {
|
|
throw new BizException(ResultCode.NOT_FOUND, "客户尺寸计算配置不存在");
|
|
}
|
|
return response(value);
|
|
}
|
|
|
|
@Transactional
|
|
public Response update(long tenantId, long operatorId, UpdateRequest request) {
|
|
CustomerDimensionConfigEntity value = repository.find(tenantId);
|
|
if (value == null) {
|
|
throw new BizException(ResultCode.NOT_FOUND, "客户尺寸计算配置不存在");
|
|
}
|
|
if (!value.getVersion().equals(request.version())) {
|
|
throw new BizException(ResultCode.CONFLICT, "尺寸计算配置已被修改,请刷新后重试");
|
|
}
|
|
value.setEdgeAdditionCm(request.edgeAdditionCm());
|
|
value.setRemark(normalizeRemark(request.remark()));
|
|
value.setUpdatedBy(operatorId);
|
|
if (repository.update(value) != 1) {
|
|
throw new BizException(ResultCode.CONFLICT, "尺寸计算配置已被修改,请刷新后重试");
|
|
}
|
|
return response(value);
|
|
}
|
|
|
|
@Transactional
|
|
public InitializeResponse initializeDefault(long tenantId, long operatorId, String initializationKey) {
|
|
requireTenant(tenantId);
|
|
String normalizedKey = requireInitializationKey(initializationKey);
|
|
CustomerDimensionConfigEntity existing = repository.find(tenantId);
|
|
if (existing != null) {
|
|
return new InitializeResponse(existing.getId(),
|
|
normalizedKey.equals(existing.getInitializationKey()));
|
|
}
|
|
|
|
long id = IdWorker.getId();
|
|
boolean created = repository.insertDefault(id, tenantId, DEFAULT_EDGE_ADDITION_CM,
|
|
DEFAULT_REMARK, normalizedKey, operatorId);
|
|
CustomerDimensionConfigEntity persisted = repository.find(tenantId);
|
|
if (persisted == null) {
|
|
throw new BizException(ResultCode.INTERNAL_ERROR, "客户尺寸计算配置初始化失败");
|
|
}
|
|
boolean ownedByKey = normalizedKey.equals(persisted.getInitializationKey());
|
|
return new InitializeResponse(persisted.getId(), created || ownedByKey);
|
|
}
|
|
|
|
@Transactional
|
|
public void compensateDefault(long tenantId, String initializationKey) {
|
|
requireTenant(tenantId);
|
|
String normalizedKey = requireInitializationKey(initializationKey);
|
|
CustomerDimensionConfigEntity existing = repository.find(tenantId);
|
|
if (existing == null || !normalizedKey.equals(existing.getInitializationKey())) return;
|
|
repository.deleteUnchangedDefault(tenantId, existing.getId(), normalizedKey, DEFAULT_EDGE_ADDITION_CM);
|
|
}
|
|
|
|
private static Response response(CustomerDimensionConfigEntity value) {
|
|
return new Response(value.getId(), value.getTenantId(), value.getEdgeAdditionCm(), value.getRemark(),
|
|
value.getVersion(), value.getCreatedAt(), value.getUpdatedAt());
|
|
}
|
|
|
|
private static String normalizeRemark(String value) {
|
|
if (value == null || value.isBlank()) return null;
|
|
return value.trim();
|
|
}
|
|
|
|
private static void requireTenant(long tenantId) {
|
|
if (tenantId <= 0) throw new BizException(ResultCode.BAD_REQUEST, "客户ID无效");
|
|
}
|
|
|
|
private static String requireInitializationKey(String value) {
|
|
if (value == null || value.isBlank() || value.trim().length() > 128) {
|
|
throw new BizException(ResultCode.BAD_REQUEST, "初始化幂等键无效");
|
|
}
|
|
return value.trim();
|
|
}
|
|
}
|