CommentService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\common\Service\Goods;
  3. use app\common\model\Comment;
  4. use app\common\model\Order;
  5. use app\common\model\OrderAction;
  6. use app\common\Enum\OrderEnum;
  7. use app\common\Enum\CommentEnum;
  8. use think\Db;
  9. use think\Exception;
  10. use app\common\exception\BusinessException;
  11. use app\common\Enum\StatusEnum;
  12. class CommentService
  13. {
  14. /**
  15. * 获取评论列表
  16. * @param array $params 查询参数
  17. * @return \think\Paginator
  18. */
  19. public static function getCommentList($params = [])
  20. {
  21. $goods_id = isset($params['goods_id']) ? (int)$params['goods_id'] : 0;
  22. $pid = isset($params['pid']) ? (int)$params['pid'] : 0;
  23. $has_picture = isset($params['has_picture']) ? (int)$params['has_picture'] : '';
  24. $orderBy = isset($params['order_by']) ? $params['order_by'] : 'id';
  25. $orderWay = isset($params['order_way']) ? strtolower($params['order_way']) : 'desc';
  26. $page = isset($params['page']) ? (int)$params['page'] : 1;
  27. $pageSize = isset($params['page_size']) ? $params['page_size'] : 10;
  28. $orderWay = in_array($orderWay, ['asc', 'desc']) ? $orderWay : 'desc';
  29. $where = [
  30. 'evaluate_status' => CommentEnum::EVALUATE_STATUS_APPROVED,
  31. 'status' => StatusEnum::ENABLED
  32. ];
  33. if ($goods_id > 0) {
  34. $where['goods_id'] = $goods_id;
  35. }
  36. if ($pid !== '') {
  37. $where['pid'] = $pid;
  38. }
  39. // 添加有图评论过滤
  40. if ($has_picture !== '') {
  41. $where['has_picture'] = $has_picture;
  42. }
  43. $order = $orderBy == 'rand' ? 'rand()' : (in_array($orderBy, ['pid', 'id', 'createtime', 'updatetime']) ? "{$orderBy} {$orderWay}" : "id {$orderWay}");
  44. $list = Comment::with(['user', 'reply' => function ($query) {
  45. $query->with(['manage' => function ($user) {
  46. $user->field('id,nickname');
  47. }]);
  48. }])
  49. ->where($where)
  50. ->order($order)
  51. ->paginate($pageSize, false, ['page' => $page]);
  52. return $list;
  53. }
  54. /**
  55. * 获取商品好评度
  56. * @param int $goods_id 商品ID
  57. * @return float
  58. */
  59. public static function degree($goods_id)
  60. {
  61. $total = Comment::where('goods_id', $goods_id)
  62. ->where('pid', 0)
  63. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  64. ->where('status', CommentEnum::STATUS_NORMAL)
  65. ->sum('star');
  66. $favorable = Comment::where('goods_id', $goods_id)
  67. ->where('pid', 0)
  68. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  69. ->where('status', CommentEnum::STATUS_NORMAL)
  70. ->where('star', '>', 3)
  71. ->sum('star');
  72. if (!$total || !$favorable) {
  73. return 100;
  74. }
  75. return bcmul(bcdiv($favorable, $total, 2), 100);
  76. }
  77. /**
  78. * 获取用户评论列表
  79. * @param int $user_id 用户ID
  80. * @param int $page 页码
  81. * @param int $pagesize 每页数量
  82. * @return \think\Paginator
  83. */
  84. public static function getUserCommentList($user_id, $page = 1, $pagesize = 10)
  85. {
  86. $list = Comment::with([
  87. 'Goods' => function ($query) {
  88. $query->field('id,title,image');
  89. }
  90. ])
  91. ->where('user_id', $user_id)
  92. ->where('pid', 0)
  93. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  94. ->where('status', CommentEnum::STATUS_NORMAL)
  95. ->order('createtime desc')
  96. ->paginate($pagesize, false, ['page' => $page]);
  97. return $list;
  98. }
  99. /**
  100. * 添加评论
  101. * @param int $user_id 用户ID
  102. * @param int $order_id 订单ID
  103. * @param array $remark 评论内容
  104. * @param int $pid 父评论ID
  105. * @return bool
  106. * @throws Exception
  107. */
  108. public static function addComment($user_id, $order_id, $remark, $pid = 0)
  109. {
  110. // 验证订单
  111. $order = Order::with(['OrderGoods'])
  112. ->where('id', $order_id)
  113. ->where('order_status', OrderEnum::STATUS_CONFIRM)
  114. ->where('user_id', $user_id)
  115. ->find();
  116. if (!$order) {
  117. throw new BusinessException('未找到可评论的订单');
  118. }
  119. // 检查是否已评论
  120. $existComment = Comment::where('user_id', $user_id)
  121. ->where('order_id', $order->id)
  122. ->find();
  123. if ($existComment) {
  124. throw new BusinessException('订单已评论');
  125. }
  126. // 获取可评价的商品
  127. $goods_ids = [];
  128. foreach ($order->order_goods as $item) {
  129. $goods_ids[] = $item['goods_id'];
  130. }
  131. // 验证评论数据并组装
  132. $data = [];
  133. foreach ($remark as $item) {
  134. if (!in_array($item['goods_id'], $goods_ids)) {
  135. throw new BusinessException('存在不可评价的商品');
  136. }
  137. // 处理图片
  138. $images = isset($item['images']) ? $item['images'] : [];
  139. $has_picture = !empty($images) ? CommentEnum::HAS_PICTURE_YES : CommentEnum::HAS_PICTURE_NO;
  140. $data[] = [
  141. 'pid' => $pid,
  142. 'order_id' => $order['id'],
  143. 'user_id' => $user_id,
  144. 'goods_id' => $item['goods_id'],
  145. 'star' => $item['star'],
  146. 'content' => $item['content'],
  147. 'images' => is_array($images) ? implode(',', $images) : $images,
  148. 'has_picture' => $has_picture,
  149. 'ip' => request()->ip(),
  150. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  151. 'evaluate_status' => CommentEnum::EVALUATE_STATUS_PENDING,
  152. ];
  153. }
  154. // 事务处理
  155. Db::startTrans();
  156. try {
  157. // 批量添加评论
  158. $comment = new Comment();
  159. $comment->saveAll($data);
  160. // 更新订单状态
  161. $order->order_status = OrderEnum::STATUS_COMMENT;
  162. $order->save();
  163. // 更新订单商品评论状态
  164. foreach ($order->order_goods as $item) {
  165. $item->save(['comment_status' => 1]);
  166. }
  167. // 添加订单日志
  168. OrderAction::push($order->order_sn, '系统', '订单已完成');
  169. Db::commit();
  170. return true;
  171. } catch (\Exception $e) {
  172. Db::rollback();
  173. throw new BusinessException('添加评论失败:' . $e->getMessage());
  174. }
  175. }
  176. /**
  177. * 批量保存评论(原方法保留)
  178. * @param array $data 评论数据
  179. * @return bool
  180. */
  181. public static function saveComments($data)
  182. {
  183. $comment = new Comment();
  184. return $comment->saveAll($data);
  185. }
  186. /**
  187. * 审核评论
  188. * @param int $comment_id 评论ID
  189. * @param int $evaluate_status 评价状态
  190. * @return bool
  191. * @throws Exception
  192. */
  193. public static function auditComment($comment_id, $evaluate_status)
  194. {
  195. if (!CommentEnum::isValidEvaluateStatus($evaluate_status)) {
  196. throw new BusinessException('评价状态参数错误');
  197. }
  198. $comment = Comment::where('id', $comment_id)->find();
  199. if (!$comment) {
  200. throw new BusinessException('评论不存在');
  201. }
  202. $status = $evaluate_status == CommentEnum::EVALUATE_STATUS_APPROVED ?
  203. CommentEnum::STATUS_NORMAL : CommentEnum::STATUS_HIDDEN;
  204. return $comment->save([
  205. 'evaluate_status' => $evaluate_status,
  206. 'status' => $status
  207. ]);
  208. }
  209. /**
  210. * 获取商品评论统计
  211. * @param int $goods_id 商品ID
  212. * @return array
  213. */
  214. public static function getCommentStats($goods_id)
  215. {
  216. $total = Comment::where('goods_id', $goods_id)
  217. ->where('pid', 0)
  218. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  219. ->where('status', CommentEnum::STATUS_NORMAL)
  220. ->count();
  221. $star_counts = [];
  222. for ($i = 1; $i <= 5; $i++) {
  223. $star_counts[$i] = Comment::where('goods_id', $goods_id)
  224. ->where('pid', 0)
  225. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  226. ->where('status', CommentEnum::STATUS_NORMAL)
  227. ->where('star', $i)
  228. ->count();
  229. }
  230. // 有图评论统计
  231. $picture_count = Comment::where('goods_id', $goods_id)
  232. ->where('pid', 0)
  233. ->where('evaluate_status', CommentEnum::EVALUATE_STATUS_APPROVED)
  234. ->where('status', CommentEnum::STATUS_NORMAL)
  235. ->where('has_picture', CommentEnum::HAS_PICTURE_YES)
  236. ->count();
  237. return [
  238. 'total' => $total,
  239. 'star_counts' => $star_counts,
  240. 'picture_count' => $picture_count,
  241. 'degree' => self::degree($goods_id)
  242. ];
  243. }
  244. }