new-admin-api/app/common/dao/store/StoreBrandDao.php

89 lines
2.8 KiB
PHP

<?php
namespace app\common\dao\store;
use app\common\dao\BaseDao;
use app\common\model\store\StoreBrand as model;
use crmeb\traits\CategoresDao;
class StoreBrandDao extends BaseDao
{
use CategoresDao;
protected function getModel(): string
{
return model::class;
}
public function getAll()
{
$query = $this->getModel()::hasWhere('brandCategory',function($query){
$query->where('is_show',1);
});
$query->where('StoreBrand.is_show',1);
$list = $query->order('StoreBrand.sort DESC,StoreBrand.create_time DESC')->select()->toArray();
array_push($list,[
"brand_id" => 0,
"brand_category_id" => 0,
"brand_name" => "其他",
"sort" => 999,
"pic" => "",
"is_show" => 1,
"create_time" => "",
]);
return $list;
}
public function merFieldExists($field, $value, $except = null)
{
return ($this->getModel())::getDB()
->when($except, function ($query, $except) use ($field) {
$query->where($field, '<>', $except);
})
->where($field, $value)->count() > 0;
}
public function search(array $where)
{
$query = $this->getModel()::getDB();
if(isset($where['brand_category_id']) && $where['brand_category_id'])
$query->where('brand_category_id',$where['brand_category_id']);
if(isset($where['brand_name']) && $where['brand_name'])
$query->where('brand_name','like','%'.$where['brand_name'].'%');
if((isset($where['ids']) && $where['ids']))
$query->where($this->getPk(),'in',$where['ids']);
return $query->order('sort DESC,create_time desc');
}
/**
* 获取品牌列表
* @param array $where
* @param array $with
* @param array $field
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $where, array $with = [], array $field = ['*'], int $page = 0, int $limit = 0)
{
return $this->search($where)->field($field)
->when(in_array('product', $with), function ($query) use ($with) {
$with = array_merge(array_diff($with, ['product']), ['product' => function ($que) {
$que->field(['brand_id', "count(`brand_id`) as brand_num"])->where('is_del', 0)->group('brand_id');
}]);
$query->with($with);
})->when($page && $limit, function ($query) use($page, $limit) {
$query->page($page, $limit);
})->order('sort desc,brand_id desc')->select()->toArray();
}
}