Review.php 13.3 KB
Newer Older
_
__FresHmaN 已提交
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */

namespace fecshop\services\product;

T
Terry 已提交
12
//use fecshop\models\mongodb\product\Review as ReviewModel;
_
__FresHmaN 已提交
13 14 15 16 17
use fecshop\services\Service;
use Yii;
use yii\base\InvalidValueException;

/**
18
 * Product Review Service
_
__FresHmaN 已提交
19 20 21 22 23 24
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Review extends Service
{
    public $filterByLang;
T
Terry 已提交
25 26 27 28 29 30
    protected $_reviewModelName = '\fecshop\models\mongodb\product\Review';
    protected $_reviewModel;
    
    public function __construct(){
        list($this->_reviewModelName,$this->_reviewModel) = \Yii::mapGet($this->_reviewModelName);  
    }
_
__FresHmaN 已提交
31 32 33 34 35
    /**
     * 得到review noactive status,默认状态
     */
    protected function actionNoActiveStatus()
    {
T
Terry 已提交
36 37
        $model = $this->_reviewModel;
        return $model::NOACTIVE_STATUS;
_
__FresHmaN 已提交
38 39 40 41 42 43 44
    }

    /**
     * 得到review active status 审核通过的状态
     */
    protected function actionActiveStatus()
    {
T
Terry 已提交
45 46
        $model = $this->_reviewModel;
        return $model::ACTIVE_STATUS;
_
__FresHmaN 已提交
47 48 49 50 51 52 53
    }

    /**
     * 得到review refuse status 审核拒绝的状态
     */
    protected function actionRefuseStatus()
    {
T
Terry 已提交
54 55
        $model = $this->_reviewModel;
        return $model::REFUSE_STATUS;
_
__FresHmaN 已提交
56 57 58 59 60 61 62 63 64
    }

    /**
     * @property $arr | Array
     * 初始化review model的属性,因为每一个产品的可能添加的评论字段不同。
     */
    protected function actionInitReviewAttr($arr)
    {
        if (!empty($arr) && is_array($arr)) {
T
Terry 已提交
65
            $attr_arr = $this->_reviewModel->attributes(true);
_
__FresHmaN 已提交
66 67
            $arr_keys = array_keys($arr);
            $attrs = array_diff($arr_keys, $attr_arr);
T
Terry 已提交
68
            $this->_reviewModel->addCustomAttrs($attrs);
_
__FresHmaN 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        }
    }

    public function getPrimaryKey()
    {
        return '_id';
    }

    /**
     * @property $spu | String.
     * 通过spu找到评论总数。
     */
    protected function actionGetCountBySpu($spu)
    {
        $where = [
            'product_spu' => $spu,
        ];

        if ($this->filterByLang && ($currentLangCode = Yii::$service->store->currentLangCode)) {
            $where['lang_code'] = $currentLangCode;
        }
T
Terry 已提交
90
        $count = $this->_reviewModel->find()->asArray()->where($where)->count();
_
__FresHmaN 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

        return  $count ? $count : 0;
    }

    /**
     * example filter:
     * [
     * 		'numPerPage' 	=> 20,
     * 		'pageNum'		=> 1,
     * 		'orderBy'	=> ['review_date' => SORT_DESC],
     * 		where'			=> [
     * 			['spu' => 'uk10001'],
     * 		],
     * 		'asArray' => true,
     * ]
     * 通过spu找到评论listing.
     */
    protected function actionGetListBySpu($filter)
    {
        if ($this->filterByLang && ($currentLangCode = Yii::$service->store->currentLangCode)) {
            $filter['where'][] = ['lang_code' => $currentLangCode];
        }
T
Terry 已提交
113
        $query = $this->_reviewModel->find();
_
__FresHmaN 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);

        return [
            'coll' => $query->all(),
            'count'=> $query->count(),
        ];
    }

    /**
     * @property $review_data | Array
     *
     * 增加评论 前台增加评论调用的函数。
     */
    protected function actionAddReview($review_data)
    {
        //$this->initReviewAttr($review_data);
T
Terry 已提交
130
        $model = new $this->_reviewModelName();
_
__FresHmaN 已提交
131 132 133
        if (isset($review_data[$this->getPrimaryKey()])) {
            unset($review_data[$this->getPrimaryKey()]);
        }
T
Terry 已提交
134 135
        $model = $this->_reviewModel;
        $review_data['status'] = $model::NOACTIVE_STATUS;
_
__FresHmaN 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

        $review_data['store'] = Yii::$service->store->currentStore;
        $review_data['lang_code'] = Yii::$service->store->currentLangCode;
        $review_data['review_date'] = time();
        if (!Yii::$app->user->isGuest) {
            $identity = Yii::$app->user->identity;
            $user_id = $identity['id'];
            $review_data['user_id'] = $user_id;
        }

        $review_data['ip'] = \fec\helpers\CFunc::get_real_ip();
        $saveStatus = Yii::$service->helper->ar->save($model, $review_data);

        return true;
    }

    /**
     * @property $review_data | Array
     * 保存评论
     */
    protected function actionUpdateReview($review_data)
    {
        //$this->initReviewAttr($review_data);
T
Terry 已提交
159
        $model = $this->_reviewModel->findOne([$this->getPrimaryKey()=> $review_data[$this->getPrimaryKey()]]);
_
__FresHmaN 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        unset($review_data[$this->getPrimaryKey()]);
        $saveStatus = Yii::$service->helper->ar->save($model, $review_data);

        return true;
    }

    /*
     * example filter:
     * [
     * 		'numPerPage' 	=> 20,
     * 		'pageNum'		=> 1,
     * 		'orderBy'	=> [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ],
     * 		'where'			=> [
                ['>','price',1],
                ['<=','price',10]
     * 			['sku' => 'uk10001'],
     * 		],
     * 	'asArray' => true,
     * ]
     * 查看review 的列表
     */
    protected function actionList($filter)
    {
T
Terry 已提交
183
        $query = $this->_reviewModel->find();
_
__FresHmaN 已提交
184 185 186 187
        $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);

        return [
            'coll' => $query->all(),
T
Terry 已提交
188
            'count'=> $query->limit(null)->offset(null)->count(),
_
__FresHmaN 已提交
189 190 191 192 193 194 195 196 197 198 199
        ];
    }

    /**
     * @property $_id | String
     * 后台编辑 通过评论id找到评论
     * 注意:因为每个产品的评论可能加入了新的字段,因此不能使用ActiveRecord的方式取出来,
     * 使用下面的方式可以把字段都取出来。
     */
    protected function actionGetByReviewId($_id)
    {
T
Terry 已提交
200
        return $this->_reviewModel->getCollection()->findOne([$this->getPrimaryKey() => $_id]);
_
__FresHmaN 已提交
201 202 203
    }

    /**
204
     * @property $primaryKey | String 主键值
_
__FresHmaN 已提交
205 206 207 208 209
     * get artile model by primary key.
     */
    protected function actionGetByPrimaryKey($primaryKey)
    {
        if ($primaryKey) {
T
Terry 已提交
210
            return $this->_reviewModel->findOne($primaryKey);
_
__FresHmaN 已提交
211
        } else {
T
Terry 已提交
212
            return new $this->_reviewModelName();
_
__FresHmaN 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        }
    }

    /**
     * @property $filter|array
     * get artile collection by $filter
     * example filter:
     * [
     * 		'numPerPage' 	=> 20,
     * 		'pageNum'		=> 1,
     * 		'orderBy'	=> [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ],
     'where'			=> [
     ['>','price',1],
     ['<=','price',10]
     * 			['sku' => 'uk10001'],
     * 		],
     * 	'asArray' => true,
     * ]
     */
    protected function actionColl($filter = '')
    {
        return $this->list($filter);
    }

    /**
     * @property $one|array , save one data .
     * @property $originUrlKey|string , article origin url key.
     * 评论,后台审核评论的保存方法。
     * 保存后,把评论信息更新到产品表中。
     */
    protected function actionSave($one)
    {
        $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
        $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
        $one['status'] = (int) $one['status'];
        $one['rate_star'] = (int) $one['rate_star'];

        if ($primaryVal) {
T
Terry 已提交
251
            $model = $this->_reviewModel->findOne($primaryVal);
_
__FresHmaN 已提交
252
            if (!$model) {
T
Terry 已提交
253
                Yii::$service->helper->errors->add('reviewModel '.$this->getPrimaryKey().' is not exist');
_
__FresHmaN 已提交
254 255 256 257

                return;
            }
        } else {
T
Terry 已提交
258
            $model = new $this->_reviewModelName();
_
__FresHmaN 已提交
259 260 261 262
            $model->created_admin_user_id = \fec\helpers\CUser::getCurrentUserId();
            $primaryVal = new \MongoDB\BSON\ObjectId();
            $model->{$this->getPrimaryKey()} = $primaryVal;
        }
T
Terry 已提交
263
        //$review_data['status'] = $this->_reviewModel->ACTIVE_STATUS;
_
__FresHmaN 已提交
264 265 266 267 268 269 270 271 272
        $model->review_date = time();
        unset($one[$this->getPrimaryKey()]);
        $saveStatus = Yii::$service->helper->ar->save($model, $one);
        $model->save();
        // 更新评论信息到产品表中。
        $this->updateProductSpuReview($model['product_spu'], $model['lang_code']);

        return true;
    }
