erp/app/api/lists/goods/GoodsLists.php

153 lines
4.3 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;
use think\facade\Db;
/**
* 商品列表列表
* 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
{
$name=$this->request->get('name');
$where= [
'=' => ['class']
];
if($name && preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
$where['%like%']=['name'];
}else{
$where['=']=['code','class'];
}
return $where;
}
/**
* @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');
$order_param = $this->request->get('order');
$where=[];
$order = [];
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];
}
}
if(!empty($order_param)){
if($order_param == 'asc'){
$order['sell'] = 'asc';
}elseif ($order_param == 'desc') {
$order['sell'] = 'desc';
}elseif ($order_param=='sales') {
$order['sales'] = 'desc';
}
}else{
$order['id'] = 'desc';
}
$mer_id=$this->request->__get('mer_id');
if($mer_id){
$goods_id=Db::name('merchant_bind_goods')->where('mer_id',$mer_id)->limit($this->limitOffset, $this->limitLength)->column('goods_id');
if($goods_id){
return Goods::where($this->searchWhere)->where('id','in',$goods_id)
->field(['id', 'name','brand','class','unit', 'sell', 'code','imgs','sales','spec'])
->with(['className','brandName','unitName'])
->order($order)
->select()
->toArray();
}else{
return [];
}
}
return Goods::where($this->searchWhere)->where($where)
->field(['id', 'name','brand','class','unit', 'sell', 'code','imgs','sales','spec'])
->limit($this->limitOffset, $this->limitLength)
->with(['className','brandName','unitName'])
->order($order)
->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];
}
}
$mer_id=$this->request->__get('mer_id');
if($mer_id){
return Db::name('merchant_bind_goods')->where('mer_id',$mer_id)->count();
}
return Goods::where($this->searchWhere)->where($where)->count();
}
}