Comment.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. 'data' => [
  112. 'is_comment' => 1
  113. ],
  114. 'where' => [
  115. 'order_product_id' => $comment['order_product_id'],
  116. ],
  117. ];
  118. }
  119. // 更新订单
  120. $isComment && $order->save(['is_comment' => 1]);
  121. return (new OrderProduct)->updateAll($orderProductData);
  122. }
  123. /**
  124. * 生成评价数据
  125. */
  126. private function createCommentData($user_id, $order_id, $productList, $formData)
  127. {
  128. $data = [];
  129. foreach ($productList as $product) {
  130. if (!isset($formData[$product['order_product_id']])) {
  131. throw new BaseException(['msg' => '提交的数据不合法']);
  132. }
  133. $item = $formData[$product['order_product_id']];
  134. !empty($item['content']) && $data[$product['order_product_id']] = [
  135. 'score' => $item['score'],
  136. 'content' => $item['content'],
  137. 'is_picture' => !empty($item['image_list']),
  138. 'sort' => 100,
  139. 'status' => 0,
  140. 'user_id' => $user_id,
  141. 'order_id' => $order_id,
  142. 'product_id' => $item['product_id'],
  143. 'order_product_id' => $item['order_product_id'],
  144. 'app_id' => self::$app_id
  145. ];
  146. }
  147. return $data;
  148. }
  149. /**
  150. * 格式化 formData
  151. */
  152. private function formatFormData($formJsonData)
  153. {
  154. return array_column(json_decode($formJsonData, true), null, 'order_product_id');
  155. }
  156. /**
  157. * 记录评价图片
  158. */
  159. private function saveAllImages($commentList, $formData)
  160. {
  161. // 生成评价图片数据
  162. $imageData = [];
  163. foreach ($commentList as $comment) {
  164. $item = $formData[$comment['order_product_id']];
  165. foreach ($item['image_list'] as $imageId) {
  166. $imageData[] = [
  167. 'comment_id' => $comment['id'],
  168. 'image_id' => $imageId['file_id'],
  169. 'app_id' => self::$app_id
  170. ];
  171. }
  172. }
  173. $model = new CommentImage;
  174. return !empty($imageData) && $model->saveAll($imageData);
  175. }
  176. }