mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
鲜花2.0-库存盘点和批量导入+订单库存扣减乐观锁和重试机制
This commit is contained in:
parent
91e4e5342e
commit
ab03594786
@ -34,7 +34,17 @@ class BusinessOrdersUpdate
|
||||
{
|
||||
$this->businessGoodSku = $businessGoodSku->toArray();
|
||||
$this->num = $num;
|
||||
$this->updateStock();
|
||||
$updateResult = false;
|
||||
//暂时设定重试5次
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
if ($this->updateStock()) {
|
||||
$updateResult = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$updateResult) {
|
||||
Log::error("sku 业务更新失败", (array)$this->businessGoodSku);
|
||||
}
|
||||
}
|
||||
|
||||
private function updateStock()
|
||||
@ -45,18 +55,20 @@ class BusinessOrdersUpdate
|
||||
if (is_null($this->goodsSku)) {
|
||||
return false;
|
||||
}
|
||||
$oldStock = $this->goodsSku->stock;
|
||||
$stock = $this->goodsSku->stock + $this->num;
|
||||
$saleStock = $this->goodsSku->sale_stock + $this->num;
|
||||
$updateParam = ["stock" => $stock, "sale_stock" => $saleStock];
|
||||
if (0 >= $saleStock) {
|
||||
$this->goodsSku->status = GoodsSku::$STATUS_DOWN;
|
||||
$updateParam['status'] = GoodsSku::$STATUS_DOWN;
|
||||
} else {
|
||||
$this->goodsSku->status = GoodsSku::$STATUS_ON_SALE;
|
||||
$updateParam['status'] = GoodsSku::$STATUS_ON_SALE;
|
||||
}
|
||||
|
||||
$this->goodsSku->sale_stock = $saleStock;
|
||||
$this->goodsSku->stock = $stock;
|
||||
Log::info("sku 业务订单库存更新", (array)$this->goodsSku);
|
||||
$this->goodsSku->save();
|
||||
Log::info("sku 业务订单库存更新", $updateParam);
|
||||
//乐观锁更新
|
||||
return GoodsSku::query()->where('external_sku_id', $this->businessGoodSku['external_sku_id'])->
|
||||
where("stock", "=", $oldStock)->update($updateParam);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -30,10 +30,10 @@ class DailyStockRecordController extends Controller
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$build = DailyStockRecord::query()->filter()->with("goodsSku:id,title");
|
||||
$build = DailyStockRecord::query()->filter()->with("goodsSku:id,name");
|
||||
if (!empty($request->title)) {
|
||||
$build->whereHas('goodsSku', function ($query) use ($request) {
|
||||
$query->where('title', 'like', '%' . $request->title . '%');
|
||||
$query->where('name', 'like', '%' . $request->title . '%');
|
||||
});
|
||||
}
|
||||
if (!empty($request->get('external_sku_id'))) {
|
||||
|
||||
@ -25,13 +25,13 @@ class LossRecordController extends Controller
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$builder = LossRecords::query()->filter()->with("goodsSku:title,external_sku_id");
|
||||
$builder = LossRecords::query()->filter()->with("goodsSku:name,external_sku_id");
|
||||
if (!empty($request->start_time) && !empty($request->end_time)) {
|
||||
$builder->whereBetween('created_at', [$request->start_time, $request->end_time]);
|
||||
}
|
||||
if (!empty($request->title)) {
|
||||
$builder->whereHas('goodsSku', function ($query) use ($request) {
|
||||
$query->where('title', 'like', '%' . $request->title . '%');
|
||||
$query->where('name', 'like', '%' . $request->title . '%');
|
||||
});
|
||||
}
|
||||
$dailyStockRecord = $builder->paginate($request->get('per_page'));
|
||||
@ -114,7 +114,8 @@ class LossRecordController extends Controller
|
||||
return response($this->res, $this->res['httpCode']);
|
||||
}
|
||||
|
||||
public function lossBatchStore(Request $request){
|
||||
public function lossBatchStore(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'lossOrders' => 'required|array',
|
||||
'lossOrders.*.external_sku_id' => 'required|string',
|
||||
@ -185,5 +186,4 @@ class LossRecordController extends Controller
|
||||
return response($this->res, $this->res['httpCode']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -29,13 +29,13 @@ class PurchaseRecordController extends Controller
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$builder = PurchaseRecords::query()->filter()->with("goodsSku:id,title,external_sku_id");
|
||||
$builder = PurchaseRecords::query()->filter()->with("goodsSku:id,name,external_sku_id");
|
||||
if (!empty($request->start_time) && !empty($request->end_time)) {
|
||||
$builder->whereBetween('created_at', [$request->start_time, $request->end_time]);
|
||||
}
|
||||
if (!empty($request->title)) {
|
||||
$builder->whereHas('goodsSku', function ($query) use ($request) {
|
||||
$query->where('title', 'like', '%' . $request->title . '%');
|
||||
$query->where('name', 'like', '%' . $request->title . '%');
|
||||
});
|
||||
}
|
||||
$dailyStockRecord = $builder->paginate($request->get('per_page'));
|
||||
|
||||
24
tests/Feature/BusinessOrderUpdateTest.php
Normal file
24
tests/Feature/BusinessOrderUpdateTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
|
||||
use App\Models\BusinessGoodsSku;
|
||||
use App\Models\BusinessOrderItem;
|
||||
use App\Models\GoodsSku;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BusinessOrderUpdateTest extends TestCase
|
||||
{
|
||||
use \Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasicTest()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user