// +---------------------------------------------------------------------- namespace qiniu\basic; use qiniu\traits\CacheModelTrait; use qiniu\traits\ModelTrait; use Symfony\Contracts\Cache\CacheTrait; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\db\Query; use think\Model; /** * Class BaseModel * @package crmeb\basic * @mixin ModelTrait */ abstract class BaseModel extends Model { use CacheModelTrait; protected $order = ''; protected $auto_expire; public $limit = 1000; /** * 查询一条数据是否存在 * @param $map * @param string $field * @return bool 是否存在 * @throws DbException */ public function be($map, string $field = '') { if (!is_array($map) && empty($field)) $field = $this->getPk(); $map = !is_array($map) ? [$field => $map] : $map; return 0 < $this->getModel()->where($map)->count(); } /** * 搜索 * @param array $where * @return Model|Query */ public function search(array $where = []) { if ($where) { return $this->withSearchSelect(array_keys($where), $where); } else { return $this->getModel(); } } /** * 根据搜索器获取搜索内容 * @param array $withSearch * @param array|null $data * @return Model|Query */ public function withSearchSelect(array $withSearch, ?array $data = []) { return $this->getModel()->withSearch($withSearch, $data); } /** * 读取数据条数 * @param array $where * @return int * @throws DbException */ public function getCount(array $where = []): int { return $this->search($where)->count(); } /** * 获取一条数据 * @param int|array $id * @param array|string|null $field * @param array|null $with * @return array|Model|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function get($id, $field = [], ?array $with = []) { if (is_array($id)) { $where = $id; } else { $where = [$this->getPk() => $id]; } return $this->getModel()::where($where)->when(count($with), function ($query) use ($with) { $query->with($with); })->field($field ?? ['*'])->find(); } /** * 获取一条数据 * @param array $where * @param array|string|null $field * @param array|null $with * @return array|Model|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getOne(array $where, $field = '*', array $with = []) { $field = explode(',', $field); return $this->get($where, $field, $with); } public function getList(array $where, $field = '*', int $page = 0, int $limit = 0, ?array $with = [], $order = '') { return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) { $query->with($with); })->when($page && $limit, function ($query) use ($page, $limit) { $query->page($page, $limit); })->where($order != '', function ($query) use ($order) { $query->order($order); })->where($order == '', function ($query) use ($where) { if ($this->order) { $query->order($this->order); } else { $query->order($this->getPk(), 'desc'); } })->select()->toArray(); } public function getExportList(array $where, $field = '*', ?array $with = [], $order = '') { return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) { $query->with($with); })->where($order != '', function ($query) use ($order) { $query->order($order); })->where($order == '', function ($query) use ($where) { if ($this->order) { $query->order($this->order); } else { $query->order($this->getPk(), 'desc'); } })->page(1, $this->limit)->select()->toArray(); } /** * 获取某个字段数组 * @param array $where * @param string|null|array $field * @param string $key * @param bool $search * @return array */ public function getColumn(array $where, $field, string $key = '', bool $search = false) { if ($search) { return $this->search($where)->column($field, $key); } else { return $this->getModel()::where($where)->column($field, $key); } } /** * 获取某个字段值 * @param array $where * @param string|null|array $field * @param bool $search * @return mixed */ public function getOneValue(array $where, $field, bool $search = false) { if ($search) { return $this->search($where)->value($field); } else { return $this->getModel()::where($where)->value($field); } } public function getSum($where, $field) { return $this->search($where)->sum($field); } /** * 批量更新数据 * @param array $ids * @param array $data * @param string|null $key * @return Model|null */ public function batchUpdate(array $ids, array $data, ?string $key = null) { return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data); } public function modelName() { return $this->getModel()->chsName ?? $this->getModel()->name; } }