Esempio n. 1
0
 public function actionUpdate($id)
 {
     $params = $this->getParams();
     if (empty($params['url']) && empty($params['total']) && empty($params['time'])) {
         throw new InvalidParameterException(Yii::t('product', 'invalide_params'));
     }
     $where = ['_id' => new MongoId($id)];
     $coupon = Coupon::findOne($where);
     if (empty($coupon)) {
         throw new InvalidParameterException(Yii::t('product', 'invalide_params'));
     }
     if (!empty($params['time'])) {
         $params = Coupon::converCouponTime($params);
     }
     $data = [];
     if (!empty($params['total'])) {
         if ($coupon->total + $params['total'] < 0) {
             throw new InvalidParameterException(Yii::t('product', 'coupon_total_overflow'));
         } else {
             $data['$inc'] = ['total' => $params['total']];
             //add a condition to check the total
             if ($params['total'] < 0) {
                 $where['total'] = ['$gte' => intval($params['total'])];
             }
         }
     }
     $saveKeys = ['url', 'time'];
     foreach ($saveKeys as $saveKey) {
         if (isset($params[$saveKey])) {
             $data['$set'][$saveKey] = $params[$saveKey];
         }
     }
     Coupon::updateAll($data, $where);
     return ['result' => 'Ok', 'message' => 'update coupon successful'];
 }