public static function getSearchCondition($accountId, $params)
 {
     $condition = ['accountId' => $accountId];
     if (!empty($params['searchKey'])) {
         $searchKey = StringUtil::regStrFormat($params['searchKey']);
         $searchKey = new MongoRegex("/{$searchKey}/i");
         $condition['transactionId'] = $searchKey;
     }
     if (!empty($params['startTime'])) {
         $condition['refundAt']['$gte'] = new MongoDate(TimeUtil::ms2sTime($params['startTime']));
     }
     if (!empty($params['endTime'])) {
         $condition['refundAt']['$lt'] = new MongoDate(TimeUtil::ms2sTime($params['endTime']));
     }
     return $condition;
 }
 /**
  * Prepares the data provider that should return the requested collection of the models.
  * @return ActiveDataProvider
  */
 protected function prepareDataProvider()
 {
     if ($this->prepareDataProvider !== null) {
         return call_user_func($this->prepareDataProvider, $this);
     }
     /* @var $modelClass \yii\db\BaseActiveRecord */
     $modelClass = $this->modelClass;
     $query = $modelClass::find();
     $token = $this->getAccessToken();
     $accountId = $this->getAccountId();
     $whereCondition = ['accountId' => $accountId];
     if (!new $modelClass() instanceof PlainModel) {
         $whereCondition['isDeleted'] = $modelClass::NOT_DELETED;
     }
     if ($orderBy = Yii::$app->request->get('orderBy', 'createdAt')) {
         if (StringUtil::isJson($orderBy)) {
             $orderBy = Json::decode($orderBy, true);
             foreach ($orderBy as $key => $value) {
                 if ($value === 'asc' || $value === 'ASC') {
                     $orderBy[$key] = SORT_ASC;
                 } else {
                     $orderBy[$key] = SORT_DESC;
                 }
             }
         } else {
             $orderBy = [$orderBy => SORT_DESC];
         }
         $query->orderBy($orderBy);
     }
     if ($where = Yii::$app->request->get('where')) {
         $keys = ['lt', 'lte', 'gt', 'gte', 'ne', 'in', 'nin', 'all'];
         $where = Json::decode($where, true);
         $newWhere = [];
         //set a default method to get whitch value in a array need to conver to mongoId
         $conver2MongoIds = [];
         if (method_exists($modelClass, 'conver2MongoId')) {
             $conver2MongoIds = $modelClass::conver2MongoId();
         }
         foreach ($where as $index => $condition) {
             if ($index == '_id' || in_array($index, $conver2MongoIds)) {
                 $condition = $this->ensureMongoId($condition);
             }
             if (is_array($condition)) {
                 $newCondition = [];
                 foreach ($condition as $key => $value) {
                     if (in_array($key, $keys)) {
                         $key = '$' . $key;
                     }
                     $newCondition[$key] = $value;
                 }
                 $condition = $newCondition;
             }
             $newWhere[$index] = $condition;
         }
         $whereCondition = array_merge($whereCondition, $newWhere);
     }
     if ($search = Yii::$app->request->get('search')) {
         $search = Json::decode($search, true);
         foreach ($search as $key => $value) {
             $value = trim($value);
             $value = StringUtil::regStrFormat($value);
             $where[$key] = new \MongoRegex("/{$value}/i");
         }
         $whereCondition = array_merge($whereCondition, $where);
     }
     $query->where($whereCondition);
     $unlimited = Yii::$app->request->get('unlimited', false);
     if ($unlimited) {
         return ['items' => $query->all()];
     }
     return new ActiveDataProvider(['query' => $query]);
 }
