CommentService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\GoodsCommentModel;
  13. use App\Models\OrderModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 商品评论-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class CommentService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new GoodsCommentModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $query = $this->getQuery($params);
  53. $query->orderBy('goods_comments.id','desc');
  54. $list = $query->select(['goods_comments.*'])->paginate($pageSize > 0 ? $pageSize : 9999999);
  55. $list = $list? $list->toArray() :[];
  56. return [
  57. 'pageSize'=> $pageSize,
  58. 'total'=>isset($list['total'])? $list['total'] : 0,
  59. 'list'=> isset($list['data'])? $list['data'] : []
  60. ];
  61. }
  62. /**
  63. * 查询
  64. * @param $params
  65. * @return mixed
  66. */
  67. public function getQuery($params)
  68. {
  69. $where = ['goods_comments.mark' => 1];
  70. return $this->model->with(['user'])->from('goods_comments')
  71. ->where(function ($query) use($params){
  72. $status = isset($params['status'])? $params['status'] : 0;
  73. $type = isset($params['type'])? $params['type'] : 0;
  74. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  75. $goodsId = isset($params['goods_id'])? $params['goods_id'] : 0;
  76. $orderId = isset($params['order_id'])? $params['order_id'] : 0;
  77. if($status>0){
  78. $query->where('goods_comments.status',$status);
  79. }
  80. if($userId>0){
  81. $query->where('goods_comments.user_id',$userId);
  82. }
  83. if($orderId>0){
  84. $query->where('goods_comments.order_id',$orderId);
  85. }
  86. if($goodsId>0){
  87. $query->where('goods_comments.goods_ids','like',"{$goodsId},");
  88. }
  89. if($type>0){
  90. $query->where('goods_comments.type',$type);
  91. }
  92. })->where($where);
  93. }
  94. /**
  95. * 评论
  96. * @param $userId
  97. * @param $params
  98. * @return array|false
  99. */
  100. public function submit($userId, $params)
  101. {
  102. $orderId = isset($params['order_id']) ? intval($params['order_id']) : 0;
  103. $score = isset($params['score']) ? intval($params['score']) : 0;
  104. $content = isset($params['content']) ? trim($params['content']) : '';
  105. $albums = isset($params['albums']) ? $params['albums'] : [];
  106. if(empty($orderId)){
  107. $this->error = '订单参数错误';
  108. return false;
  109. }
  110. if($score<=0 || $score>5){
  111. $this->error = '请选择评分';
  112. return false;
  113. }
  114. if(empty($content)){
  115. $this->error = '请填写评价内容';
  116. return false;
  117. }
  118. if($userId<=0){
  119. $this->error = '请先登录';
  120. $this->errorCode = 403;
  121. return false;
  122. }
  123. $cacheKey = "caches:goods:comment_{$userId}_{$orderId}";
  124. if(RedisService::get($cacheKey.'_lock')){
  125. $this->error = '请不要频繁提交';
  126. return false;
  127. }
  128. RedisService::set($cacheKey, $params, rand(20,30));
  129. $info = OrderModel::with(['user','orderGoods'])->where(['id'=>$orderId,'mark'=>1])->first();
  130. $orderGoods = isset($info['orderGoods'])?$info['orderGoods']:[];
  131. if (empty($info)) {
  132. RedisService::clear($cacheKey.'_lock');
  133. $this->error = '该订单不存在';
  134. return false;
  135. }
  136. // 是否已经评价
  137. if(GoodsCommentModel::where(['order_id'=>$orderId,'user_id'=>$userId,'mark'=>1])){
  138. RedisService::clear($cacheKey.'_lock');
  139. $this->error = '您已经评价过';
  140. return false;
  141. }
  142. $goodsIds = array_column($orderGoods,'goods_id');
  143. $data = [
  144. 'goods_ids'=>implode(',',$goodsIds).',',
  145. 'order_id'=>$orderId,
  146. 'user_id'=> $userId,
  147. 'score'=> $score,
  148. 'albums'=> $albums,
  149. 'create_time'=>time(),
  150. 'content'=> $content,
  151. 'status'=> 1,
  152. ];
  153. if(!$cid = $this->model->insertGetId($data)){
  154. $this->error = '评价失败';
  155. }
  156. $this->error = '评价成功';
  157. return ['id'=> $cid,'order_id'=>$orderId];
  158. }
  159. /**
  160. * 删除
  161. * @return array
  162. */
  163. public function delete()
  164. {
  165. $this->model->where('mark',0)->where('create_time','<=', 600)->delete();
  166. return parent::delete(); // TODO: Change the autogenerated stub
  167. }
  168. }