join('order_goods OG', 'O.id = OG.order_id') ->where(['shop_id' => $shopId, 'is_comment' => 1]) ->field('OG.id') ->select() ->toArray(); $orderGoodsIds = array_column($orderGoodsIds, 'id'); $all_count = GoodsComment::where('order_goods_id', 'in', $orderGoodsIds)->count(); $image_count = GoodsComment::alias('gc')->where('order_goods_id', 'in', $orderGoodsIds)->join('goods_comment_image gci', 'gc.id = gci.comment_id')->group('gci.comment_id')->count(); $good_count = GoodsComment::where('order_goods_id', 'in', $orderGoodsIds)->where('service_comment', '>', 3)->count(); $medium_bad_count = GoodsComment::where('order_goods_id', 'in', $orderGoodsIds)->where('service_comment', '<=', 3)->count(); if ($all_count == 0) { $percentStr = '100%'; $star = 5; } else { $percent = round((($good_count / $all_count) * 100)); $percentStr = round((($good_count / $all_count) * 100)) . '%'; if ($percent >= 100) { $star = 5; } else if ($percent >= 80) { $star = 4; } else if ($percent >= 60) { $star = 3; } else if ($percent >= 40) { $star = 2; } else if ($percent >= 20) { $star = 1; } else { $star = 0; } } return ['comment' => [ [ 'id' => 0, 'name' => '全部', 'count' => $all_count ], [ 'id' => 1, 'name' => '有图', 'count' => $image_count ], [ 'id' => 2, 'name' => '好评', 'count' => $good_count ], [ 'id' => 3, 'name' => '中差评', 'count' => $medium_bad_count ] ], 'percent' => $percentStr, 'star' => $star, ]; } }