Comment.php 6.6 KB

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