feat(格口管理): 添加存入物品分配格口功能

新增StoreItemToCellCommand用于接收存入物品请求参数
在CabinetCellDTO中添加格口密码字段
实现根据店铺和格口类型分配空闲格口逻辑,并生成随机密码
This commit is contained in:
dzq 2025-12-17 16:22:14 +08:00
parent 8ae1f773d5
commit 16e85fd278
4 changed files with 84 additions and 0 deletions

View File

@ -8,6 +8,8 @@ import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.cabinet.cell.CabinetCellApplicationService;
import com.agileboot.domain.cabinet.cell.command.OpenCellByPasswordCommand;
import com.agileboot.domain.cabinet.cell.command.StoreItemToCellCommand;
import com.agileboot.domain.cabinet.cell.dto.CabinetCellDTO;
import com.agileboot.domain.cabinet.cell.db.CabinetCellEntity;
import com.agileboot.domain.cabinet.cell.model.CabinetCellModel;
import com.agileboot.domain.cabinet.cell.model.CabinetCellModelFactory;
@ -178,4 +180,11 @@ public class CabinetCellController {
throw new ApiException(ErrorCode.Internal.INTERNAL_ERROR, e.getMessage());
}
}
@Operation(summary = "存入物品分配格口")
@PostMapping("/storeItem")
public ResponseDTO<CabinetCellDTO> storeItem(@RequestBody StoreItemToCellCommand command) {
CabinetCellDTO result = cabinetCellApplicationService.storeItemToCell(command);
return ResponseDTO.ok(result);
}
}

View File

@ -9,6 +9,7 @@ import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.cabinet.cell.command.AddCabinetCellCommand;
import com.agileboot.domain.cabinet.cell.command.OpenCellByPasswordCommand;
import com.agileboot.domain.cabinet.cell.command.UpdateCabinetCellCommand;
import com.agileboot.domain.cabinet.cell.command.StoreItemToCellCommand;
import com.agileboot.domain.cabinet.cell.db.CabinetCellEntity;
import com.agileboot.domain.cabinet.cell.db.CabinetCellService;
import com.agileboot.domain.cabinet.cell.dto.CabinetCellDTO;
@ -26,7 +27,9 @@ import com.agileboot.domain.cabinet.mainboard.model.CabinetMainboardModel;
import com.agileboot.domain.cabinet.mainboard.model.CabinetMainboardModelFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
@ -198,4 +201,52 @@ public class CabinetCellApplicationService {
cellModel.setUsageStatus(1); // 空闲
cellModel.updateById();
}
@Transactional
public CabinetCellDTO storeItemToCell(StoreItemToCellCommand command) {
// 根据shopId获取该店铺下的所有柜子ID列表
List<Long> cabinetIds = smartCabinetService.lambdaQuery()
.eq(SmartCabinetEntity::getShopId, command.getShopId())
.select(SmartCabinetEntity::getCabinetId)
.list()
.stream()
.map(SmartCabinetEntity::getCabinetId)
.collect(Collectors.toList());
if (cabinetIds.isEmpty()) {
throw new RuntimeException("该店铺下无柜子");
}
// 查找符合条件的格口符合类型无密码空闲正常未删除
CabinetCellEntity cell = cabinetCellService.lambdaQuery()
.in(CabinetCellEntity::getCabinetId, cabinetIds)
.eq(CabinetCellEntity::getCellType, command.getCellType())
.isNull(CabinetCellEntity::getPassword)
.eq(CabinetCellEntity::getUsageStatus, 1) // 空闲
.eq(CabinetCellEntity::getAvailableStatus, 1) // 正常
.eq(CabinetCellEntity::getDeleted, false)
.one();
if (cell == null) {
throw new RuntimeException("找不到符合条件的可用格口");
}
// 生成4位随机数字密码
String password = generateFourDigitPassword();
// 加载格口模型并更新密码和状态
CabinetCellModel cellModel = cabinetCellModelFactory.loadById(cell.getCellId());
cellModel.setPassword(password);
cellModel.setUsageStatus(2); // 设置为已占用
cellModel.updateById();
// 返回格口信息包含密码
return new CabinetCellDTO(cellModel);
}
private String generateFourDigitPassword() {
Random random = new Random();
int password = random.nextInt(10000); // 生成0-9999之间的数字
return String.format("%04d", password); // 格式化为4位数字前面补零
}
}

View File

@ -0,0 +1,21 @@
package com.agileboot.domain.cabinet.cell.command;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
public class StoreItemToCellCommand {
@ApiModelProperty(value = "店铺ID", required = true)
@NotNull(message = "店铺ID不能为空")
private Long shopId;
@ApiModelProperty(value = "格口类型1小格 2中格 3大格 4超大格", required = true)
@NotNull(message = "格口类型不能为空")
@Min(value = 1, message = "格口类型必须为1-4之间的整数")
@Max(value = 4, message = "格口类型必须为1-4之间的整数")
private Integer cellType;
}

View File

@ -61,6 +61,9 @@ public class CabinetCellDTO {
@ExcelColumn(name = "格口类型")
private Integer cellType;
@ExcelColumn(name = "格口密码")
private String password;
@ExcelColumn(name = "使用状态")
private Integer usageStatus;