Comment.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /**
  84. * 获取指定用户评价总数量
  85. * @param int $userId
  86. * @return int
  87. */
  88. public function rowsUserTotal(int $userId)
  89. {
  90. return $this->where(['user_id',$userId])->where('status',1)->where('is_delete',0)->count();
  91. }
  92. public function rowsTotalBatch(array $goodsIds)
  93. {
  94. $filter = [
  95. ['status', '=', 1],
  96. ['is_delete', '=', 0],];
  97. return $this->field('goods_id, count(comment_id) as cnt')->where($filter)->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
  98. }
  99. /**
  100. * 获取查询条件
  101. * @param int $goodsId 商品ID
  102. * @param int|null $scoreType 评分 (10好评 20中评 30差评)
  103. * @return array[]
  104. */
  105. private function getFilter(int $goodsId, int $scoreType = null, int $userId = null)
  106. {
  107. // 筛选条件
  108. $filter = [
  109. ['goods_id', '=', $goodsId],
  110. ['status', '=', 1],
  111. ['is_delete', '=', 0],
  112. ];
  113. // 评分
  114. $scoreType > 0 && $filter[] = ['score', '=', $scoreType];
  115. return $filter;
  116. }
  117. /**
  118. * 获取指定评分总数
  119. * @param int $goodsId
  120. * @return array|null|\think\Model
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getTotal(int $goodsId)
  126. {
  127. return $this->field([
  128. 'count(comment_id) AS `all`',
  129. 'count(score = 10 OR NULL) AS `praise`',
  130. 'count(score = 20 OR NULL) AS `review`',
  131. 'count(score = 30 OR NULL) AS `negative`',
  132. ])->where([
  133. 'goods_id' => $goodsId,
  134. 'is_delete' => 0,
  135. 'status' => 1
  136. ])->find();
  137. }
  138. public function getTotalAll(array $goodsIds)
  139. {
  140. return $this->field('goods_id,sum(score) as score_total')->where([
  141. 'is_delete' => 0,
  142. 'status' => 1])->whereIn('goods_id', $goodsIds)->group('goods_id')->select();
  143. }
  144. /**
  145. * 验证订单是否允许评价
  146. * @param OrderModel $order
  147. * @return boolean
  148. */
  149. public function checkOrderAllowComment(OrderModel $order)
  150. {
  151. // 验证订单是否已完成
  152. if ($order['order_status'] != 30) {
  153. $this->error = 'Not supported now.';
  154. return false;
  155. }
  156. // 验证订单是否已评价
  157. if ($order['is_comment'] == 1) {
  158. $this->error = 'The order has been reviewed';
  159. return false;
  160. }
  161. return true;
  162. }
  163. /**
  164. * 根据已完成订单商品 添加评价
  165. * @param OrderModel $order
  166. * @param $goodsList
  167. * @param array $data
  168. * @return boolean
  169. * @throws BaseException
  170. */
  171. public function increased(OrderModel $order, $goodsList, array $data)
  172. {
  173. // 生成 formData
  174. $formData = $this->formatFormData($data);
  175. // 生成评价数据
  176. $data = $this->createCommentData($order['order_id'], $goodsList, $formData);
  177. if (empty($data)) {
  178. $this->error = 'No content.';
  179. return false;
  180. }
  181. return $this->transaction(function () use ($order, $goodsList, $formData, $data) {
  182. // 记录评价内容
  183. $result = $this->addAll($data);
  184. // 记录评价图片`
  185. //$this->saveAllImages($result, $formData);
  186. // 更新订单评价状态
  187. $isComment = count($goodsList) === count($data);
  188. $this->updateOrderIsComment($order, $isComment, $result);
  189. return true;
  190. });
  191. }
  192. /**
  193. * 更新订单评价状态
  194. * @param OrderModel $order
  195. * @param $isComment
  196. * @param $commentList
  197. * @return array|false
  198. * @throws \Exception
  199. */
  200. private function updateOrderIsComment(OrderModel $order, $isComment, $commentList)
  201. {
  202. // 更新订单商品
  203. $orderGoodsData = [];
  204. foreach ($commentList as $comment) {
  205. $orderGoodsData[] = [
  206. 'where' => [
  207. 'order_goods_id' => $comment['order_goods_id'],
  208. ],
  209. 'data' => [
  210. 'is_comment' => 1
  211. ]
  212. ];
  213. }
  214. // 更新订单
  215. $isComment && $order->save(['is_comment' => 1]);
  216. return (new OrderGoodsModel)->updateAll($orderGoodsData);
  217. }
  218. /**
  219. * 生成评价数据
  220. * @param int $orderId
  221. * @param $goodsList
  222. * @param array $formData
  223. * @return array
  224. * @throws BaseException
  225. */
  226. private function createCommentData(int $orderId, $goodsList, array $formData)
  227. {
  228. $data = [];
  229. foreach ($goodsList as $goods) {
  230. if (!isset($formData[$goods['order_goods_id']])) {
  231. throwError('提交的数据不合法');
  232. }
  233. $commentItem = $formData[$goods['order_goods_id']];
  234. $commentItem['content'] = trim($commentItem['content']);
  235. !empty($commentItem['content']) && $data[$goods['order_goods_id']] = [
  236. 'score' => $commentItem['score'],
  237. 'content' => $commentItem['content'],
  238. 'is_picture' => !empty($commentItem['uploaded']),
  239. 'sort' => 100,
  240. 'status' => 1,
  241. 'user_id' => UserService::getCurrentLoginUserId(),
  242. 'order_id' => $orderId,
  243. 'goods_id' => $commentItem['goods_id'],
  244. 'order_goods_id' => $commentItem['order_goods_id'],
  245. 'store_id' => self::$storeId
  246. ];
  247. }
  248. return $data;
  249. }
  250. /**
  251. * 格式化 formData
  252. * @param array $data
  253. * @return array
  254. */
  255. private function formatFormData(array $data)
  256. {
  257. return helper::arrayColumn2Key($data, 'order_goods_id');
  258. }
  259. }