273 274 275 276 277
    /**
     * @property $ids | Array or String
     * @return boolean
     * 根据提供的ReviewId,删除产品评论
     */
_
__FresHmaN 已提交
278 279 280 281 282 283 284 285 286
    protected function actionRemove($ids)
    {
        if (!$ids) {
            Yii::$service->helper->errors->add('remove id is empty');

            return false;
        }
        if (is_array($ids) && !empty($ids)) {
            foreach ($ids as $id) {
T
Terry 已提交
287
                $model = $this->_reviewModel->findOne($id);
_
__FresHmaN 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
                if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
                    $product_spu = $model['product_spu'];
                    $model->delete();
                    // 更新评论信息到产品表中。
                    $this->updateProductSpuReview($product_spu, $model['lang_code']);
                } else {
                    //throw new InvalidValueException("ID:$id is not exist.");
                    Yii::$service->helper->errors->add("Review Remove Errors:ID $id is not exist.");

                    return false;
                }
            }
        } else {
            $id = $ids;
T
Terry 已提交
302
            $model = $this->_reviewModel->findOne($id);
_
__FresHmaN 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
                $model->delete();
            } else {
                Yii::$service->helper->errors->add("Review Remove Errors:ID:$id is not exist.");

                return false;
            }
        }

        return true;
    }

    /**
     * @property $ids | Array
     * 通过 $ids 数组,批量审核通过评论
     */
    protected function actionAuditReviewByIds($ids)
    {
T
Terry 已提交
321
        $reviewModel = $this->_reviewModel;
_
__FresHmaN 已提交
322 323 324 325
        if (is_array($ids) && !empty($ids)) {
            $identity = Yii::$app->user->identity;
            $user_id = $identity['id'];
            foreach ($ids as $id) {
T
Terry 已提交
326
                $model = $this->_reviewModel->findOne($id);
_
__FresHmaN 已提交
327 328 329
                if ($model[$this->getPrimaryKey()]) {
                    $model->audit_user = $user_id;
                    $model->audit_date = time();
330
                    $model->status = $reviewModel->getActiveStatus();
_
__FresHmaN 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344
                    $model->save();
                    // 更新评论信息到产品表中。
                    $this->updateProductSpuReview($model['product_spu'], $model['lang_code']);
                }
            }
        }
    }

    /**
     * @property $ids | Array
     * 通过 $ids 数组,批量审核评论拒绝
     */
    protected function actionAuditRejectedReviewByIds($ids)
    {
T
Terry 已提交
345
        $reviewModel = $this->_reviewModel;
_
__FresHmaN 已提交
346 347 348 349
        if (is_array($ids) && !empty($ids)) {
            $identity = Yii::$app->user->identity;
            $user_id = $identity['id'];
            foreach ($ids as $id) {
T
Terry 已提交
350
                $model = $this->_reviewModel->findOne($id);
_
__FresHmaN 已提交
351 352 353
                if ($model[$this->getPrimaryKey()]) {
                    $model->audit_user = $user_id;
                    $model->audit_date = time();
354
                    $model->status = $reviewModel->getRefuseStatus();
_
__FresHmaN 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368
                    $model->save();
                    // 更新评论的信息到产品表
                    $this->updateProductSpuReview($model['product_spu'], $model['lang_code']);
                }
            }
        }
    }

    /**
     * @property $spu | String
     * 当评论保存,更新评论的总数,平均评分信息到产品表的所有spu
     */
    protected function actionUpdateProductSpuReview($spu, $lang_code)
    {
T
Terry 已提交
369
        $reviewModel = $this->_reviewModel;
_
__FresHmaN 已提交
370 371 372
        $filter = [
            'where'            => [
                ['product_spu' => $spu],
373
                ['status' => $reviewModel->getActiveStatus()],
_
__FresHmaN 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            ],
        ];
        $coll = $this->coll($filter);
        $count = $coll['count'];
        $data = $coll['coll'];
        $rate_total = 0;
        $rate_lang_total = 0;
        $lang_count = 0;
        if (!empty($data) && is_array($data)) {
            foreach ($data as $one) {
                $rate_total += $one['rate_star'];
                if ($lang_code == $one['lang_code']) {
                    $rate_lang_total += $one['rate_star'];
                    $lang_count++;
                }
            }
        }
        if ($count == 0) {
            $avag_rate = 0;
        } else {
            $avag_rate = ceil($rate_total / $count);
        }
        if ($lang_count == 0) {
            $avag_lang_rate = 0;
        } else {
            $avag_lang_rate = ceil($rate_lang_total / $lang_count);
        }

        Yii::$service->product->updateProductReviewInfo($spu, $avag_rate, $count, $lang_code, $avag_lang_rate, $lang_count);

        return true;
    }

    /**
     * @property $filter|array
     * get artile collection by $filter
     * example filter:
     * [
     * 		'numPerPage' 	=> 20,
     * 		'pageNum'		=> 1,
     * 		'orderBy'	=> [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ],
415 416 417
     *      'where'			=> [
     *          ['>','price',1],
     *          ['<=','price',10]
_
__FresHmaN 已提交
418 419
     * 			['sku' => 'uk10001'],
     * 		],
420
     * 	    'asArray' => true,
_
__FresHmaN 已提交
421 422 423 424
     * ]
     */
    protected function actionGetReviewsByUserId($filter)
    {
T
Terry 已提交
425
        $query = $this->_reviewModel->find();
_
__FresHmaN 已提交
426 427 428 429 430 431 432 433
        $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);

        return [
            'coll' => $query->all(),
            'count'=> $query->count(),
        ];
    }
}