//
//  FavoritesData.m
//  Apex Mobile
//
//  Created by Ray on 14-4-25.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "FavoritesData.h"

@implementation FavoritesData

-(long) get_count
{
    return self.pagedata.count;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.pagedata =[[NSMutableArray alloc]init];
        
        ApexMobileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        //    NSString* required_ver=[objheader objectForKey:@"client_ver"] ;
        //
        //    appDelegate.sessionid = [objheader valueForKey:@"sessionid"];
        //    appDelegate.user = user;
        //    appDelegate.password = password;
        
        sqlite3 *db =[ApexMobileDB get_db];
        NSString *quary = [NSString stringWithFormat:@"select _id,create_time,params,module_name,action,name from favorites where user='%@' order by _id desc",appDelegate.user];
        
        
        
        sqlite3_stmt *stmt;
        if (sqlite3_prepare_v2(db, [quary UTF8String], -1, &stmt, nil) == SQLITE_OK) {
            DebugLog(@"sql:%@",quary);
            while (sqlite3_step(stmt)==SQLITE_ROW)
            {
                //                NSString *aname = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
                int _id = sqlite3_column_int(stmt, 0);
                //                NSString* str_time = sqlite3
                NSString* create_time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
                NSString* params =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
                NSString* module_name =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 3)];
                NSString* action =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 4)];
                NSString* name =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 5)];
                NSMutableDictionary * map = [[NSMutableDictionary alloc] init];
                [map setValue:[NSString stringWithFormat:@"%d",_id] forKey:@"_id"];
                [map setValue:[NSString stringWithFormat:create_time,create_time] forKey:@"create_time"];
                [map setValue:params forKey:@"params"];
                [map setValue:module_name forKey:@"module_name"];
                [map setValue:name forKey:@"name"];
                [map setValue:action forKey:@"action"];
                
                [self.pagedata addObject:map];
                
                
            }
            
            sqlite3_finalize(stmt);
        }
        //用完了一定记得关闭，释放内存
        sqlite3_close(db);
        
    }
    return self;
}
@end
