BaseDao.php 15 KB

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