123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- 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->search($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);
- })->when($order != '', function ($query) use ($order) {
- $query->order($order);
- })->when($order == '', function ($query) {
- 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);
- })->when($order != '', function ($query) use ($order) {
- $query->order($order);
- })->when($order == '', function ($query) {
- 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;
- }
- }
|