同步库存增加库存记录

This commit is contained in:
wucongxing8150 2024-03-19 16:44:34 +08:00
parent 5a4bf6feb6
commit f625d14fc7
2 changed files with 96 additions and 1 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Command;
use App\Model\Product;
use App\Model\ProductAmountRecord;
use App\Model\ProductPlatformAmount;
use Extend\Prescription\Prescription;
use Hyperf\Command\Command as HyperfCommand;
@ -93,6 +94,9 @@ class getProductStockCommand extends HyperfCommand
try {
Db::beginTransaction();
// 当前库存数量
$stock = 0;
$params = array();
$params['product_platform_id'] = $product_platform_id;
$params['product_platform_code'] = $product_platform_code;
@ -109,6 +113,8 @@ class getProductStockCommand extends HyperfCommand
$this->line("商品库存更新失败,无法新增库存数据" . json_encode($data, JSON_UNESCAPED_UNICODE));
}
}else{
$stock = $product_platform_amount['stock'];
// 存在库存数据,修改
$data = array();
$data['stock'] = $resultData['quantity'];
@ -118,8 +124,27 @@ class getProductStockCommand extends HyperfCommand
ProductPlatformAmount::edit($params,$data);
}
// 获取商品数据
$params = array();
$params['product_platform_id'] = $product_platform_id;
$product = Product::getOne($params);
if (!empty($product)){
// 增加库存记录
$data = array();
$data['product_id'] = $product['product_id'];
$data['change_quantity'] = $resultData['quantity'] - $stock; // 库存变动的数量 变动的库存-原库存
$data['quantity'] = $resultData['quantity']; // 变动后库存数量
$data['change_time'] = date('Y-m-d H:i:s',time());
$data['remark'] = "库存同步";
$product_amount_record = ProductAmountRecord::addProductAmountRecord($data);
if (empty($product_amount_record)){
Db::rollBack();
$this->line("商品库存更新失败,增加库存记录失败" . json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
Db::commit();
} catch (\Exception $e) {
} catch (\Throwable $e) {
Db::rollBack();
$this->line("商品库存更新失败:" . $e->getMessage());
return false;

View File

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Carbon\Carbon;
use Hyperf\Database\Model\Collection;
use Hyperf\Snowflake\Concern\Snowflake;
/**
* @property int $amount_record_id 主键id
* @property int $product_id 商品id
* @property int $change_quantity 库存变动的数量
* @property int $quantity 变动后库存数量
* @property string $change_time 变动时间
* @property string $remark 备注
* @property Carbon $created_at 创建时间
* @property Carbon $updated_at 修改时间
*/
class ProductAmountRecord extends Model
{
use Snowflake;
/**
* The table associated with the model.
*/
protected ?string $table = 'product_amount_record';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['amount_record_id', 'product_id', 'change_quantity', 'quantity', 'change_time', 'remark', 'created_at', 'updated_at'];
protected string $primaryKey = "amount_record_id";
/**
* 获取数据-
* @param array $params
* @param array $fields
* @return object|null
*/
public static function getOne(array $params, array $fields = ['*']): object|null
{
return self::where($params)->first($fields);
}
/**
* 获取数据-
* @param array $params
* @param array $fields
* @return Collection|array
*/
public static function getList(array $params = [], array $fields = ['*']): Collection|array
{
return self::where($params)->get($fields);
}
/**
* 新增
* @param array $data
* @return \Hyperf\Database\Model\Model|ProductAmountRecord
*/
public static function addProductAmountRecord(array $data = []): \Hyperf\Database\Model\Model|ProductAmountRecord
{
return self::create($data);
}
}