Comment.php 6.7 KB

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