AdminDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\common\dao\system\admin;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\admin\Admin;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Model;
  20. class AdminDao extends BaseDao
  21. {
  22. /**
  23. * @return BaseModel
  24. * @author xaboy
  25. * @day 2020-03-30
  26. */
  27. protected function getModel(): string
  28. {
  29. return Admin::class;
  30. }
  31. /**
  32. * @param array $where
  33. * @return BaseQuery
  34. * @author xaboy
  35. * @day 2020-04-09
  36. */
  37. public function search(array $where = [], $is_del = 0,$level = true)
  38. {
  39. $query = Admin::getDB();
  40. if($level) $query->where('level', '<>', 0);
  41. $query->when($is_del !== null, function ($query) use ($is_del) {
  42. $query->where('is_del', $is_del);
  43. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  44. getModelTime($query, $where['date']);
  45. });
  46. if (isset($where['keyword']) && $where['keyword'] !== '') {
  47. $query = $query->whereLike('real_name|account', '%' . $where['keyword'] . '%');
  48. }
  49. if (isset($where['status']) && $where['status'] !== '') {
  50. $query = $query->where('status', intval($where['status']));
  51. }
  52. return $query;
  53. }
  54. public function exists(int $id)
  55. {
  56. $query = ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_del', 0);
  57. return $query->count() > 0;
  58. }
  59. /**
  60. * @param int $id
  61. * @return array|Model|null
  62. * @throws DataNotFoundException
  63. * @throws DbException
  64. * @throws ModelNotFoundException
  65. * @author xaboy
  66. * @day 2020-04-09
  67. */
  68. public function get( $id)
  69. {
  70. return Admin::getInstance()->where('is_del', 0)->find($id);
  71. }
  72. /**
  73. * @param $field
  74. * @param $value
  75. * @param int|null $except
  76. * @return bool
  77. * @author xaboy
  78. * @day 2020-03-30
  79. */
  80. public function fieldExists($field, $value, ?int $except = null): bool
  81. {
  82. $query = ($this->getModel())::getDB()->where($field, $value)->where('is_del', 0);
  83. if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
  84. return $query->count() > 0;
  85. }
  86. /**
  87. * @param string $account
  88. * @return array|Model|null
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. * @author xaboy
  93. * @day 2020-04-09
  94. */
  95. public function accountByAdmin(string $account)
  96. {
  97. return Admin::getInstance()->where('account', $account)
  98. ->where('is_del', 0)
  99. ->field(['account', 'pwd', 'real_name', 'login_count', 'admin_id', 'status'])
  100. ->find();
  101. }
  102. }