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

This commit is contained in:
mkm 2024-08-11 17:55:09 +08:00
parent ffe38b81f1
commit 262c730674

View File

@ -11,13 +11,14 @@ use app\common\model\system_store\SystemStore;
use app\common\lists\ListsSearchInterface;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\lists\ListsExcelInterface;
use app\common\lists\ListsSortInterface;
/**
* 门店商品辅助表
* Class StoreBranchProductLists
* @package app\admin\listssystem_store
*/
class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface, ListsSortInterface
{
@ -30,14 +31,29 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
public function setSearch(): array
{
return [
'=' => ['product_id', 'cate_id','store_id','status'],
'%pipe_like%' => ['store_name_code'=>'store_name|bar_code'],
'=' => ['product_id', 'cate_id', 'store_id', 'status'],
'%pipe_like%' => ['store_name_code' => 'store_name|bar_code'],
'%like%' => ['store_name'],
'<='=> ['stock'],
'<=' => ['stock'],
];
}
/**
* @notes 设置支持排序字段
* @return string[]
* @remark 格式: ['前端传过来的字段名' => '数据库中的字段名'];
*/
public function setSortFields(): array
{
return ['stock' => 'stock',];
}
/**
* @notes 设置默认排序
* @return string[]
*/
public function setDefaultOrder(): array
{
return ['sort' => 'desc', 'stock' => 'desc', 'id' => 'desc'];
}
/**
* @notes 获取门店列表列表
* @return array
@ -49,32 +65,32 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
*/
public function lists(): array
{
$class_all=$this->request->get('class_all');
$where=[];
if($class_all){
$arr=Cate::where('pid',$class_all)->column('id');
if($arr){
$arr2=Cate::where('pid','in',$arr)->column('id');
$where[]=['cate_id','in',array_merge($arr,$arr2)];
$class_all = $this->request->get('class_all');
$where = [];
if ($class_all) {
$arr = Cate::where('pid', $class_all)->column('id');
if ($arr) {
$arr2 = Cate::where('pid', 'in', $arr)->column('id');
$where[] = ['cate_id', 'in', array_merge($arr, $arr2)];
}
}
if(empty($where) && $class_all){
if (empty($where) && $class_all) {
//2或者1
$where[]=['cate_id','=',$class_all];
$where[] = ['cate_id', '=', $class_all];
}
if($where){
$this->searchWhere[]=$where;
if ($where) {
$this->searchWhere[] = $where;
}
return StoreBranchProduct::where($this->searchWhere)
->field(['id','store_id','product_id', 'image', 'store_name','store_info', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost','purchase', 'status','batch','vip_price','manufacturer_information'])
->field(['id', 'store_id', 'product_id', 'image', 'store_name', 'store_info', 'cate_id', 'price', 'sales', 'stock', 'unit', 'cost', 'purchase', 'status', 'batch', 'vip_price', 'manufacturer_information'])
->when(!empty($this->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->adminInfo['store_id']);
})
->limit($this->limitOffset, $this->limitLength)
->order(['sort' => 'desc','stock'=>'desc','id' => 'desc'])
->order($this->sortOrder)
->select()
->each(function ($item) {
$item['system_store_name']=SystemStore::where('id',$item['store_id'])->value('name');
$item['system_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;
@ -98,7 +114,7 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
->count();
}
/**
/**
* @notes 导出文件名
* @return string
* @author 乔峰
@ -106,9 +122,9 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
*/
public function setFileName(): string
{
if($this->request->get('store_id')){
$name=SystemStore::where('id',$this->request->get('store_id'))->value('name');
return $name.'商品列表';
if ($this->request->get('store_id')) {
$name = SystemStore::where('id', $this->request->get('store_id'))->value('name');
return $name . '商品列表';
}
return '门店商品列表';
}
@ -122,16 +138,16 @@ class StoreBranchProductLists extends BaseAdminDataLists implements ListsSearchI
*/
public function setExcelFields(): array
{
$data=[
$data = [
'store_name' => '商品名称',
'store_info' => '规格',
'unit_name' => '单位',
'cate_name' => '分类',
'stock' => '库存',
'sales' => '销量',
'purchase'=>'供货价',
'cost'=>'商户价',
'vip_price'=>'会员价',
'purchase' => '供货价',
'cost' => '商户价',
'vip_price' => '会员价',
'price' => '零售价',
];
return $data;