CommentService.php 13 KB

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