// // FLExploreKeywordViewModel.m // FirstLink // // Created by jack on 15/8/21. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FLExploreKeywordViewModel.h" #import "FKExploreConditionItem.h" static NSInteger const FK_EXPLORE_KEYWORD_LIMIT_COUNT = 8; @implementation FLExploreKeywordViewModel - (instancetype)init { self = [super init]; if (self) { _keywordArray = [self queryKeywordsFromDB]; } return self; } - (BOOL)insertKeywordToDB:(NSString *)keyword createTime:(NSString *)createTime { if (!keyword || keyword.length == 0) { return NO; } FMDatabase* db = [FLDataCenter defaultDatabase]; if ([db open]) { [db beginTransaction]; NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?" , FK_TABLE_NAME_FOR_EXPLORE_HISTORY , FK_EXPLORE_KEYWORD_FIELD_KEYWORD]; NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@, %@) VALUES (?, ?)" , FK_TABLE_NAME_FOR_EXPLORE_HISTORY , FK_EXPLORE_KEYWORD_FIELD_KEYWORD , FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME]; [db executeUpdate:deleteSQL, [FLStringHelper replaceNilWithEmpty:keyword]]; [db executeUpdate:insertSQL, [FLStringHelper replaceNilWithEmpty:keyword], [FLStringHelper replaceNilWithEmpty:createTime]]; return [db commit]; } return NO; } - (NSArray *)queryKeywordsFromDB { FMDatabase *db = [FLDataCenter defaultDatabase]; NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY id DESC LIMIT %ld" , FK_TABLE_NAME_FOR_EXPLORE_HISTORY , (long)FK_EXPLORE_KEYWORD_LIMIT_COUNT]; FKExploreKeyword *item; NSMutableArray *mutableArray = [NSMutableArray array]; FMResultSet *rs = [db executeQuery:querySQL]; while ([rs next]) { item = [FKExploreKeyword new]; item.primaryKey = [rs intForColumn:FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY]; item.keyword = [rs stringForColumn:FK_EXPLORE_KEYWORD_FIELD_KEYWORD]; item.createTime = [rs stringForColumn:FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME]; [mutableArray addObject:item]; } [rs close]; if (mutableArray.count >= FK_EXPLORE_KEYWORD_LIMIT_COUNT) { item = mutableArray.lastObject; NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE id < %ld" , FK_TABLE_NAME_FOR_EXPLORE_HISTORY , (long)item.primaryKey]; [db executeUpdate:deleteSQL]; } return mutableArray; } - (BOOL)deleteAllKeywordsInDB { return [[FLDataCenter defaultDatabase] executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@", FK_TABLE_NAME_FOR_EXPLORE_HISTORY]]; } - (NSString *)keywordTextAtIndex:(NSInteger)index { if (index < self.keywordArray.count) { FKExploreKeyword *item = self.keywordArray[index]; return item.keyword; } return @""; } - (NSString *)hotKeywordTextAtIndex:(NSInteger)index { if (index < self.hotKeywordArray.count) { return self.hotKeywordArray[index]; } return nil; } - (NSString *)sectionTitleAtSection:(NSInteger)section { if (section == 0) { return @"热门搜索"; } return @"历史搜索"; } - (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return [self hotKeywordTextAtIndex:indexPath.row]; } return [self keywordTextAtIndex:indexPath.row]; } #pragma mark - Brand API - (NSInteger)insertIntoBrandTable:(NSArray *)brands { if (!brands || brands.count == 0) { return 0; } FMDatabase* db = [FLDataCenter defaultDatabase]; if ([db open]) { [db beginTransaction]; NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@" , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND]; [db executeUpdate:deleteSQL]; NSString *insertSQL; for (NSString *brandName in brands) { insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (?)", FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND, FK_EXPLORE_BRAND_FIELD_NAME]; [db executeUpdate:insertSQL, brandName]; } BOOL isSuccess = [db commit]; return (isSuccess ? brands.count : 0); } return 0; } - (NSArray *)queryBrandsStartWith:(NSString *)prefix { if (prefix.length > 0) { FMDatabase* db = [FLDataCenter defaultDatabase]; if ([db open]) { NSString *querySQL = [NSString stringWithFormat:@"SELECT %@ FROM %@ WHERE %@ LIKE '%@\%%'" , FK_EXPLORE_BRAND_FIELD_NAME , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND , FK_EXPLORE_BRAND_FIELD_NAME , prefix]; FKExploreConditionItem *item; NSMutableArray *mutableArray = [NSMutableArray array]; FMResultSet *rs = [db executeQuery:querySQL]; while ([rs next]) { item = [FKExploreConditionItem new]; item.type = FKExploreConditionTypeKeyword; item.name = [rs stringForColumn:FK_EXPLORE_BRAND_FIELD_NAME]; [mutableArray addObject:item]; } [db close]; return mutableArray; } } return nil; } @end