107 lines
6.2 KiB
Java
107 lines
6.2 KiB
Java
package com.baiyue.warehouse.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.warehouse.dto.PalletTransitionRequests;
|
||
|
|
import com.baiyue.warehouse.dto.PalletTransitionResponse;
|
||
|
|
import com.baiyue.warehouse.entity.WarehousePalletEntity;
|
||
|
|
import com.baiyue.warehouse.entity.WarehousePalletOperationEntity;
|
||
|
|
import com.baiyue.warehouse.mapper.WarehousePalletTransitionMapper;
|
||
|
|
import jakarta.annotation.Resource;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.security.MessageDigest;
|
||
|
|
import java.security.NoSuchAlgorithmException;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.HexFormat;
|
||
|
|
import java.util.Set;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
public class WarehousePalletTransitionService {
|
||
|
|
private static final Set<String> MUTABLE_STATUSES = Set.of("IDLE", "OCCUPIED");
|
||
|
|
@Resource private WarehousePalletTransitionMapper mapper;
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public PalletTransitionResponse apply(long palletId, PalletTransitionRequests.Apply request) {
|
||
|
|
String key = request.operationKey().trim();
|
||
|
|
String status = request.targetStatus().trim().toUpperCase();
|
||
|
|
if (!MUTABLE_STATUSES.contains(status)) {
|
||
|
|
throw new BizException(ResultCode.BAD_REQUEST, "托盘作业状态只允许 IDLE 或 OCCUPIED");
|
||
|
|
}
|
||
|
|
String requestHash = hash(request.warehouseId() + "|" + palletId + "|" + request.expectedVersion()
|
||
|
|
+ "|" + status + "|" + request.targetLocationId());
|
||
|
|
WarehousePalletOperationEntity existing = mapper.findOperation(key);
|
||
|
|
if (existing != null) return replay(existing, requestHash);
|
||
|
|
|
||
|
|
WarehousePalletEntity pallet = mapper.lockPublicPallet(request.warehouseId(), palletId);
|
||
|
|
if (pallet == null) throw new BizException(ResultCode.NOT_FOUND, "当前冻结仓内托盘不存在");
|
||
|
|
int version = pallet.getVersion() == null ? 0 : pallet.getVersion();
|
||
|
|
if (version != request.expectedVersion()) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "托盘版本已变化,请重新扫码");
|
||
|
|
}
|
||
|
|
if (mapper.countActiveLocation(request.warehouseId(), request.targetLocationId()) != 1) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "目标库位无效或已停用");
|
||
|
|
}
|
||
|
|
if (pallet.getLocationId() == null) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "托盘没有有效库位,不能执行库内作业");
|
||
|
|
}
|
||
|
|
if (mapper.transition(request.warehouseId(), palletId, status, request.targetLocationId(), version) != 1) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "托盘版本已变化,请重新扫码");
|
||
|
|
}
|
||
|
|
WarehousePalletOperationEntity operation = new WarehousePalletOperationEntity();
|
||
|
|
operation.setId(IdWorker.getId()); operation.setOperationKey(key); operation.setRequestHash(requestHash);
|
||
|
|
operation.setWarehouseId(request.warehouseId()); operation.setPalletId(palletId);
|
||
|
|
operation.setPalletCode(pallet.getPalletCode()); operation.setBeforeStatus(pallet.getStatus());
|
||
|
|
operation.setAfterStatus(status); operation.setBeforeLocationId(pallet.getLocationId());
|
||
|
|
operation.setAfterLocationId(request.targetLocationId()); operation.setBeforeVersion(version);
|
||
|
|
operation.setAfterVersion(version + 1); operation.setOperationStatus("APPLIED");
|
||
|
|
operation.setReason(trim(request.reason())); operation.setCreatedAt(LocalDateTime.now());
|
||
|
|
operation.setUpdatedAt(operation.getCreatedAt()); mapper.insert(operation);
|
||
|
|
return response(operation, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public PalletTransitionResponse compensate(long palletId, String operationKey,
|
||
|
|
PalletTransitionRequests.Compensate request) {
|
||
|
|
WarehousePalletOperationEntity operation = mapper.lockOperation(operationKey);
|
||
|
|
if (operation == null || !Long.valueOf(palletId).equals(operation.getPalletId())
|
||
|
|
|| !Long.valueOf(request.warehouseId()).equals(operation.getWarehouseId())) {
|
||
|
|
throw new BizException(ResultCode.NOT_FOUND, "托盘状态变更记录不存在");
|
||
|
|
}
|
||
|
|
if ("COMPENSATED".equals(operation.getOperationStatus())) return response(operation, true);
|
||
|
|
if (!"APPLIED".equals(operation.getOperationStatus())) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "托盘状态变更不可补偿");
|
||
|
|
}
|
||
|
|
if (mapper.transition(request.warehouseId(), palletId, operation.getBeforeStatus(),
|
||
|
|
operation.getBeforeLocationId(), operation.getAfterVersion()) != 1) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "托盘已被后续作业修改,自动补偿失败");
|
||
|
|
}
|
||
|
|
operation.setOperationStatus("COMPENSATED"); operation.setUpdatedAt(LocalDateTime.now());
|
||
|
|
mapper.updateById(operation);
|
||
|
|
return response(operation, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
private PalletTransitionResponse replay(WarehousePalletOperationEntity operation, String hash) {
|
||
|
|
if (!hash.equals(operation.getRequestHash())) {
|
||
|
|
throw new BizException(ResultCode.CONFLICT, "操作幂等键已被不同托盘请求使用");
|
||
|
|
}
|
||
|
|
return response(operation, true);
|
||
|
|
}
|
||
|
|
private static PalletTransitionResponse response(WarehousePalletOperationEntity value, boolean replayed) {
|
||
|
|
return new PalletTransitionResponse(value.getOperationKey(), value.getPalletId(), value.getPalletCode(),
|
||
|
|
value.getWarehouseId(), value.getBeforeStatus(), value.getAfterStatus(), value.getBeforeLocationId(),
|
||
|
|
value.getAfterLocationId(), value.getBeforeVersion(), value.getAfterVersion(),
|
||
|
|
value.getOperationStatus(), replayed);
|
||
|
|
}
|
||
|
|
private static String hash(String value) {
|
||
|
|
try {
|
||
|
|
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256")
|
||
|
|
.digest(value.getBytes(StandardCharsets.UTF_8)));
|
||
|
|
} catch (NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
|
||
|
|
}
|
||
|
|
private static String trim(String value) { return value == null || value.isBlank() ? null : value.trim(); }
|
||
|
|
}
|