BaseDao.php 13 KB

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