优化api商品查询处理

This commit is contained in:
liu 2024-06-12 14:17:15 +08:00
parent 0a693d5e37
commit 191aaa1f56

View File

@ -4,6 +4,7 @@ namespace app\api\lists\product;
use app\admin\lists\BaseAdminDataLists; use app\admin\lists\BaseAdminDataLists;
use app\common\lists\ListsSortInterface;
use app\common\model\dict\DictType; use app\common\model\dict\DictType;
use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
@ -17,7 +18,7 @@ use think\facade\Db;
* Class goods * Class goods
* @package app\api\goods * @package app\api\goods
*/ */
class ProductLists extends BaseAdminDataLists implements ListsSearchInterface class ProductLists extends BaseAdminDataLists implements ListsSearchInterface,ListsSortInterface
{ {
@ -29,16 +30,18 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function setSearch(): array public function setSearch(): array
{ {
$name=$this->request->get('name'); $name=$this->request->get('name','');
// $where= [ // $where['%pipe_like%'] =['name'=>'store_name|bar_code'];
// '=' => ['class'] if($name){
// ]; if(preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
// if($name && preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){ $where['%like%']=['store_name'];
// $where['%like%']=['store_name']; }else{
$where[] = ['store_name','like','%'.$name.'%']; $where['=']=['bar_code','cate_id'];
// }else{ }
// $where['=']=['bar_code','cate_id']; }else{
// } $where['=']=['store_id','bar_code','cate_id'];
}
return $where; return $where;
} }
/** /**
@ -49,8 +52,7 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function setSortFields(): array public function setSortFields(): array
{ {
// return ['sell' => 'sell', 'sales' => 'sales',]; return ['sell' => 'price', 'sales' => 'sales',];
return ['sell' => 'ot_price', 'sales' => 'sales',];
} }
@ -61,7 +63,7 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function setDefaultOrder(): array public function setDefaultOrder(): array
{ {
return ['sales' => 'desc','ot_price' => 'asc']; return ['sales' => 'desc','price' => 'asc'];
} }
/** /**
@ -75,50 +77,30 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function lists(): array public function lists(): array
{ {
$found = false;
if($this->searchWhere){
foreach ($this->searchWhere as $subArray) {
if (in_array('store_id', $subArray)) {
$found = true;
break; // 找到后退出循环
}
}
}
if(!$found){
$this->searchWhere[]=['store_id','=',2];
}
$class_all=$this->request->get('class_all'); $class_all=$this->request->get('class_all');
$cate_id=$this->request->get('class');
$name = $this->request->get('name','');
$order_param = $this->request->get('order');
$store_id = $this->request->get('store_id',2);
$where=[]; $where=[];
$order = [];
if($class_all){ if($class_all){
$arr=[];
$arr2=[];
$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)];
} }
}else{
if($cate_id){
$where[]=['cate_id','=',$cate_id];
}
} }
if(!empty($order_param)){
if($order_param == 'asc'){
$order['price'] = 'asc';
}elseif ($order_param == 'desc') {
$order['price'] = 'desc';
}elseif ($order_param=='sales') {
$order['sales'] = 'desc';
}
}else{
$order['id'] = 'desc';
}
if($name){
if(preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
$where[] = ['store_name','like','%'.$name.'%'];
}else{
$where[] = ['bar_code','=',$name];
}
}
if($store_id){
$where[]=['store_id','=',$store_id];
}else{
$where[]=['store_id','=',2];
}
$this->searchWhere[]=['status','=',1]; $this->searchWhere[]=['status','=',1];
$this->searchWhere[]=['stock','>',0]; $this->searchWhere[]=['stock','>',0];
$M_store_id=$this->request->__get('store_id'); $M_store_id=$this->request->__get('store_id');
@ -128,7 +110,7 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
->field(['id', 'product_id','cate_id','store_name', 'cost','store_id','price', 'bar_code','image','sales','store_info','delete_time','unit']) ->field(['id', 'product_id','cate_id','store_name', 'cost','store_id','price', 'bar_code','image','sales','store_info','delete_time','unit'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->with(['className','unitName']) ->with(['className','unitName'])
->order($order) ->order($this->sortOrder)
->select() ->select()
->toArray(); ->toArray();
@ -137,12 +119,12 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
->field(['id', 'product_id','cate_id','store_name','cost', 'store_id','price', 'bar_code','image','sales','store_info','delete_time','unit']) ->field(['id', 'product_id','cate_id','store_name','cost', 'store_id','price', 'bar_code','image','sales','store_info','delete_time','unit'])
->limit($this->limitOffset, $this->limitLength) ->limit($this->limitOffset, $this->limitLength)
->with(['className','unitName']) ->with(['className','unitName'])
->order($order) ->order($this->sortOrder)
->select() ->select()
->toArray(); ->toArray();
} }
$check = DictType::where('type','activities')->find(); // $check = DictType::where('type','activities')->find();
// foreach ($data as &$value){ // foreach ($data as &$value){
// if(isset($check) && $check['status'] == 1){ // if(isset($check) && $check['status'] == 1){
// // $value['price'] = StoreProduct::where('id',$value['product_id'])->value('ot_price'); // // $value['price'] = StoreProduct::where('id',$value['product_id'])->value('ot_price');
@ -155,13 +137,7 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
// } // }
// } // }
return $data; return $data;
// return StoreProduct::where($this->searchWhere)->where($where)
// ->field(['id', 'cate_id','store_name','unit', 'ot_price', 'bar_code','image','sales','store_info'])
// ->limit($this->limitOffset, $this->limitLength)
// ->with(['className','unitName'])
// ->order($order)
// ->select()
// ->toArray();
} }
@ -173,37 +149,30 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
*/ */
public function count(): int public function count(): int
{ {
$found = false;
if($this->searchWhere){
foreach ($this->searchWhere as $subArray) {
if (in_array('store_id', $subArray)) {
$found = true;
break; // 找到后退出循环
}
}
}
if(!$found){
$this->searchWhere[]=['store_id','=',2];
}
$class_all=$this->request->get('class_all'); $class_all=$this->request->get('class_all');
$store_id = $this->request->get('store_id',2);
$name = $this->request->get('name','');
$where=[]; $where=[];
if($class_all){ if($class_all){
$arr=[];
$arr2=[];
$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)];
}else{
$where[]=['cate_id','=',$class_all];
} }
}
if($name){
if(preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
$where[] = ['store_name','like','%'.$name.'%'];
}else{
$where[] = ['bar_code','=',$name];
}
}
if($store_id){
$where[]=['store_id','=',$store_id];
}else{
$where[]=['store_id','=',2];
} }
$M_store_id=$this->request->__get('store_id'); $M_store_id=$this->request->__get('store_id');
$this->searchWhere[]=['status','=',1];
$this->searchWhere[]=['stock','>',0];
if($M_store_id){ if($M_store_id){
$where[]=['store_id','=',$M_store_id]; $where[]=['store_id','=',$M_store_id];
$data = StoreBranchProduct::where($this->searchWhere)->where($where) $data = StoreBranchProduct::where($this->searchWhere)->where($where)