Comment.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\model\Order as OrderModel;
  14. use app\common\model\Comment as CommentModel;
  15. use app\api\service\User as UserService;
  16. use app\common\library\helper;
  17. use cores\exception\BaseException;
  18. /**
  19. * 商品评价模型
  20. * Class Comment
  21. * @package app\api\model
  22. */
  23. class Comment extends CommentModel
  24. {
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'user_id',
  31. 'status',
  32. 'sort',
  33. 'order_id',
  34. 'goods_id',
  35. 'order_goods_id',
  36. 'store_id',
  37. 'is_delete',
  38. 'update_time'
  39. ];
  40. /**
  41. * 获取指定商品评价列表
  42. * @param int $goodsId 商品ID
  43. * @param int|null $scoreType 评分 (10好评 20中评 30差评)
  44. * @return \think\Paginator
  45. * @throws \think\db\exception\DbException
  46. */
  47. public function getCommentList(int $goodsId, int $scoreType = null): \think\Paginator
  48. {
  49. // 获取评价列表记录
  50. $filter = $this->getFilter($goodsId, $scoreType);
  51. return $this->with(['user.avatar', 'orderGoods', 'images.file'])
  52. ->where($filter)
  53. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  54. ->paginate(15);
  55. }
  56. /**
  57. * 获取指定商品评价列表 (限制数量, 不分页)
  58. * @param int $goodsId 商品ID
  59. * @param int $limit 限制的数量
  60. * @return \think\Collection
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function listRows(int $goodsId, int $limit = 5): \think\Collection
  66. {
  67. $filter = $this->getFilter($goodsId);
  68. return $this->with(['user.avatar'])
  69. ->where($filter)
  70. ->order(['sort' => 'asc', $this->getPk()])
  71. ->limit($limit)
  72. ->select();
  73. }
  74. /**
  75. * 获取指定商品评价总数量
  76. * @param int $goodsId
  77. * @return int
  78. */
  79. public function rowsTotal(int $goodsId): int
  80. {
  81. $filter = $this->getFilter($goodsId);
  82. return $this->where($filter)->count();
  83. }
  84. /**
  85. * 获取查询条件
  86. * @param int $goodsId 商品ID
  87. * @param int|null $scoreType 评分 (10好评 20中评 30差评)
  88. * @return array[]
  89. */
  90. private function getFilter(int $goodsId, int $scoreType = null): array
  91. {
  92. // 筛选条件
  93. $filter = [
  94. ['goods_id', '=', $goodsId],
  95. ['status', '=', 1],
  96. ['is_delete', '=', 0],
  97. ];
  98. // 评分
  99. $scoreType > 0 && $filter[] = ['score', '=', $scoreType];
  100. return $filter;
  101. }
  102. /**
  103. * 获取指定评分总数
  104. * @param int $goodsId
  105. * @return array|null|\think\Model
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function getTotal(int $goodsId)
  111. {
  112. return $this->field([
  113. 'count(comment_id) AS `all`',
  114. 'count(score = 10 OR NULL) AS `praise`',
  115. 'count(score = 20 OR NULL) AS `review`',
  116. 'count(score = 30 OR NULL) AS `negative`',
  117. ])->where([
  118. 'goods_id' => $goodsId,
  119. 'is_delete' => 0,
  120. 'status' => 1
  121. ])->find();
  122. }
  123. /**
  124. * 验证订单是否允许评价
  125. * @param OrderModel $order
  126. * @return boolean
  127. */
  128. public function checkOrderAllowComment(OrderModel $order): bool
  129. {
  130. // 验证订单是否已完成
  131. if ($order['order_status'] != 30) {
  132. $this->error = '该订单未完成,无法评价';
  133. return false;
  134. }
  135. // 验证订单是否已评价
  136. if ($order['is_comment'] == 1) {
  137. $this->error = '该订单已完成评价';
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * 根据已完成订单商品 添加评价
  144. * @param OrderModel $order
  145. * @param $goodsList
  146. * @param array $data
  147. * @return boolean
  148. * @throws BaseException
  149. */
  150. public function increased(OrderModel $order, $goodsList, array $data): bool
  151. {
  152. // 生成 formData
  153. $formData = $this->formatFormData($data);
  154. // 生成评价数据
  155. $data = $this->createCommentData($order['order_id'], $goodsList, $formData);
  156. if (empty($data)) {
  157. $this->error = '没有输入评价内容';
  158. return false;
  159. }
  160. return $this->transaction(function () use ($order, $goodsList, $formData, $data) {
  161. // 记录评价内容
  162. $result = $this->addAll($data);
  163. // 记录评价图片`
  164. $this->saveAllImages($result, $formData);
  165. // 更新订单评价状态
  166. $isComment = count($goodsList) === count($data);
  167. $this->updateOrderIsComment($order, $isComment, $result);
  168. return true;
  169. });
  170. }
  171. /**
  172. * 更新订单评价状态
  173. * @param OrderModel $order
  174. * @param $isComment
  175. * @param $commentList
  176. * @return void
  177. */
  178. private function updateOrderIsComment(OrderModel $order, $isComment, $commentList): void
  179. {
  180. // 更新订单商品
  181. $orderGoodsData = [];
  182. foreach ($commentList as $comment) {
  183. $orderGoodsData[] = [
  184. 'where' => [
  185. 'order_goods_id' => $comment['order_goods_id'],
  186. ],
  187. 'data' => [
  188. 'is_comment' => 1
  189. ]
  190. ];
  191. }
  192. // 更新订单
  193. $isComment && $order->save(['is_comment' => 1]);
  194. (new OrderGoods)->updateAll($orderGoodsData);
  195. }
  196. /**
  197. * 生成评价数据
  198. * @param int $orderId
  199. * @param $goodsList
  200. * @param array $formData
  201. * @return array
  202. * @throws BaseException
  203. */
  204. private function createCommentData(int $orderId, $goodsList, array $formData): array
  205. {
  206. $data = [];
  207. foreach ($goodsList as $goods) {
  208. if (!isset($formData[$goods['order_goods_id']])) {
  209. throwError('提交的数据不合法');
  210. }
  211. $commentItem = $formData[$goods['order_goods_id']];
  212. $commentItem['content'] = trim($commentItem['content']);
  213. !empty($commentItem['content']) && $data[$goods['order_goods_id']] = [
  214. 'score' => $commentItem['score'],
  215. 'content' => $commentItem['content'],
  216. 'is_picture' => !empty($commentItem['uploaded']),
  217. 'sort' => 100,
  218. 'status' => 1,
  219. 'user_id' => UserService::getCurrentLoginUserId(),
  220. 'order_id' => $orderId,
  221. 'goods_id' => $commentItem['goods_id'],
  222. 'order_goods_id' => $commentItem['order_goods_id'],
  223. 'store_id' => self::$storeId
  224. ];
  225. }
  226. return $data;
  227. }
  228. /**
  229. * 格式化 formData
  230. * @param array $data
  231. * @return array
  232. */
  233. private function formatFormData(array $data): array
  234. {
  235. return helper::arrayColumn2Key($data, 'order_goods_id');
  236. }
  237. /**
  238. * 记录评价图片
  239. * @param $commentList
  240. * @param $formData
  241. * @return void
  242. */
  243. private function saveAllImages($commentList, $formData): void
  244. {
  245. // 生成评价图片数据
  246. $imageData = [];
  247. foreach ($commentList as $comment) {
  248. $item = $formData[$comment['order_goods_id']];
  249. foreach ($item['uploaded'] as $imageId) {
  250. $imageData[] = [
  251. 'comment_id' => $comment['comment_id'],
  252. 'image_id' => $imageId,
  253. 'store_id' => self::$storeId
  254. ];
  255. }
  256. }
  257. $model = new CommentImage;
  258. !empty($imageData) && $model->addAll($imageData) !== false;
  259. }
  260. }