Comment.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\index\model;
  13. use app\index\model\Order as OrderModel;
  14. use app\common\model\Comment as CommentModel;
  15. use app\index\model\OrderGoods as OrderGoodsModel;
  16. use app\index\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'])
  50. ->where($filter)
  51. //->order(['sort' => 'asc', 'create_time' => 'desc'])
  52. ->order(['comment_id' => 'desc'])
  53. ->paginate(15);
  54. }
  55. /**
  56. * 获取指定商品评价列表 (限制数量, 不分页)
  57. * @param int $goodsId 商品ID
  58. * @param int $limit 限制的数量
  59. * @return \think\Collection
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function listRows(int $goodsId, int $limit = 5)
  65. {
  66. $filter = $this->getFilter($goodsId);
  67. return $this->with(['user.avatar'])
  68. ->where($filter)
  69. ->order(['sort' => 'asc', $this->getPk()])
  70. ->limit($limit)
  71. ->select();
  72. }
  73. /**
  74. * 获取指定商品评价总数量
  75. * @param int $goodsId
  76. * @return int
  77. */
  78. public function rowsTotal(int $goodsId)
  79. {
  80. $filter = $this->getFilter($goodsId);
  81. return $this->where($filter)->count();
  82. }
  83. public function rowsTotalBatch(array $goodsIds)
  84. {
  85. $filter = [
  86. ['status', '=', 1],
  87. ['is_delete', '=', 0],];
  88. return $this->field('goods_id, count(comment_id) as cnt')->where($filter)->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
  89. }
  90. /**
  91. * 获取查询条件
  92. * @param int $goodsId 商品ID
  93. * @param int|null $scoreType 评分 (10好评 20中评 30差评)
  94. * @return array[]
  95. */
  96. private function getFilter(int $goodsId, int $scoreType = null)
  97. {
  98. // 筛选条件
  99. $filter = [
  100. ['goods_id', '=', $goodsId],
  101. ['status', '=', 1],
  102. ['is_delete', '=', 0],
  103. ];
  104. // 评分
  105. $scoreType > 0 && $filter[] = ['score', '=', $scoreType];
  106. return $filter;
  107. }
  108. /**
  109. * 获取指定评分总数
  110. * @param int $goodsId
  111. * @return array|null|\think\Model
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function getTotal(int $goodsId)
  117. {
  118. return $this->field([
  119. 'count(comment_id) AS `all`',
  120. 'count(score = 10 OR NULL) AS `praise`',
  121. 'count(score = 20 OR NULL) AS `review`',
  122. 'count(score = 30 OR NULL) AS `negative`',
  123. ])->where([
  124. 'goods_id' => $goodsId,
  125. 'is_delete' => 0,
  126. 'status' => 1
  127. ])->find();
  128. }
  129. public function getTotalAll(array $goodsIds)
  130. {
  131. return $this->field('goods_id,sum(score) as score_total')->where([
  132. 'is_delete' => 0,
  133. 'status' => 1])->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
  134. }
  135. /**
  136. * 验证订单是否允许评价
  137. * @param OrderModel $order
  138. * @return boolean
  139. */
  140. public function checkOrderAllowComment(OrderModel $order)
  141. {
  142. // 验证订单是否已完成
  143. if ($order['order_status'] != 30) {
  144. $this->error = '该订单未完成,无法评价';
  145. return false;
  146. }
  147. // 验证订单是否已评价
  148. if ($order['is_comment'] == 1) {
  149. $this->error = '该订单已完成评价';
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * 根据已完成订单商品 添加评价
  156. * @param OrderModel $order
  157. * @param $goodsList
  158. * @param array $data
  159. * @return boolean
  160. * @throws BaseException
  161. */
  162. public function increased(OrderModel $order, $goodsList, array $data)
  163. {
  164. // 生成 formData
  165. $formData = $this->formatFormData($data);
  166. // 生成评价数据
  167. $data = $this->createCommentData($order['order_id'], $goodsList, $formData);
  168. if (empty($data)) {
  169. $this->error = '没有输入评价内容';
  170. return false;
  171. }
  172. return $this->transaction(function () use ($order, $goodsList, $formData, $data) {
  173. // 记录评价内容
  174. $result = $this->addAll($data);
  175. // 记录评价图片`
  176. //$this->saveAllImages($result, $formData);
  177. // 更新订单评价状态
  178. $isComment = count($goodsList) === count($data);
  179. $this->updateOrderIsComment($order, $isComment, $result);
  180. return true;
  181. });
  182. }
  183. /**
  184. * 更新订单评价状态
  185. * @param OrderModel $order
  186. * @param $isComment
  187. * @param $commentList
  188. * @return array|false
  189. * @throws \Exception
  190. */
  191. private function updateOrderIsComment(OrderModel $order, $isComment, $commentList)
  192. {
  193. // 更新订单商品
  194. $orderGoodsData = [];
  195. foreach ($commentList as $comment) {
  196. $orderGoodsData[] = [
  197. 'where' => [
  198. 'order_goods_id' => $comment['order_goods_id'],
  199. ],
  200. 'data' => [
  201. 'is_comment' => 1
  202. ]
  203. ];
  204. }
  205. // 更新订单
  206. $isComment && $order->save(['is_comment' => 1]);
  207. return (new OrderGoodsModel)->updateAll($orderGoodsData);
  208. }
  209. /**
  210. * 生成评价数据
  211. * @param int $orderId
  212. * @param $goodsList
  213. * @param array $formData
  214. * @return array
  215. * @throws BaseException
  216. */
  217. private function createCommentData(int $orderId, $goodsList, array $formData)
  218. {
  219. $data = [];
  220. foreach ($goodsList as $goods) {
  221. if (!isset($formData[$goods['order_goods_id']])) {
  222. throwError('提交的数据不合法');
  223. }
  224. $commentItem = $formData[$goods['order_goods_id']];
  225. $commentItem['content'] = trim($commentItem['content']);
  226. !empty($commentItem['content']) && $data[$goods['order_goods_id']] = [
  227. 'score' => $commentItem['score'],
  228. 'content' => $commentItem['content'],
  229. 'is_picture' => !empty($commentItem['uploaded']),
  230. 'sort' => 100,
  231. 'status' => 1,
  232. 'user_id' => UserService::getCurrentLoginUserId(),
  233. 'order_id' => $orderId,
  234. 'goods_id' => $commentItem['goods_id'],
  235. 'order_goods_id' => $commentItem['order_goods_id'],
  236. 'store_id' => self::$storeId
  237. ];
  238. }
  239. return $data;
  240. }
  241. /**
  242. * 格式化 formData
  243. * @param array $data
  244. * @return array
  245. */
  246. private function formatFormData(array $data)
  247. {
  248. return helper::arrayColumn2Key($data, 'order_goods_id');
  249. }
  250. }