Comment.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/3
  6. * Time: 下午2:35
  7. */
  8. namespace app\frontend\modules\goods\models;
  9. use app\common\models\comment\CommentConfig;
  10. use Carbon\Carbon;
  11. class Comment extends \app\common\models\Comment
  12. {
  13. public $Append;
  14. protected $appends = ['append','type_name'];
  15. public static function getCommentsByGoods($goods_id,$withReply = true)
  16. {
  17. $top_sort = CommentConfig::getSetConfig('top_sort');
  18. $with = [
  19. 'hasOneMember' => function ($query) {
  20. $query->with(['yzMember' => function ($query2) {
  21. $query2->select('member_id', 'level_id');
  22. }])->select('uid');
  23. }
  24. ];
  25. if ($withReply) {
  26. $with['hasManyReply'] = function ($query) {
  27. return $query->where('type', 2)
  28. ->where('is_show', 1)
  29. ->orderBy('created_at', 'asc');
  30. };
  31. }
  32. if (!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('frontend_comment_with'))) {
  33. foreach ($event_arr as $v) {
  34. $class = array_get($v, 'class');
  35. $function = array_get($v, 'function');
  36. $res = $class::$function();
  37. foreach ($res as $vv) {
  38. $with[$vv['key']] = $vv['value'];
  39. }
  40. }
  41. }
  42. //FIELD 倒序才能让特殊排序(置顶)排前面,但是里面的顺序也会改变,因此需要反转一下
  43. if ($top_sort == 'desc') {
  44. $top_sort = 'asc';
  45. } else {
  46. $top_sort = 'desc';
  47. }
  48. $topCommentIds = self::uniacid()
  49. ->where('goods_id', $goods_id)
  50. ->where('comment_id', 0)
  51. ->where('is_top',1)
  52. ->orderBy('created_at',$top_sort)
  53. ->pluck('id');
  54. $model = self::select(
  55. 'id', 'order_id', 'goods_id', 'uid', 'nick_name', 'head_img_url', 'content', 'level',
  56. 'images', 'created_at', 'type', 'additional_comment_id','is_show','is_top','audit_status','has_default_good_reputation','order_goods_id','level_set')
  57. ->uniacid()
  58. ->with($with)
  59. ->where(function ($query) {
  60. $query->where('has_default_good_reputation',0)
  61. ->orWhere(function($query2){
  62. $query2->where('has_default_good_reputation',1)->where('additional_comment_id','!=',0);//默认好评的评价需要追评才显示
  63. });
  64. })
  65. ->where('audit_status','!=', self::wait_audit)//审核中不显示
  66. ->where('is_show', 1)
  67. ->where('goods_id', $goods_id)
  68. ->where('comment_id', 0);
  69. if ($topCommentIds->isNotEmpty()) {
  70. //特殊排序放最前,为了置顶功能
  71. $model->orderByRaw('FIELD(id, ' . implode(',', $topCommentIds->toArray()) . ') desc');
  72. }
  73. //正常的排序放后面
  74. return $model->orderBy('created_at', 'desc');
  75. }
  76. //获取所有评价数量(包括默认好评)
  77. public static function getAllCommentTotal($goods_id)
  78. {
  79. return self::uniacid()
  80. ->where('goods_id',$goods_id)
  81. ->where('comment_id',0)
  82. ->where('audit_status','!=', self::wait_audit)
  83. ->where('is_show', 1)
  84. ->count('id');
  85. }
  86. //前端’默认好评已隐藏‘字样 需要默认好评已追评不进行该好评字样判断的话就加上 ->where('additional_comment_id',0)
  87. public static function isShowGoodReputationText($goods_id)
  88. {
  89. return self::uniacid()
  90. ->selectRaw('1')
  91. ->where('goods_id',$goods_id)
  92. ->where('has_default_good_reputation',1)
  93. ->where('additional_comment_id',0)
  94. ->first();
  95. }
  96. public function getAppendAttribute()
  97. {
  98. if (!isset($this->Append)) {
  99. $this->Append = static::getAppendById($this->id);
  100. if ($this->additional_comment_id) {
  101. $this->Append->diff_date = self::diffDate($this->created_at->timestamp,$this->Append->created_at->timestamp);//有存在0天的情况
  102. }
  103. }
  104. return $this->Append;
  105. }
  106. public static function getAppendById($id)
  107. {
  108. return self::uniacid()
  109. ->select('content','images','comment_id','type','created_at')
  110. ->where('comment_id', $id)
  111. ->where('type', 3)
  112. ->where('audit_status', '!=', self::wait_audit)
  113. ->orderBy('created_at', 'asc')
  114. ->first();//todo type=3为追评,每条评论只能有一条追评,不需要get
  115. }
  116. public static function getOrderGoodsComment()
  117. {
  118. return self::uniacid()
  119. ->with(['hasManyReply'=>function ($query) {
  120. return $query->where('type', 2)
  121. ->where('is_show',1)
  122. ->orderBy('created_at', 'asc');
  123. }]);
  124. }
  125. public function hasOneOrderGoods()
  126. {
  127. return $this->hasOne('app\common\models\OrderGoods', 'order_id', 'order_id');
  128. }
  129. public function hasOneGoods(){
  130. return $this->hasOne('app\common\models\Goods','id','goods_id');
  131. }
  132. public function hasOneLiveInstallComment(){
  133. return $this->hasOne(\Yunshop\LiveInstall\models\Comment::class,'comment_id','id');
  134. }
  135. public function diffDate(int $commentTime,int $additionalCommentTime)
  136. {
  137. $old_time = Carbon::parse(date('Y-m-d H:i:s',$commentTime));
  138. $new_time = Carbon::createFromTimestamp($additionalCommentTime);
  139. $dateDiff = [
  140. $old_time->diffInDays($new_time),//日
  141. $old_time->diffInMonths($new_time),//月
  142. $old_time->diffInYears($new_time),//年
  143. ];
  144. if ($dateDiff[2] != 0) {
  145. $res = [
  146. 'value' => $dateDiff[2],
  147. 'sku' => '年后'
  148. ];
  149. } else if($dateDiff[1] != 0) {
  150. $res = [
  151. 'value' => $dateDiff[1],
  152. 'sku' => '个月后'
  153. ];
  154. } else {
  155. $res = [
  156. 'value' => $dateDiff[0],
  157. 'sku' => '天后'
  158. ];
  159. }
  160. return $res;
  161. }
  162. }