Community.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\model\community;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\product\Spu;
  14. use app\common\model\system\Relevance;
  15. use app\common\model\user\User;
  16. use app\common\repositories\system\RelevanceRepository;
  17. class Community extends BaseModel
  18. {
  19. /**
  20. * TODO
  21. * @return string
  22. * @author Qinii
  23. * @day 10/26/21
  24. */
  25. public static function tablePk(): string
  26. {
  27. return 'community_id';
  28. }
  29. /**
  30. * TODO
  31. * @return string
  32. * @author Qinii
  33. * @day 10/26/21
  34. */
  35. public static function tableName(): string
  36. {
  37. return 'community';
  38. }
  39. public function getImageAttr($value)
  40. {
  41. return explode(',',$value);
  42. }
  43. public function author()
  44. {
  45. return $this->hasOne(User::class,'uid','uid');
  46. }
  47. public function topic()
  48. {
  49. return $this->hasOne(CommunityTopic::class,'topic_id','topic_id');
  50. }
  51. public function reply()
  52. {
  53. return $this->hasMany(CommunityReply::class,'community_id','community_id');
  54. }
  55. public function relevance()
  56. {
  57. return $this->hasMany(Relevance::class, 'left_id','community_id')
  58. ->where('type',RelevanceRepository::TYPE_COMMUNITY_PRODUCT);
  59. }
  60. /*
  61. * 右侧为内容ID的
  62. */
  63. public function relevanceRight()
  64. {
  65. return $this->hasMany(Relevance::class, 'right_id','community_id');
  66. }
  67. public function isStart()
  68. {
  69. return $this->hasOne(Relevance::class, 'right_id','community_id')->where('type', RelevanceRepository::TYPE_COMMUNITY_START)->bind(['relevance_id']);
  70. }
  71. public function isFans()
  72. {
  73. return $this->hasOne(Relevance::class, 'right_id','uid')->where('type', RelevanceRepository::TYPE_COMMUNITY_FANS)->bind(['is_fans' => 'right_id']);
  74. }
  75. public function category()
  76. {
  77. return $this->hasOne(CommunityCategory::class, 'category_id','category_id');
  78. }
  79. public function getTimeAttr()
  80. {
  81. return date('m月d日',strtotime($this->create_time));
  82. }
  83. public function getCountReplyAttr()
  84. {
  85. return CommunityReply::where('community_id',$this->community_id)->where('status',1)->count();
  86. }
  87. public function searchTopicIdAttr($query, $value)
  88. {
  89. $query->where('topic_id', $value);
  90. }
  91. public function searchTitleAttr($query, $value)
  92. {
  93. $query->whereLike('title', "%{$value}%");
  94. }
  95. public function searchKeywordAttr($query, $value)
  96. {
  97. $query->whereLike('title|content', "%{$value}%");
  98. }
  99. public function searchCategoryIdAttr($query, $value)
  100. {
  101. $query->where('category_id', $value);
  102. }
  103. public function searchIsShowAttr($query, $value)
  104. {
  105. $query->where('is_show', $value);
  106. }
  107. public function searchStatusAttr($query, $value)
  108. {
  109. $query->where('status', $value);
  110. }
  111. public function searchUidAttr($query, $value)
  112. {
  113. $query->where('uid', $value);
  114. }
  115. public function searchIsHotAttr($query, $value)
  116. {
  117. $query->where('is_hot', $value);
  118. }
  119. public function searchUidsAttr($query, $value)
  120. {
  121. $query->whereIn('uid', $value);
  122. }
  123. public function searchSpuIdAttr($query, $value)
  124. {
  125. $id = Relevance::where('right_id',$value)
  126. ->where('type',RelevanceRepository::TYPE_COMMUNITY_PRODUCT)
  127. ->column('left_id');
  128. $query->where('community_id','in', $id);
  129. }
  130. public function searchIsTypeAttr($query, $value)
  131. {
  132. $query->whereIn('is_type', $value);
  133. }
  134. public function searchCommunityIdAttr($query, $value)
  135. {
  136. $query->where('community_id', $value);
  137. }
  138. public function searchNotIdAttr($query, $value)
  139. {
  140. $query->where('community_id', '<>',$value);
  141. }
  142. }