AdminDao.php 2.8 KB

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