|
|
@@ -0,0 +1,196 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 官方网站: http://www.laravel.cn
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Author: laravel开发员 <laravel.qq.com>
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace App\Services\Api;
|
|
|
+
|
|
|
+use App\Models\GoodsCommentModel;
|
|
|
+use App\Models\OrderModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+use App\Services\RedisService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商品评论-服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * @package App\Services\Api
|
|
|
+ */
|
|
|
+class CommentService extends BaseService
|
|
|
+{
|
|
|
+ // 静态对象
|
|
|
+ protected static $instance = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new GoodsCommentModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = new static();
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 15)
|
|
|
+ {
|
|
|
+ $query = $this->getQuery($params);
|
|
|
+ $query->orderBy('goods_comments.id','desc');
|
|
|
+ $list = $query->select(['goods_comments.*'])->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ $list = $list? $list->toArray() :[];
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pageSize'=> $pageSize,
|
|
|
+ 'total'=>isset($list['total'])? $list['total'] : 0,
|
|
|
+ 'list'=> isset($list['data'])? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询
|
|
|
+ * @param $params
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getQuery($params)
|
|
|
+ {
|
|
|
+ $where = ['goods_comments.mark' => 1];
|
|
|
+ return $this->model->with(['user'])->from('goods_comments')
|
|
|
+ ->where(function ($query) use($params){
|
|
|
+
|
|
|
+ $status = isset($params['status'])? $params['status'] : 0;
|
|
|
+ $type = isset($params['type'])? $params['type'] : 0;
|
|
|
+ $userId = isset($params['user_id'])? $params['user_id'] : 0;
|
|
|
+ $goodsId = isset($params['goods_id'])? $params['goods_id'] : 0;
|
|
|
+ $orderId = isset($params['order_id'])? $params['order_id'] : 0;
|
|
|
+
|
|
|
+ if($status>0){
|
|
|
+ $query->where('goods_comments.status',$status);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($userId>0){
|
|
|
+ $query->where('goods_comments.user_id',$userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($orderId>0){
|
|
|
+ $query->where('goods_comments.order_id',$orderId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if($goodsId>0){
|
|
|
+ $query->where('goods_comments.goods_ids','like',"{$goodsId},");
|
|
|
+ }
|
|
|
+
|
|
|
+ if($type>0){
|
|
|
+ $query->where('goods_comments.type',$type);
|
|
|
+ }
|
|
|
+ })->where($where);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 评论
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public function submit($userId, $params)
|
|
|
+ {
|
|
|
+ $orderId = isset($params['order_id']) ? intval($params['order_id']) : 0;
|
|
|
+ $score = isset($params['score']) ? intval($params['score']) : 0;
|
|
|
+ $content = isset($params['content']) ? trim($params['content']) : '';
|
|
|
+ $albums = isset($params['albums']) ? $params['albums'] : [];
|
|
|
+ if(empty($orderId)){
|
|
|
+ $this->error = '订单参数错误';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($score<=0 || $score>5){
|
|
|
+ $this->error = '请选择评分';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($content)){
|
|
|
+ $this->error = '请填写评价内容';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($userId<=0){
|
|
|
+ $this->error = '请先登录';
|
|
|
+ $this->errorCode = 403;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $cacheKey = "caches:goods:comment_{$userId}_{$orderId}";
|
|
|
+ if(RedisService::get($cacheKey.'_lock')){
|
|
|
+ $this->error = '请不要频繁提交';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ RedisService::set($cacheKey, $params, rand(20,30));
|
|
|
+ $info = OrderModel::with(['user','orderGoods'])->where(['id'=>$orderId,'mark'=>1])->first();
|
|
|
+ $orderGoods = isset($info['orderGoods'])?$info['orderGoods']:[];
|
|
|
+ if (empty($info)) {
|
|
|
+ RedisService::clear($cacheKey.'_lock');
|
|
|
+ $this->error = '该订单不存在';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否已经评价
|
|
|
+ if(GoodsCommentModel::where(['order_id'=>$orderId,'user_id'=>$userId,'mark'=>1])){
|
|
|
+ RedisService::clear($cacheKey.'_lock');
|
|
|
+ $this->error = '您已经评价过';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $goodsIds = array_column($orderGoods,'goods_id');
|
|
|
+ $data = [
|
|
|
+ 'goods_ids'=>$goodsIds,
|
|
|
+ 'order_id'=>$orderId,
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'score'=> $score,
|
|
|
+ 'albums'=> $albums,
|
|
|
+ 'create_time'=>time(),
|
|
|
+ 'content'=> $content,
|
|
|
+ 'status'=> 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if(!$cid = $this->model->insertGetId($data)){
|
|
|
+ $this->error = '评价失败';
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = '评价成功';
|
|
|
+ return ['id'=> $cid,'order_id'=>$orderId];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function delete()
|
|
|
+ {
|
|
|
+ $this->model->where('mark',0)->where('create_time','<=', 600)->delete();
|
|
|
+ return parent::delete(); // TODO: Change the autogenerated stub
|
|
|
+ }
|
|
|
+
|
|
|
+}
|