BaseDao.php 14 KB

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