Exemple #3
0
 /**
  * Search product by conditions
  * @param Array $params
  * @param string $accountId
  * @return product info
  */
 public static function search($params, $accountId)
 {
     $query = self::find();
     $comma = ',';
     $condition = ['accountId' => $accountId, 'isDeleted' => StoreGoods::NOT_DELETED];
     if (!empty($params['categoryIds'])) {
         $categorys = explode($comma, $params['categoryIds']);
         $categoryIds = [];
         foreach ($categorys as $category) {
             $categoryIds[] = new \MongoId($category);
         }
         $categorys = ['$in' => $categoryIds];
         $condition['categoryId'] = $categorys;
     }
     if (array_key_exists('searchKey', $params) && '' != $params['searchKey']) {
         $key = $params['searchKey'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new \MongoRegex("/{$key}/i");
         $search = ['$or' => [['productName' => $keyReg], ['sku' => $keyReg]]];
         $condition = array_merge($condition, $search);
     }
     if (!empty($params['status'])) {
         $condition['status'] = $params['status'];
     }
     if (!empty($params['storeId'])) {
         $condition['storeId'] = $params['storeId'];
     }
     if (isset($params['saleTimeFrom']) && $params['saleTimeFrom'] !== '') {
         $condition['onSaleTime']['$gte'] = new \MongoDate(TimeUtil::ms2sTime($params['saleTimeFrom']));
     }
     if (isset($params['saleTimeTo']) && $params['saleTimeTo'] !== '') {
         $condition['onSaleTime']['$lte'] = new \MongoDate(TimeUtil::ms2sTime($params['saleTimeTo']));
     }
     if (isset($params['priceFrom']) && $params['priceFrom'] !== '') {
         $condition['price']['$gte'] = floatval($params['priceFrom']);
     }
     if (isset($params['priceTo']) && $params['priceTo'] !== '') {
         $condition['price']['$lte'] = floatval($params['priceTo']);
     }
     $query->orderBy(self::normalizeOrderBy($params));
     $query->where($condition);
     $searchQuery = ['query' => $query];
     return new ActiveDataProvider($searchQuery);
 }
Exemple #4
0
 /**
  * Search product by conditions
  * @param Array $params
  * @param string $accountId
  * @return product info
  */
 public static function search($params, $accountId)
 {
     $query = Goods::find();
     $comma = ',';
     $condition = ['accountId' => $accountId, 'isDeleted' => Goods::NOT_DELETED];
     if (!empty($params['category'])) {
         $categorys = explode($comma, $params['category']);
         $categoryIds = [];
         foreach ($categorys as $category) {
             $categoryIds[] = new MongoId($category);
         }
         $categorys = ['$in' => $categoryIds];
         $condition = array_merge($condition, ['categoryId' => $categorys]);
     }
     if (array_key_exists('searchKey', $params) && '' != $params['searchKey']) {
         $key = $params['searchKey'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new \MongoRegex("/{$key}/i");
         $search = ['$or' => [['productName' => $keyReg], ['sku' => $keyReg]]];
         $condition = array_merge($condition, $search);
     }
     if (!empty($params['notSoldOut'])) {
         $condition['total'] = ['$ne' => 0];
     }
     if (!empty($params['status'])) {
         $condition = self::createStatusCondition($params['status'], $condition);
     }
     $query->orderBy(self::normalizeOrderBy($params));
     $query->where($condition);
     $searchQuery = ['query' => $query];
     if (isset($params['isAll']) && $params['isAll']) {
         $searchQuery = array_merge($searchQuery, ['pagination' => ['pageSize' => 99999]]);
     }
     return new ActiveDataProvider($searchQuery);
 }
Exemple #5
0
 /**
  * Search member by name limit 10
  * @param string $accountId
  * @param string $number
  * @return member info
  */
 public static function searchByName($accountId, $name)
 {
     $name = trim($name);
     $name = StringUtil::regStrFormat($name);
     $nameReg = new \MongoRegex("/({$name})+/i");
     $condition = ['accountId' => $accountId, 'isDeleted' => \backend\components\BaseModel::NOT_DELETED, 'properties' => ['$elemMatch' => ['name' => self::DEFAULT_PROPERTIES_NAME, 'value' => $nameReg]]];
     return Member::find()->andWhere($condition)->limit(10)->all();
 }
Exemple #6
0
 /**
  * Search for coupon recieved, redeemed,deleted records.
  * @param array $params The search condition
  * @param string $accountId
  * @return array The couponLog list for recieved, redeemed,deleted records
  */
 public static function search($params, $accountId)
 {
     $condition = ['accountId' => $accountId, 'status' => $params['status']];
     $query = CouponLog::find();
     if (!empty($params['startTime'])) {
         $startTime = MongodbUtil::msTimetamp2MongoDate($params['startTime']);
         $condition['operationTime']['$gte'] = $startTime;
     }
     if (!empty($params['endTime'])) {
         $endTime = MongodbUtil::msTimetamp2MongoDate($params['endTime']);
         $condition['operationTime']['$lte'] = $endTime;
     }
     if (!empty($params['searchKey'])) {
         $key = $params['searchKey'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new \MongoRegex("/{$key}/i");
         $condition['$or'] = [['member.name' => $keyReg], ['member.phone' => $keyReg], ['title' => $keyReg]];
     }
     if (empty($params['orderBy'])) {
         $orderBy = ['operationTime' => SORT_DESC];
     } else {
         switch ($params['orderBy']) {
             case 'asc':
                 $orderBy = ['operationTime' => SORT_ASC];
                 break;
             default:
                 $orderBy = ['operationTime' => SORT_DESC];
                 break;
         }
     }
     $query = $query->where($condition)->orderBy($orderBy);
     $searchQuery = ['query' => $query];
     return new ActiveDataProvider($searchQuery);
 }
Exemple #7
0
 /**
  * search coupon
  */
 public static function search($params)
 {
     $query = Coupon::find();
     $condition = ['accountId' => $params['accountId'], 'isDeleted' => self::NOT_DELETED];
     if (!empty($params['title'])) {
         $key = $params['title'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new MongoRegex("/{$key}/i");
         $search = ['title' => $keyReg];
         $condition = array_merge($condition, $search);
         unset($search);
     }
     if (!empty($params['unexpired'])) {
         $time = new MongoDate(strtotime(TimeUtil::msTime2String($params['unexpired'], 'Y-m-d')));
         $search = ['$or' => [['time.type' => self::COUPON_ABSOLUTE_TIME, 'time.endTime' => ['$gte' => $time]], ['time.type' => self::COUPON_RELATIVE_TIME]]];
         $condition = array_merge($condition, $search);
         unset($search, $time);
     }
     $query->orderBy(self::normalizeOrderBy($params));
     $query->where($condition);
     $unlimited = Yii::$app->request->get('unlimited', false);
     if ($unlimited) {
         return ['items' => $query->all()];
     }
     return new ActiveDataProvider(['query' => $query]);
 }
 public static function getSearchCondition($accountId, $params)
 {
     $condition = ['accountId' => $accountId, 'status' => self::STATUS_PAID, 'payMode' => self::PAY_MODE_WECHAT];
     if (!empty($params['searchKey'])) {
         $searchKey = StringUtil::regStrFormat($params['searchKey']);
         $searchKey = new MongoRegex("/{$searchKey}/i");
         $condition['transactionId'] = $searchKey;
     }
     if (!empty($params['startTime'])) {
         $condition['paymentTime']['$gte'] = new MongoDate(TimeUtil::ms2sTime($params['startTime']));
     }
     if (!empty($params['endTime'])) {
         $condition['paymentTime']['$lt'] = new MongoDate(TimeUtil::ms2sTime($params['endTime']));
     }
     return $condition;
 }
Exemple #9
0
 /**
  * create condition for search
  */
 public static function createCondition($params, $accountId)
 {
     $condition = ['accountId' => $accountId, 'isDeleted' => self::NOT_DELETED];
     //order number
     if (!empty($params['orderNumber'])) {
         $orderNumber = StringUtil::regStrFormat(trim($params['orderNumber']));
         $condition['orderNumber'] = new MongoRegex("/{$orderNumber}/i");
     }
     if (!empty($params['memberId'])) {
         $condition['consumer.id'] = $params['memberId'];
     }
     //order status
     if (!empty($params['status'])) {
         $status = explode(',', $params['status']);
         $condition['status'] = ['$in' => $status];
     }
     //store id
     if (!empty($params['storeId'])) {
         $condition['storeId'] = new \MongoId($params['storeId']);
     }
     //createdAt
     // After run new MongoDate, The time can lost accuracy, so it will plus 1 or subtract 1.
     if (!empty($params['beginCreatedAt'])) {
         $beginCreatedAt = TimeUtil::ms2sTime($params['beginCreatedAt']) - 1;
         $condition['createdAt']['$gt'] = new MongoDate($beginCreatedAt);
     }
     if (!empty($params['endCreatedAt'])) {
         $endCreatedAt = TimeUtil::ms2sTime($params['endCreatedAt']) + 1;
         $condition['createdAt']['$lt'] = new MongoDate($endCreatedAt);
     }
     //price
     if (!empty($params['minAmount'])) {
         $condition['totalPrice']['$gte'] = floatval($params['minAmount']);
     }
     if (!empty($params['maxAmount'])) {
         $condition['totalPrice']['$lte'] = floatval($params['maxAmount']);
     }
     //staff info
     if (!empty($params['staff'])) {
         $staffName = StringUtil::regStrFormat(trim($params['staff']));
         $staffName = new MongoRegex("/{$staffName}/i");
         $condition['staff.name'] = $staffName;
     }
     //member info
     if (!empty($params['member'])) {
         $member = StringUtil::regStrFormat(trim($params['member']));
         $condition['consumer.name'] = new MongoRegex("/{$member}/i");
     }
     return $condition;
 }
Exemple #10
0
 public static function searchByNameAndUrl($accountId, $limit, $search = null, $createdAt = null)
 {
     $condition = ['isDeleted' => false, 'accountId' => $accountId];
     if ($search !== null) {
         $search = StringUtil::regStrFormat(trim($search));
         $searchReg = new \MongoRegex("/{$search}/i");
         $condition['$or'] = [['url' => $searchReg], ['name' => $searchReg]];
     }
     if ($createdAt !== null) {
         $condition['createdAt'] = ['$lt' => $createdAt];
     }
     return self::find()->where($condition)->orderBy(['createdAt' => SORT_DESC])->limit($limit)->all();
 }
Exemple #11
0
 public static function createCondition($params, $accountId)
 {
     $comma = ',';
     $condition = ['accountId' => $accountId];
     if (!empty($params['code'])) {
         $condition = array_merge($condition, ['code' => $params['code']]);
     }
     if (!empty($params['memberId'])) {
         $condition = array_merge($condition, ['member.id' => new \MongoId($params['memberId'])]);
     }
     if (!empty($params['filter'])) {
         $condition = array_merge($condition, ['member.type' => ['$in' => $params['filter']]]);
     }
     if (array_key_exists('key', $params) && !empty($params['key'])) {
         $key = $params['key'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new \MongoRegex("/{$key}/i");
         $search = ['$or' => [['code' => $keyReg], ['member.name' => $keyReg], ['member.phone' => $keyReg], ['productName' => $keyReg], ['sku' => $keyReg]]];
         $condition = array_merge($condition, $search);
     }
     if (!empty($params['startTime'])) {
         $condition['redeemTime']['$gte'] = new MongoDate(TimeUtil::ms2sTime($params['startTime']));
     }
     if (!empty($params['endTime'])) {
         $condition['redeemTime']['$lte'] = new MongoDate(TimeUtil::ms2sTime($params['endTime']));
     }
     if (!empty($params['accounts'])) {
         $accounts = explode($comma, $params['accounts']);
         $channelIds = [];
         foreach ($accounts as $account) {
             $channelIds[] = $account;
         }
         $channelCondition = ['$or' => [['usedFrom.id' => ['$in' => $channelIds]], ['usedFrom.type' => ['$in' => $channelIds]]]];
         $condition = ['and', $condition, $channelCondition];
     }
     return $condition;
 }
 public static function getByName($accountId, $name)
 {
     $name = StringUtil::regStrFormat($name);
     $name = new \MongoRegex("/^{$name}\$/i");
     return self::findOne(['accountId' => $accountId, 'name' => $name]);
 }
 public static function createKeyWordCondition($key, $condition)
 {
     $key = StringUtil::regStrFormat(trim($key));
     $keyReg = new \MongoRegex("/{$key}/i");
     $search = ['$or' => [['memberName' => $keyReg], ['goods.productName' => $keyReg], ['goods.sku' => $keyReg], ['telephone' => $keyReg]]];
     return array_merge($condition, $search);
 }
 /**
  * Bulk add tags
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/member/member/bulk-add-tags<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for bulk add tags.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     accounts: array, <br/>
  *     tags: array,<br/>
  *     cards: array,<br/>
  *     createdAt: timestamp<br/>
  *     gender: male, female<br/>
  *     country: string<br/>
  *     province: string<br/>
  *     city: string<br/>
  *     searchKey: string<br/>
  *     addTags: array<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * </pre>
  */
 public function actionBulkAddTags()
 {
     $params = $this->getParams();
     $accountId = $this->getAccountId();
     $addTags = $this->getParams('addTags');
     if (empty($addTags)) {
         throw new BadRequestHttpException(\Yii::t('member', 'tags_required'));
     }
     unset($params['addTags']);
     $comma = ',';
     $condition = ['accountId' => $accountId, 'isDeleted' => \backend\components\BaseModel::NOT_DELETED];
     if (!empty($params['accounts'])) {
         $condition = array_merge($condition, ['socialAccountId' => ['$in' => $accounts]]);
     }
     if (!empty($params['cards'])) {
         $cardIds = [];
         foreach ($cards as $card) {
             $cardIds[] = new \MongoId($card);
         }
         $cards = ['$in' => $cardIds];
         $condition = array_merge($condition, ['cardId' => $cards]);
     }
     if (!empty($params['tags'])) {
         $tags = $params['tags'];
         $tags = ['$all' => $tags];
         $condition = array_merge($condition, ['tags' => $tags]);
     }
     if (!empty($params['searchKey'])) {
         $key = trim($params['searchKey']);
         $key = StringUtil::regStrFormat($key);
         $keyReg = new \MongoRegex("/({$key})+/");
         $search = ['$or' => [['cardNumber' => ['$regex' => $keyReg]], ['properties.name' => Member::DEFAULT_PROPERTIES_NAME, 'properties.value' => ['$regex' => $keyReg]]]];
         $condition = array_merge($condition, $search);
     }
     if (!empty($params['createdAt'])) {
         $createdAt = ['$gte' => new \MongoDate($params['createdAt'] / 1000)];
         //Millisecond to mongoDate
         $condition = array_merge($condition, ['createdAt' => $createdAt]);
     }
     if (!empty($params['gender'])) {
         $gender = ['properties.name' => Member::DEFAULT_PROPERTIES_GENDER, 'properties.value' => strtolower($params['gender'])];
         $condition = array_merge($condition, $gender);
     }
     foreach ($params as $key => $value) {
         if (!empty($value)) {
             if ($key == 'country' || $key == 'province' || $key == 'city') {
                 $key = 'location.' . $key;
                 $condition = array_merge($condition, [$key => $value]);
             }
         }
     }
     $result = Member::updateAll(['$addToSet' => ['tags' => ['$each' => $addTags]]], $condition);
     if ($result) {
         return ['message' => 'OK'];
     } else {
         throw new ServerErrorHttpException('Failed to set tags');
     }
 }