BaseModel.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace qiniu\basic;
  12. use qiniu\traits\CacheModelTrait;
  13. use qiniu\traits\ModelTrait;
  14. use Symfony\Contracts\Cache\CacheTrait;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\db\Query;
  19. use think\Model;
  20. /**
  21. * Class BaseModel
  22. * @package crmeb\basic
  23. * @mixin ModelTrait
  24. */
  25. abstract class BaseModel extends Model
  26. {
  27. use CacheModelTrait;
  28. protected $order = '';
  29. protected $auto_expire;
  30. public $limit = 1000;
  31. /**
  32. * 查询一条数据是否存在
  33. * @param $map
  34. * @param string $field
  35. * @return bool 是否存在
  36. * @throws DbException
  37. */
  38. public function be($map, string $field = '')
  39. {
  40. if (!is_array($map) && empty($field)) $field = $this->getPk();
  41. $map = !is_array($map) ? [$field => $map] : $map;
  42. return 0 < $this->getModel()->where($map)->count();
  43. }
  44. /**
  45. * 搜索
  46. * @param array $where
  47. * @return Model|Query
  48. */
  49. public function search(array $where = [])
  50. {
  51. if ($where) {
  52. return $this->withSearchSelect(array_keys($where), $where);
  53. } else {
  54. return $this->getModel();
  55. }
  56. }
  57. /**
  58. * 根据搜索器获取搜索内容
  59. * @param array $withSearch
  60. * @param array|null $data
  61. * @return Model|Query
  62. */
  63. public function withSearchSelect(array $withSearch, ?array $data = [])
  64. {
  65. return $this->getModel()->withSearch($withSearch, $data);
  66. }
  67. /**
  68. * 读取数据条数
  69. * @param array $where
  70. * @return int
  71. * @throws DbException
  72. */
  73. public function getCount(array $where = []): int
  74. {
  75. return $this->search($where)->count();
  76. }
  77. /**
  78. * 获取一条数据
  79. * @param int|array $id
  80. * @param array|string|null $field
  81. * @param array|null $with
  82. * @return array|Model|null
  83. * @throws DataNotFoundException
  84. * @throws DbException
  85. * @throws ModelNotFoundException
  86. */
  87. public function get($id, $field = [], ?array $with = [])
  88. {
  89. if (is_array($id)) {
  90. $where = $id;
  91. } else {
  92. $where = [$this->getPk() => $id];
  93. }
  94. return $this->search($where)->when(count($with), function ($query) use ($with) {
  95. $query->with($with);
  96. })->field($field ?? ['*'])->find();
  97. }
  98. /**
  99. * 获取一条数据
  100. * @param array $where
  101. * @param array|string|null $field
  102. * @param array|null $with
  103. * @return array|Model|null
  104. * @throws DataNotFoundException
  105. * @throws DbException
  106. * @throws ModelNotFoundException
  107. */
  108. public function getOne(array $where, $field = '*', array $with = [])
  109. {
  110. $field = explode(',', $field);
  111. return $this->get($where, $field, $with);
  112. }
  113. public function getList(array $where, $field = '*', int $page = 0, int $limit = 0, ?array $with = [], $order = '')
  114. {
  115. return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) {
  116. $query->with($with);
  117. })->when($page && $limit, function ($query) use ($page, $limit) {
  118. $query->page($page, $limit);
  119. })->when($order != '', function ($query) use ($order) {
  120. $query->order($order);
  121. })->when($order == '', function ($query) {
  122. if ($this->order != '') {
  123. $query->order($this->order);
  124. } else {
  125. $query->order($this->getPk(), 'desc');
  126. }
  127. })->select()->toArray();
  128. }
  129. public function getExportList(array $where, $field = '*', ?array $with = [], $order = '')
  130. {
  131. return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) {
  132. $query->with($with);
  133. })->when($order != '', function ($query) use ($order) {
  134. $query->order($order);
  135. })->when($order == '', function ($query) {
  136. if ($this->order) {
  137. $query->order($this->order);
  138. } else {
  139. $query->order($this->getPk(), 'desc');
  140. }
  141. })->page(1, $this->limit)->select()->toArray();
  142. }
  143. /**
  144. * 获取某个字段数组
  145. * @param array $where
  146. * @param string|null|array $field
  147. * @param string $key
  148. * @param bool $search
  149. * @return array
  150. */
  151. public function getColumn(array $where, $field, string $key = '', bool $search = false)
  152. {
  153. if ($search) {
  154. return $this->search($where)->column($field, $key);
  155. } else {
  156. return $this->getModel()::where($where)->column($field, $key);
  157. }
  158. }
  159. /**
  160. * 获取某个字段值
  161. * @param array $where
  162. * @param string|null|array $field
  163. * @param bool $search
  164. * @return mixed
  165. */
  166. public function getOneValue(array $where, $field, bool $search = false)
  167. {
  168. if ($search) {
  169. return $this->search($where)->value($field);
  170. } else {
  171. return $this->getModel()::where($where)->value($field);
  172. }
  173. }
  174. public function getSum($where, $field)
  175. {
  176. return $this->search($where)->sum($field);
  177. }
  178. /**
  179. * 批量更新数据
  180. * @param array $ids
  181. * @param array $data
  182. * @param string|null $key
  183. * @return Model|null
  184. */
  185. public function batchUpdate(array $ids, array $data, ?string $key = null)
  186. {
  187. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  188. }
  189. public function modelName()
  190. {
  191. return $this->getModel()->chsName ?? $this->getModel()->name;
  192. }
  193. }