Comment.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\api\model\product;
  3. use app\api\model\order\OrderProduct;
  4. use app\common\model\product\Comment as CommentModel;
  5. use app\common\exception\BaseException;
  6. class Comment extends CommentModel
  7. {
  8. /**
  9. * 隐藏字段
  10. */
  11. protected $hidden = [
  12. 'status',
  13. 'sort',
  14. 'order_id',
  15. 'product_id',
  16. 'order_product_id',
  17. 'is_delete',
  18. 'update_time'
  19. ];
  20. /**
  21. * 关联用户表
  22. */
  23. public function users()
  24. {
  25. return $this->belongsTo('app\\common\\model\\user\\User')->field(['user_id', 'nickName', 'avatarUrl']);
  26. }
  27. /**
  28. * 获取指定商品评价列表
  29. */
  30. public function getProductCommentList($product_id, $scoreType = -1, $limit = 15)
  31. {
  32. // 筛选条件
  33. $filter = [
  34. 'product_id' => $product_id,
  35. 'is_delete' => 0,
  36. 'status' => 1,
  37. ];
  38. // 评分
  39. $scoreType > 0 && $filter['score'] = $scoreType;
  40. return $this->with(['OrderProduct', 'image.file', 'users'])
  41. ->where($filter)
  42. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  43. ->paginate($limit);
  44. }
  45. /**
  46. * 获取指定评分总数
  47. */
  48. public function getTotal($product_id)
  49. {
  50. return $this->field([
  51. 'count(comment_id) AS `all`',
  52. 'count(score = 10 OR NULL) AS `praise`',
  53. 'count(score = 20 OR NULL) AS `review`',
  54. 'count(score = 30 OR NULL) AS `negative`',
  55. ])->where([
  56. 'product_id' => $product_id,
  57. 'is_delete' => 0,
  58. 'status' => 1
  59. ])->find();
  60. }
  61. /**
  62. * 验证订单是否允许评价
  63. */
  64. public function checkOrderAllowComment($order)
  65. {
  66. // 验证订单是否已完成
  67. if ($order['order_status']['value'] != 30) {
  68. $this->error = '该订单未完成,无法评价';
  69. return false;
  70. }
  71. // 验证订单是否已评价
  72. if ($order['is_comment'] == 1) {
  73. $this->error = '该订单已完成评价';
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * 根据已完成订单商品 添加评价
  80. */
  81. public function addForOrder($order, $productList, $formJsonData)
  82. {
  83. // 生成 formData
  84. $formData = $this->formatFormData($formJsonData);
  85. // 生成评价数据
  86. $data = $this->createCommentData($order['user_id'], $order['order_id'], $productList, $formData);
  87. if (empty($data)) {
  88. $this->error = '没有输入评价内容';
  89. return false;
  90. }
  91. return $this->transaction(function () use ($order, $productList, $formData, $data) {
  92. // 记录评价内容
  93. $result = $this->saveAll($data);
  94. // 记录评价图片
  95. $this->saveAllImages($result, $formData);
  96. // 更新订单评价状态
  97. $isComment = count($productList) === count($data);
  98. $this->updateOrderIsComment($order, $isComment, $result);
  99. return true;
  100. });
  101. }
  102. /**
  103. * 更新订单评价状态
  104. */
  105. private function updateOrderIsComment($order, $isComment, $commentList)
  106. {
  107. // 更新订单商品
  108. $orderProductData = [];
  109. foreach ($commentList as $comment) {
  110. $orderProductData[] = [
  111. 'order_product_id' => $comment['order_product_id'],
  112. 'is_comment' => 1
  113. ];
  114. }
  115. // 更新订单
  116. $isComment && $order->save(['is_comment' => 1]);
  117. return (new OrderProduct)->saveAll($orderProductData);
  118. }
  119. /**
  120. * 生成评价数据
  121. */
  122. private function createCommentData($user_id, $order_id, $productList, $formData)
  123. {
  124. $data = [];
  125. foreach ($productList as $product) {
  126. if (!isset($formData[$product['order_product_id']])) {
  127. throw new BaseException(['msg' => '提交的数据不合法']);
  128. }
  129. $item = $formData[$product['order_product_id']];
  130. !empty($item['content']) && $data[$product['order_product_id']] = [
  131. 'express_score' => $item['express_score'],
  132. 'server_score' => $item['server_score'],
  133. 'describe_score' => $item['describe_score'],
  134. 'content' => $item['content'],
  135. 'is_picture' => !empty($item['image_list']),
  136. 'sort' => 100,
  137. 'status' => 0,
  138. 'user_id' => $user_id,
  139. 'order_id' => $order_id,
  140. 'product_id' => $item['product_id'],
  141. 'order_product_id' => $item['order_product_id'],
  142. 'app_id' => self::$app_id,
  143. 'shop_supplier_id' => $product['orderM']['shop_supplier_id']
  144. ];
  145. }
  146. return $data;
  147. }
  148. /**
  149. * 格式化 formData
  150. */
  151. private function formatFormData($formJsonData)
  152. {
  153. return array_column(json_decode($formJsonData, true), null, 'order_product_id');
  154. }
  155. /**
  156. * 记录评价图片
  157. */
  158. private function saveAllImages($commentList, $formData)
  159. {
  160. // 生成评价图片数据
  161. $imageData = [];
  162. foreach ($commentList as $comment) {
  163. $item = $formData[$comment['order_product_id']];
  164. foreach ($item['image_list'] as $imageId) {
  165. $imageData[] = [
  166. 'comment_id' => $comment['id'],
  167. 'image_id' => $imageId['file_id'],
  168. 'app_id' => self::$app_id
  169. ];
  170. }
  171. }
  172. $model = new CommentImage;
  173. return !empty($imageData) && $model->saveAll($imageData);
  174. }
  175. }