CommentService.php 10 KB

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