Comment.php 6.7 KB

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