AdminDao.php 2.8 KB

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