public static function createStatsCouponLog($dateStr, $stats)
 {
     //check data whether exists today
     $result = ModelStatsCouponLogDaily::findOne(['date' => $dateStr]);
     if ($result) {
         LogUtil::info(['message' => $dateStr . ' coupon log data is exists'], 'resque');
         return true;
     }
     $stats = CouponLog::formatStruct($stats);
     $data = $couponIds = [];
     foreach ($stats as $stat) {
         $receivedKey = $stat['couponId'] . '_' . CouponLog::RECIEVED;
         $redeemedKey = $stat['couponId'] . '_' . CouponLog::REDEEMED;
         if (!in_array($stat['couponId'], $couponIds)) {
             $couponIds[] = $stat['couponId'];
             $receivedNum = isset($stats[$receivedKey]['count']) ? $stats[$receivedKey]['count'] : 0;
             $redeemedNum = isset($stats[$redeemedKey]['count']) ? $stats[$redeemedKey]['count'] : 0;
             $data[] = ['couponId' => $stat['couponId'], 'accountId' => $stat['accountId'], 'recievedNum' => $receivedNum + $redeemedNum, 'redeemedNum' => $redeemedNum, 'date' => $dateStr];
         }
     }
     unset($stat, $stats, $couponIds);
     if (false === ModelStatsCouponLogDaily::batchInsert($data)) {
         LogUtil::error(['message' => 'Faild to create statis daily couponlog', 'date' => $dateStr, 'data' => json_encode($data)], 'resque');
     }
 }
 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;
 }
 /**
  * @args {"description": "Direct: update Stats of coupon"}
  */
 public function perform()
 {
     $args = $this->args;
     $startTime = strtotime($args['startTime']);
     $endTime = strtotime($args['endTime']);
     $current = strtotime(date('Y-m-d'));
     if ($endTime > $current) {
         $endTime = $current;
     }
     for ($t = $startTime; $t <= $endTime; $t += 3600 * 24) {
         $dateStr = date('Y-m-d', $t);
         ModelStatsCouponLogDaily::deleteAll(['date' => $dateStr]);
         $stats = CouponLog::getStats($dateStr);
         if (!empty($stats)) {
             StatsCouponLogDaily::createStatsCouponLog($dateStr, $stats);
         }
         LogUtil::info(['message' => $dateStr . ': Update StatsCouponLogDaily'], 'update_coupon_log');
     }
     return true;
 }