RelevanceDao.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\system\Relevance;
  14. use app\common\repositories\system\RelevanceRepository;
  15. class RelevanceDao extends BaseDao
  16. {
  17. protected function getModel(): string
  18. {
  19. return Relevance::class;
  20. }
  21. /**
  22. * 清空特定字段中指定ID和类型的记录。
  23. *
  24. * 此方法用于根据给定的ID和类型,从数据库中删除满足条件的记录。
  25. * 主要用于需要清理或重置数据库中某些特定类型数据的场景。
  26. *
  27. * @param int $id 需要清理的记录的ID。
  28. * @param mixed $type 需要清理的记录的类型,可以是单个类型名称或类型名称的数组。
  29. * @param string $field 指定的字段名称,默认为'left_id',表示根据该字段进行查询和删除操作。
  30. * @return int 返回删除操作影响的行数。
  31. */
  32. public function clear(int $id, $type, string $field = 'left_id')
  33. {
  34. // 如果$type是字符串,则将其转换为数组,以支持多个类型同时清理。
  35. if (is_string($type)) $type = [$type];
  36. // 调用getModel方法获取模型实例,并通过该实例的getDb方法获取数据库连接。
  37. // 然后使用where和whereIn方法构建查询条件,最后执行删除操作并返回结果。
  38. return $this->getModel()::getDb()->where($field, $id)->whereIn('type', $type)->delete();
  39. }
  40. /**
  41. * 根据条件查询用户加入的社区
  42. *
  43. * 本函数用于构建一个查询用户所加入社区的查询语句。它首先筛选出状态为正常、显示为是、未删除的社区,
  44. * 然后根据用户ID和特定的类型条件来进一步限制查询结果。
  45. *
  46. * @param array $where 查询条件数组
  47. * @return \Illuminate\Database\Eloquent\Builder|static 返回构建好的查询构建器实例
  48. */
  49. public function joinUser($where)
  50. {
  51. // 初始化查询社区的构建器,并设置基本的查询条件
  52. $query = Relevance::hasWhere('community',function($query) use($where){
  53. // 筛选状态正常、显示为是、未删除的社区
  54. $query->where('status',1)->where('is_show',1)->where('is_del',0);
  55. // 如果条件中指定了社区类型,则进一步筛选特定类型的社区
  56. $query->when(isset($where['is_type']) && $where['is_type'] !== '',function($query) use($where){
  57. $query->where('is_type',$where['is_type']);
  58. });
  59. });
  60. // 设置查询条件,筛选出与用户ID关联且类型符合的关联记录
  61. $query->where('left_id',$where['uid'])->where('type',RelevanceRepository::TYPE_COMMUNITY_START);
  62. // 返回构建好的查询构建器
  63. return $query;
  64. }
  65. }