@interface ViewController ()

@property(nonatomic,strong) NSMutableArray          *btnArray;
@property(nonatomic,strong) UIScrollView            *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

- (void)setupUI
{
    [self.view addSubview:self.scrollView];
    for (NSInteger i = 0; i < self.btnArray.count; i++)
    {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = SZBMRandomColor;
        [btn setTitle:self.btnArray[i] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self
                action:@selector(btnClick:)
      forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:btn];
        
        btn.layer.cornerRadius = 5;
        btn.layer.borderWidth = 2;
        btn.layer.borderColor = SZBMRandomColor.CGColor;
        
        NSInteger row = i / kLDTotallloc;//行号
        NSInteger loc = i % kLDTotallloc;//列号
        CGFloat x = kLDButtonMarginWidth + (kLDButtonMarginWidth + kLDButtonWidth) * loc;
        CGFloat y = kLDButtonMarginWidth + (kLDButtonMarginWidth + kLDButtonWidth) * row + 20;
        btn.frame = CGRectMake(x, y, kLDButtonWidth, kLDButtonWidth);
        
        self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.view.frame), CGRectGetMaxY(btn.frame));
        
    }
}


- (void)btnClick:(UIButton *)btn
{
    if ([[btn currentTitle] isEqualToString:@"创建数据库"])
    {
        //登录成功之后拿到用户id 这里作为测试直接写了
        NSString *userId = @"80499909";
        //创建数据库以及数据表
        NSString *user_table = @"CREATE TABLE IF NOT EXISTS t_user(rid INTEGER PRIMARY KEY,uID INTEGER NOT NULL,uName TEXT NOT NULL,uNike TEXT,phone TEXT);";
        NSString *chat_table = @"CREATE TABLE IF NOT EXISTS t_chat(rid INTEGER AUTOINCREASE PRIMARY KEY,uID INTEGER NOT NULL,uName TEXT NOT NULL,chat TEXT);";
        
        //创建 打开 数据库
//        __weak typeof(self) weakSelf = self;
        [kSZBMDBHandleMsg createUserDatabase:@"SZBM_DB.sqlite"
                                      userID:userId
                                     sqlList:@[user_table,chat_table]
                                   dbVersion:0
                                 upgradeType:SZBMDBUpgradeTypeStep
                                upgradeBlock:^(NSInteger oldVersion, NSInteger currentVersion) {
                                    //数据库更新代码
                                   
                                    
                                }];
       
    }else if ([[btn currentTitle] isEqualToString:@"插入数据"])
    {
        [self insertDataWithType:0];
        
    }else if([[btn currentTitle] isEqualToString:@"读取数据"])
    {
        NSString *queryStr = [NSString stringWithFormat:@"SELECT * FROM t_user;"];
        
        NSArray *array = [kSZBMDBHandleMsg queryUserDBWithSql:queryStr];
        
        NSLog(@"-----%zd \n%@",array.count,array);
        
    }else if([[btn currentTitle] isEqualToString:@"更新数据"])
    {
//        需要注意的是,SQL语句中的'值'都需要用单引号括起来。
         NSString *sqlStr = [NSString stringWithFormat:@"UPDATE t_user SET uName = '%@' WHERE uID = '%zd';",@"hhhhhhh",50];
        NSLog(@"----%@",sqlStr);
        BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSql:sqlStr];
        
        NSLog(@"----%d",ret);
        
        
    }else if([[btn currentTitle] isEqualToString:@"删除数据"])
    {
        NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM t_user;"];
        NSLog(@"----%@",sqlStr);
        BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSql:sqlStr];
        NSLog(@"----%d",ret);
        
    }else if([[btn currentTitle] isEqualToString:@"更新数据表"])
    {
//#warning 待更新...
        NSString *sqlStr1 = @"ALTER TABLE t_user ADD SZBM01 TEXT NOT NULL default 0";
        NSString *sqlStr2 = @"ALTER TABLE t_user ADD SZBM02 TEXT NOT NULL default 0";
        NSString *sqlStr3 = @"ALTER TABLE t_user ADD SZBM03 TEXT NOT NULL default 0";
        [kSZBMDBHandleMsg transactionUpdataUserDBWithSqlArray:@[sqlStr1,sqlStr2,sqlStr3]];
        
        
    }else if([[btn currentTitle] isEqualToString:@"删除数据表"])
    {
        NSString *sqlStr = [NSString stringWithFormat:@"DROP TABLE IF EXISTS t_user;"];
        NSLog(@"----%@",sqlStr);
        BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSql:sqlStr];
        NSLog(@"----%d",ret);
    }else if([[btn currentTitle] isEqualToString:@"删除数据库"])
    {
        
    }
}

- (void)insertDataWithType:(NSInteger)type
{
    switch (type)
    {
        case 0:
        {
//        第一种方法插入数据 这里插入多条数据,可以用事物,效率更高
            for (NSInteger i = 0; i < 100; i++)
            {
                NSNumber *num = [NSNumber numberWithInteger:i];
                NSString *insertStr = [NSString stringWithFormat:@"insert into t_user(uID,uName,uNike,phone) values('%@','%@','%@','%@');",num,@"王健林",@"王思聪",@"18588888888"];
                BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSql:insertStr];
                NSLog(@"-----%d",ret);
            }
            
        }
            break;
            
        case 1:
        {
//          第二种方法插入数据 插入的数据只能是对象
            NSString *sqlStr = @"INSERT INTO t_user(uID,uName,uNike,phone) VALUES (?, ?, ?);";
            BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSqlHasExtralData:sqlStr,@1,@"王健林",@"wangsicong",@"18588888888"];
            NSLog(@"-----%d",ret);
        }
        case 2:
        {
//        第三种方法插入数据
            NSString *sqlStr = @"INSERT INTO t_user(uName,uNike,phone) VALUES (?, ?, ?);";
            BOOL ret = [kSZBMDBHandleMsg updataUserDBWithSql:sqlStr withArgumentsInArray:@[@"王健林11",@"wangsicong11",@"18588888888"]];
            NSLog(@"-----%d",ret);
        }
        default:
            break;
    }
    
}

- (NSMutableArray *)btnArray
{
    if (!_btnArray)
    {
        _btnArray = [NSMutableArray arrayWithCapacity:2];
        [_btnArray addObject:@"创建数据库"];
        [_btnArray addObject:@"插入数据"];
        [_btnArray addObject:@"读取数据"];
        [_btnArray addObject:@"更新数据"];
        [_btnArray addObject:@"删除数据"];
        [_btnArray addObject:@"更新数据表"];
        [_btnArray addObject:@"删除数据表"];
        [_btnArray addObject:@"删除数据库"];
        
    }
    return _btnArray;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = self.view.frame;
    }
    
    return _scrollView;
}

@end
