CommentEnum::EVALUATE_STATUS_APPROVED, 'status' => StatusEnum::ENABLED ]; if ($goods_id > 0) { $where['goods_id'] = $goods_id; } if ($pid !== '') { $where['pid'] = $pid; } // 添加有图评论过滤 if ($has_picture !== '') { $where['has_picture'] = $has_picture; } $order = $orderBy == 'rand' ? 'rand()' : (in_array($orderBy, ['pid', 'id', 'createtime', 'updatetime']) ? "{$orderBy} {$orderWay}" : "id {$orderWay}"); $list = Comment::with(['user', 'reply' => function ($query) { $query->with(['manage' => function ($user) { $user->field('id,nickname'); }]); }]) ->where($where) ->order($order) ->paginate($pageSize, false, ['page' => $page]); return $list; } /** * 获取商品好评度 * @param int $goods_id 商品ID * @return float */ public static function degree($goods_id) { $total = Comment::where('goods_id', $goods_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->sum('star'); $favorable = Comment::where('goods_id', $goods_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->where('star', '>', 3) ->sum('star'); if (!$total || !$favorable) { return 100; } return bcmul(bcdiv($favorable, $total, 2), 100); } /** * 获取用户评论列表 * @param int $user_id 用户ID * @param int $page 页码 * @param int $pagesize 每页数量 * @return \think\Paginator */ public static function getUserCommentList($user_id, $page = 1, $pagesize = 10) { $list = Comment::with([ 'Goods' => function ($query) { $query->field('id,title,image'); } ]) ->where('user_id', $user_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->order('createtime desc') ->paginate($pagesize, false, ['page' => $page]); return $list; } /** * 添加评论 * @param int $user_id 用户ID * @param int $order_id 订单ID * @param array $remark 评论内容 * @param int $pid 父评论ID * @return bool * @throws Exception */ public static function addComment($user_id, $order_id, $remark, $pid = 0) { // 验证订单 $order = Order::with(['OrderGoods']) ->where('id', $order_id) ->where('order_status', OrderEnum::STATUS_CONFIRM) ->where('user_id', $user_id) ->find(); if (!$order) { throw new BusinessException('未找到可评论的订单'); } // 检查是否已评论 $existComment = Comment::where('user_id', $user_id) ->where('order_id', $order->id) ->find(); if ($existComment) { throw new BusinessException('订单已评论'); } // 获取可评价的商品 $goods_ids = []; foreach ($order->order_goods as $item) { $goods_ids[] = $item['goods_id']; } // 验证评论数据并组装 $data = []; foreach ($remark as $item) { if (!in_array($item['goods_id'], $goods_ids)) { throw new BusinessException('存在不可评价的商品'); } // 处理图片 $images = isset($item['images']) ? $item['images'] : []; $has_picture = !empty($images) ? CommentEnum::HAS_PICTURE_YES : CommentEnum::HAS_PICTURE_NO; $data[] = [ 'pid' => $pid, 'order_id' => $order['id'], 'user_id' => $user_id, 'goods_id' => $item['goods_id'], 'star' => $item['star'], 'content' => $item['content'], 'images' => is_array($images) ? implode(',', $images) : $images, 'has_picture' => $has_picture, 'ip' => request()->ip(), 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255), 'evaluate_status' => CommentEnum::EVALUATE_STATUS_PENDING, ]; } // 事务处理 Db::startTrans(); try { // 批量添加评论 $comment = new Comment(); $comment->saveAll($data); // 更新订单状态 $order->order_status = OrderEnum::STATUS_COMMENT; $order->save(); // 更新订单商品评论状态 foreach ($order->order_goods as $item) { $item->save(['comment_status' => 1]); } // 添加订单日志 OrderAction::push($order->order_sn, '系统', '订单已完成'); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw new BusinessException('添加评论失败:' . $e->getMessage()); } } /** * 批量保存评论(原方法保留) * @param array $data 评论数据 * @return bool */ public static function saveComments($data) { $comment = new Comment(); return $comment->saveAll($data); } /** * 审核评论 * @param int $comment_id 评论ID * @param int $evaluate_status 评价状态 * @return bool * @throws Exception */ public static function auditComment($comment_id, $evaluate_status) { if (!CommentEnum::isValidEvaluateStatus($evaluate_status)) { throw new BusinessException('评价状态参数错误'); } $comment = Comment::where('id', $comment_id)->find(); if (!$comment) { throw new BusinessException('评论不存在'); } $status = $evaluate_status == CommentEnum::EVALUATE_STATUS_APPROVED ? CommentEnum::STATUS_NORMAL : CommentEnum::STATUS_HIDDEN; return $comment->save([ 'evaluate_status' => $evaluate_status, 'status' => $status ]); } /** * 获取商品评论统计 * @param int $goods_id 商品ID * @return array */ public static function getCommentStats($goods_id) { $total = Comment::where('goods_id', $goods_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->count(); $star_counts = []; for ($i = 1; $i <= 5; $i++) { $star_counts[$i] = Comment::where('goods_id', $goods_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->where('star', $i) ->count(); } // 有图评论统计 $picture_count = Comment::where('goods_id', $goods_id) ->where('pid', 0) ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED) ->where('status', CommentEnum::STATUS_NORMAL) ->where('has_picture', CommentEnum::HAS_PICTURE_YES) ->count(); return [ 'total' => $total, 'star_counts' => $star_counts, 'picture_count' => $picture_count, 'degree' => self::degree($goods_id) ]; } }