BaseDao.php 12 KB

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