BaseDao.php 15 KB

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