public static function generateData($memberProperty, $property, $year, $quarter, $accountId)
 {
     $propertyKey = 'memProperty.' . $memberProperty->_id;
     $keys = ['productId' => true, $propertyKey => true];
     $condition = ['year' => $year, 'quarter' => $quarter, 'accountId' => $accountId];
     $initial = ['avg' => 0, 'codes' => ['count' => 0], 'members' => ['count' => 0]];
     $reduce = 'function(doc, prev) {
         if (!prev.members[doc.memberId]) {
             prev.members[doc.memberId] = true;
             prev.members["count"]++;
         }
         if (!prev.codes[doc.code]) {
             prev.codes[doc.code] = true;
             prev.codes["count"]++;
         }
     }';
     $finalize = 'function(prev) {
         prev.avg = prev.codes["count"] / prev.members["count"];
         delete prev.codes;
         delete prev.members;
     }';
     $statsItems = ModelStatsMemberCampaignLogDaily::getCollection()->group($keys, $initial, $reduce, ['condition' => $condition, 'finalize' => $finalize]);
     foreach ($statsItems as $statsItem) {
         $condition['propId'] = $property;
         $condition['propValue'] = $statsItem[$propertyKey];
         $productId = $statsItem['productId'];
         $condition['productId'] = $productId;
         $stats = ModelStatsMemberPropTradeCodeAvgQuarterly::findOne($condition);
         if (empty($stats)) {
             $stats = new ModelStatsMemberPropTradeCodeAvgQuarterly();
             $product = Product::findByPk($productId);
             $productName = '';
             if (!empty($product)) {
                 $productName = $product->name;
             }
             $stats->propId = $property;
             $stats->propValue = $statsItem[$propertyKey];
             $stats->productId = $productId;
             $stats->productName = $productName;
             $stats->year = $year;
             $stats->quarter = $quarter;
             $stats->accountId = $accountId;
         }
         $stats->avg = $statsItem['avg'];
         $stats->save();
     }
 }
 /**
  * @args {"description": "Direct: Analysis participate promotion code "}
  */
 public function perform()
 {
     $yesterday = ModelPromotionCodeAnalysis::getTime(-1);
     $type = new MongoInt32(ModelPromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE);
     $where = ['createdAt' => $yesterday, 'type' => $type];
     $status = ModelPromotionCodeAnalysis::checkExistData($where);
     if ($status) {
         $yesterdayStamp = TimeUtil::today() - 24 * 3600;
         $yesterday = new MongoDate($yesterdayStamp);
         $createWhere = ModelPromotionCodeAnalysis::getCreateTime();
         $campaignIds = CampaignLog::distinct('campaignId', $createWhere);
         $campaignLogs = [];
         if (!empty($campaignIds)) {
             $where = array_merge($createWhere, ['campaignId' => ['$in' => $campaignIds]]);
             $campaignLogs = CampaignLog::getCollection()->aggregate([['$match' => $where], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'memberId' => '$member.id', 'accountId' => '$accountId', 'productId' => '$productId']]]]);
         }
         if (!empty($campaignLogs)) {
             //get total for take part in a campaign
             $campaignData = [];
             foreach ($campaignLogs as $data) {
                 $campaignId = $data['_id']['campaignId'];
                 $key = (string) $campaignId . (string) $data['_id']['productId'];
                 if (isset($campaignData[$key])) {
                     //to sum the total in every product in same campaign
                     $campaignData[$key]['total'] += 1;
                 } else {
                     $product = Product::findByPk($data['_id']['productId']);
                     $productName = empty($product['name']) ? '' : $product['name'];
                     $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'campaignId' => $campaignId, 'accountId' => $data['_id']['accountId'], 'createdAt' => $yesterday, 'total' => 1, 'type' => $type];
                     $campaignData[$key] = $result;
                 }
             }
             if (false === ModelPromotionCodeAnalysis::batchInsert($campaignData)) {
                 LogUtil::error(['message' => 'Faild to create daily data', 'date' => date('Y-m-d H:i:s'), 'data' => json_encode($campaignData)], 'resque');
             }
             unset($datas, $campaignIds, $campaignData);
         }
     } else {
         LogUtil::info(['message' => 'Participate analysis data is exists', 'date' => date('Y-m-d H:i:s')], 'resque');
     }
     return true;
 }
 public function actionCreate()
 {
     $goods = $this->getParams('goods');
     $storeId = $this->getParams('storeId');
     $accountId = $this->getAccountId();
     if (empty($goods) || empty($storeId)) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $storeId = new \MongoId($storeId);
     $store = Store::findByPk($storeId);
     if (empty($store)) {
         throw new InvalidParameterException(Yii::t('common', 'data_error'));
     }
     $productIds = [];
     foreach ($goods as $item) {
         if (!StoreGoods::validatePrice($item['price'])) {
             throw new InvalidParameterException(Yii::t('store', 'price_error'));
         }
         $productIds[] = new \MongoId($item['productId']);
     }
     $storeGoodsCount = StoreGoods::countByProductId($productIds, $storeId);
     if ($storeGoodsCount > 0) {
         throw new InvalidParameterException(Yii::t('store', 'goods_exists'));
     }
     $storeGoods = [];
     foreach ($goods as $item) {
         $product = Product::findByPk($item['productId']);
         if (empty($product)) {
             throw new InvalidParameterException(Yii::t('common', 'data_error'));
         }
         $category = $product->category;
         $pictures = ArrayHelper::getColumn($product->pictures, 'url', false);
         $pictures = array_slice($pictures, 0, 5);
         $storeGoods[] = ['storeId' => $storeId, 'categoryId' => $category['id'], 'productName' => $product->name, 'sku' => $product->sku, 'productId' => $product->_id, 'pictures' => $pictures, 'status' => StoreGoods::STATUS_OFF, 'offShelfTime' => new \Mongodate(), 'price' => !empty($item['price']) ? floatval($item['price']) : 0.0, 'accountId' => $accountId];
     }
     if (StoreGoods::batchInsert($storeGoods)) {
         return ['message' => 'OK', 'data' => null];
     } else {
         throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
     }
 }
 /**
  * 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;
 }
 /**
  * create the structure for promorioncodeanalysis
  * @param $datas,array,source data
  * @param $type,mongoInt,the type for ananlysis
  * @param $yesterday,mongodate, create time
  */
 public static function createAnalysisData($datas, $type, $yesterday)
 {
     $campaignData = [];
     foreach ($datas as $data) {
         if (!empty($data['_id']['productId'])) {
             $productId = $data['_id']['productId'];
             $product = Product::findByPk($data['_id']['productId']);
             $productName = empty($product['name']) ? 'unknow' : $product['name'];
         } else {
             $productId = $productName = '';
         }
         $campaignData[] = ['createdAt' => $yesterday, 'total' => $data['total'], 'type' => $type, 'productId' => $productId, 'productName' => $productName, 'campaignId' => $data['_id']['campaignId'], 'accountId' => $data['_id']['accountId']];
     }
     return $campaignData;
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['beginTime']) || empty($args['endTime']) || empty($args['type'])) {
         ResqueUtil::log(['error' => 'missing params', 'args' => $args]);
         return false;
     }
     $beginTime = strtotime($args['beginTime']) + 3600 * 24;
     $endTime = strtotime($args['endTime']);
     if ($endTime > time()) {
         $endTime = strtotime(date('Y-m-d', time()));
     }
     $endTime += 3600 * 24;
     $type = new \MongoInt32($args['type']);
     switch ($args['type']) {
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_PARTICIPATE:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //create data begin
                 $createWhere = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignIds = CampaignLog::distinct('campaignId', $createWhere);
                 $campaignLogs = [];
                 if (!empty($campaignIds)) {
                     $where = array_merge($createWhere, ['campaignId' => ['$in' => $campaignIds]]);
                     $campaignLogs = CampaignLog::getCollection()->aggregate([['$match' => $where], ['$group' => ['_id' => ['campaignId' => '$campaignId', 'accountId' => '$accountId', 'productId' => '$productId', 'memberId' => '$member.id']]]]);
                 }
                 if (!empty($campaignLogs)) {
                     //get total for take part in a campaign
                     $campaignData = [];
                     foreach ($campaignLogs as $data) {
                         $campaignId = $data['_id']['campaignId'];
                         $key = (string) $campaignId . (string) $data['_id']['productId'];
                         if (isset($campaignData[$key])) {
                             //to sum the total in every product in same campaign
                             $campaignData[$key]['total'] += 1;
                         } else {
                             $product = Product::findByPk($data['_id']['productId']);
                             $productName = empty($product['name']) ? '' : $product['name'];
                             $result = ['productId' => $data['_id']['productId'], 'productName' => $productName, 'campaignId' => $campaignId, 'accountId' => $data['_id']['accountId'], 'createdAt' => new \MongoDate($t - 3600 * 24), 'total' => 1, 'type' => $type];
                             $campaignData[$key] = $result;
                         }
                     }
                     PromotionCodeAnalysis::batchInsert($campaignData);
                     unset($datas, $campaignIds, $campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL:
             //delete data and create data
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 //get campaignlogs in yesterday
                 $campaignLogs = PromotionCodeAnalysis::getMemberCampaignLog(false, $t);
                 //create datas
                 $yesterday = new \MongoDate($t - 2 * 24 * 3600);
                 if (!empty($campaignLogs)) {
                     $campaignData = [];
                     foreach ($campaignLogs as $key => $campaignLog) {
                         //get total the day yesterday
                         $productId = $campaignLog['_id']['productId'];
                         $campaignId = $campaignLog['_id']['campaignId'];
                         $accountId = $campaignLog['_id']['accountId'];
                         $condition = ['productId' => $productId, 'campaignId' => $campaignId, 'accountId' => $accountId, 'createdAt' => $yesterday, 'type' => $type];
                         $yesterdayData = PromotionCodeAnalysis::findOne($condition);
                         if (empty($yesterdayData)) {
                             $yesterdayData['total'] = 0;
                         }
                         $condition = ['campaignId' => $campaignId, 'accountId' => $accountId, 'productId' => $productId];
                         $number = PromotionCodeAnalysis::checkMemberUnique($condition, $t);
                         //subtract the member who is recorded before
                         $total = $yesterdayData['total'] + $number;
                         $campaignLogs[$key]['total'] = $total;
                     }
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 24 * 3600));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
                 //set the default value when the value is not exists
                 PromotionCodeAnalysis::setDefault($t - 2 * 24 * 3600, $type);
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_EVERYDAY_PRIZE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 $campaignLogs = PromotionCodeAnalysis::getCampaignLog(false, $t);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
         case PromotionCodeAnalysis::PROMOTION_CODE_ANALYSIS_TOTAL_PARTICIPATE:
             for ($t = $beginTime; $t <= $endTime; $t += 3600 * 24) {
                 //delete data
                 $where = ['createdAt' => new \MongoDate($t - 3600 * 24), 'type' => $type];
                 PromotionCodeAnalysis::deleteAll($where);
                 unset($where);
                 $where = PromotionCodeAnalysis::getCreateTime($t);
                 $campaignLogs = PromotionCodeAnalysis::getMemberAllTimes($where);
                 if (!empty($campaignLogs)) {
                     $campaignData = PromotionCodeAnalysis::createAnalysisData($campaignLogs, $type, new \MongoDate($t - 3600 * 24));
                     PromotionCodeAnalysis::batchInsert($campaignData);
                 }
             }
             break;
     }
     return true;
 }
Example #7
0
 public function changeProductStatus($productId)
 {
     $product = Product::findByPk($productId);
     if (false == $product['isBindCode']) {
         Product::updateAll(['isBindCode' => true], ['_id' => $productId]);
     }
 }
Example #8
0
 public static function _generateCodePrefix($productId)
 {
     $year = date('y');
     $year = $year - 10;
     //start from 2015
     $yearPrefix = self::_getPrefix(1, $year);
     $product = Product::findByPk($productId);
     $productIndex = Product::find()->where(['createdAt' => ['$lte' => $product->createdAt], 'accountId' => $product->accountId])->orderBy(['createdAt' => SORT_ASC])->count();
     $productPrefix = self::_getPrefix(2, $productIndex);
     $batchPrefix = self::_getPrefix(1, $product->batchCode);
     return $yearPrefix . $productPrefix . $batchPrefix;
 }