Comment.php 8.7 KB

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