public function actionStatsTotalCoupon() { $params = $this->getQuery(); if (empty($params['id'])) { throw new BadRequestHttpException(Yii::t('common', 'parameters_missing')); } $id = new MongoId($params['id']); $coupon = Coupon::findOne(["_id" => $id]); if (empty($coupon)) { throw new BadRequestHttpException(Yii::t('product', 'membershipDiscount_is_deleted')); } $couponTotalInfo = StatsCouponLogDaily::getCouponLogTotalStats($id); $item = empty($couponTotalInfo[0]) ? [] : $couponTotalInfo[0]; return $item; }
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']; }