Няма описание

FLExploreKeywordViewModel.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // FLExploreKeywordViewModel.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/8/21.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FLExploreKeywordViewModel.h"
  9. #import "FKExploreConditionItem.h"
  10. static NSInteger const FK_EXPLORE_KEYWORD_LIMIT_COUNT = 8;
  11. @implementation FLExploreKeywordViewModel
  12. - (instancetype)init {
  13. self = [super init];
  14. if (self) {
  15. _keywordArray = [self queryKeywordsFromDB];
  16. }
  17. return self;
  18. }
  19. - (BOOL)insertKeywordToDB:(NSString *)keyword createTime:(NSString *)createTime {
  20. if (!keyword || keyword.length == 0) {
  21. return NO;
  22. }
  23. FMDatabase* db = [FLDataCenter defaultDatabase];
  24. if ([db open]) {
  25. [db beginTransaction];
  26. NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?"
  27. , FK_TABLE_NAME_FOR_EXPLORE_HISTORY
  28. , FK_EXPLORE_KEYWORD_FIELD_KEYWORD];
  29. NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@, %@) VALUES (?, ?)"
  30. , FK_TABLE_NAME_FOR_EXPLORE_HISTORY
  31. , FK_EXPLORE_KEYWORD_FIELD_KEYWORD
  32. , FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME];
  33. [db executeUpdate:deleteSQL, [FLStringHelper replaceNilWithEmpty:keyword]];
  34. [db executeUpdate:insertSQL, [FLStringHelper replaceNilWithEmpty:keyword], [FLStringHelper replaceNilWithEmpty:createTime]];
  35. return [db commit];
  36. }
  37. return NO;
  38. }
  39. - (NSArray *)queryKeywordsFromDB {
  40. FMDatabase *db = [FLDataCenter defaultDatabase];
  41. NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY id DESC LIMIT %ld"
  42. , FK_TABLE_NAME_FOR_EXPLORE_HISTORY
  43. , (long)FK_EXPLORE_KEYWORD_LIMIT_COUNT];
  44. FKExploreKeyword *item;
  45. NSMutableArray *mutableArray = [NSMutableArray array];
  46. FMResultSet *rs = [db executeQuery:querySQL];
  47. while ([rs next]) {
  48. item = [FKExploreKeyword new];
  49. item.primaryKey = [rs intForColumn:FK_EXPLORE_KEYWORD_FIELD_PRIMARYKEY];
  50. item.keyword = [rs stringForColumn:FK_EXPLORE_KEYWORD_FIELD_KEYWORD];
  51. item.createTime = [rs stringForColumn:FK_EXPLORE_KEYWORD_FIELD_CREATE_TIME];
  52. [mutableArray addObject:item];
  53. }
  54. [rs close];
  55. if (mutableArray.count >= FK_EXPLORE_KEYWORD_LIMIT_COUNT) {
  56. item = mutableArray.lastObject;
  57. NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE id < %ld"
  58. , FK_TABLE_NAME_FOR_EXPLORE_HISTORY
  59. , (long)item.primaryKey];
  60. [db executeUpdate:deleteSQL];
  61. }
  62. return mutableArray;
  63. }
  64. - (BOOL)deleteAllKeywordsInDB {
  65. return [[FLDataCenter defaultDatabase] executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@", FK_TABLE_NAME_FOR_EXPLORE_HISTORY]];
  66. }
  67. - (NSString *)keywordTextAtIndex:(NSInteger)index {
  68. if (index < self.keywordArray.count) {
  69. FKExploreKeyword *item = self.keywordArray[index];
  70. return item.keyword;
  71. }
  72. return @"";
  73. }
  74. - (NSString *)hotKeywordTextAtIndex:(NSInteger)index {
  75. if (index < self.hotKeywordArray.count) {
  76. return self.hotKeywordArray[index];
  77. }
  78. return nil;
  79. }
  80. - (NSString *)sectionTitleAtSection:(NSInteger)section {
  81. if (section == 0) {
  82. return @"热门搜索";
  83. }
  84. return @"历史搜索";
  85. }
  86. - (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
  87. if (indexPath.section == 0) {
  88. return [self hotKeywordTextAtIndex:indexPath.row];
  89. }
  90. return [self keywordTextAtIndex:indexPath.row];
  91. }
  92. #pragma mark - Brand API
  93. - (NSInteger)insertIntoBrandTable:(NSArray<NSString *> *)brands {
  94. if (!brands || brands.count == 0) {
  95. return 0;
  96. }
  97. FMDatabase* db = [FLDataCenter defaultDatabase];
  98. if ([db open]) {
  99. [db beginTransaction];
  100. NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@"
  101. , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND];
  102. [db executeUpdate:deleteSQL];
  103. NSString *insertSQL;
  104. for (NSString *brandName in brands) {
  105. insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (?)", FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND, FK_EXPLORE_BRAND_FIELD_NAME];
  106. [db executeUpdate:insertSQL, brandName];
  107. }
  108. BOOL isSuccess = [db commit];
  109. return (isSuccess ? brands.count : 0);
  110. }
  111. return 0;
  112. }
  113. - (NSArray<FKExploreConditionItem*> *)queryBrandsStartWith:(NSString *)prefix {
  114. if (prefix.length > 0) {
  115. FMDatabase* db = [FLDataCenter defaultDatabase];
  116. if ([db open]) {
  117. NSString *querySQL = [NSString stringWithFormat:@"SELECT %@ FROM %@ WHERE %@ LIKE '%@\%%'"
  118. , FK_EXPLORE_BRAND_FIELD_NAME
  119. , FK_TABLE_NAME_FOR_EXPLORE_LOCAL_BRAND
  120. , FK_EXPLORE_BRAND_FIELD_NAME
  121. , prefix];
  122. FKExploreConditionItem *item;
  123. NSMutableArray *mutableArray = [NSMutableArray array];
  124. FMResultSet *rs = [db executeQuery:querySQL];
  125. while ([rs next]) {
  126. item = [FKExploreConditionItem new];
  127. item.type = FKExploreConditionTypeKeyword;
  128. item.name = [rs stringForColumn:FK_EXPLORE_BRAND_FIELD_NAME];
  129. [mutableArray addObject:item];
  130. }
  131. [db close];
  132. return mutableArray;
  133. }
  134. }
  135. return nil;
  136. }
  137. @end