Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

2 роки тому
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace crmeb\traits;
  3. use app\dao\BaseDao;
  4. use crmeb\basic\BaseAuth;
  5. /**
  6. * Trait SearchDaoTrait
  7. * @mixin BaseDao
  8. */
  9. trait SearchDaoTrait
  10. {
  11. /**
  12. * 搜索(没有进入搜索器的自动进入where条件)
  13. * @param array $where
  14. * @param bool $authWhere
  15. */
  16. public function searchWhere(array $where, bool $authWhere = true)
  17. {
  18. [$with, $whereKey] = app()->make(BaseAuth::class)->________(array_keys($where), $this->setModel());
  19. $whereData = [];
  20. foreach ($whereKey as $key) {
  21. if (isset($where[$key]) && 'timeKey' !== $key) {
  22. $whereData[$key] = $where[$key];
  23. }
  24. }
  25. return $this->getModel()->withSearch($with, $where)->when($authWhere && $whereData, function ($query) use ($whereData) {
  26. $query->where($whereData);
  27. });
  28. }
  29. /**
  30. * @param array $where
  31. * @param bool $authWhere
  32. * @return int
  33. */
  34. public function count(array $where = [], bool $authWhere = true): int
  35. {
  36. return $this->searchWhere($where, $authWhere)->count();
  37. }
  38. /**
  39. * 搜索
  40. * @param array $where
  41. * @param array|string[] $field
  42. * @param int $page
  43. * @param int $limit
  44. * @param null $sort
  45. * @param array $with
  46. * @return array
  47. */
  48. public function getDataList(array $where, array $field = ['*'], int $page = 0, int $limit = 0, $sort = null, array $with = [])
  49. {
  50. return $this->searchWhere($where)->when($page && $limit, function ($query) use ($page, $limit) {
  51. $query->page($page, $limit);
  52. })->when(!$page && $limit, function ($query) use ($limit) {
  53. $query->limit($limit);
  54. })->when($sort, function ($query, $sort) {
  55. if (is_array($sort)) {
  56. foreach ($sort as $k => $v) {
  57. if (is_numeric($k)) {
  58. $query->order($v, 'desc');
  59. } else {
  60. $query->order($k, $v);
  61. }
  62. }
  63. } else {
  64. $query->order($sort, 'desc');
  65. }
  66. })->field($field)->with($with)->select()->toArray();
  67. }
  68. }