BaseDao.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 $where
  270. * @return array[]
  271. * @throws \ReflectionException
  272. * @author 吴汐
  273. * @email 442384644@qq.com
  274. * @date 2023/03/18
  275. */
  276. private function getSearchData(array $where)
  277. {
  278. $with = [];
  279. $otherWhere = [];
  280. $responses = new \ReflectionClass($this->setModel());
  281. foreach ($where as $key => $value) {
  282. $method = 'search' . Str::studly($key) . 'Attr';
  283. if ($responses->hasMethod($method)) {
  284. $with[] = $key;
  285. } else {
  286. $otherWhere[] = $value;
  287. }
  288. }
  289. return [$with, $otherWhere];
  290. }
  291. /**
  292. * 根据搜索器获取搜索内容
  293. * @param $where
  294. * @return BaseModel
  295. * @throws \ReflectionException
  296. * @author 吴汐
  297. * @email 442384644@qq.com
  298. * @date 2023/03/18
  299. */
  300. protected function withSearchSelect($where)
  301. {
  302. [$with, $otherWhere] = $this->getSearchData($where);
  303. return $this->getModel()->withSearch($with, $where)->where($otherWhere);
  304. }
  305. /**
  306. * 搜索
  307. * @param array $where
  308. * @return BaseModel
  309. * @throws \ReflectionException
  310. * @author 吴汐
  311. * @email 442384644@qq.com
  312. * @date 2023/03/18
  313. */
  314. protected function search(array $where = [])
  315. {
  316. if ($where) {
  317. return $this->withSearchSelect($where);
  318. } else {
  319. return $this->getModel();
  320. }
  321. }
  322. /**
  323. * 求和
  324. * @param array $where
  325. * @param string $field
  326. * @param bool $search
  327. * @return float
  328. * @throws \ReflectionException
  329. */
  330. public function sum(array $where, string $field, bool $search = false)
  331. {
  332. if ($search) {
  333. return $this->search($where)->sum($field);
  334. } else {
  335. return $this->getModel()->where($where)->sum($field);
  336. }
  337. }
  338. /**
  339. * 高精度加法
  340. * @param $key
  341. * @param string $incField
  342. * @param string $inc
  343. * @param string|null $keyField
  344. * @param int $acc
  345. * @return bool
  346. */
  347. public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
  348. {
  349. return $this->bc($key, $incField, $inc, $keyField, 1);
  350. }
  351. /**
  352. * 高精度 减法
  353. * @param $key
  354. * @param string $decField
  355. * @param string $dec
  356. * @param string|null $keyField
  357. * @param int $acc
  358. * @return bool
  359. */
  360. public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2)
  361. {
  362. return $this->bc($key, $decField, $dec, $keyField, 2);
  363. }
  364. /**
  365. * 高精度计算并保存
  366. * @param $key
  367. * @param string $incField
  368. * @param string $inc
  369. * @param string|null $keyField
  370. * @param int $type
  371. * @param int $acc
  372. * @return bool
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2)
  378. {
  379. if ($keyField === null) {
  380. $result = $this->get($key);
  381. } else {
  382. $result = $this->getOne([$keyField => $key]);
  383. }
  384. if (!$result) return false;
  385. $new = 0;
  386. if ($type === 1) {
  387. $new = bcadd($result[$incField], $inc, $acc);
  388. } else if ($type === 2) {
  389. if ($result[$incField] < $inc) return false;
  390. $new = bcsub($result[$incField], $inc, $acc);
  391. }
  392. $result->{$incField} = $new;
  393. return false !== $result->save();
  394. }
  395. /**
  396. * 减库存加销量
  397. * @param array $where
  398. * @param int $num
  399. * @param string $stock
  400. * @param string $sales
  401. * @return false
  402. * @throws \think\db\exception\DataNotFoundException
  403. * @throws \think\db\exception\DbException
  404. * @throws \think\db\exception\ModelNotFoundException
  405. */
  406. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  407. {
  408. $isQuota = false;
  409. if (isset($where['type']) && $where['type']) {
  410. $isQuota = true;
  411. if (count($where) == 2) {
  412. unset($where['type']);
  413. }
  414. }
  415. $field = $isQuota ? 'stock,quota' : 'stock';
  416. $product = $this->getModel()->where($where)->field($field)->find();
  417. if ($product) {
  418. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  419. $query->dec('quota', $num);
  420. })->dec($stock, $num)->inc($sales, $num)->update();
  421. }
  422. return false;
  423. // $field = $isQuota ? [$stock, $sales, 'quota'] : [$stock, $sales];
  424. // $info = $this->getModel()->where($where)->field($field)->find();
  425. // if ($info) {
  426. // if ($isQuota) {
  427. // $info->quota = (int)$info->quota - $num;
  428. // }
  429. // $info->stock = (int)$info->stock - $num;
  430. // $info->sales = (int)$info->sales + $num;
  431. // return $info->save();
  432. // } else {
  433. // return false;
  434. // }
  435. }
  436. /**
  437. * 加库存减销量
  438. * @param array $where
  439. * @param int $num
  440. * @param string $stock
  441. * @param string $sales
  442. * @return mixed
  443. */
  444. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  445. {
  446. $isQuota = false;
  447. if (isset($where['type']) && $where['type']) {
  448. $isQuota = true;
  449. if (count($where) == 2) {
  450. unset($where['type']);
  451. }
  452. }
  453. $salesOne = $this->getModel()->where($where)->value($sales);
  454. if ($salesOne) {
  455. $salesNum = $num;
  456. if ($num > $salesOne) {
  457. $salesNum = $salesOne;
  458. }
  459. return $this->getModel()->where($where)->when($isQuota, function ($query) use ($num) {
  460. $query->inc('quota', $num);
  461. })->inc($stock, $num)->dec($sales, $salesNum)->update();
  462. }
  463. return true;
  464. // $field = $isQuota ? [$stock, $sales, 'quota'] : [$stock, $sales];
  465. // $info = $this->getModel()->where($where)->field($field)->find();
  466. // if ($info) {
  467. // if ($isQuota) {
  468. // $info->quota = (int)$info->quota + $num;
  469. // }
  470. // $info->stock = (int)$info->stock + $num;
  471. // if ((int)$info->sales > $num) {
  472. // $info->sales = (int)$info->sales - $num;
  473. // } else {
  474. // $info->sales = 0;
  475. // }
  476. // return $info->save();
  477. // } else {
  478. // return false;
  479. // }
  480. }
  481. /**
  482. * 获取条件数据中的某个值的最大值
  483. * @param array $where
  484. * @param string $field
  485. * @return mixed
  486. */
  487. public function getMax(array $where = [], string $field = '')
  488. {
  489. return $this->getModel()->where($where)->max($field);
  490. }
  491. /**
  492. * 获取条件数据中的某个值的最小值
  493. * @param array $where
  494. * @param string $field
  495. * @return mixed
  496. */
  497. public function getMin(array $where = [], string $field = '')
  498. {
  499. return $this->getModel()->where($where)->min($field);
  500. }
  501. }