GoodsCommentsService.php 23.4 KB
Newer Older
D
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
5
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
D
devil_gong 已提交
6
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
7
// | Licensed ( https://opensource.org/licenses/mit-license.php )
D
devil_gong 已提交
8 9 10 11 12
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

D
Devil 已提交
13
use think\facade\Db;
D
devil_gong 已提交
14
use app\service\UserService;
G
gongfuxiang 已提交
15
use app\service\GoodsService;
D
devil_gong 已提交
16 17 18 19 20 21 22 23 24 25 26

/**
 * 商品评论服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class GoodsCommentsService
{
    /**
D
devil_gong 已提交
27
     * 订单评论
D
devil_gong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-09
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function Comments($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '订单id有误',
            ],
D
devil_gong 已提交
44 45 46 47 48
            [
                'checked_type'      => 'empty',
                'key_name'          => 'business_type',
                'error_msg'         => '业务类型标记不能为空',
            ],
D
devil_gong 已提交
49
            [
D
devil_gong 已提交
50
                'checked_type'      => 'empty',
D
devil_gong 已提交
51 52 53 54
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
D
devil_gong 已提交
55
                'checked_type'      => 'empty',
D
devil_gong 已提交
56 57 58 59
                'key_name'          => 'rating',
                'error_msg'         => '评级有误',
            ],
            [
D
devil_gong 已提交
60
                'checked_type'      => 'empty',
D
devil_gong 已提交
61
                'key_name'          => 'content',
D
devil_gong 已提交
62
                'error_msg'         => '评论内容有误',
D
devil_gong 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

D
devil_gong 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        // 参数处理
        if(!is_array($params['goods_id']))
        {
            $params['goods_id'] = json_decode(htmlspecialchars_decode($params['goods_id']), true);
        }
        if(!is_array($params['rating']))
        {
            $params['rating'] = json_decode(htmlspecialchars_decode($params['rating']), true);
        }
        if(!is_array($params['content']))
        {
            $params['content'] = json_decode(htmlspecialchars_decode($params['content']), true);
        }

        // 评分
        if(min($params['rating']) <= 0)
        {
            return DataReturn('评级有误', -1);
        }
        if(min($params['rating']) <= 0 || max($params['rating']) > 5)
        {
            return DataReturn('评级有误', -1);
        }

        // 评论内容
        foreach($params['content'] as $v)
        {
            $len = mb_strlen($v, 'utf-8');
            if($len < 6 || $len > 230)
            {
                return DataReturn('评论内容 6~230 个字符之间', -1);
            }
        }

        // 附件处理
        if(!empty($params['images']))
        {
            if(!is_array($params['images']))
            {
                $params['images'] = json_decode(htmlspecialchars_decode($params['images']), true);
            }
            foreach($params['images'] as &$v)
            {
                if(!empty($v))
                {
                    foreach($v as &$vs)
                    {
                        $vs = ResourcesService::AttachmentPathHandle($vs);
                    }
                    if(count($v) > 3)
                    {
                        return DataReturn('每项评论图片不能超过3张', -1);
                    }
                }
            }
        }

D
devil_gong 已提交
133 134 135
        // 获取订单信息
        $order_id = intval($params['id']);
        $where = ['id'=>$order_id, 'user_id'=>$params['user']['id'], 'is_delete_time'=>0, 'user_is_delete_time'=>0];
D
devil_gong 已提交
136
        $order = Db::name('Order')->where($where)->field('id,status,user_is_comments')->find();
D
devil_gong 已提交
137 138 139 140 141 142
        if(empty($order))
        {
            return DataReturn('资源不存在或已被删除', -1);
        }
        if($order['status'] != 4)
        {
D
Devil 已提交
143
            $status_text = MyConst('common_order_status')[$order['status']]['name'];
D
devil_gong 已提交
144 145 146 147
            return DataReturn('状态不可操作['.$status_text.']', -1);
        }
        if($order['user_is_comments'] != 0)
        {
D
devil_gong 已提交
148
            return DataReturn('该订单你已进行过评论', -10);
D
devil_gong 已提交
149 150 151 152
        }

        // 处理数据
        Db::startTrans();
D
devil_gong 已提交
153
        $is_anonymous = isset($params['is_anonymous']) ? min(1, intval($params['is_anonymous'])) : 0;
D
devil_gong 已提交
154 155 156 157 158 159
        foreach($params['goods_id'] as $k=>$goods_id)
        {
            $data = [
                'user_id'       => $params['user']['id'],
                'order_id'      => $order_id,
                'goods_id'      => $goods_id,
D
devil_gong 已提交
160
                'business_type' => $params['business_type'],
D
devil_gong 已提交
161
                'content'       => isset($params['content'][$k]) ? htmlspecialchars(trim($params['content'][$k])) : '',
D
devil_gong 已提交
162
                'images'        => empty($params['images'][$k]) ? '' : json_encode($params['images'][$k]),
D
devil_gong 已提交
163
                'rating'        => isset($params['rating'][$k]) ? intval($params['rating'][$k]) : 0,
D
devil_gong 已提交
164
                'is_anonymous'  => $is_anonymous,
D
devil_gong 已提交
165 166
                'add_time'      => time(),
            ];
D
devil_gong 已提交
167
            if(Db::name('GoodsComments')->insertGetId($data) <= 0)
D
devil_gong 已提交
168 169
            {
                Db::rollback();
D
devil_gong 已提交
170
                return DataReturn('评论内容添加失败', -100);
D
devil_gong 已提交
171 172 173
            }
        }

D
devil_gong 已提交
174
        // 订单评论状态更新
D
devil_gong 已提交
175 176 177 178 179 180 181
        if(!Db::name('Order')->where($where)->update(['user_is_comments'=>time(), 'upd_time'=>time()]))
        {
            Db::rollback();
            return DataReturn('订单更新失败', -101);
        }

        Db::commit();
D
devil_gong 已提交
182
        return DataReturn('评论成功', 0);
D
devil_gong 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    }

    /**
     * 获取商品评论列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function GoodsCommentsList($params = [])
    {
        $where = empty($params['where']) ? [] : $params['where'];
197 198
        $field = empty($params['field']) ? '*' : $params['field'];
        $order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];
D
devil_gong 已提交
199 200 201 202
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;

        // 获取数据列表
D
Devil 已提交
203
        $data = Db::name('GoodsComments')->where($where)->field($field)->limit($m, $n)->order($order_by)->select()->toArray();
X
xindan 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        return DataReturn('处理成功', 0, self::DataHandle($data, $params));
    }

    /**
     * 数据处理
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-14
     * @desc    description
     * @param   [array]          $data      [数据]
     * @param   [array]          $params    [输入参数]
     */
    public static function DataHandle($data, $params = [])
    {
D
devil_gong 已提交
219 220
        if(!empty($data))
        {
D
devil 已提交
221
            // 获取商品信息
X
xindan 已提交
222
            $is_goods = (isset($params['is_goods']) && $params['is_goods'] == 1) ? 1 : 0;
D
devil 已提交
223
            $goods = [];
X
xindan 已提交
224
            if($is_goods == 1)
D
devil 已提交
225
            {
X
xindan 已提交
226 227 228 229 230 231 232 233 234
                $goods_params = [
                    'where' => [
                        'id'                => array_unique(array_column($data, 'goods_id')),
                        'is_delete_time'    => 0,
                    ],
                    'field'  => 'id,title,images,price,min_price',
                ];
                $ret = GoodsService::GoodsList($goods_params);
                if(!empty($ret['data']))
D
devil 已提交
235
                {
X
xindan 已提交
236 237 238 239
                    foreach($ret['data'] as $g)
                    {
                        $goods[$g['id']] = $g;
                    }
D
devil 已提交
240 241 242 243
                }
            }

            // 静态数据
D
Devil 已提交
244 245 246
            $common_is_text_list = MyConst('common_is_text_list');
            $comments_rating_list = MyConst('common_goods_comments_rating_list');
            $comments_business_type_list = MyConst('common_goods_comments_business_type_list');
D
devil 已提交
247 248

            // 数据处理
D
devil_gong 已提交
249 250 251
            foreach($data as &$v)
            {
                // 用户信息
X
xindan 已提交
252
                if(array_key_exists('user_id', $v))
G
gongfuxiang 已提交
253
                {
X
xindan 已提交
254 255 256 257
                    $user = UserService::GetUserViewInfo($v['user_id']);
                    if(!isset($params['is_public']) || $params['is_public'] == 1)
                    {
                        $v['user'] = [
258 259
                            'avatar'            => isset($user['avatar']) ? $user['avatar'] : '',
                            'user_name_view'    => (!isset($v['is_anonymous']) || $v['is_anonymous'] == 1 || empty($user['user_name_view'])) ? '匿名' : mb_substr($user['user_name_view'], 0, 1, 'utf-8').'***'.mb_substr($user['user_name_view'], -1, null, 'utf-8'),
X
xindan 已提交
260 261 262 263
                        ];
                    } else {
                        $v['user'] = $user;
                    }
G
gongfuxiang 已提交
264 265
                }

D
devil_gong 已提交
266
                // 图片
X
xindan 已提交
267
                if(array_key_exists('images', $v))
D
devil_gong 已提交
268 269 270 271 272 273 274 275 276 277 278 279
                {
                    if(!empty($v['images']))
                    {
                        $images = json_decode($v['images'], true);
                        foreach($images as &$img)
                        {
                            $img = ResourcesService::AttachmentPathViewHandle($img);
                        }
                        $v['images'] = $images;
                    }
                }

D
devil 已提交
280
                // 商品信息
X
xindan 已提交
281 282 283 284
                if(array_key_exists('goods_id', $v) && $is_goods == 1)
                {
                    $v['goods'] = isset($goods[$v['goods_id']]) ? $goods[$v['goods_id']] : [];   
                }
G
gongfuxiang 已提交
285

D
devil_gong 已提交
286
                // 业务类型
X
xindan 已提交
287
                if(array_key_exists('business_type', $v))
D
devil_gong 已提交
288
                {
X
xindan 已提交
289 290 291 292 293 294 295 296 297 298 299 300
                    $v['business_type_text'] = array_key_exists($v['business_type'], $comments_business_type_list) ? $comments_business_type_list[$v['business_type']]['name'] : null;
                    $msg = null;
                    switch($v['business_type'])
                    {
                        // 订单
                        case 'order' :
                            if(!empty($v['order_id']) && !empty($v['goods_id']) && !empty($v['user_id']))
                            {
                                $msg = self::BusinessTypeOrderSpec($v['order_id'], $v['goods_id'], $v['user_id']);
                            }
                    }
                    $v['msg'] = empty($msg) ? null : $msg;
D
devil_gong 已提交
301
                }
D
devil_gong 已提交
302

D
devil_gong 已提交
303
                // 评分
X
xindan 已提交
304 305 306 307 308
                if(array_key_exists('rating', $v))
                {
                    $v['rating_text'] = $comments_rating_list[$v['rating']]['name'];
                    $v['rating_badge'] = $comments_rating_list[$v['rating']]['badge'];
                }
D
devil_gong 已提交
309

D
devil_gong 已提交
310
                // 是否
X
xindan 已提交
311 312 313 314 315 316 317 318 319 320 321 322
                if(array_key_exists('is_reply', $v))
                {
                    $v['is_reply_text'] = isset($common_is_text_list[$v['is_reply']]) ? $common_is_text_list[$v['is_reply']]['name'] : '';
                }
                if(array_key_exists('is_anonymous', $v))
                {
                    $v['is_anonymous_text'] = isset($common_is_text_list[$v['is_anonymous']]) ? $common_is_text_list[$v['is_anonymous']]['name'] : '';
                }
                if(array_key_exists('is_show', $v))
                {
                    $v['is_show_text'] = isset($common_is_text_list[$v['is_show']]) ? $common_is_text_list[$v['is_show']]['name'] : '';
                }
G
gongfuxiang 已提交
323 324

                // 回复时间
X
xindan 已提交
325 326 327 328 329
                if(array_key_exists('reply_time', $v))
                {
                    $v['reply_time_time'] = empty($v['reply_time']) ? null : date('Y-m-d H:i:s', $v['reply_time']);
                    $v['reply_time_date'] = empty($v['reply_time']) ? null : date('m-d H:i', $v['reply_time']);
                }
D
devil_gong 已提交
330 331

                // 评论时间
X
xindan 已提交
332 333 334 335 336 337
                if(array_key_exists('add_time', $v))
                {
                    $v['add_time_hour'] = date('H:i:s', $v['add_time']);
                    $v['add_time_time'] = date('Y-m-d H:i:s', $v['add_time']);
                    $v['add_time_date'] = date('m-d H:i', $v['add_time']);
                }
D
devil_gong 已提交
338

G
gongfuxiang 已提交
339
                // 更新时间
X
xindan 已提交
340 341 342 343 344
                if(array_key_exists('upd_time', $v))
                {
                    $v['upd_time_time'] = empty($v['upd_time']) ? null : date('Y-m-d H:i:s', $v['upd_time']);
                    $v['upd_time_date'] = empty($v['upd_time']) ? null : date('m-d H:I', $v['upd_time']);
                }
D
devil_gong 已提交
345
            }
X
xindan 已提交
346 347
        } else {
            $data = [];
D
devil_gong 已提交
348
        }
X
xindan 已提交
349
        return $data;
D
devil_gong 已提交
350 351
    }

