更新若干功能
Some checks failed
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled

This commit is contained in:
2026-08-20 19:16:49 +08:00
parent 72bc6502eb
commit 4ddf5e3c5f
48 changed files with 1908 additions and 18 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Service\business;
use App\Models\business\PriceSheetModel;
use App\Service\common\UtilsService;
/**
* 规格库存stock=-1 表示不限,兼容历史数据全是不限。
*/
class StockService
{
private static mixed $_instance;
public static function getInstance(): null|static
{
$name = get_called_class();
if (!isset(self::$_instance[$name])) {
self::$_instance[$name] = new static();
}
return self::$_instance[$name];
}
/**
* 加入清单 / 下单前校验。sheetId=0 时跳过(还没选规格)。
*/
public function assertAvailable(int $sheetId, int $quantity): void
{
if ($sheetId <= 0) {
return;
}
$sheet = PriceSheetModel::where('id', $sheetId)->where('deleted_at', 0)->first();
if (empty($sheet)) {
UtilsService::getInstance()->errorThrow('规格不存在');
}
$stock = (int) ($sheet['stock'] ?? -1);
if ($stock < 0) {
return;
}
if ($stock < $quantity) {
UtilsService::getInstance()->errorThrow('库存不足(剩余 ' . $stock . '');
}
}
/**
* 下单成功后扣库存;不限库存不写库。
*/
public function deduct(int $sheetId, int $quantity): void
{
if ($sheetId <= 0 || $quantity <= 0) {
return;
}
$sheet = PriceSheetModel::where('id', $sheetId)->where('deleted_at', 0)->first();
if (empty($sheet)) {
return;
}
$stock = (int) ($sheet['stock'] ?? -1);
if ($stock < 0) {
return;
}
$next = max(0, $stock - $quantity);
PriceSheetModel::where('id', $sheetId)->update([
'stock' => $next,
'updated_at' => time(),
]);
}
}