BaseDao.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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|string|array $id
  236. * @param array $data
  237. * @param string|null $key
  238. * @return BaseModel
  239. */
  240. public function update($id, array $data, ?string $key = null)
  241. {
  242. if (is_array($id)) {
  243. $where = $id;
  244. } else {
  245. $where = [is_null($key) ? $this->getPk() : $key => $id];
  246. }
  247. return $this->getModel()::update($data, $where);
  248. }
  249. /**
  250. * @param $where
  251. * @return array|mixed
  252. * @author 等风来
  253. * @email 136327134@qq.com
  254. * @date 2023/4/6
  255. */
  256. protected function setWhere($where, ?string $key = null)
  257. {
  258. if (!is_array($where)) {
  259. $where = [is_null($key) ? $this->getPk() : $key => $where];
  260. }
  261. return $where;
  262. }
  263. /**
  264. * 批量更新数据
  265. * @param array $ids
  266. * @param array $data
  267. * @param string|null $key
  268. * @return BaseModel
  269. */
  270. public function batchUpdate(array $ids, array $data, ?string $key = null)
  271. {
  272. return $this->getModel()->whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  273. }
  274. /**
  275. * 插入数据
  276. * @param array $data
  277. * @return mixed
  278. */
  279. public function save(array $data)
  280. {
  281. return $this->getModel()::create($data);
  282. }
  283. /**
  284. * 插入数据
  285. * @param array $data
  286. * @return \think\Collection
  287. * @throws \Exception
  288. */
  289. public function saveAll(array $data)
  290. {
  291. return $this->getModel()->saveAll($data);
  292. }
  293. /**
  294. * 获取某个字段内的值
  295. * @param $value
  296. * @param string $filed
  297. * @param string|null $valueKey
  298. * @param array|string[] $where
  299. * @return mixed
  300. */
  301. public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
  302. {
  303. return $this->getModel()->getFieldValue($value, $filed, $valueKey, $where);
  304. }
  305. /**
  306. * 获取搜索器和搜索条件key,以及不在搜索器的条件数组
  307. * @param array $where
  308. * @return array[]
  309. * @throws \ReflectionException
  310. * @author 吴汐
  311. * @email 442384644@qq.com
  312. * @date 2023/03/18
  313. */
  314. private function getSearchData(array $where)
  315. {
  316. $with = [];
  317. $otherWhere = [];
  318. $responses = new \ReflectionClass($this->setModel());
  319. foreach ($where as $key => $value) {
  320. $method = 'search' . Str::studly($key) . 'Attr';
  321. if ($responses->hasMethod($method)) {
  322. $with[] = $key;
  323. } else {
  324. if (!in_array($key, ['timeKey', 'store_stock', 'integral_time'])) {
  325. if (!is_array($value)) {
  326. $otherWhere[] = [$key, '=', $value];
  327. } else if (count($value) === 3) {
  328. $otherWhere[] = $value;
  329. }
  330. }
  331. }
  332. }
  333. return [$with, $otherWhere];
  334. }
  335. /**
  336. * 根据搜索器获取搜索内容
  337. * @param $where
  338. * @param $search
  339. * @return BaseModel
  340. * @throws \ReflectionException
  341. * @author 吴汐
  342. * @email 442384644@qq.com
  343. * @date 2023/03/18
  344. */
  345. protected function withSearchSelect($where, $search)
  346. {
  347. [$with, $otherWhere] = $this->getSearchData($where);
  348. return $this->getModel()->withSearch($with, $where)->when($search, function ($query) use ($otherWhere) {
  349. $query->where($this->filterWhere($otherWhere));
  350. });
  351. }
  352. /**
  353. * 过滤数据表中不存在的where条件字段
  354. * @param array $where
  355. * @return array
  356. * @author 吴汐
  357. * @email 442384644@qq.com
  358. * @date 2023/04/11
  359. */
  360. protected function filterWhere(array $where = [])
  361. {
  362. $fields = $this->getModel()->getTableFields();
  363. foreach ($where as $key => $item) {
  364. if (!in_array($item[0], $fields)) {
  365. unset($where[$key]);
  366. }
  367. }
  368. return $where;
  369. }
  370. /**
  371. * 搜索
  372. * @param array $where
  373. * @param bool $search
  374. * @return BaseModel
  375. * @throws \ReflectionException
  376. * @author 吴汐
  377. * @email 442384644@qq.com
  378. * @date 2023/03/18
  379. */
  380. public function search(array $where = [], bool $search = true)
  381. {
  382. if ($where) {
  383. return $this->withSearchSelect($where, $search);
  384. } else {
  385. return $this->getModel();
  386. }
  387. }
  388. /**
  389. * 求和
  390. * @param array $where
  391. * @param string $field
  392. * @param bool $search
  393. * @return float
  394. * @throws \ReflectionException
  395. */
  396. public function sum(array $where, string $field, bool $search = false)
  397. {
  398. if ($search) {
  399. return $this->search($where)->sum($field);
  400. } else {
  401. return $this->getModel()->where($where)->sum($field);
  402. }
  403. }
  404. /**
  405. * 高精度加法
  406. * @param $key
  407. * @param string $incField
  408. * @param string $inc
  409. * @param string|null $keyField
  410. * @param int $acc
  411. * @return bool
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\DbException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
  417. {
  418. return $this->bc($key, $incField, $inc, $keyField, 1);
  419. }
  420. /**
  421. * 高精度 减法
  422. * @param $key
  423. * @param string $decField
  424. * @param string $dec
  425. * @param string|null $keyField
  426. * @param int $acc
  427. * @return bool
  428. * @throws \think\db\exception\DataNotFoundException
  429. * @throws \think\db\exception\DbException
  430. * @throws \think\db\exception\ModelNotFoundException
  431. */
  432. public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2)
  433. {
  434. return $this->bc($key, $decField, $dec, $keyField, 2);
  435. }
  436. /**
  437. * 高精度计算并保存
  438. * @param $key
  439. * @param string $incField
  440. * @param string $inc
  441. * @param string|null $keyField
  442. * @param int $type
  443. * @param int $acc
  444. * @return bool
  445. * @throws \think\db\exception\DataNotFoundException
  446. * @throws \think\db\exception\DbException
  447. * @throws \think\db\exception\ModelNotFoundException
  448. */
  449. public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2)
  450. {
  451. if ($keyField === null) {
  452. $result = $this->get($key);
  453. } else {
  454. $result = $this->getOne([$keyField => $key]);
  455. }
  456. if (!$result) return false;
  457. $new = 0;
  458. if ($type === 1) {
  459. $new = bcadd($result[$incField], $inc, $acc);
  460. } else if ($type === 2) {
  461. if ($result[$incField] < $inc) return false;
  462. $new = bcsub($result[$incField], $inc, $acc);
  463. }
  464. $result->{$incField} = $new;
  465. return false !== $result->save();
  466. }
  467. /**
  468. * 减库存加销量
  469. * @param array $where
  470. * @param int $num
  471. * @param string $stock
  472. * @param string $sales
  473. * @return false
  474. * @throws \think\db\exception\DataNotFoundException
  475. * @throws \think\db\exception\DbException
  476. * @throws \think\db\exception\ModelNotFoundException
  477. */
  478. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  479. {
  480. $isQuota = false;
  481. if (isset($where['type']) && $where['type']) {
  482. $isQuota = true;
  483. if (count($where) == 2) {
  484. unset($where['type']);
  485. }
  486. }
  487. $field = $isQuota ? 'stock,quota' : 'stock';
  488. $product = $this->getModel()->where($where)->field($field)->find();
  489. if ($product) {
  490. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  491. $query->dec('quota', $num);
  492. })->dec($stock, $num)->inc($sales, $num)->update();
  493. }
  494. return false;
  495. }
  496. /**
  497. * 加库存减销量
  498. * @param array $where
  499. * @param int $num
  500. * @param string $stock
  501. * @param string $sales
  502. * @return mixed
  503. */
  504. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  505. {
  506. $isQuota = false;
  507. if (isset($where['type']) && $where['type']) {
  508. $isQuota = true;
  509. if (count($where) == 2) {
  510. unset($where['type']);
  511. }
  512. }
  513. $salesOne = $this->getModel()->where($where)->value($sales);
  514. if ($salesOne) {
  515. $salesNum = $num;
  516. if ($num > $salesOne) {
  517. $salesNum = $salesOne;
  518. }
  519. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  520. $query->inc('quota', $num);
  521. })->inc($stock, $num)->dec($sales, $salesNum)->update();
  522. }
  523. return true;
  524. }
  525. /**
  526. * 获取条件数据中的某个值的最大值
  527. * @param array $where
  528. * @param string $field
  529. * @return mixed
  530. */
  531. public function getMax(array $where = [], string $field = '')
  532. {
  533. return $this->getModel()->where($where)->max($field);
  534. }
  535. /**
  536. * 获取条件数据中的某个值的最小值
  537. * @param array $where
  538. * @param string $field
  539. * @return mixed
  540. */
  541. public function getMin(array $where = [], string $field = '')
  542. {
  543. return $this->getModel()->where($where)->min($field);
  544. }
  545. }