D
devil_gong 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    /**
     * 订单规格字符串处理
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-14
     * @desc    description
     * @param   [int]          $order_id    [订单id]
     * @param   [int]          $goods_id    [商品id]
     * @param   [int]           $user_id    [用户id]
     * @return  [string]                    [规格字符串]
     */
    private static function BusinessTypeOrderSpec($order_id, $goods_id, $user_id = 0)
    {
        $string = null;
        $spec = Db::name('OrderDetail')->where(['order_id'=>$order_id, 'goods_id'=>$goods_id])->value('spec');
        if(!empty($spec))
        {
            $spec = json_decode($spec, true);
            if(is_array($spec) && !empty($spec))
            {
                foreach($spec as $k=>$v)
                {
                    if($k > 0)
                    {
                        $string .= ' | ';
                    }
                    $string .= $v['type'].':'.$v['value'];
                }
            }
        }
        return $string;
    }

D
devil_gong 已提交
386 387 388 389 390 391 392 393 394 395 396
    /**
     * 商品评论总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
    public static function GoodsCommentsTotal($where = [])
    {
D
devil_gong 已提交
397
        return (int) Db::name('GoodsComments')->where($where)->count();
D
devil_gong 已提交
398 399
    }

D
devil_gong 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    /**
     * 评论保存
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-17
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function GoodsCommentsSave($params = [])
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'business_type',
D
Devil 已提交
421
                'checked_data'      => array_keys(MyConst('common_order_aftersale_refundment_list')),
D
devil_gong 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                'error_msg'         => '请选择业务类型',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'content',
                'checked_data'      => '6,230',
                'error_msg'         => '评论内容 6~230 个字符之间',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'reply',
                'checked_data'      => '230',
                'error_msg'         => '回复内容最多 230 个字符',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'rating',
D
Devil 已提交
439
                'checked_data'      => array_keys(MyConst('common_goods_comments_rating_list')),
D
devil_gong 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
                'error_msg'         => '请选择评分',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 开始操作
        $data = [
            'content'           => $params['content'],
            'reply'             => $params['reply'],
            'business_type'     => $params['business_type'],
            'rating'            => intval($params['rating']),
            'reply_time'        => empty($params['reply_time']) ? 0 : strtotime($params['reply_time']),
            'is_reply'          => isset($params['is_reply']) ? intval($params['is_reply']) : 0,
            'is_show'           => isset($params['is_show']) ? intval($params['is_show']) : 0,
            'is_anonymous'      => isset($params['is_anonymous']) ? intval($params['is_anonymous']) : 0,
            'upd_time'          => time(),
        ];

        // 更新
        if(Db::name('GoodsComments')->where(['id'=>intval($params['id'])])->update($data))
        {
            return DataReturn('编辑成功', 0);
        }
        return DataReturn('编辑失败或数据不存在', -100); 
    }

G
gongfuxiang 已提交
470 471 472 473 474 475 476 477 478 479 480
    /**
     * 删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-30
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function GoodsCommentsDelete($params = [])
    {
D
devil 已提交
481 482
        // 参数是否有误
        if(empty($params['ids']))
G
gongfuxiang 已提交
483
        {
D
devil 已提交
484 485 486 487 488 489
            return DataReturn('操作id有误', -1);
        }
        // 是否数组
        if(!is_array($params['ids']))
        {
            $params['ids'] = explode(',', $params['ids']);
G
gongfuxiang 已提交
490 491 492
        }

        // 开始删除
D
devil 已提交
493
        if(Db::name('GoodsComments')->where(['id'=>$params['ids']])->delete())
G
gongfuxiang 已提交
494 495 496
        {
            return DataReturn('删除成功', 0);
        }
D
devil 已提交
497
        return DataReturn('删除失败', -100);
G
gongfuxiang 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    }

    /**
     * 回复
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function GoodsCommentsReply($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'reply',
                'error_msg'         => '回复内容不能为空',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'reply',
                'checked_data'      => '1,230',
                'error_msg'         => '回复内容格式 1~230 个字符',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 评论是否存在
        $comments_id = Db::name('GoodsComments')->field('id')->find(intval($params['id']));
        if(empty($comments_id))
        {
            return DataReturn('资源不存在或已被删除', -2);
        }
        // 更新问答
        $data = [
            'reply'         => $params['reply'],
            'is_reply'      => 1,
            'reply_time'    => time(),
            'upd_time'      => time()
        ];
        if(Db::name('GoodsComments')->where(['id'=>$comments_id])->update($data))
        {
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }

    /**
     * 状态更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function GoodsCommentsStatusUpdate($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
D
devil_gong 已提交
579 580 581 582 583 584
            [
                'checked_type'      => 'in',
                'key_name'          => 'field',
                'checked_data'      => ['is_anonymous', 'is_show', 'is_reply'],
                'error_msg'         => '操作字段有误',
            ],
G
gongfuxiang 已提交
585 586 587 588 589 590 591 592
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据更新
D
devil_gong 已提交
593 594 595 596 597
        $data = [
            $params['field']    => intval($params['state']),
            'upd_time'          => time(),
        ];
        if(Db::name('GoodsComments')->where(['id'=>intval($params['id'])])->update($data))
G
gongfuxiang 已提交
598 599 600 601 602
        {
            return DataReturn('编辑成功');
        }
        return DataReturn('编辑失败或数据未改变', -100);
    }
D
devil_gong 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

    /**
     * 商品动态评分
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-15
     * @desc    description
     * @param   [int]           $goods_id [商品id]
     */
    public static function GoodsCommentsScore($goods_id)
    {
        // 默认
        $rating_list = [
            1 => ['rating'=>1, 'name'=>'1分', 'count'=>0, 'portion'=>0],
            2 => ['rating'=>2, 'name'=>'2分', 'count'=>0, 'portion'=>0],
            3 => ['rating'=>3, 'name'=>'3分', 'count'=>0, 'portion'=>0],
            4 => ['rating'=>4, 'name'=>'4分', 'count'=>0, 'portion'=>0],
            5 => ['rating'=>5, 'name'=>'5分', 'count'=>0, 'portion'=>0],
        ];
        $where = [
            ['goods_id', '=', $goods_id],
            ['rating', '>', 0],
        ];
D
Devil 已提交
627
        $data = Db::name('GoodsComments')->where($where)->group('rating')->column('count(*) as count', 'rating');
D
devil_gong 已提交
628 629 630 631 632 633 634 635
        if(!empty($data))
        {
            $sum = array_sum($data);
            foreach($data as $rating=>$count)
            {
                if($rating > 0 && $rating <= 5)
                {
                    $rating_list[$rating]['count'] = $count;
G
gongfuxiang 已提交
636
                    $rating_list[$rating]['portion'] = round(($count/$sum)*100);
D
devil_gong 已提交
637 638 639 640
                }
            }
        }
        sort($rating_list);
X
xindan 已提交
641 642 643 644 645
        $avg = PriceNumberFormat(Db::name('GoodsComments')->where($where)->avg('rating'), 1);
        $rate = ($avg <= 0) ? 100 : intval(($avg/5)*100);
        return [
            'avg'       => $avg,
            'rate'      => $rate,
D
devil_gong 已提交
646 647
            'rating'    => $rating_list,
        ];
X
xindan 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    }

    /**
     * 商品最新几条评论
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-15
     * @desc    description
     * @param   [int]           $goods_id [商品id]
     * @param   [int]           $number   [获取数量、默认3条]
     */
    public static function GoodsFirstSeveralComments($goods_id, $number = 3)
    {
        $where = [
            ['goods_id', '=', $goods_id],
            ['is_show', '=', 1],
        ];
        $field = 'id,user_id,order_id,business_type,content,reply,is_reply,rating,images,is_anonymous,reply_time,add_time';
        return self::DataHandle(Db::name('GoodsComments')->where($where)->field($field)->limit(0, $number)->order('id desc')->select()->toArray());
D
devil_gong 已提交
668
    }
D
devil_gong 已提交
669 670
}
?>