BaseDao.php 16 KB

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