BaseDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 app\dao;
  12. use crmeb\basic\BaseAuth;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\dao\CacheDaoTrait;
  15. use think\Model;
  16. /**
  17. * Class BaseDao
  18. * @package app\dao
  19. */
  20. abstract class BaseDao
  21. {
  22. use CacheDaoTrait;
  23. /**
  24. * 当前表名别名
  25. * @var string
  26. */
  27. protected $alias;
  28. /**
  29. * join表别名
  30. * @var string
  31. */
  32. protected $joinAlis;
  33. /**
  34. * 获取当前模型
  35. * @return string
  36. */
  37. abstract protected function setModel(): string;
  38. /**
  39. * 设置join链表模型
  40. * @return string
  41. */
  42. protected function setJoinModel(): string
  43. {
  44. }
  45. /**
  46. * 读取数据条数
  47. * @param array $where
  48. * @return int
  49. */
  50. public function count(array $where = []): int
  51. {
  52. return $this->search($where)->count();
  53. }
  54. /**
  55. * 获取某些条件总数
  56. * @param array $where
  57. * @return int
  58. */
  59. public function getCount(array $where)
  60. {
  61. return $this->getModel()->where($where)->count();
  62. }
  63. /**
  64. * 获取某些条件去重总数
  65. * @param array $where
  66. * @param $field
  67. * @param $search
  68. */
  69. public function getDistinctCount(array $where, $field, $search = true)
  70. {
  71. if ($search) {
  72. return $this->search($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  73. } else {
  74. return $this->getModel()->where($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  75. }
  76. }
  77. /**
  78. * 获取模型
  79. * @return BaseModel
  80. */
  81. protected function getModel()
  82. {
  83. return app()->make($this->setModel());
  84. }
  85. /**
  86. * 获取主键
  87. * @return mixed
  88. */
  89. protected function getPk()
  90. {
  91. return $this->getModel()->getPk();
  92. }
  93. /**
  94. * 获取一条数据
  95. * @param int|array $id
  96. * @param array|null $field
  97. * @param array|null $with
  98. * @return array|Model|null
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function get($id, ?array $field = [], ?array $with = [])
  104. {
  105. if (is_array($id)) {
  106. $where = $id;
  107. } else {
  108. $where = [$this->getPk() => $id];
  109. }
  110. return $this->getModel()::where($where)->when(count($with), function ($query) use ($with) {
  111. $query->with($with);
  112. })->field($field ?? ['*'])->find();
  113. }
  114. /**
  115. * 查询一条数据是否存在
  116. * @param $map
  117. * @param string $field
  118. * @return bool 是否存在
  119. */
  120. public function be($map, string $field = '')
  121. {
  122. if (!is_array($map) && empty($field)) $field = $this->getPk();
  123. $map = !is_array($map) ? [$field => $map] : $map;
  124. return 0 < $this->getModel()->where($map)->count();
  125. }
  126. /**
  127. * 根据条件获取一条数据
  128. * @param array $where
  129. * @param string|null $field
  130. * @param array $with
  131. * @return array|Model|null
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function getOne(array $where, ?string $field = '*', array $with = [])
  137. {
  138. $field = explode(',', $field);
  139. return $this->get($where, $field, $with);
  140. }
  141. /**
  142. * 获取单个字段值
  143. * @param array $where
  144. * @param string|null $field
  145. * @return mixed
  146. */
  147. public function value(array $where, ?string $field = '')
  148. {
  149. $pk = $this->getPk();
  150. return $this->getModel()::where($where)->value($field ?: $pk);
  151. }
  152. /**
  153. * 获取某个字段数组
  154. * @param array $where
  155. * @param string $field
  156. * @param string $key
  157. * @param bool $search
  158. * @return array
  159. */
  160. public function getColumn(array $where, string $field, string $key = '', bool $search = false)
  161. {
  162. if ($search) {
  163. return $this->search($where)->column($field, $key);
  164. } else {
  165. return $this->getModel()::where($where)->column($field, $key);
  166. }
  167. }
  168. /**
  169. * 删除
  170. * @param int|string|array $id
  171. * @return mixed
  172. */
  173. public function delete($id, ?string $key = null)
  174. {
  175. if (is_array($id)) {
  176. $where = $id;
  177. } else {
  178. $where = [is_null($key) ? $this->getPk() : $key => $id];
  179. }
  180. return $this->getModel()::where($where)->delete();
  181. }
  182. /**
  183. * 更新数据
  184. * @param int|string|array $id
  185. * @param array $data
  186. * @param string|null $key
  187. * @return mixed
  188. */
  189. public function update($id, array $data, ?string $key = null)
  190. {
  191. if (is_array($id)) {
  192. $where = $id;
  193. } else {
  194. $where = [is_null($key) ? $this->getPk() : $key => $id];
  195. }
  196. return $this->getModel()::update($data, $where);
  197. }
  198. /**
  199. * 批量更新数据
  200. * @param array $ids
  201. * @param array $data
  202. * @param string|null $key
  203. * @return BaseModel
  204. */
  205. public function batchUpdate(array $ids, array $data, ?string $key = null)
  206. {
  207. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  208. }
  209. /**
  210. * 批量更新数据,增加条件
  211. * @param array $ids
  212. * @param array $data
  213. * @param array $where
  214. * @param string|null $key
  215. * @return BaseModel
  216. */
  217. public function batchUpdateAppendWhere(array $ids, array $data, array $where, ?string $key = null)
  218. {
  219. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->where($where)->update($data);
  220. }
  221. /**
  222. * 插入数据
  223. * @param array $data
  224. * @return BaseModel|Model
  225. */
  226. public function save(array $data)
  227. {
  228. return $this->getModel()::create($data);
  229. }
  230. /**
  231. * 插入数据
  232. * @param array $data
  233. * @return mixed
  234. */
  235. public function saveAll(array $data)
  236. {
  237. return $this->getModel()::insertAll($data);
  238. }
  239. /**
  240. * 获取某个字段内的值
  241. * @param $value
  242. * @param string $filed
  243. * @param string $valueKey
  244. * @param array|string[] $where
  245. * @return mixed
  246. */
  247. public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
  248. {
  249. return $this->getModel()->getFieldValue($value, $filed, $valueKey, $where);
  250. }
  251. /**
  252. * 根据搜索器获取搜索内容
  253. * @param array $withSearch
  254. * @param array|null $data
  255. * @return Model
  256. */
  257. protected function withSearchSelect(array $withSearch, ?array $data = [])
  258. {
  259. [$with] = app()->make(BaseAuth::class)->________($withSearch, $this->setModel());
  260. return $this->getModel()->withSearch($with, $data);
  261. }
  262. /**
  263. * 搜索
  264. * @param array $where
  265. * @return \crmeb\basic\BaseModel|mixed
  266. */
  267. protected function search(array $where = [])
  268. {
  269. if ($where) {
  270. return $this->withSearchSelect(array_keys($where), $where);
  271. } else {
  272. return $this->getModel();
  273. }
  274. }
  275. /**
  276. * 求和
  277. * @param array $where
  278. * @param string $field
  279. * @param bool $search
  280. * @return float
  281. */
  282. public function sum(array $where, string $field, bool $search = false)
  283. {
  284. if ($search) {
  285. return $this->search($where)->sum($field);
  286. } else {
  287. return $this->getModel()::where($where)->sum($field);
  288. }
  289. }
  290. /**
  291. * 高精度加法
  292. * @param int|string $key
  293. * @param string $incField
  294. * @param string $inc
  295. * @param string|null $keyField
  296. * @param int $acc
  297. * @return bool
  298. * @throws \think\db\exception\DataNotFoundException
  299. * @throws \think\db\exception\DbException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. */
  302. public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
  303. {
  304. return $this->bc($key, $incField, $inc, $keyField, 1);
  305. }
  306. /**
  307. * 高精度 减法
  308. * @param int|string $uid id
  309. * @param string $decField 相减的字段
  310. * @param float|int $dec 减的值
  311. * @param string $keyField id的字段
  312. * @param bool $minus 是否可以为负数
  313. * @param int $acc 精度
  314. * @param bool $dec_return_false 减法 不够减是否返回false || 减为0
  315. * @return bool
  316. */
  317. public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2, bool $dec_return_false = true)
  318. {
  319. return $this->bc($key, $decField, $dec, $keyField, 2, $acc, $dec_return_false);
  320. }
  321. /**
  322. * 高精度计算并保存
  323. * @param $key
  324. * @param string $incField
  325. * @param string $inc
  326. * @param string|null $keyField
  327. * @param int $type
  328. * @param int $acc
  329. * @param bool $dec_return_false 减法 不够减是否返回false || 减为0
  330. * @return bool
  331. * @throws \think\db\exception\DataNotFoundException
  332. * @throws \think\db\exception\DbException
  333. * @throws \think\db\exception\ModelNotFoundException
  334. */
  335. public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2, bool $dec_return_false = true)
  336. {
  337. if ($keyField === null) {
  338. $result = $this->get($key);
  339. } else {
  340. $result = $this->getOne([$keyField => $key]);
  341. }
  342. if (!$result) return false;
  343. if ($type === 1) {
  344. $new = bcadd($result[$incField], $inc, $acc);
  345. } else if ($type === 2) {
  346. if ($result[$incField] < $inc) {
  347. if ($dec_return_false) return false;
  348. $new = 0;
  349. } else {
  350. $new = bcsub($result[$incField], $inc, $acc);
  351. }
  352. }
  353. $result->{$incField} = $new;
  354. return false !== $result->save();
  355. }
  356. /**
  357. * 减库存加销量
  358. * @param array $where
  359. * @param int $num
  360. * @return mixed
  361. */
  362. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  363. {
  364. return app()->make(BaseAuth::class)->_____($this->getModel(), $where, $num, $stock, $sales) !== false;
  365. }
  366. /**
  367. * 加库存减销量
  368. * @param array $where
  369. * @param int $num
  370. * @return mixed
  371. */
  372. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  373. {
  374. return app()->make(BaseAuth::class)->___($this->getModel(), $where, $num, $stock, $sales) !== false;
  375. }
  376. /**
  377. * 软删除
  378. * @param $id
  379. * @param string|null $key
  380. * @return bool
  381. */
  382. public function destroy($id, ?string $key = null)
  383. {
  384. if (is_array($id)) {
  385. $where = $id;
  386. } else {
  387. $where = [is_null($key) ? $this->getPk() : $key => $id];
  388. }
  389. return $this->getModel()::destroy($where);
  390. }
  391. /**
  392. * 自增单个数据
  393. * @param $where
  394. * @param string $field
  395. * @param int $number
  396. * @return mixed
  397. */
  398. public function incUpdate($where, string $field, int $number = 1)
  399. {
  400. return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->inc($field, $number)->update();
  401. }
  402. /**
  403. * 自减单个数据
  404. * @param $where
  405. * @param string $field
  406. * @param int $number
  407. * @return mixed
  408. */
  409. public function decUpdate($where, string $field, int $number = 1)
  410. {
  411. return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->dec($field, $number)->update();
  412. }
  413. }