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