Comment.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\api\model\sharing;
  3. use app\common\exception\BaseException;
  4. use app\common\model\sharing\Comment as CommentModel;
  5. /**
  6. * 拼团商品评价模型
  7. * Class Comment
  8. * @package app\api\model\sharing
  9. */
  10. class Comment extends CommentModel
  11. {
  12. /**
  13. * 隐藏字段
  14. * @var array
  15. */
  16. protected $hidden = [
  17. 'status',
  18. 'sort',
  19. 'order_id',
  20. 'goods_id',
  21. 'order_goods_id',
  22. 'is_delete',
  23. 'update_time'
  24. ];
  25. /**
  26. * 关联用户表
  27. * @return \think\model\relation\BelongsTo
  28. */
  29. public function user()
  30. {
  31. $module = self::getCalledModule() ?: 'common';
  32. return $this->belongsTo("app\\{$module}\\model\\User")
  33. ->field(['user_id', 'nickName', 'avatarUrl']);
  34. }
  35. /**
  36. * 获取指定商品评价列表
  37. * @param $goods_id
  38. * @param int $scoreType
  39. * @return \think\Paginator
  40. * @throws \think\exception\DbException
  41. */
  42. public function getGoodsCommentList($goods_id, $scoreType = -1)
  43. {
  44. // 筛选条件
  45. $filter = [
  46. 'goods_id' => $goods_id,
  47. 'is_delete' => 0,
  48. 'status' => 1,
  49. ];
  50. // 评分
  51. $scoreType > 0 && $filter['score'] = $scoreType;
  52. return $this->with(['user', 'OrderGoods', 'image.file'])
  53. ->where($filter)
  54. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  55. ->paginate(15, false, [
  56. 'query' => request()->request()
  57. ]);
  58. }
  59. /**
  60. * 获取指定评分总数
  61. * @param $goods_id
  62. * @return array|false|\PDOStatement|string|\think\Model
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function getTotal($goods_id)
  68. {
  69. return $this->field([
  70. 'count(comment_id) AS `all`',
  71. 'count(score = 10 OR NULL) AS `praise`',
  72. 'count(score = 20 OR NULL) AS `review`',
  73. 'count(score = 30 OR NULL) AS `negative`',
  74. ])->where([
  75. 'goods_id' => $goods_id,
  76. 'is_delete' => 0,
  77. 'status' => 1
  78. ])->find();
  79. }
  80. /**
  81. * 验证订单是否允许评价
  82. * @param Order $order
  83. * @return boolean
  84. */
  85. public function checkOrderAllowComment($order)
  86. {
  87. // 验证订单是否已完成
  88. if ($order['order_status']['value'] != 30) {
  89. $this->error = '该订单未完成,无法评价';
  90. return false;
  91. }
  92. // 验证订单是否已评价
  93. if ($order['is_comment'] == 1) {
  94. $this->error = '该订单已完成评价';
  95. return false;
  96. }
  97. return true;
  98. }
  99. /**
  100. * 根据已完成订单商品 添加评价
  101. * @param Order $order
  102. * @param \think\Collection|OrderGoods $goodsList
  103. * @param $formJsonData
  104. * @return boolean
  105. * @throws \Exception
  106. */
  107. public function addForOrder($order, $goodsList, $formJsonData)
  108. {
  109. // 生成 formData
  110. $formData = $this->formatFormData($formJsonData);
  111. // 生成评价数据
  112. $data = $this->createCommentData($order['user_id'], $order['order_id'], $goodsList, $formData);
  113. if (empty($data)) {
  114. $this->error = '没有输入评价内容';
  115. return false;
  116. }
  117. return $this->transaction(function () use ($order, $goodsList, $formData, $data) {
  118. // 记录评价内容
  119. $result = $this->isUpdate(false)->saveAll($data);
  120. // 记录评价图片
  121. $this->saveAllImages($result, $formData);
  122. // 更新订单评价状态
  123. $isComment = count($goodsList) === count($data);
  124. $this->updateOrderIsComment($order, $isComment, $result);
  125. return true;
  126. });
  127. }
  128. /**
  129. * 更新订单评价状态
  130. * @param Order $order
  131. * @param $isComment
  132. * @param $commentList
  133. * @return array|false
  134. * @throws \Exception
  135. */
  136. private function updateOrderIsComment($order, $isComment, &$commentList)
  137. {
  138. // 更新订单商品
  139. $orderGoodsData = [];
  140. foreach ($commentList as $comment) {
  141. $orderGoodsData[] = [
  142. 'order_goods_id' => $comment['order_goods_id'],
  143. 'is_comment' => 1
  144. ];
  145. }
  146. // 更新订单
  147. $isComment && $order->save(['is_comment' => 1]);
  148. return (new OrderGoods)->saveAll($orderGoodsData);
  149. }
  150. /**
  151. * 生成评价数据
  152. * @param $user_id
  153. * @param $order_id
  154. * @param $goodsList
  155. * @param $formData
  156. * @return array
  157. * @throws BaseException
  158. */
  159. private function createCommentData($user_id, $order_id, &$goodsList, &$formData)
  160. {
  161. $data = [];
  162. foreach ($goodsList as $goods) {
  163. if (!isset($formData[$goods['order_goods_id']])) {
  164. throw new BaseException(['msg' => '提交的数据不合法']);
  165. }
  166. $item = $formData[$goods['order_goods_id']];
  167. !empty($item['content']) && $data[$goods['order_goods_id']] = [
  168. 'score' => $item['score'],
  169. 'content' => $item['content'],
  170. 'is_picture' => !empty($item['uploaded']),
  171. 'sort' => 100,
  172. 'status' => 1,
  173. 'user_id' => $user_id,
  174. 'order_id' => $order_id,
  175. 'goods_id' => $item['goods_id'],
  176. 'order_goods_id' => $item['order_goods_id'],
  177. 'wxapp_id' => self::$wxapp_id
  178. ];
  179. }
  180. return $data;
  181. }
  182. /**
  183. * 格式化 formData
  184. * @param string $formJsonData
  185. * @return array
  186. */
  187. private function formatFormData($formJsonData)
  188. {
  189. return array_column(json_decode($formJsonData, true), null, 'order_goods_id');
  190. }
  191. /**
  192. * 记录评价图片
  193. * @param $commentList
  194. * @param $formData
  195. * @return bool
  196. * @throws \Exception
  197. */
  198. private function saveAllImages(&$commentList, &$formData)
  199. {
  200. // 生成评价图片数据
  201. $imageData = [];
  202. foreach ($commentList as $comment) {
  203. $item = $formData[$comment['order_goods_id']];
  204. foreach ($item['uploaded'] as $imageId) {
  205. $imageData[] = [
  206. 'comment_id' => $comment['comment_id'],
  207. 'image_id' => $imageId,
  208. 'wxapp_id' => self::$wxapp_id
  209. ];
  210. }
  211. }
  212. $model = new CommentImage;
  213. return !empty($imageData) && $model->saveAll($imageData);
  214. }
  215. }