集装箱码头 TOS:舱单箱卸船业务(贫血模型与富领域模型)

张开发
2026/4/7 12:49:22 15 分钟阅读

分享文章

集装箱码头 TOS:舱单箱卸船业务(贫血模型与富领域模型)
一、最终枚举1. 舱单状态枚举Manifestjava运行// 舱单状态 public enum Manifest { PLAN, // 计划 COMPLETE // 完成 }2. 任务状态枚举Task原 TaskStatusjava运行// 任务状态 public enum Task { PENDING, // 待执行 FINISHED // 已完成 }二、贫血模型1. 领域对象Vessel船舶java运行Data TableName(vessel) public class Vessel { private Long id; private String vesselCode; private String vesselName; }Manifest舱单实体java运行Data TableName(manifest) public class Manifest { private Long id; private Long vesselId; private String manifestNo; private Manifest status; // 枚举Manifest private Integer totalBoxes; }UnloadTask卸船任务java运行Data TableName(unload_task) public class UnloadTask { private Long id; private Long manifestId; private String containerNo; private Task status; // 枚举Task }Container箱java运行Data TableName(container) public class Container { private Long id; private String containerNo; private Long manifestId; private Long vesselId; private String location; private Date createTime; }2. Mapperjava运行Mapper public interface VesselMapper extends BaseMapperVessel {} Mapper public interface ManifestMapper extends BaseMapperManifest {} Mapper public interface UnloadTaskMapper extends BaseMapperUnloadTask {} Mapper public interface ContainerMapper extends BaseMapperContainer {}3. 贫血 Servicejava运行Service RequiredArgsConstructor public class AnemiaUnloadService { private final UnloadTaskMapper taskMapper; private final ManifestMapper manifestMapper; private final ContainerMapper containerMapper; Transactional public void finishUnload(Long taskId) { // 1. 获取任务 UnloadTask task taskMapper.selectById(taskId); if (task null) throw new RuntimeException(任务不存在); // 2. 完成任务 task.setStatus(Task.FINISHED); taskMapper.updateById(task); // 3. 获取舱单 Manifest manifest manifestMapper.selectById(task.getManifestId()); if (manifest null) throw new RuntimeException(舱单不存在); // 4. 反写状态 if (manifest.getStatus() Manifest.PLAN) { manifest.setStatus(Manifest.COMPLETE); manifestMapper.updateById(manifest); } // 5. 创建箱 Container container new Container(); container.setContainerNo(task.getContainerNo()); container.setManifestId(manifest.getId()); container.setVesselId(manifest.getVesselId()); container.setLocation(DEFAULT_STACK); container.setCreateTime(new Date()); containerMapper.insert(container); } }三、富领域模型1. 领域对象Manifest舱单java运行Data TableName(manifest) public class Manifest { private Long id; private Long vesselId; private String manifestNo; private Manifest status; private Integer totalBoxes; // 领域行为完成卸船 public void complete() { if (status ! Manifest.PLAN) { throw new RuntimeException(仅计划状态可完成); } this.status Manifest.COMPLETE; } }UnloadTask任务java运行Data TableName(unload_task) public class UnloadTask { private Long id; private Long manifestId; private String containerNo; private Task status; // 领域行为完成任务 public void finish() { if (status ! Task.PENDING) { throw new RuntimeException(任务状态异常); } this.status Task.FINISHED; } }ContainerFactoryjava运行public class ContainerFactory { public static Container create(UnloadTask task, Manifest manifest) { Container container new Container(); container.setContainerNo(task.getContainerNo()); container.setManifestId(manifest.getId()); container.setVesselId(manifest.getVesselId()); container.setLocation(DEFAULT_STACK); container.setCreateTime(new Date()); return container; } }2. 富领域 Servicejava运行Service RequiredArgsConstructor public class RichDomainUnloadService { private final UnloadTaskMapper taskMapper; private final ManifestMapper manifestMapper; private final ContainerMapper containerMapper; Transactional public void finishUnload(Long taskId) { UnloadTask task taskMapper.selectById(taskId); Manifest manifest manifestMapper.selectById(task.getManifestId()); // 领域行为 task.finish(); manifest.complete(); // 持久化 taskMapper.updateById(task); manifestMapper.updateById(manifest); // 创建箱 Container container ContainerFactory.create(task, manifest); containerMapper.insert(container); } }

更多文章