/**
  * export excel for the promotioncode to upload to qiniu,and return key to frontend
  */
 public function actionExport()
 {
     $params = $this->getQuery();
     if (empty($params['createdAt']) || empty($params['productId'])) {
         throw new BadRequestHttpException('missing param createdAt or productId');
     }
     $productId = new MongoId($params['productId']);
     $product = Product::findByPk($productId);
     if (empty($product)) {
         throw new BadRequestHttpException('invalid productId');
     }
     $startTime = new MongoDate($params['createdAt']);
     $endTime = new MongoDate($params['createdAt'] + 1);
     $condition = ['productId' => $productId, 'createdAt' => ['$gte' => $startTime, '$lt' => $endTime]];
     $data = PromotionCode::findOne($condition);
     if ($data) {
         $accountId = $this->getAccountId();
         list($sku, $code, $isUsed) = explode(',', Yii::t('product', 'export_promotioncode_title'));
         $header = ['code' => $code, 'isUsed' => $isUsed];
         $key = $product['name'] . '_' . date('Ymd') . '_' . $product['sku'];
         $status = ['vaild' => 'Y', 'unvaild' => 'N'];
         $fields = 'code,isUsed';
         $exportArgs = ['status' => $status, 'header' => $header, 'key' => $key, 'sku' => $product->sku, 'accountId' => (string) $accountId, 'condition' => serialize($condition), 'fields' => $fields, 'description' => 'Direct: export promotionCodes'];
         $jobId = Yii::$app->job->create('backend\\modules\\product\\job\\ExportPromotionCode', $exportArgs);
         $result = ['result' => 'success', 'message' => 'exporting file', 'data' => ['jobId' => $jobId, 'key' => $key]];
     } else {
         $result = ['result' => 'error', 'message' => 'no datas', 'data' => []];
     }
     return $result;
 }
Esempio n. 2
0
 /**
  * exchange the campaign code
  * 1.check the code whether is vaild
  * 2.if the code is vaild,get the productId from the promotioncode,
  * then to find which campaign is fit with this product and add score for member and record log
  * 3.if code is invalid,throw a exception
  * @param $params, array, user to commit data to server
  * @param $accountId, MongoId, account id
  * @param $type, string, whitch chnanel that user to exchange
  */
 public static function exchangeCampaignCode($params, $accountId, $type = 'mobile')
 {
     $member = Member::findOne(['_id' => $params['memberId'], 'accountId' => $accountId]);
     if (empty($member) || $member->isDisabled) {
         throw new InvalidParameterException('无效的会员!');
     }
     //get productid
     $where = ['code' => $params['code'], 'accountId' => $member->accountId, 'isUsed' => false];
     $promotionCode = PromotionCode::findOne($where);
     if (empty($promotionCode)) {
         if ('mobile' == $type) {
             throw new InvalidParameterException(['exchange-tip' => '促销码无效!']);
         } else {
             return ['result' => 'error', 'message' => '促销码无效!'];
         }
     }
     $params['productId'] = $promotionCode->productId;
     //add a operation to clear cache  when member exchange code in offline
     if ($type != 'mobile') {
         self::clearExchangeRecord((string) $member->_id, '');
     }
     //add score for member and log log
     $isTip = $type == 'mobile' ? true : false;
     //whether have campaign to redeem this code
     list($result, $data) = self::getCode2Campaign($params, $member);
     LogUtil::info(['message' => 'whether have campaign to redeem this code', 'result' => $result, 'number' => count($data), 'params' => $params], 'exchangeCode');
     //update promotion code
     if ($result) {
         $row = self::updatePromotionCode($params, $member, $promotionCode->_id);
         LogUtil::info(['message' => 'update promotion code', 'affectRow' => $row, 'promotionCode' => Json::encode($promotionCode->toArray()), 'member' => Json::encode($member->toArray()), 'params' => $params], 'exchangeCode');
     }
     if ($result && $row == 1) {
         //redeem code in every camoaign, if the result is true and row is 1
         return self::redeemCampaign($params, $data, $member, true, $isTip);
     } else {
         if ('mobile' == $type) {
             throw new InvalidParameterException(['exchange-tip' => $data]);
         } else {
             LogUtil::error(['message' => 'Faild to exchange code', 'code' => $code, 'result' => $result, 'row' => $row], 'product');
             return ['result' => 'error', 'message' => $data];
         }
     }
 }