CommunityDao.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\community;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\community\Community;
  14. use app\common\model\system\Relevance;
  15. use app\common\repositories\system\RelevanceRepository;
  16. class CommunityDao extends BaseDao
  17. {
  18. /**
  19. * @return Community
  20. *
  21. * @date 2023/10/21
  22. * @author yyw
  23. */
  24. protected function getModel(): string
  25. {
  26. return Community::class;
  27. }
  28. /**
  29. * 搜索社区帖子
  30. * @param array $where
  31. * @return \think\db\BaseQuery
  32. * @author wuhaotian
  33. * @email 442384644@qq.com
  34. * @date 2024/7/12
  35. */
  36. public function search(array $where)
  37. {
  38. $query = Community::hasWhere('author', function($query) use ($where){
  39. $query->when(isset($where['username']) && $where['username'] !== '', function ($query) use($where) {
  40. $query->whereLike('real_name|phone|nickname',"%{$where['username']}%");
  41. })
  42. ->when(isset($where['phone']) && $where['phone'] !== '', function ($query) use($where) {
  43. $query->whereLike('phone',"%{$where['phone']}%");
  44. })
  45. ->when(isset($where['real_name']) && $where['real_name'] !== '', function ($query) use($where) {
  46. $query->whereLike('real_name',"%{$where['real_name']}%");
  47. })
  48. ->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use($where) {
  49. $query->whereLike('nickname',"%{$where['nickname']}%");
  50. });
  51. $query->where(true);
  52. });
  53. $query->when(isset($where['search_type']) && $where['search_type'] !== '', function ($query) use ($where) {
  54. if(isset($where['keyword']) && $where['keyword'] !== ''){
  55. if($where['search_type'] == 'all'){
  56. $query->whereLike('Community.title|Community.content|User.nickname',"%{$where['keyword']}%");
  57. }
  58. if($where['search_type'] == 'content'){
  59. $query->whereLike('Community.title|Community.content',"%{$where['keyword']}%");
  60. }
  61. if($where['search_type'] == 'user'){
  62. $query->whereLike('User.nickname',"%{$where['keyword']}%");
  63. }
  64. }
  65. },function ($query) use ($where) { // 兼容之前逻辑
  66. if(isset($where['keyword']) && $where['keyword'] !== ''){
  67. $query->whereLike('Community.title',"%{$where['keyword']}%");
  68. }
  69. })
  70. // ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use($where) {
  71. // $query->whereLike('Community.title',"%{$where['keyword']}%");
  72. // })
  73. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use($where) {
  74. $query->where('Community.uid',$where['uid']);
  75. })
  76. ->when(isset($where['uids']) && $where['uids'] !== '', function ($query) use($where) {
  77. $query->whereIn('Community.uid',$where['uids']);
  78. })
  79. ->when(isset($where['topic_id']) && $where['topic_id'] !== '', function ($query) use($where) {
  80. $query->where('Community.topic_id',$where['topic_id']);
  81. })
  82. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use($where) {
  83. $query->where('Community.mer_id',$where['mer_id']);
  84. })
  85. ->when(isset($where['community_id']) && $where['community_id'] !== '', function ($query) use($where) {
  86. $query->where('Community.community_id',$where['community_id']);
  87. })
  88. ->when(isset($where['not_id']) && $where['not_id'] !== '', function ($query) use($where) {
  89. $query->whereNotIn('Community.community_id',$where['not_id']);
  90. })
  91. ->when(isset($where['in_id']) && $where['in_id'] !== '', function ($query) use($where) {
  92. $query->whereOr(function($query) use($where){
  93. $query->whereIn('Community.community_id',$where['in_id']);
  94. });
  95. })
  96. ->when(isset($where['community_ids']) && $where['community_ids'] !== '', function ($query) use($where) {
  97. $query->whereIn('Community.community_id',$where['community_ids']);
  98. })
  99. ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use($where) {
  100. $query->whereIn('Community.is_type',$where['is_type']);
  101. })
  102. ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use($where) {
  103. $query->where('Community.is_show',$where['is_show']);
  104. })
  105. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
  106. $query->where('Community.status',$where['status']);
  107. })
  108. ->when(isset($where['start']) && $where['start'] !== '', function ($query) use($where) {
  109. $query->where('Community.start',$where['start']);
  110. })
  111. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
  112. $query->where('Community.is_del',$where['is_del']);
  113. })
  114. ->when(isset($where['category_id']) && $where['category_id'] !== '', function ($query) use($where) {
  115. $query->where('Community.category_id',$where['category_id']);
  116. })
  117. ->when(isset($where['spu_id']) && $where['spu_id'] !== '', function ($query) use($where) {
  118. $id = Relevance::where('right_id', $where['spu_id'])
  119. ->where('type',RelevanceRepository::TYPE_COMMUNITY_PRODUCT)
  120. ->column('left_id');
  121. $query->where('community_id','in', $id);
  122. });
  123. $order = 'Community.create_time DESC';
  124. if (isset($where['order']) && $where['order'] == 'start') {
  125. $order = 'Community.start DESC,Community.create_time DESC';
  126. }
  127. $query->order($order);
  128. return $query;
  129. }
  130. /**
  131. * 查询用户是否发过帖子
  132. * @param int $id
  133. * @param int $uid
  134. * @return bool
  135. * @throws \think\db\exception\DbException
  136. * @author wuhaotian
  137. * @email 442384644@qq.com
  138. * @date 2024/7/12
  139. */
  140. public function uidExists(int $id, int $uid)
  141. {
  142. return $this->getModel()::getDb()->where('uid',$uid)->where($this->getPk(),$id)->count() > 0;
  143. }
  144. /**
  145. * id查询帖子是否存在
  146. * @param int $id
  147. * @return bool
  148. * @throws \think\db\exception\DbException
  149. * @author wuhaotian
  150. * @email 442384644@qq.com
  151. * @date 2024/7/12
  152. */
  153. public function exists(int $id)
  154. {
  155. return $this->getModel()::getDb()->where('is_del',0)->where($this->getPk(),$id)->count() > 0;
  156. }
  157. /**
  158. * 删除某个用户的帖子
  159. * @param $uid
  160. * @return int
  161. * @throws \think\db\exception\DbException
  162. * @author wuhaotian
  163. * @email 442384644@qq.com
  164. * @date 2024/7/12
  165. */
  166. public function destoryByUid($uid)
  167. {
  168. return $this->getModel()::getDb()->where('uid' ,$uid)->update(['is_del' => 1]);
  169. }
  170. /**
  171. * 关联用户
  172. * @param $where
  173. * @return \think\db\BaseQuery
  174. * @author wuhaotian
  175. * @email 442384644@qq.com
  176. * @date 2024/7/12
  177. */
  178. public function joinUser($where)
  179. {
  180. return Community::hasWhere('relevanceRight',function($query) use($where){
  181. $query->where('type',RelevanceRepository::TYPE_COMMUNITY_START)->where('left_id',$where['uid']);
  182. })
  183. ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use($where) {
  184. $query->whereIn('Community.is_type',$where['is_type']);
  185. })
  186. ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use($where) {
  187. $query->where('Community.is_show',$where['is_show']);
  188. })
  189. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
  190. $query->where('Community.status',$where['status']);
  191. })
  192. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
  193. $query->where('Community.is_del',$where['is_del']);
  194. });
  195. }
  196. /**
  197. * 统计每个用户的帖子数量
  198. * @return mixed
  199. *
  200. * @date 2023/10/21
  201. * @author yyw
  202. */
  203. public function getCountByGroupUid()
  204. {
  205. return $this->getModel()::getDb()->where('is_del', 0)->field('uid,count(community_id) as count')->group('uid')->select()->toArray();
  206. }
  207. public function isApprove(int $id)
  208. {
  209. return $this->getModel()::getDb()->where('is_del',0)->where('status',1)->where($this->getPk(),$id)->count() > 0;
  210. }
  211. }