GoodsCommentLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\common\enum\GoodsCommentEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\GoodsComment;
  23. use app\common\model\GoodsCommentImage;
  24. use app\common\model\OrderGoods;
  25. use app\common\service\FileService;
  26. use think\facade\Db;
  27. class GoodsCommentLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 添加商品评价
  31. * @param $params
  32. * @return bool
  33. * @author ljj
  34. * @date 2021/8/6 8:15 下午
  35. */
  36. public function add($params)
  37. {
  38. // 启动事务
  39. Db::startTrans();
  40. try {
  41. //获取订单商品信息
  42. $order_goods = OrderGoods::find($params['order_goods_id'])->append(['spec_value_str'])->toArray();
  43. //添加评价数据
  44. $goods_comment = new GoodsComment;
  45. $goods_comment->goods_id = $order_goods['goods_id'];
  46. $goods_comment->item_id = $order_goods['item_id'];
  47. $goods_comment->spec_value_str = $order_goods['spec_value_str'];
  48. $goods_comment->user_id = $params['user_id'];
  49. $goods_comment->order_goods_id = $params['order_goods_id'];
  50. $goods_comment->goods_comment = $params['goods_comment'];
  51. $goods_comment->service_comment = $params['service_comment'];
  52. $goods_comment->description_comment = $params['description_comment'];
  53. $goods_comment->express_comment = $params['express_comment'];
  54. $goods_comment->comment = $params['comment'] ?? '';
  55. $goods_comment->save();
  56. //添加评价图片数据
  57. if (isset($params['image'])) {
  58. $image_data = [];
  59. foreach ($params['image'] as $val) {
  60. $image_data[] = [
  61. 'comment_id' => $goods_comment->id,
  62. 'uri' => FileService::setFileUrl($val),
  63. ];
  64. }
  65. $goods_comment_image = new GoodsCommentImage;
  66. $goods_comment_image->saveAll($image_data);
  67. }
  68. //修改订单商品表评价状态
  69. OrderGoods::update(['is_comment' => 1], ['id' => $params['order_goods_id']]);
  70. // 提交事务
  71. Db::commit();
  72. return true;
  73. } catch (\Exception $e) {
  74. // 回滚事务
  75. Db::rollback();
  76. self::$error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. /**
  81. * @notes 查看商品评价分类
  82. * @param $parmas
  83. * @return array
  84. * @author ljj
  85. * @date 2021/8/6 9:04 下午
  86. */
  87. public function commentCategory($parmas)
  88. {
  89. $all_count = GoodsComment::where('goods_id', $parmas['goods_id'])->where(['status'=>GoodsCommentEnum::APPROVED])->count();
  90. $image_count = GoodsComment::alias('gc')->where('goods_id', $parmas['goods_id'])->where(['status'=>GoodsCommentEnum::APPROVED])->join('goods_comment_image gci', 'gc.id = gci.comment_id')->group('gci.comment_id')->count();
  91. $good_count = GoodsComment::where('goods_id', $parmas['goods_id'])->where('goods_comment','>',3)->where(['status'=>GoodsCommentEnum::APPROVED])->count();
  92. $medium_count = GoodsComment::where('goods_id', $parmas['goods_id'])->where('goods_comment','=',3)->where(['status'=>GoodsCommentEnum::APPROVED])->count();
  93. $bad_count = GoodsComment::where('goods_id', $parmas['goods_id'])->where('goods_comment','<',3)->where(['status'=>GoodsCommentEnum::APPROVED])->count();
  94. if($all_count == 0) {
  95. $percentStr = '100%';
  96. $star = 5;
  97. }else {
  98. $percent = round((($good_count / $all_count) * 100));
  99. $percentStr = round((($good_count / $all_count) * 100)).'%';
  100. if ($percent >= 100) {
  101. $star = 5;
  102. } else if ($percent >= 80) {
  103. $star = 4;
  104. } else if ($percent >= 60) {
  105. $star = 3;
  106. } else if ($percent >= 40) {
  107. $star = 2;
  108. } else if ($percent >= 20) {
  109. $star = 1;
  110. } else {
  111. $star = 0;
  112. }
  113. }
  114. return ['comment'=>
  115. [
  116. [
  117. 'id' => 0,
  118. 'name' => '全部',
  119. 'count' => $all_count
  120. ],
  121. [
  122. 'id' => 1,
  123. 'name' => '晒图',
  124. 'count' => $image_count
  125. ],
  126. [
  127. 'id' => 2,
  128. 'name' => '好评',
  129. 'count' => $good_count
  130. ],
  131. [
  132. 'id' => 3,
  133. 'name' => '中评',
  134. 'count' => $medium_count
  135. ],
  136. [
  137. 'id' => 4,
  138. 'name' => '差评',
  139. 'count' => $bad_count
  140. ]
  141. ] ,
  142. 'percent' => $percentStr,
  143. 'star' => $star,
  144. ];
  145. }
  146. /**
  147. * @notes 查看评价商品信息
  148. * @param $params
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @author ljj
  154. * @date 2021/8/9 3:12 下午
  155. */
  156. public function commentGoodsInfo($params)
  157. {
  158. $info = OrderGoods::field('id,goods_id,goods_name,goods_price,goods_num,goods_snap,total_price,total_pay_price')
  159. ->where('id', '=', $params['order_goods_id'])
  160. ->append(['goods_image'])
  161. ->hidden(['goods_snap'])
  162. ->find()
  163. ->toArray();
  164. return $info;
  165. }
  166. }