123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace app\common\Service\Goods;
- use app\common\model\Comment;
- use app\common\model\Order;
- use app\common\model\OrderAction;
- use app\common\Enum\OrderEnum;
- use app\common\Enum\CommentEnum;
- use think\Db;
- use think\Exception;
- use app\common\exception\BusinessException;
- use app\common\Enum\StatusEnum;
- class CommentService
- {
- /**
- * 获取评论列表
- * @param array $params 查询参数
- * @return \think\Paginator
- */
- public static function getCommentList($params = [])
- {
- $goods_id = isset($params['goods_id']) ? (int)$params['goods_id'] : 0;
- $pid = isset($params['pid']) ? (int)$params['pid'] : 0;
- $has_picture = isset($params['has_picture']) ? (int)$params['has_picture'] : '';
-
- $orderBy = isset($params['order_by']) ? $params['order_by'] : 'id';
- $orderWay = isset($params['order_way']) ? strtolower($params['order_way']) : 'desc';
-
- $page = isset($params['page']) ? (int)$params['page'] : 1;
- $pageSize = isset($params['page_size']) ? $params['page_size'] : 10;
- $orderWay = in_array($orderWay, ['asc', 'desc']) ? $orderWay : 'desc';
- $where = [
- 'evaluate_status' => 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)
- ];
- }
- }
|