BaseDao.php 14 KB

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