BaseDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao;
  12. use crmeb\basic\BaseAuth;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\dao\CacheDaoTrait;
  15. use think\Model;
  16. /**
  17. * Class BaseDao
  18. * @package app\dao
  19. */
  20. abstract class BaseDao
  21. {
  22. use CacheDaoTrait;
  23. /**
  24. * 当前表名别名
  25. * @var string
  26. */
  27. protected $alias;
  28. /**
  29. * join表别名
  30. * @var string
  31. */
  32. protected $joinAlis;
  33. /**
  34. * 获取当前模型
  35. * @return string
  36. */
  37. abstract protected function setModel(): string;
  38. /**
  39. * 设置join链表模型
  40. * @return string
  41. */
  42. protected function setJoinModel(): string
  43. {
  44. }
  45. /**
  46. * 读取数据条数
  47. * @param array $where
  48. * @return int
  49. */
  50. public function count(array $where = []): int
  51. {
  52. return $this->search($where)->count();
  53. }
  54. /**
  55. * 获取某些条件总数
  56. * @param array $where
  57. * @return int
  58. */
  59. public function getCount(array $where, $search = false)
  60. {
  61. if ($search) {
  62. return $this->search($where)->count();
  63. } else
  64. return $this->getModel()->where($where)->count();
  65. }
  66. /**
  67. * 获取某些条件去重总数
  68. * @param array $where
  69. * @param $field
  70. * @param $search
  71. */
  72. public function getDistinctCount(array $where, $field, $search = true)
  73. {
  74. if ($search) {
  75. return $this->search($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  76. } else {
  77. return $this->getModel()->where($where)->field('COUNT(distinct(' . $field . ')) as count')->select()->toArray()[0]['count'] ?? 0;
  78. }
  79. }
  80. /**
  81. * 获取模型
  82. * @return BaseModel
  83. */
  84. protected function getModel()
  85. {
  86. return app()->make($this->setModel());
  87. }
  88. /**
  89. * 获取主键
  90. * @return mixed
  91. */
  92. protected function getPk()
  93. {
  94. return $this->getModel()->getPk();
  95. }
  96. /**
  97. * 获取一条数据
  98. * @param int|array $id
  99. * @param array|null $field
  100. * @param array|null $with
  101. * @return array|Model|null
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function get($id, ?array $field = [], ?array $with = [])
  107. {
  108. if (is_array($id)) {
  109. $where = $id;
  110. } else {
  111. $where = [$this->getPk() => $id];
  112. }
  113. return $this->getModel()::where($where)->when(count($with), function ($query) use ($with) {
  114. $query->with($with);
  115. })->field($field ?? ['*'])->find();
  116. }
  117. /**
  118. * 查询一条数据是否存在
  119. * @param $map
  120. * @param string $field
  121. * @return bool 是否存在
  122. */
  123. public function be($map, string $field = '')
  124. {
  125. if (!is_array($map) && empty($field)) $field = $this->getPk();
  126. $map = !is_array($map) ? [$field => $map] : $map;
  127. return 0 < $this->getModel()->where($map)->count();
  128. }
  129. /**
  130. * 根据条件获取一条数据
  131. * @param array $where
  132. * @param string|null $field
  133. * @param array $with
  134. * @return array|Model|null
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public function getOne(array $where, ?string $field = '*', array $with = [])
  140. {
  141. $field = explode(',', $field);
  142. return $this->get($where, $field, $with);
  143. }
  144. /**
  145. * 获取单个字段值
  146. * @param array $where
  147. * @param string|null $field
  148. * @return mixed
  149. */
  150. public function value(array $where, ?string $field = '')
  151. {
  152. $pk = $this->getPk();
  153. return $this->getModel()::where($where)->value($field ?: $pk);
  154. }
  155. /**
  156. * 获取某个字段数组
  157. * @param array $where
  158. * @param string $field
  159. * @param string $key
  160. * @param bool $search
  161. * @return array
  162. */
  163. public function getColumn(array $where, string $field, string $key = '', bool $search = false)
  164. {
  165. if ($search) {
  166. return $this->search($where)->column($field, $key);
  167. } else {
  168. return $this->getModel()::where($where)->column($field, $key);
  169. }
  170. }
  171. /**
  172. * 删除
  173. * @param int|string|array $id
  174. * @return mixed
  175. */
  176. public function delete($id, ?string $key = null)
  177. {
  178. if (is_array($id)) {
  179. $where = $id;
  180. } else {
  181. $where = [is_null($key) ? $this->getPk() : $key => $id];
  182. }
  183. return $this->getModel()::where($where)->delete();
  184. }
  185. /**
  186. * 更新数据
  187. * @param int|string|array $id
  188. * @param array $data
  189. * @param string|null $key
  190. * @return mixed
  191. */
  192. public function update($id, array $data, ?string $key = null)
  193. {
  194. if (is_array($id)) {
  195. $where = $id;
  196. } else {
  197. $where = [is_null($key) ? $this->getPk() : $key => $id];
  198. }
  199. return $this->getModel()::update($data, $where);
  200. }
  201. /**
  202. * 批量更新数据
  203. * @param array $ids
  204. * @param array $data
  205. * @param string|null $key
  206. * @return BaseModel
  207. */
  208. public function batchUpdate(array $ids, array $data, ?string $key = null)
  209. {
  210. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  211. }
  212. /**
  213. * 批量更新数据,增加条件
  214. * @param array $ids
  215. * @param array $data
  216. * @param array $where
  217. * @param string|null $key
  218. * @return BaseModel
  219. */
  220. public function batchUpdateAppendWhere(array $ids, array $data, array $where, ?string $key = null)
  221. {
  222. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->where($where)->update($data);
  223. }
  224. /**
  225. * 插入数据
  226. * @param array $data
  227. * @return BaseModel|Model
  228. */
  229. public function save(array $data)
  230. {
  231. return $this->getModel()::create($data);
  232. }
  233. /**
  234. * 插入数据
  235. * @param array $data
  236. * @return mixed
  237. */
  238. public function saveAll(array $data)
  239. {
  240. return $this->getModel()::insertAll($data);
  241. }
  242. /**
  243. * 获取某个字段内的值
  244. * @param $value
  245. * @param string $filed
  246. * @param string $valueKey
  247. * @param array|string[] $where
  248. * @return mixed
  249. */
  250. public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])
  251. {
  252. return $this->getModel()->getFieldValue($value, $filed, $valueKey, $where);
  253. }
  254. /**
  255. * 根据搜索器获取搜索内容
  256. * @param array $withSearch
  257. * @param array|null $data
  258. * @return Model
  259. */
  260. protected function withSearchSelect(array $withSearch, ?array $data = [])
  261. {
  262. [$with] = app()->make(BaseAuth::class)->________($withSearch, $this->setModel());
  263. return $this->getModel()->withSearch($with, $data);
  264. }
  265. /**
  266. * 搜索
  267. * @param array $where
  268. * @return \crmeb\basic\BaseModel|mixed
  269. */
  270. protected function search(array $where = [])
  271. {
  272. if ($where) {
  273. return $this->withSearchSelect(array_keys($where), $where);
  274. } else {
  275. return $this->getModel();
  276. }
  277. }
  278. /**
  279. * 求和
  280. * @param array $where
  281. * @param string $field
  282. * @param bool $search
  283. * @return float
  284. */
  285. public function sum(array $where, string $field, bool $search = false)
  286. {
  287. if ($search) {
  288. return $this->search($where)->sum($field);
  289. } else {
  290. return $this->getModel()::where($where)->sum($field);
  291. }
  292. }
  293. /**
  294. * 高精度加法
  295. * @param int|string $key
  296. * @param string $incField
  297. * @param string $inc
  298. * @param string|null $keyField
  299. * @param int $acc
  300. * @return bool
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)
  306. {
  307. return $this->bc($key, $incField, $inc, $keyField, 1);
  308. }
  309. /**
  310. * 高精度 减法
  311. * @param int|string $uid id
  312. * @param string $decField 相减的字段
  313. * @param float|int $dec 减的值
  314. * @param string $keyField id的字段
  315. * @param bool $minus 是否可以为负数
  316. * @param int $acc 精度
  317. * @param bool $dec_return_false 减法 不够减是否返回false || 减为0
  318. * @return bool
  319. */
  320. public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2, bool $dec_return_false = true)
  321. {
  322. return $this->bc($key, $decField, $dec, $keyField, 2, $acc, $dec_return_false);
  323. }
  324. /**
  325. * 高精度计算并保存
  326. * @param $key
  327. * @param string $incField
  328. * @param string $inc
  329. * @param string|null $keyField
  330. * @param int $type
  331. * @param int $acc
  332. * @param bool $dec_return_false 减法 不够减是否返回false || 减为0
  333. * @return bool
  334. * @throws \think\db\exception\DataNotFoundException
  335. * @throws \think\db\exception\DbException
  336. * @throws \think\db\exception\ModelNotFoundException
  337. */
  338. public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2, bool $dec_return_false = true)
  339. {
  340. if ($keyField === null) {
  341. $result = $this->get($key);
  342. } else {
  343. $result = $this->getOne([$keyField => $key]);
  344. }
  345. if (!$result) return false;
  346. if ($type === 1) {
  347. $new = bcadd($result[$incField], $inc, $acc);
  348. } else if ($type === 2) {
  349. if ($result[$incField] < $inc) {
  350. if ($dec_return_false) return false;
  351. $new = 0;
  352. } else {
  353. $new = bcsub($result[$incField], $inc, $acc);
  354. }
  355. }
  356. $result->{$incField} = $new;
  357. return false !== $result->save();
  358. }
  359. /**
  360. * 减库存加销量
  361. * @param array $where
  362. * @param int $num
  363. * @return mixed
  364. */
  365. public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  366. {
  367. return app()->make(BaseAuth::class)->_____($this->getModel(), $where, $num, $stock, $sales) !== false;
  368. }
  369. /**
  370. * 加库存减销量
  371. * @param array $where
  372. * @param int $num
  373. * @return mixed
  374. */
  375. public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')
  376. {
  377. return app()->make(BaseAuth::class)->___($this->getModel(), $where, $num, $stock, $sales) !== false;
  378. }
  379. /**
  380. * 软删除
  381. * @param $id
  382. * @param string|null $key
  383. * @return bool
  384. */
  385. public function destroy($id, ?string $key = null)
  386. {
  387. if (is_array($id)) {
  388. $where = $id;
  389. } else {
  390. $where = [is_null($key) ? $this->getPk() : $key => $id];
  391. }
  392. return $this->getModel()::destroy($where);
  393. }
  394. /**
  395. * 自增单个数据
  396. * @param $where
  397. * @param string $field
  398. * @param int $number
  399. * @return mixed
  400. */
  401. public function incUpdate($where, string $field, int $number = 1)
  402. {
  403. return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->inc($field, $number)->update();
  404. }
  405. /**
  406. * 自减单个数据
  407. * @param $where
  408. * @param string $field
  409. * @param int $number
  410. * @return mixed
  411. */
  412. public function decUpdate($where, string $field, int $number = 1)
  413. {
  414. return $this->getModel()->where(is_array($where) ? $where : [$this->getPk() => $where])->dec($field, $number)->update();
  415. }
  416. }