| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/3/3
- * Time: 下午2:35
- */
- namespace app\frontend\modules\goods\models;
- use app\common\models\comment\CommentConfig;
- use Carbon\Carbon;
- class Comment extends \app\common\models\Comment
- {
- public $Append;
- protected $appends = ['append','type_name'];
- public static function getCommentsByGoods($goods_id,$withReply = true)
- {
- $top_sort = CommentConfig::getSetConfig('top_sort');
- $with = [
- 'hasOneMember' => function ($query) {
- $query->with(['yzMember' => function ($query2) {
- $query2->select('member_id', 'level_id');
- }])->select('uid');
- }
- ];
- if ($withReply) {
- $with['hasManyReply'] = function ($query) {
- return $query->where('type', 2)
- ->where('is_show', 1)
- ->orderBy('created_at', 'asc');
- };
- }
- if (!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('frontend_comment_with'))) {
- foreach ($event_arr as $v) {
- $class = array_get($v, 'class');
- $function = array_get($v, 'function');
- $res = $class::$function();
- foreach ($res as $vv) {
- $with[$vv['key']] = $vv['value'];
- }
- }
- }
- //FIELD 倒序才能让特殊排序(置顶)排前面,但是里面的顺序也会改变,因此需要反转一下
- if ($top_sort == 'desc') {
- $top_sort = 'asc';
- } else {
- $top_sort = 'desc';
- }
- $topCommentIds = self::uniacid()
- ->where('goods_id', $goods_id)
- ->where('comment_id', 0)
- ->where('is_top',1)
- ->orderBy('created_at',$top_sort)
- ->pluck('id');
- $model = self::select(
- 'id', 'order_id', 'goods_id', 'uid', 'nick_name', 'head_img_url', 'content', 'level',
- 'images', 'created_at', 'type', 'additional_comment_id','is_show','is_top','audit_status','has_default_good_reputation','order_goods_id','level_set')
- ->uniacid()
- ->with($with)
- ->where(function ($query) {
- $query->where('has_default_good_reputation',0)
- ->orWhere(function($query2){
- $query2->where('has_default_good_reputation',1)->where('additional_comment_id','!=',0);//默认好评的评价需要追评才显示
- });
- })
- ->where('audit_status','!=', self::wait_audit)//审核中不显示
- ->where('is_show', 1)
- ->where('goods_id', $goods_id)
- ->where('comment_id', 0);
- if ($topCommentIds->isNotEmpty()) {
- //特殊排序放最前,为了置顶功能
- $model->orderByRaw('FIELD(id, ' . implode(',', $topCommentIds->toArray()) . ') desc');
- }
- //正常的排序放后面
- return $model->orderBy('created_at', 'desc');
- }
- //获取所有评价数量(包括默认好评)
- public static function getAllCommentTotal($goods_id)
- {
- return self::uniacid()
- ->where('goods_id',$goods_id)
- ->where('comment_id',0)
- ->where('audit_status','!=', self::wait_audit)
- ->where('is_show', 1)
- ->count('id');
- }
- //前端’默认好评已隐藏‘字样 需要默认好评已追评不进行该好评字样判断的话就加上 ->where('additional_comment_id',0)
- public static function isShowGoodReputationText($goods_id)
- {
- return self::uniacid()
- ->selectRaw('1')
- ->where('goods_id',$goods_id)
- ->where('has_default_good_reputation',1)
- ->where('additional_comment_id',0)
- ->first();
- }
- public function getAppendAttribute()
- {
- if (!isset($this->Append)) {
- $this->Append = static::getAppendById($this->id);
- if ($this->additional_comment_id) {
- $this->Append->diff_date = self::diffDate($this->created_at->timestamp,$this->Append->created_at->timestamp);//有存在0天的情况
- }
- }
- return $this->Append;
- }
- public static function getAppendById($id)
- {
- return self::uniacid()
- ->select('content','images','comment_id','type','created_at')
- ->where('comment_id', $id)
- ->where('type', 3)
- ->where('audit_status', '!=', self::wait_audit)
- ->orderBy('created_at', 'asc')
- ->first();//todo type=3为追评,每条评论只能有一条追评,不需要get
- }
- public static function getOrderGoodsComment()
- {
- return self::uniacid()
- ->with(['hasManyReply'=>function ($query) {
- return $query->where('type', 2)
- ->where('is_show',1)
- ->orderBy('created_at', 'asc');
- }]);
- }
- public function hasOneOrderGoods()
- {
- return $this->hasOne('app\common\models\OrderGoods', 'order_id', 'order_id');
- }
- public function hasOneGoods(){
- return $this->hasOne('app\common\models\Goods','id','goods_id');
- }
- public function hasOneLiveInstallComment(){
- return $this->hasOne(\Yunshop\LiveInstall\models\Comment::class,'comment_id','id');
- }
- public function diffDate(int $commentTime,int $additionalCommentTime)
- {
- $old_time = Carbon::parse(date('Y-m-d H:i:s',$commentTime));
- $new_time = Carbon::createFromTimestamp($additionalCommentTime);
- $dateDiff = [
- $old_time->diffInDays($new_time),//日
- $old_time->diffInMonths($new_time),//月
- $old_time->diffInYears($new_time),//年
- ];
- if ($dateDiff[2] != 0) {
- $res = [
- 'value' => $dateDiff[2],
- 'sku' => '年后'
- ];
- } else if($dateDiff[1] != 0) {
- $res = [
- 'value' => $dateDiff[1],
- 'sku' => '个月后'
- ];
- } else {
- $res = [
- 'value' => $dateDiff[0],
- 'sku' => '天后'
- ];
- }
- return $res;
- }
- }
|