BaseDao.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * @author: liaofei<136327134@qq.com>
  13. * @day: 2020/7/6
  14. */
  15. namespace app\dao;
  16. use crmeb\basic\BaseModel;
  17. use think\helper\Str;
  18. use think\Model;
  19. /**
  20. * Class BaseDao
  21. * @package app\dao
  22. */
  23. abstract class BaseDao
  24. {
  25. /**
  26. * 当前表名别名
  27. * @var string
  28. */
  29. protected $alias;
  30. /**
  31. * join表别名
  32. * @var string
  33. */
  34. protected $joinAlis;
  35. /**
  36. * 获取当前模型
  37. * @return string
  38. */
  39. abstract protected function setModel(): string;
  40. /**
  41. * 设置join链表模型
  42. * @return string
  43. */
  44. protected function setJoinModel(): string
  45. {
  46. }
  47. /**
  48. * 读取数据条数
  49. * @param array $where
  50. * @param bool $search
  51. * @return int
  52. * @throws \ReflectionException
  53. */
  54. public function count(array $where = [], bool $search = true)
  55. {
  56. return $this->search($where, $search)->count();
  57. }
  58. /**
  59. * 获取某些条件数据
  60. * @param array $where
  61. * @param string $field
  62. * @param int $page
  63. * @param int $limit
  64. * @param string $order
  65. * @param bool $search
  66. * @return \think\Collection
  67. * @throws \ReflectionException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function selectList(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', array $with = [], bool $search = false)
  73. {
  74. return $this->selectModel($where, $field, $page, $limit, $order, $with, $search)->select();
  75. }
  76. /**
  77. * @param array $where
  78. * @param string $field
  79. * @param int $page
  80. * @param int $limit
  81. * @param string $order
  82. * @param array $with
  83. * @param bool $search
  84. * @return BaseModel
  85. * @throws \ReflectionException
  86. * @author 等风来
  87. * @email 136327134@qq.com
  88. * @date 2023/4/14
  89. */
  90. public function selectModel(array $where, string $field = '*', int $page = 0, int $limit = 0, string $order = '', array $with = [], bool $search = false)
  91. {
  92. if ($search) {
  93. $model = $this->search($where);
  94. } else {
  95. $model = $this->getModel()->where($where);
  96. }
  97. return $model->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  98. $query->page($page, $limit);
  99. })->when($order !== '', function ($query) use ($order) {
  100. $query->order($order);
  101. })->when($with, function ($query) use ($with) {
  102. $query->with($with);
  103. });
  104. }
  105. /**
  106. * 获取某些条件总数
  107. * @param array $where
  108. * @return int
  109. */
  110. public function getCount(array $where)
  111. {
  112. return $this->getModel()->where($where)->count();
  113. }
  114. /**
  115. * 获取某些条件去重总数
  116. * @param array $where
  117. * @param $field
  118. * @param bool $search
  119. * @return int|mixed
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function getDistinctCount(array $where, $field, bool $search = true)
  125. {
  126. if ($search) {
  127. return $this->search($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  128. } else {
  129. return $this->getModel()->where($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  130. }
  131. }
  132. /**
  133. * 获取模型
  134. * @return BaseModel
  135. */
  136. protected function getModel()
  137. {
  138. return app()->make($this->setModel());
  139. }
  140. /**
  141. * 获取主键
  142. * @return array|string
  143. */
  144. protected function getPk()
  145. {
  146. return $this->getModel()->getPk();
  147. }
  148. /**
  149. * @return string
  150. * @author 等风来
  151. * @email 136327134@qq.com
  152. * @date 2023/2/8
  153. */
  154. public function getTableName()
  155. {
  156. return $this->getModel()->getName();
  157. }
  158. /**
  159. * 获取一条数据
  160. * @param $id
  161. * @param array|null $field
  162. * @param array|null $with
  163. * @return array|Model|null
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function get($id, ?array $field = [], ?array $with = [])
  169. {
  170. if (is_array($id)) {
  171. $where = $id;
  172. } else {
  173. $where = [$this->getPk() => $id];
  174. }
  175. return $this->getModel()->where($where)->when(count($with), function ($query) use ($with) {
  176. $query->with($with);
  177. })->field($field ?? ['*'])->find();
  178. }
  179. /**
  180. * 查询一条数据是否存在
  181. * @param $map
  182. * @param string $field
  183. * @return bool 是否存在
  184. */
  185. public function be($map, string $field = '')
  186. {
  187. if (!is_array($map) && empty($field)) $field = $this->getPk();
  188. $map = !is_array($map) ? [$field => $map] : $map;
  189. return 0 < $this->getModel()->where($map)->count();
  190. }
  191. /**
  192. * 根据条件获取一条数据
  193. * @param array $where
  194. * @param string|null $field
  195. * @param array $with
  196. * @return array|Model|null
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function getOne(array $where, ?string $field = '*', array $with = [])
  202. {
  203. $field = explode(',', $field);
  204. return $this->get($where, $field, $with);
  205. }
  206. /**
  207. * 获取单个字段值
  208. * @param $where
  209. * @param string|null $field
  210. * @return mixed
  211. */
  212. public function value($where, ?string $field = '')
  213. {
  214. $pk = $this->getPk();
  215. return $this->search($this->setWhere($where))->value($field ?: $pk);
  216. }
  217. /**
  218. * 获取某个字段数组
  219. * @param array $where
  220. * @param string $field
  221. * @param string $key
  222. * @return array
  223. */
  224. public function getColumn(array $where, string $field, string $key = '')
  225. {
  226. return $this->getModel()->where($where)->column($field, $key);
  227. }
  228. /**
  229. * 删除
  230. * @param int|string|array $id
  231. * @return mixed
  232. */
  233. public function delete($id, ?string $key = null)
  234. {
  235. if (is_array($id)) {
  236. $where = $id;
  237. } else {
  238. $where = [is_null($key) ? $this->getPk() : $key => $id];
  239. }
  240. return $this->getModel()->where($where)->delete();
  241. }
  242. /**
  243. * 删除记录
  244. * @param int $id
  245. * @param bool $force
  246. * @return bool
  247. * @author 等风来
  248. * @email 136327134@qq.com
  249. * @date 2023/4/15
  250. */
  251. public function destroy(int $id, bool $force = false)
  252. {
  253. return $this->getModel()->destroy($id, $force);
  254. }
  255. /**
  256. * 更新数据
  257. * @param int|string|array $id
  258. * @param array $data
  259. * @param string|null $key
  260. * @return BaseModel
  261. */
  262. public function update($id, array $data, ?string $key = null)
  263. {
  264. if (is_array($id)) {
  265. $where = $id;
  266. } else {
  267. $where = [is_null($key) ? $this->getPk() : $key => $id];
  268. }
  269. return $this->getModel()::update($data, $where);
  270. }
  271. /**
  272. * @param $where
  273. * @return array|mixed
  274. * @author 等风来
  275. * @email 136327134@qq.com
  276. * @date 2023/4/6
  277. */
  278. protected function setWhere($where, ?string $key = null)
  279. {
  280. if (!is_array($where)) {
  281. $where = [is_null($key) ? $this->getPk() : $key => $where];
  282. }
  283. return $where;
  284. }
  285. /**
  286. * 批量更新数据
  287. * @param array $ids
  288. * @param array $data
  289. * @param string|null $key
  290. * @return BaseModel
  291. */
  292. public function batchUpdate(array $ids, array $data, ?string $key = null)
  293. {
  294. return $this->getModel()->whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  295. }
  296. /**
  297. * 插入数据
  298. * @param array $data
  299. * @return mixed
  300. */
  301. public function save(array $data)
  302. {
  303. return $this->getModel()::create($data);
  304. }
  305. /**
  306. * 插入数据
  307. * @param array $data
  308. * @return \think\Collection
  309. * @throws \Exception
  310. */
  311. public function saveAll(array $data)
  312. {
  313. return $this->getModel()->saveAll($data);
  314. }
  315. /**
  316. * 获取某个字段内的值
  317. * @param $value
  318. * @param string $filed
  319. * @param string|null $valueKey
  320. * @param array|string[] $where
  321. * @return mixed
  322. */
  323. public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
  324. {
  325. return $this->getModel()->getFieldValue($value, $filed, $valueKey, $where);
  326. }
  327. /**
  328. * 获取搜索器和搜索条件key,以及不在搜索器的条件数组
  329. * @param array $where
  330. * @return array[]
  331. * @throws \ReflectionException
  332. * @author 吴汐
  333. * @email 442384644@qq.com
  334. * @date 2023/03/18
  335. */
  336. private function getSearchData(array $where)
  337. {
  338. $with = [];
  339. $otherWhere = [];
  340. $responses = new \ReflectionClass($this->setModel());
  341. foreach ($where as $key => $value) {
  342. $method = 'search' . Str::studly($key) . 'Attr';
  343. if ($responses->hasMethod($method)) {
  344. $with[] = $key;
  345. } else {
  346. if (!in_array($key, ['timeKey', 'store_stock', 'integral_time'], true)) {
  347. if (!is_array($value)) {
  348. $otherWhere[] = [$key, '=', $value];
  349. } else if (count($value) === 3) {
  350. $otherWhere[] = $value;
  351. }
  352. }
  353. }
  354. }
  355. return [$with, $otherWhere];
  356. }
  357. /**
  358. * 根据搜索器获取搜索内容
  359. * @param $where
  360. * @param $search
  361. * @return BaseModel
  362. * @throws \ReflectionException
  363. * @author 吴汐
  364. * @email 442384644@qq.com
  365. * @date 2023/03/18
  366. */
  367. protected function withSearchSelect($where, $search)
  368. {
  369. [$with, $otherWhere] = $this->getSearchData($where);
  370. return $this->getModel()->withSearch($with, $where)->when($search, function ($query) use ($otherWhere) {
  371. $query->where($this->filterWhere($otherWhere));
  372. });
  373. }
  374. /**
  375. * 过滤数据表中不存在的where条件字段
  376. * @param array $where
  377. * @return array
  378. * @author 吴汐
  379. * @email 442384644@qq.com
  380. * @date 2023/04/11
  381. */
  382. protected function filterWhere(array $where = [])
  383. {
  384. $fields = $this->getModel()->getTableFields();
  385. foreach ($where as $key => $item) {
  386. if (!in_array($item[0], $fields)) {
  387. unset($where[$key]);
  388. }
  389. }
  390. return $where;
  391. }
  392. /**
  393. * 搜索
  394. * @param array $where
  395. * @param bool $search
  396. * @return BaseModel
  397. * @throws \ReflectionException
  398. * @author 吴汐
  399. * @email 442384644@qq.com
  400. * @date 2023/03/18
  401. */
  402. public function search(array $where = [], bool $search = true)
  403. {
  404. if ($where) {
  405. return $this->withSearchSelect($where, $search);
  406. } else {
  407. return $this->getModel();
  408. }
  409. }
  410. /**
  411. * 求和
  412. * @param array $where
  413. * @param string $field
  414. * @param bool $search
  415. * @return float
  416. * @throws \ReflectionException
  417. */
  418. public function sum(array $where, string $field, bool $search = false)
  419. {
  420. if ($search) {
  421. return $this->search($where)->sum($field);
  422. } else {
  423. return $this->getModel()->where($where)->sum($field);
  424. }
  425. }
  426. /**
  427. * 高精度加法
  428. * @param $key
  429. * @param string $incField
  430. * @param string $inc
  431. * @param string|null $keyField
  432. * @param int $acc
  433. * @return bool
  434. * @throws \think\db\exception\DataNotFoundException
  435. * @throws \think\db\exception\DbException
  436. * @throws \think\db\exception\ModelNotFoundException
  437. */
  438. public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
  439. {
  440. return $this->bc($key, $incField, $inc, $keyField, 1);
  441. }
  442. /**
  443. * 高精度 减法
  444. * @param $key
  445. * @param string $decField
  446. * @param string $dec
  447. * @param string|null $keyField
  448. * @param int $acc
  449. * @return bool
  450. * @throws \think\db\exception\DataNotFoundException
  451. * @throws \think\db\exception\DbException
  452. * @throws \think\db\exception\ModelNotFoundException
  453. */
  454. public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2)
  455. {
  456. return $this->bc($key, $decField, $dec, $keyField, 2);
  457. }
  458. /**
  459. * 高精度计算并保存
  460. * @param $key
  461. * @param string $incField
  462. * @param string $inc
  463. * @param string|null $keyField
  464. * @param int $type
  465. * @param int $acc
  466. * @return bool
  467. * @throws \think\db\exception\DataNotFoundException
  468. * @throws \think\db\exception\DbException
  469. * @throws \think\db\exception\ModelNotFoundException
  470. */
  471. public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2)
  472. {
  473. if ($keyField === null) {
  474. $result = $this->get($key);
  475. } else {
  476. $result = $this->getOne([$keyField => $key]);
  477. }
  478. if (!$result) return false;
  479. $new = 0;
  480. if ($type === 1) {
  481. $new = bcadd($result[$incField], $inc, $acc);
  482. } else if ($type === 2) {
  483. if ($result[$incField] < $inc) return false;
  484. $new = bcsub($result[$incField], $inc, $acc);
  485. }
  486. $result->{$incField} = $new;
  487. return false !== $result->save();
  488. }
  489. /**
  490. * 减库存加销量
  491. * @param array $where
  492. * @param int $num
  493. * @param string $stock
  494. * @param string $sales
  495. * @return false
  496. * @throws \think\db\exception\DataNotFoundException
  497. * @throws \think\db\exception\DbException
  498. * @throws \think\db\exception\ModelNotFoundException
  499. */
  500. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  501. {
  502. $isQuota = false;
  503. if (isset($where['type']) && $where['type']) {
  504. $isQuota = true;
  505. if (count($where) == 2) {
  506. unset($where['type']);
  507. }
  508. }
  509. $field = $isQuota ? 'stock,quota' : 'stock';
  510. $product = $this->getModel()->where($where)->field($field)->find();
  511. if ($product) {
  512. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  513. $query->dec('quota', $num);
  514. })->dec($stock, $num)->inc($sales, $num)->update();
  515. }
  516. return false;
  517. }
  518. /**
  519. * 加库存减销量
  520. * @param array $where
  521. * @param int $num
  522. * @param string $stock
  523. * @param string $sales
  524. * @return mixed
  525. */
  526. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  527. {
  528. $isQuota = false;
  529. if (isset($where['type']) && $where['type']) {
  530. $isQuota = true;
  531. if (count($where) == 2) {
  532. unset($where['type']);
  533. }
  534. }
  535. $salesOne = $this->getModel()->where($where)->value($sales);
  536. if ($salesOne) {
  537. $salesNum = $num;
  538. if ($num > $salesOne) {
  539. $salesNum = $salesOne;
  540. }
  541. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  542. $query->inc('quota', $num);
  543. })->inc($stock, $num)->dec($sales, $salesNum)->update();
  544. }
  545. return true;
  546. }
  547. /**
  548. * 获取条件数据中的某个值的最大值
  549. * @param array $where
  550. * @param string $field
  551. * @return mixed
  552. */
  553. public function getMax(array $where = [], string $field = '')
  554. {
  555. return $this->getModel()->where($where)->max($field);
  556. }
  557. /**
  558. * 获取条件数据中的某个值的最小值
  559. * @param array $where
  560. * @param string $field
  561. * @return mixed
  562. */
  563. public function getMin(array $where = [], string $field = '')
  564. {
  565. return $this->getModel()->where($where)->min($field);
  566. }
  567. /**
  568. * 获取(条件)按照(排序)的第一条
  569. * @param array $where
  570. * @param string $order
  571. * @return mixed
  572. * @throws \think\db\exception\DataNotFoundException
  573. * @throws \think\db\exception\DbException
  574. * @throws \think\db\exception\ModelNotFoundException
  575. * @author wuhaotian
  576. * @email 442384644@qq.com
  577. * @date 2024/9/12
  578. */
  579. public function getOrderOne(array $where = [], string $order = 'id desc')
  580. {
  581. return $this->getModel()->where($where)->order($order)->find();
  582. }
  583. /**
  584. * 插入数据并返回自增ID
  585. * @param array $data
  586. * @return int|string
  587. * @author wuhaotian
  588. * @email 442384644@qq.com
  589. * @date 2025/12/11
  590. */
  591. public function insertGetId(array $data)
  592. {
  593. return $this->getModel()->insertGetId($data);
  594. }
  595. }