CommunityDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\community;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\community\Community;
  14. use app\common\repositories\system\RelevanceRepository;
  15. class CommunityDao extends BaseDao
  16. {
  17. protected function getModel(): string
  18. {
  19. return Community::class;
  20. }
  21. public function search(array $where)
  22. {
  23. $query = Community::hasWhere('author', function($query) use ($where){
  24. $query->when(isset($where['username']) && $where['username'] !== '', function ($query) use($where) {
  25. $query->whereLike('real_name|phone|nickname',"%{$where['username']}%");
  26. });
  27. $query->where(true);
  28. });
  29. $query
  30. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use($where) {
  31. $query->whereLike('Community.title',"%{$where['keyword']}%");
  32. })
  33. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use($where) {
  34. $query->where('Community.uid',$where['uid']);
  35. })
  36. ->when(isset($where['uids']) && $where['uids'] !== '', function ($query) use($where) {
  37. $query->whereIn('Community.uid',$where['uids']);
  38. })
  39. ->when(isset($where['topic_id']) && $where['topic_id'] !== '', function ($query) use($where) {
  40. $query->where('Community.topic_id',$where['topic_id']);
  41. })
  42. ->when(isset($where['community_id']) && $where['community_id'] !== '', function ($query) use($where) {
  43. $query->where('Community.community_id',$where['community_id']);
  44. })
  45. ->when(isset($where['not_id']) && $where['not_id'] !== '', function ($query) use($where) {
  46. $query->whereNotIn('Community.community_id',$where['not_id']);
  47. })
  48. ->when(isset($where['community_ids']) && $where['community_ids'] !== '', function ($query) use($where) {
  49. $query->whereIn('Community.community_id',$where['community_ids']);
  50. })
  51. ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use($where) {
  52. $query->whereIn('Community.is_type',$where['is_type']);
  53. })
  54. ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use($where) {
  55. $query->where('Community.is_show',$where['is_show']);
  56. })
  57. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
  58. $query->where('Community.status',$where['status']);
  59. })
  60. ->when(isset($where['start']) && $where['start'] !== '', function ($query) use($where) {
  61. $query->where('Community.start',$where['start']);
  62. })
  63. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
  64. $query->where('Community.is_del',$where['is_del']);
  65. })
  66. ->when(isset($where['category_id']) && $where['category_id'] !== '', function ($query) use($where) {
  67. $query->where('Community.category_id',$where['category_id']);
  68. });
  69. $order = 'Community.create_time DESC';
  70. if (isset($where['order']) && $where['order'] == 'start') {
  71. $order = 'Community.start DESC,Community.create_time DESC';
  72. }
  73. $query->order($order);
  74. return $query;
  75. }
  76. public function uidExists(int $id, int $uid)
  77. {
  78. return $this->getModel()::getDb()->where('uid',$uid)->where($this->getPk(),$id)->count() > 0;
  79. }
  80. public function exists(int $id)
  81. {
  82. return $this->getModel()::getDb()->where('is_del',0)->where($this->getPk(),$id)->count() > 0;
  83. }
  84. public function destoryByUid($uid)
  85. {
  86. return $this->getModel()::getDb()->where('uid' ,$uid)->update(['is_del' => 1]);
  87. }
  88. public function joinUser($where)
  89. {
  90. return Community::hasWhere('relevanceRight',function($query) use($where){
  91. $query->where('type',RelevanceRepository::TYPE_COMMUNITY_START)->where('left_id',$where['uid']);
  92. })
  93. ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use($where) {
  94. $query->whereIn('Community.is_type',$where['is_type']);
  95. })
  96. ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use($where) {
  97. $query->where('Community.is_show',$where['is_show']);
  98. })
  99. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
  100. $query->where('Community.status',$where['status']);
  101. })
  102. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
  103. $query->where('Community.is_del',$where['is_del']);
  104. });
  105. }
  106. }