feat(store_product): 修改商品导入功能,优化交易统计查询

This commit is contained in:
mkm 2024-06-07 13:38:42 +08:00
parent d0bbd71dde
commit f1179ececf
5 changed files with 111 additions and 16 deletions

View File

@ -826,7 +826,7 @@ class WorkbenchController extends BaseAdminController
}
//-------------------------------交易统计---------------------------------------//
//当日订单金额
public function top__trade()
public function top_trade()
{
$data = [
"left" => [

View File

@ -4,7 +4,7 @@ namespace app\admin\controller\store_branch_product;
use app\admin\controller\BaseAdminController;
use app\admin\lists\store_product\StoreProductLists;
use app\admin\lists\store_branch_product\StoreBranchProductLists;
use app\admin\logic\store_product\StoreProductLogic;
use app\admin\validate\store_product\StoreProductValidate;
use app\common\model\store_branch_product\StoreBranchProduct;
@ -26,7 +26,7 @@ class StoreBranchProductController extends BaseAdminController
*/
public function lists()
{
return $this->dataLists(new StoreProductLists());
return $this->dataLists(new StoreBranchProductLists());
}

View File

@ -7,7 +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 Webman\RedisQueue\Redis;
/**
* 商品列表控制器
@ -91,5 +91,25 @@ class StoreProductController extends BaseAdminController
return $this->data($result);
}
}
/**
* 商品导入到门店
*/
public function import()
{
$product_arr = $this->request->post('product_arr');
$store_arr = $this->request->post('store_arr');
if (count($store_arr) == 1) {
$store_id = $store_arr[0];
foreach ($product_arr as $key => $arr) {
Redis::send('store-storage', ['product_arr' => $arr, 'store_id' => $store_id, 'admin_id' => $this->adminId]);
}
} 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, 'admin_id' => $this->adminId]);
}
}
}
return $this->success('已导入后台队列,请在门店入库记录中查看');
}
}

View File

@ -28,7 +28,7 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
public function setSearch(): array
{
return [
'=' => ['product_id', 'cate_id'],
'=' => ['product_id', 'cate_id','store_id'],
'%like%' => ['store_name'],
];
}
@ -47,7 +47,7 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
{
$status = $this->params['status'] ?? '';
return StoreBranchProduct::where($this->searchWhere)
->field(['id', 'product_id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost', 'status'])
->field(['id','store_id','product_id', 'image', 'store_name', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost', 'status'])
->when(!empty($this->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->adminInfo['store_id']);
})
@ -66,6 +66,7 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->each(function ($item) {
$item['store_name']=SystemStore::where('id',$item['store_id'])->value('name');
$item['unit_name'] = StoreProductUnit::where('id', $item['unit'])->value('name');
$item['cate_name'] = StoreCategory::where('id', $item['cate_id'])->value('name');
return $item;

View File

@ -3,8 +3,14 @@
namespace app\queue\redis;
use app\admin\logic\store_product\StoreProductLogic;
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\model\store_product_attr_value\StoreProductAttrValue;
use app\common\model\system_store_storage\SystemStoreStorage;
use Webman\RedisQueue\Consumer;
use support\Log;
use think\facade\Db;
/**
* 门店库存记录商品
@ -12,7 +18,7 @@ use support\Log;
class StoreStorageSend implements Consumer
{
// 要消费的队列名
public $queue = 'store_storage';
public $queue = 'store-storage';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
@ -20,15 +26,83 @@ class StoreStorageSend implements Consumer
// 消费
public function consume($data)
{
// $product_id=$data['product_id'];
// $store_id=$data['store_id'];
// $store_id=$data['store_id'];
// StoreBranchProduct::w
$product_arr = $data['product_arr'];
$store_id = $data['store_id'];
$admin_id = $data['admin_id'];
Log::error('StoreStorageSend: ' . json_encode($data));
$find = StoreProduct::where('id', $product_arr['id'])->findOrEmpty()->toArray();
$store_find = StoreBranchProduct::where(['product_id' => $product_arr['id'], 'store_id' => $store_id])->findOrEmpty()->toArray();
if ($find && !$store_find) {
$attr_value = StoreProductAttrValue::where('product_id', $product_arr['id'])->findOrEmpty();
Db::startTrans();
try {
$product = [
'product_id' => $find['id'],
'image' => $find['image'],
'store_name' => $find['store_name'],
'store_info' => $find['store_info'],
'keyword' => $find['keyword'],
'bar_code' => $find['bar_code'],
'cate_id' => $find['cate_id'],
'price' => $find['price'],
'unit' => $find['unit'],
'store_id' => $store_id,
'sales' => 0,
'stock' => 0,
];
$branch=StoreBranchProduct::create($product);
$arr = [
'product_id' => $product_arr['id'],
'store_id' => $store_id,
'unique' => setUnique($branch['id'], '', 0),
'sales' => 0,
'type' => 0,
'bar_code' => $attr_value['bar_code']
];
StoreBranchProductAttrValue::create($arr);
$this->storage($find, $store_id, $admin_id, $product_arr);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
Log::error('store-storage队列消费失败: ' . $e->getMessage().',line:'.$e->getLine().',file:'.$e->getFile());
return false;
}
} else {
Db::startTrans();
try {
$this->storage($find, $store_id, $admin_id, $product_arr);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
Log::error('store-storage队列消费失败: ' . $e->getMessage().',line:'.$e->getLine().',file:'.$e->getFile());
return false;
}
}
}
public function storage($find, $store_id, $admin_id, $product_arr)
{
$storage = [
'product_id' => $product_arr['id'],
'store_id' => $store_id,
'nums' => $product_arr['stock'],
'admin_id' => $admin_id,
];
if ($find['stock'] < $product_arr['stock']) {
$storage['status'] = -1;
$storage['mark'] = '库存不足,主库存为:' . $find['stock'];
SystemStoreStorage::create($storage);
}else{
SystemStoreStorage::create($storage);
StoreProduct::where('id', $product_arr['id'])->dec('stock',$product_arr['stock'])->update();
}
}
public function onConsumeFailure(\Throwable $e, $package)
{
$package['max_attempts']=0;
Log::error('复制商品失败product_id:'.$package['data']['product_id']);
$package['max_attempts'] = 0;
return $package;
}
}
}