仓库编号自动生成

main
liuyiliang 12 months ago
parent df0a256d9c
commit cec9a2725b
  1. 20
      src/main/java/org/energy/modules/spares/controller/WarehouseController.java
  2. 17
      src/main/java/org/energy/modules/system/util/DataUtils.java

@ -140,6 +140,26 @@ public class WarehouseController extends DafController {
@ApiOperationSupport(order = 6) @ApiOperationSupport(order = 6)
@ApiOperation(value = "新增或修改", notes = "传入warehouse") @ApiOperation(value = "新增或修改", notes = "传入warehouse")
public R submit(@Valid @RequestBody Warehouse warehouse) { public R submit(@Valid @RequestBody Warehouse warehouse) {
if (warehouse.getId() == null) {
QueryWrapper<Warehouse> qw = new QueryWrapper<>();
qw.lambda().select(Warehouse::getCode)
.eq(Warehouse::getType, warehouse.getType())
.eq(Warehouse::getIsDeleted, DafConstant.DB_NOT_DELETED)
.orderByDesc(Warehouse::getCode)
.last("LIMIT 1");
String code = "";
Warehouse warehouseSelect = warehouseService.getOne(qw);
if (warehouseSelect != null) {
String maxCode = warehouseSelect.getCode();
if ("9999".equals(maxCode)) {
return R.fail("操作失败,编号已越位");
}
code = "CK" + warehouse.getType() + DataUtils.incrementLast(maxCode, 4);
} else {
code = "CK" + warehouse.getType() + "0001";
}
warehouse.setCode(code);
}
return R.status(warehouseService.saveOrUpdate(warehouse)); return R.status(warehouseService.saveOrUpdate(warehouse));
} }

@ -68,4 +68,21 @@ public class DataUtils {
// Add more type conversions as needed // Add more type conversions as needed
return value; return value;
} }
/**
* 将字符串末尾的latterIndex位数字加一并格式化成latterIndex位数
* @param str
* @return
*/
public static String incrementLast(String str, int latterIndex) {
// 获取字符串末尾的latterIndex位字符
String lastFour = str.substring(Math.max(0, str.length() - latterIndex));
// 将末尾字符转换为整数
int number = Integer.parseInt(lastFour);
// 整数加一
number++;
// 格式化整数为latterIndex位数的字符串
String result = String.format("%0" + latterIndex + "d", number);
return result;
}
} }

Loading…
Cancel
Save