Files
huangjingfen/pro_v3.5.1/crmeb/traits/SearchDaoTrait.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

89 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace crmeb\traits;
use app\dao\BaseDao;
use crmeb\basic\BaseAuth;
use crmeb\basic\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* Trait SearchDaoTrait
* @package crmeb\traits
* @mixin BaseDao
*/
trait SearchDaoTrait
{
/**
* 搜索没有进入搜索器的自动进入where条件
* @param array $where
* @param bool $authWhere
* @return BaseModel
*/
public function searchWhere(array $where, bool $authWhere = true)
{
[$with, $whereKey] = app()->make(BaseAuth::class)->________(array_keys($where), $this->setModel());
$whereData = [];
foreach ($whereKey as $key) {
if (isset($where[$key]) && 'timeKey' !== $key) {
$whereData[$key] = $where[$key];
}
}
return $this->getModel()->withSearch($with, $where)->when($authWhere && $whereData, function ($query) use ($whereData) {
$query->where($whereData);
});
}
/**
* @param array $where
* @param bool $authWhere
* @return int
* @throws DbException
*/
public function count(array $where = [], bool $authWhere = true): int
{
return $this->searchWhere($where, $authWhere)->count();
}
/**
* 搜索
* @param array $where
* @param array|string[] $field
* @param int $page
* @param int $limit
* @param null $sort
* @param array $with
* @return array
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function getDataList(array $where, array $field = ['*'], int $page = 0, int $limit = 0, $sort = null, array $with = [])
{
return $this->searchWhere($where)->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->when(!$page && $limit, function ($query) use ($limit) {
$query->limit($limit);
})->when($sort, function ($query, $sort) {
if (is_array($sort)) {
foreach ($sort as $k => $v) {
if (is_numeric($k)) {
$query->order($v, 'desc');
} else {
$query->order($k, $v);
}
}
} else {
$query->order($sort, 'desc');
}
})->field($field)->with($with)->select()->toArray();
}
}