count(); $image_count = GoodsComment::alias('gc')->where('goods_id', $parmas['goods_id'])->join('goods_comment_image gci', 'gc.id = gci.comment_id')->group('gci.comment_id')->count(); $good_count = GoodsComment::where('goods_id', $parmas['goods_id'])->where('service_comment','>',3)->count(); $medium_bad_count = GoodsComment::where('goods_id', $parmas['goods_id'])->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, ]; } /** * @notes 评价服务信息 * @param $params * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author ljj * @date 2022/2/21 6:12 下午 */ public function commentGoodsInfo($params) { $info = GoodsComment::alias('GC') ->where(['order_goods_id'=>$params['order_goods_id'],'user_id'=>$params['user_id']]) ->join('user U','U.id = GC.user_id') ->append(['goods_comment_image']) ->field('U.nickname,U.avatar,GC.id,GC.service_comment,GC.comment,GC.reply,GC.create_time') ->findOrEmpty() ->toArray(); $info['avatar'] = FileService::getFileUrl($info['avatar']); return $info; } /** * @notes 添加服务评价 * @param $params * @return bool * @author ljj * @date 2022/2/21 6:23 下午 */ public function add($params) { // 启动事务 Db::startTrans(); try { //获取订单商品信息 $order_goods = OrderGoods::where(['id'=>$params['order_goods_id']])->findOrEmpty(); $order = Order::where(['id'=>$order_goods['order_id']])->findOrEmpty(); //添加评价数据 $goods_comment = GoodsComment::create([ 'goods_id' => $order_goods['goods_id'], 'user_id' => $params['user_id'], 'coach_id' => $order['coach_id'], 'shop_id' => $order['shop_id'], 'order_goods_id' => $order_goods['id'], 'service_comment' => $params['service_comment'], 'comment' => $params['comment'] ?? '', ]); //添加评价图片数据 if (isset($params['image'])) { $image_data = []; foreach ($params['image'] as $val) { $image_data[] = [ 'comment_id' => $goods_comment->id, 'uri' => $val, ]; } $goods_comment_image = new GoodsCommentImage; $goods_comment_image->saveAll($image_data); } //技师的好评 \app\common\logic\CoachLogic::updateCoachComment($order['coach_id']); if($order['shop_id']){ \app\common\logic\ShopLogic::updateShopComment($order['shop_id']); } //修改订单商品表评价状态 OrderGoods::update(['is_comment' => 1], ['id' => $order_goods['id']]); // 提交事务 Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 Db::rollback(); self::$error = $e->getMessage(); return false; } } /** * @notes 评价详情 * @param $params * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author ljj * @date 2024/7/31 下午5:37 */ public function commentDetail($params) { $info = GoodsComment::alias('GC') ->where(['order_goods_id'=>$params['id'],'user_id'=>$params['user_id']]) ->join('user U','U.id = GC.user_id') ->append(['goods_comment_image']) ->field('U.nickname,U.avatar,GC.id,GC.service_comment,GC.comment,GC.reply,GC.create_time') ->findOrEmpty() ->toArray(); $info['avatar'] = FileService::getFileUrl($info['avatar']); return $info; } }