BaseDao.php 14 KB

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