//
//  VisitCountManager.m
//  Open
//
//  Created by 雷俊博 on 2019/11/12.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "VisitCountManager.h"

@interface VisitCountManager ()

@property (nonatomic, strong)FMDatabase *db;

@property (nonatomic, strong)NSArray *buttonArray;

@end

@implementation VisitCountManager

+ (VisitCountManager *)shareInstance
{
    static VisitCountManager *manager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [[VisitCountManager alloc] init];
        });
        return manager;
}

- (BOOL)creatTable
{
    
    BOOL ret = false;
    //2.获得数据库
       FMDatabase *db=[FMDatabase databaseWithPath:[self getDBPath]];
       
       //3.打开数据库
       if ([db open]) {
           NSLog(@"数据库打开成功");
           
           //4.创表
           BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_button1(id INTEGER PRIMARY KEY AUTOINCREMENT, imageName TEXT,buttonTitle TEXT,url TEXT, count INTEGER);"];
           
           ret = result;
           
           if (result) {
               
               [db executeUpdate:@"DELETE FROM t_button1 WHERE buttonTitle = ?",@"抢红包"];
               [db executeUpdate:@"DELETE FROM t_button1 WHERE buttonTitle = ?",@"短视频"];
               NSLog(@"创表成功");
           }else{
               NSLog(@"创表失败");
           }
       }else{
           NSLog(@"数据库打开失败");
       }
       
       self.db=db;
    
       [self.db close];
    
    
    return ret;
   
}

- (void)deleteOldTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    
    if ([db open]) {
        BOOL res =  [db executeUpdate:@"drop table t_button"];
        
        if (!res) {
            NSLog(@"error to insert data");
        }else{
            NSLog(@"success to insert data");
        }
        
        [db close];
    }
}

- (void)insertButtonWithimage:(NSString *)imageName title:(NSString *)buttonTitle url:(NSString *)url
{
    
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    
    if ([db open]) {
        BOOL res =  [db executeUpdate:@"INSERT INTO t_button1 (imageName,buttonTitle,url,count) VALUES (?, ?, ?, ?);", imageName,buttonTitle,url, @(0)];
        
        if (!res) {
            NSLog(@"error to insert data");
        }else{
            NSLog(@"success to insert data");
        }
        
        [db close];
    }
}




- (BOOL)queryButtonWithTitle:(NSString *)buttonTitle imageName:(NSString *)imageName url:(NSString *)url
{
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    if ([db open]){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM t_button1 WHERE imageName='%@'and buttonTitle='%@' and url='%@';",imageName,buttonTitle,url];
        FMResultSet *resultSet = [db executeQuery:sql];
        
        if (resultSet.next) {
            [db close];
            return YES;
        }else{
            [db close];
            return NO;
        }
    }
    
    [db close];
    return YES;
}

- (void)updateDataWithTitle:(NSString *)buttonTitle imageName:(NSString *)imageName url:(NSString *)url
{
    
//    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self getDBPath]];
    dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
       dispatch_async(q1, ^{
           FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
           
           if ([db open]) {
                NSString *sql = [NSString stringWithFormat:@"SELECT * FROM t_button1 WHERE imageName='%@'and buttonTitle='%@' and url='%@';",imageName,buttonTitle,url];
               
               FMResultSet *resultSet = [db executeQuery:sql];
               while ([resultSet next]){
                    int ID = [resultSet intForColumn:@"id"];
                   int count = [resultSet intForColumn:@"count"];
                   [db close];
                   [self update:count+1 index:ID];
               }
           }
           
           [db close];
       });
    
    
}

- (void)update:(int)count index:(int)index
{
     FMDatabase *db = [FMDatabase databaseWithPath:[self getDBPath]];
    
    if ([db open]) {
         NSString *sql = [NSString stringWithFormat:@"UPDATE t_button1 SET count = %d WHERE id = '%d'",count,index];
         BOOL ret = [db executeUpdate:sql];
        if (ret) {
            NSLog(@"数据更新成功");
        // [self query];
        }else{
            NSLog(@"数据更新失败");
        }
        [db close];
    }
}


- (NSString *)getDBPath
{
     NSString *doc= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        return [doc stringByAppendingPathComponent:@"visitCount.sqlite"];

}



@end
