Comment.php 8.4 KB

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