feat: 修改商品库存逻辑,优化库存检查与错误处理,增强代码安全性

This commit is contained in:
mkm 2024-08-10 19:48:18 +08:00
parent 7365d1830a
commit 7eaa1db32e

View File

@ -7,6 +7,7 @@ use app\admin\controller\BaseAdminController;
use app\admin\lists\store_product\StoreProductLists;
use app\admin\logic\store_product\StoreProductLogic;
use app\admin\validate\store_product\StoreProductValidate;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use Webman\RedisQueue\Redis;
@ -101,26 +102,31 @@ class StoreProductController extends BaseAdminController
$store_arr = $this->request->post('store_arr');
$stock_type = $this->request->post('stock_type', 1);
$warehouse_id = $this->request->post('warehouse_id');
$count=count($store_arr);
$count = count($store_arr);
foreach ($product_arr as $key => $arr) {
$stock=bcmul($arr['stock'],$count);
$nums=WarehouseProductStorege::where('warehouse_id',$warehouse_id)->where('product_id',$arr['id'])->value('nums');
if($nums<$stock){
$stock = bcmul($arr['stock'], $count);
$nums = WarehouseProductStorege::where('warehouse_id', $warehouse_id)->where('product_id', $arr['id'])->value('nums');
if ($nums < $stock) {
return $this->fail('商品库存不足');
}
}
if ($count == 1) {
$store_id = $store_arr[0];
foreach ($product_arr as $key => $arr) {
Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
$find = StoreProduct::where('id', $product_arr['id'])->findOrEmpty()->toArray();
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
// Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
}
} else {
foreach ($product_arr as $key => $arr) {
foreach ($store_arr as $k => $store_id) {
Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
$find = StoreProduct::where('id', $product_arr['id'])->findOrEmpty()->toArray();
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $find, $warehouse_id);
// Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'stock_type' => $stock_type, 'admin_id' => $this->adminId, 'warehouse_id' => $warehouse_id]);
}
}
}
return $this->success('已导入后台队列,请在门店入库记录中查看',[],1,1);
return $this->success('已导入后台队列,请在门店入库记录中查看', [], 1, 1);
}
}