BaseDao.php 13 KB

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