BaseDao.php 15 KB

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