调整商品入库

This commit is contained in:
luofei 2024-06-05 11:39:31 +08:00
parent 516078ead1
commit fe5a0db3f0
4 changed files with 81 additions and 18 deletions

View File

@ -340,7 +340,7 @@ if (!function_exists('setUnique')) {
{
return substr(md5($sku . $id), 12, 11) . $type;
}
}
@ -379,5 +379,42 @@ if (!function_exists('haversineDistance')) {
}
}
if (!function_exists('reset_index')) {
/**
* 重置数组索引
* @param array $data
* @param string $index
* @return array
*/
function reset_index(array $data, string $index)
{
$return = [];
foreach ($data as $item) {
$return[$item[$index]] = $item;
}
return $return;
}
}
if (!function_exists('append_to_array')) {
/**
* 追加元素到数组
* @param array $data
* @param array $append
* @return array
*/
function append_to_array(array $data, array $append)
{
$return = [];
foreach ($data as $item) {
if (isset($append['relation'])) {
$item[$append['value']] = $append['relation'][$item[$append['field']]];
} else {
$item[$append['value']] = $item[$append['field']];
}
$return[] = $item;
}
return $return;
}
}

View File

@ -4,6 +4,7 @@ namespace app\store\controller\store_product;
use app\admin\lists\store_branch_product\StoreBranchProductLists;
use app\common\controller\Definitions;
use app\store\controller\BaseAdminController;
use app\store\logic\store_branch_product\StoreBranchProductLogic;
use app\store\logic\store_product\StoreProductLogic;
@ -28,7 +29,8 @@ class StoreProductController extends BaseAdminController
ApiDoc\Query(name: 'cate_id', type: 'int', require: false, desc: '分类id'),
ApiDoc\Query(name: 'store_name', type: 'string', require: false, desc: '商品名称'),
ApiDoc\Query(name: 'status', type: 'int', require: false, desc: '状态1上架2下架3售罄4库存告警'),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\Query(ref: [Definitions::class, "page"]),
ApiDoc\ResponseSuccess("data", type: "array", children: [
['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
['name' => 'image', 'desc' => '图片', 'type' => 'string'],
@ -52,7 +54,7 @@ class StoreProductController extends BaseAdminController
ApiDoc\url('/store/store_product/storeProduct/add'),
ApiDoc\Method('POST'),
ApiDoc\NotHeaders(),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array", children: [
['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
['name' => 'image', 'desc' => '图片', 'type' => 'string'],
@ -81,7 +83,7 @@ class StoreProductController extends BaseAdminController
ApiDoc\Method('POST'),
ApiDoc\NotHeaders(),
ApiDoc\Query(name: "id", type: "int", require: true, desc: "id"),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array", children: [
['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
['name' => 'image', 'desc' => '图片', 'type' => 'string'],
@ -110,7 +112,7 @@ class StoreProductController extends BaseAdminController
ApiDoc\Method('POST'),
ApiDoc\NotHeaders(),
ApiDoc\Param(name: "id", type: "int", require: true, desc: "id"),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array", children: [
['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
['name' => 'image', 'desc' => '图片', 'type' => 'string'],
@ -136,7 +138,7 @@ class StoreProductController extends BaseAdminController
ApiDoc\Method('GET'),
ApiDoc\NotHeaders(),
ApiDoc\Query(name: "id", type: "int", require: true, desc: "id"),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array", children: [
['name' => 'id', 'desc' => 'ID', 'type' => 'int'],
['name' => 'image', 'desc' => '图片', 'type' => 'string'],
@ -162,7 +164,7 @@ class StoreProductController extends BaseAdminController
ApiDoc\Method('POST'),
ApiDoc\NotHeaders(),
ApiDoc\Param(name: "id", type: "int", require: true, desc: "id"),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function status()
@ -176,16 +178,19 @@ class StoreProductController extends BaseAdminController
ApiDoc\Title('商品库存增减'),
ApiDoc\url('/store/store_product/storeProduct/stock'),
ApiDoc\Method('POST'),
ApiDoc\Param(name: "id", type: "int", require: true, desc: "id"),
ApiDoc\Param(name: "type", type: "int", require: true, desc: "类型1增加2减少"),
ApiDoc\Param(name: "number", type: "int", require: true, desc: "数量"),
ApiDoc\Param(name: "attrs", type: "array", require: true, desc: "id", children: [
['name' => 'unique', 'desc' => '唯一值', 'type' => 'string'],
['name' => 'type', 'desc' => '类型1增加2减少', 'type' => 'int'],
['name' => 'number', 'desc' => '数量', 'type' => 'int'],
]),
ApiDoc\NotHeaders(),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function stock()
{
$params = (new StoreProductValidate())->post()->goCheck('stock');
$params['store_id'] = $this->request->adminInfo['store_id'];
StoreBranchProductLogic::stock($params);
return $this->success('操作成功', [], 1, 1);
}

View File

@ -4,6 +4,7 @@ namespace app\store\logic\store_branch_product;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_branch_product_attr_value\StoreBranchProductAttrValue;
use app\common\model\store_product\StoreProduct;
use app\common\logic\BaseLogic;
use app\common\model\store_category\StoreCategory;
@ -178,25 +179,43 @@ class StoreBranchProductLogic extends BaseLogic
*/
public static function stock(array $params): bool
{
$StoreProduct = StoreBranchProduct::where('id', $params['id'])->find();
$attrs = reset_index($params['attrs'], 'unique');
$attrValue = StoreBranchProductAttrValue::where('store_id', $params['store_id'])->whereIn('unique', array_keys($attrs))->select()->toArray();
if (empty($attrValue)) {
throw new \Exception('商品属性不存在');
}
$StoreProduct = StoreBranchProduct::where('store_id', $params['store_id'])->where('product_id', $attrValue[0]['product_id'])->find();
if (empty($StoreProduct)) {
throw new \Exception('商品不存在');
}
self::checkAuth($StoreProduct);
Db::startTrans();
try {
$stock = $params['type'] == 1 ? $StoreProduct['stock'] + $params['number'] : $StoreProduct['stock'] - $params['number'];
$stock = max($stock, 0);
StoreBranchProduct::where('id', $params['id'])->update(['stock' => $stock]);
$productStockIn = 0;
foreach ($attrValue as $k => $v) {
$current = $attrs[$v['unique']] ?? [];
if (empty($current) || $v['unique'] != $current['unique']) {
continue;
}
$stock = $current['type'] == 1 ? $v['stock'] + $current['number'] : $v['stock'] - $current['number'];
$stock = max($stock, 0);
$productStockIn = $current['type'] == 1 ? $StoreProduct['stock'] + $current['number'] : $StoreProduct['stock'] - $current['number'];
$productStockIn = max($productStockIn, 0);
StoreBranchProductAttrValue::where('id', $v['id'])->update(['stock' => $stock]);
}
StoreBranchProduct::where('id', $StoreProduct['id'])->update(['stock' => $productStockIn]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
throw new \Exception($e->getMessage());
}
}
public static function checkAuth($product)
{
if (request()->adminInfo['store_id'] != $product['store_id']) {
throw new BusinessException('没有权限操作');
throw new \Exception('没有权限操作');
}
}

View File

@ -21,6 +21,7 @@ class StoreProductValidate extends BaseValidate
protected $rule = [
'id' => 'require',
'store_name' => 'require',
'attrs' => 'require',
];
@ -31,6 +32,7 @@ class StoreProductValidate extends BaseValidate
protected $field = [
'id' => 'id',
'store_name' => '商品名称',
'attrs' => '商品名称',
];
@ -89,7 +91,7 @@ class StoreProductValidate extends BaseValidate
*/
public function sceneStock()
{
return $this->only(['id', 'type', 'number']);
return $this->only(['attrs']);
}
}