AdminDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 根据条件搜索管理员信息。
  33. *
  34. * 方法通过懒加载方式构建查询条件,只有在需要时才添加相应的查询条件,提高了查询的灵活性和性能。
  35. *
  36. * @param array $where 查询条件数组,包含各种可能的搜索条件。
  37. * @param int $is_del 是否已删除的标志,0表示未删除,非0表示已删除。
  38. * @param bool $level 是否考虑级别条件,true表示只查询级别不为0的记录。
  39. * @return BaseQuery 查询构建器实例,可用于进一步的查询操作或数据获取。
  40. */
  41. public function search(array $where = [], $is_del = 0,$level = true)
  42. {
  43. // 获取管理员表的数据库查询构建器
  44. $query = Admin::getDB();
  45. // 如果考虑级别条件,添加级别不为0的查询条件
  46. if($level) $query->where('level', '<>', 0);
  47. // 如果指定了删除状态,添加相应的查询条件
  48. $query->when($is_del !== null, function ($query) use ($is_del) {
  49. $query->where('is_del', $is_del);
  50. });
  51. // 如果指定了日期范围,添加日期范围的查询条件
  52. $query->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  53. getModelTime($query, $where['date']);
  54. });
  55. // 如果指定了关键字,添加关键字的模糊查询条件
  56. if (isset($where['keyword']) && $where['keyword'] !== '') {
  57. $query->whereLike('real_name|account', '%' . $where['keyword'] . '%');
  58. }
  59. // 如果指定了状态,添加状态的查询条件
  60. if (isset($where['status']) && $where['status'] !== '') {
  61. $query->where('status', intval($where['status']));
  62. }
  63. if (isset($where['region_id']) && $where['region_id'] !== '') {
  64. $query->whereLike('region_ids', "%,{$where['region_id']},%");
  65. }
  66. // 返回构建好的查询构建器实例
  67. return $query;
  68. }
  69. /**
  70. * 检查指定ID的记录是否存在且未被删除。
  71. *
  72. * 本函数通过查询数据库来确定给定ID的记录是否存在,并且其删除标志位是否为0(即未被删除)。
  73. * 主要用于在执行删除、更新等操作前验证记录的有效性,避免错误的操作导致数据丢失或错误。
  74. *
  75. * @param int $id 要检查的记录的唯一标识ID。
  76. * @return bool 如果记录存在且未被删除,则返回true;否则返回false。
  77. */
  78. public function exists(int $id)
  79. {
  80. // 构建查询条件,查询指定ID且未被删除的记录
  81. $query = ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_del', 0);
  82. // 判断查询结果的数量是否大于0,如果大于0则表示记录存在
  83. return $query->count() > 0;
  84. }
  85. /**
  86. * 根据ID获取管理员信息
  87. *
  88. * 本函数用于通过管理员的ID,从数据库中查询并返回对应的管理员信息。
  89. * 特别注意,这里只返回未被删除(is_del字段为0)的管理员信息。
  90. *
  91. * @param int $id 管理员的唯一标识ID
  92. * @return array|false 返回管理员的信息数组,如果找不到则返回false。
  93. */
  94. public function get( $id)
  95. {
  96. // 通过Admin类的单例模式获取管理员实例,然后查询数据库中is_del为0且ID为$id的管理员信息
  97. return Admin::getInstance()->where('is_del', 0)->find($id);
  98. }
  99. /**
  100. * 检查指定字段是否存在指定值(且未被删除)。
  101. *
  102. * 此方法用于查询数据库中是否存在指定字段的值,并且该记录没有被删除。
  103. * 可以通过传递一个排除ID来排除特定的记录。
  104. *
  105. * @param string $field 要检查的字段名。
  106. * @param mixed $value 字段应该具有的值。
  107. * @param int|null $except 排除的ID,可选参数,用于排除特定的记录。
  108. * @return bool 如果找到符合条件的记录,则返回true,否则返回false。
  109. */
  110. public function fieldExists($field, $value, ?int $except = null): bool
  111. {
  112. // 初始化查询,查询具有指定字段值并且未被删除的记录
  113. $query = ($this->getModel())::getDB()->where($field, $value)->where('is_del', 0);
  114. // 如果提供了排除ID,则添加条件以排除该ID的记录
  115. if (!is_null($except)) {
  116. $query->where($this->getPk(), '<>', $except);
  117. }
  118. // 返回查询结果是否存在,即记录数是否大于0
  119. return $query->count() > 0;
  120. }
  121. /**
  122. * 通过管理员账号获取管理员信息
  123. *
  124. * 本函数用于查询并返回与指定管理员账号相关联的信息。它通过管理员账号查找管理员数据库记录,
  125. * 并返回包含管理员账号、密码、真实姓名、登录次数、管理员ID和状态等信息的对象。
  126. * 这样做的目的是为了提供一种方式来验证管理员身份或获取管理员的详细信息。
  127. *
  128. * @param string $account 管理员账号
  129. * @return array|null 包含管理员信息的数组,如果找不到管理员则返回null
  130. */
  131. public function accountByAdmin(string $account)
  132. {
  133. // 通过管理员类的单例获取管理员实例
  134. return Admin::getInstance()->where('account', $account)
  135. // 确保查询的管理员未被删除
  136. ->where('is_del', 0)
  137. // 指定查询的字段,以减少不必要的数据返回
  138. ->field(['account', 'pwd', 'real_name', 'login_count', 'admin_id', 'status'])
  139. // 执行查询并返回结果
  140. ->find();
  141. }
  142. }