| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?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'=>implode(',',$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
- }
- }
|