//
//  FGDBHelper.m
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/13.
//  Copyright © 2019 yin chen. All rights reserved.
//

#import "FGDBHelper.h"

@implementation FGDBHelper

+(instancetype)sharedInstance{
    static FGDBHelper *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        _sharedInstance = [[FGDBHelper alloc] init];
    });
    return _sharedInstance;
}
//+(void)attempDealloc{
//    onceToken = 0; // 只有置成0,GCD才会认为它从未执行过.它默认为0.这样才能保证下次再次调用shareInstance的时候,再次创建对象.
//    [_instance release];
//    _instance = nil;
//}
#pragma mark -首页数据
// 首页数据存储
-(void)saveHomeHttpInfoModel:(HomeHttpInfoModel *)selecHomeHttpInfoModel callBack:(void (^)(BOOL))block{
    if (selecHomeHttpInfoModel == nil) {
        return;
    }
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    BOOL __block tranResult = YES;
    [dbHelper executeForTransaction:^(LKDBHelper* helper){
        BOOL result = NO;
        result = [self saveHomeHttpInfoModel:selecHomeHttpInfoModel];
        if (!result) {
            
        }
        tranResult = result;
        return result;
    }];
    block(tranResult);
}
-(BOOL)saveHomeHttpInfoModel:(HomeHttpInfoModel *)selecHomeHttpInfoModel {
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    return [dbHelper insertToDB:selecHomeHttpInfoModel];
}
// 首页数据更新
-(void)updateHomeHttpInfoModel:(HomeHttpInfoModel *)selecHomeHttpInfoModel callBack:(void (^)(BOOL))block{
    if (selecHomeHttpInfoModel == nil ) {
        return;
    }
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    BOOL __block tranResult = YES;
    [dbHelper executeForTransaction:^(LKDBHelper* helper){
        BOOL result = NO;
        result = [self updateHomeHttpInfoModel:selecHomeHttpInfoModel];
        if (!result) {
            
        }
        tranResult = result;
        return result;
    }];
    block(tranResult);
}
-(BOOL)updateHomeHttpInfoModel:(HomeHttpInfoModel *)selecHomeHttpInfoModel
{
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    return [dbHelper updateToDB:selecHomeHttpInfoModel where:nil];
}
// 是否存储该数据
-(BOOL)hasHomeHttpInfoModel{
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    NSMutableArray *arr=[dbHelper search:[HomeHttpInfoModel class] column:nil where:nil orderBy:nil offset:0 count:0];
    return [arr count]>0 ? true : false ;
}
// 获取所有数据
-(NSMutableArray *)allHomeHttpInfoModel{
    LKDBHelper* dbHelper = [HomeHttpInfoModel getUsingLKDBHelper];
    return [dbHelper search:[HomeHttpInfoModel class] column:nil where:nil orderBy:nil offset:0 count:0];
}
#pragma mark - 公司部门详情

-(void)saveCompanyDepartmentInfo:(SelecCompanyDepartmentInfo *)selecCompanyDepartmentInfo callBack:(void (^)(BOOL))block{
    
    if (selecCompanyDepartmentInfo == nil) {
        return;
    }
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    BOOL __block tranResult = YES;
    [dbHelper executeForTransaction:^(LKDBHelper* helper){
        BOOL result = NO;
        result = [self saveCompanyDepartmentInfo:selecCompanyDepartmentInfo];
        if (!result) {
            
        }
        tranResult = result;
        return result;
    }];
    block(tranResult);
    
}
-(NSMutableArray *)queryCompanyDepartmentInfo:(NSString *)companyType andUserId:(NSString *)userid{
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    NSDictionary *where = [NSDictionary dictionaryWithObjectsAndKeys:companyType,@"tags",userid,@"userId",nil];
    return [dbHelper search:[SelecCompanyDepartmentInfo class] column:nil where:where orderBy:nil offset:0 count:0];
}
-(BOOL)saveCompanyDepartmentInfo:(SelecCompanyDepartmentInfo *)selecCompanyDepartmentInfo {
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    return [dbHelper insertToDB:selecCompanyDepartmentInfo];
}
-(NSMutableArray *)allCompanyDepartmentInfo:(NSString *)userId{
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    NSDictionary *where = [NSDictionary dictionaryWithObjectsAndKeys:userId,@"userId",nil];
    return [dbHelper search:[SelecCompanyDepartmentInfo class] column:nil where:where orderBy:nil offset:0 count:0];
}
-(void)updateCompanyDepartmentInfo:(SelecCompanyDepartmentInfo *)selecCompanyDepartmentInfo callBack:(void (^)(BOOL))block{
    if (selecCompanyDepartmentInfo == nil ) {
        return;
    }
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    BOOL __block tranResult = YES;
    [dbHelper executeForTransaction:^(LKDBHelper* helper){
        BOOL result = NO;
        result = [self updateCompanyDepartmentInfo:selecCompanyDepartmentInfo];
        if (!result) {
            
        }
        tranResult = result;
        return result;
    }];
    block(tranResult);
}

-(BOOL)updateCompanyDepartmentInfo:(SelecCompanyDepartmentInfo *)selecCompanyDepartmentInfo
{
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    NSDictionary *where = [NSDictionary dictionaryWithObjectsAndKeys:selecCompanyDepartmentInfo.tags,@"tags",selecCompanyDepartmentInfo.userId,@"userId",nil];
    return [dbHelper updateToDB:selecCompanyDepartmentInfo where:where];
}
-(BOOL)hasCompanyDepartmentInfo:(NSString *)userId andTages:(NSString *)tags{
    LKDBHelper* dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    NSDictionary *where = [NSDictionary dictionaryWithObjectsAndKeys:tags,@"tags",userId,@"userId",nil];
    
    NSMutableArray *arr=[dbHelper search:[SelecCompanyDepartmentInfo class] column:nil where:where orderBy:nil offset:0 count:0];
    return [arr count]>0 ? true : false ;
}
-(void)deleteAllCompanyDepartmentInfo:(NSString *)userId callBack:(void (^)(BOOL))block{
    LKDBHelper *dbHelper = [SelecCompanyDepartmentInfo getUsingLKDBHelper];
    BOOL __block tranResult = YES;
    NSDictionary *where = [NSDictionary dictionaryWithObjectsAndKeys:userId,@"userId",nil];
    [dbHelper deleteWithClass:[SelecCompanyDepartmentInfo class] where:where callback:^(BOOL result){
        tranResult = result;
    }];
    block(tranResult);
}
@end
