erp/app/api/lists/goods/GoodsLists.php
2024-04-29 16:36:12 +08:00

114 lines
3.0 KiB
PHP

<?php
namespace app\api\lists\goods;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\goods\Goods;
use app\common\lists\ListsSearchInterface;
use app\common\model\goods\Goodsclass;
use app\common\model\goods\GoodsLabel;
/**
* 商品列表列表
* Class goods
* @package app\api\goods
*/
class GoodsLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function setSearch(): array
{
return [
'%like%' => ['name'],
'=' => ['class']
];
}
/**
* @notes 设置支持排序字段
* @return string[]
* @date 2021/12/29 10:07
* @remark 格式: ['前端传过来的字段名' => '数据库中的字段名'];
*/
public function setSortFields(): array
{
return ['sell' => 'sell', 'sales' => 'sales',];
}
/**
* @notes 设置默认排序
* @return string[]
* @date 2021/12/29 10:06
*/
public function setDefaultOrder(): array
{
return ['sales' => 'desc','sell' => 'asc'];
}
/**
* @notes 获取商品列表列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function lists(): array
{
$class_all=$this->request->get('class_all');
$where=[];
if($class_all){
$arr=[];
$arr2=[];
$arr=Goodsclass::where('pid',$class_all)->column('id');
if($arr){
$arr2=Goodsclass::where('pid','in',$arr)->column('id');
$where[]=['class','in',array_merge($arr,$arr2)];
}else{
$where[]=['class','=',$class_all];
}
}
return Goods::where($this->searchWhere)->where($where)
->field(['id', 'name','brand','class','unit', 'sell', 'code','imgs',])
->limit($this->limitOffset, $this->limitLength)
->with(['className','brandName','unitName'])
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取商品列表数量
* @return int
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function count(): int
{
$class_all=$this->request->get('class_all');
$where=[];
if($class_all){
$arr=[];
$arr2=[];
$arr=Goodsclass::where('pid',$class_all)->column('id');
if($arr){
$arr2=Goodsclass::where('pid','in',$arr)->column('id');
$where[]=['class','in',array_merge($arr,$arr2)];
}else{
$where[]=['class','=',$class_all];
}
}
return Goods::where($this->searchWhere)->where($where)->count();
}
}