//
//  MessageViewController.m
//  TianLing_App
//
//  Created by 刘荣毅 on 2017/10/20.
//  Copyright © 2017年 Liu. All rights reserved.
//
/**
 *Class Name:  消息(废弃)
 */
#import "MessageViewController.h"
#import "JX_NewLoginViewController.h"
#import "UIButton+ImageTitleStyle.h"
#import "JX_MessageTableViewCell.h"
#import "JX_SystemMessageViewController.h"
#import "GetNotificationModelsDataModels.h"

#import "JX_ContactSeachViewController.h"
#import "JX_SeachResultViewController.h"

#import "YTKKeyValueStore.h"

#import "UITabBar+Badge.h"

#define  tableName  @"systemMessage"
#define separatH 60

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
{
    BOOL _isOnceHud;
}

@property(nonatomic ,strong)UITableView *tableView;

/**
 没有消息View
 */
@property(nonatomic ,strong)UIView *noMessageDataTempView;

@property(nonatomic ,strong)GetNotificationModelsBaseClass *dataBaseModel;
@property(nonatomic ,strong)GetNotificationModelsData *dataModel;

@property(nonatomic ,strong)YTKKeyValueStore *store;
/**
 定时器
 */
@property(nonatomic ,strong) NSTimer *timer;
/**
 系统的cell标头
 */
@property(nonatomic ,strong)NSMutableArray *cellTitleArr;

/**
 内容的数组
 */
@property(nonatomic ,strong)NSMutableArray *contentModelArr;
/**
 行政办公Arr
 */
@property(nonatomic ,strong)NSMutableArray *OAWorkArr;

@property(nonatomic ,assign)NSInteger OANoReadCountInt;


@end

@implementation MessageViewController

-(void)viewWillAppear:(BOOL)animated{
    [self getNetworkingRequest];
    
  
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (_isOnceHud == NO) {
        [self autoLogin];
    }
    [self initData];
    [self initUI];
    
//    [self getNetworkingRequest];
    
    [self hiddenExtraCellLine:self.tableView];
    
    
    //推荐-->创建方式2
    self.timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    
}

#pragma mark- initUI && initData
-(void)initData{
    self.contentModelArr = [[NSMutableArray  alloc]init];
    self.OAWorkArr = [[NSMutableArray alloc]init];
     self.cellTitleArr = [[NSMutableArray alloc]init];
    
   
 
}


-(void)dealloc{
    [self.timer invalidate];
    
}


-(void)initUI{
    //角标
    [self.tabBarController.tabBar updateBadge:@"99" bgColor:[UIColor redColor] atIndex:0];
    [self.tabBarController.tabBar updateBadgeTextColor:[UIColor whiteColor] atIndex:0];
    
    self.navigationItem.title = @"消息";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    
   
    
//
//    UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0, IPHONE_WIDTH, 39)];
//    tempView.backgroundColor = [UIColor colorWithHexString:@"#efefef"];
//    UIButton *searchtabBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    searchtabBtn.frame =  CGRectMake(7, 5, IPHONE_WIDTH-14, 29);;
//    searchtabBtn.backgroundColor = [UIColor whiteColor];
//    [searchtabBtn setTitle:@"搜索" forState:UIControlStateNormal];
//    [searchtabBtn setTitleColor:[UIColor colorWithHexString:@"#808080"] forState:UIControlStateNormal];
//    searchtabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 6);
//    searchtabBtn.layer.cornerRadius = 2;
//    searchtabBtn.layer.masksToBounds = YES;
//    [searchtabBtn setFont:[UIFont systemFontOfSize:14]];
//    [searchtabBtn addTarget:self action:@selector(DidTapSearchBtnHandle) forControlEvents:UIControlEventTouchUpInside];
//    [searchtabBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
//    [searchtabBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:10];
//    [tempView addSubview:searchtabBtn];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-44) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 68;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.tableHeaderView = tempView;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"JX_MessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"messageCell"];
    UISwipeGestureRecognizer *tapGestureRecongizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    tapGestureRecongizer.delegate = self;
    [tapGestureRecongizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.tableView  addGestureRecognizer:tapGestureRecongizer];
    
   
    

    //没有消息
    self.noMessageDataTempView = [[UIView alloc]initWithFrame:CGRectMake(0,64, IPHONE_WIDTH, IPHONE_HEIGHT-64)];
    self.noMessageDataTempView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(IPHONE_WIDTH/2-120,  IPHONE_HEIGHT/2-180, 240, 240)];
    imageV.image = [UIImage imageNamed:@"home_news_placeholder"];
    [self.noMessageDataTempView addSubview:imageV];
    
    UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 43)];
    tempView.backgroundColor = [UIColor  colorWithHexString:@"#efefef"];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame =  CGRectMake(7,10, IPHONE_WIDTH-14, 29);;
    searchBtn.backgroundColor = [UIColor whiteColor];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor colorWithHexString:@"#808080"] forState:UIControlStateNormal];
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 6);
    searchBtn.layer.cornerRadius = 2;
    searchBtn.layer.masksToBounds = YES;
    [searchBtn setFont:[UIFont systemFontOfSize:14]];
    [searchBtn addTarget:self action:@selector(DidTapSearchBtnHandle) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:10];
    
    [self.noMessageDataTempView addSubview:tempView];
    [tempView addSubview:searchBtn];
    self.noMessageDataTempView.hidden =YES;
    [self.view addSubview:self.noMessageDataTempView];


}

#pragma mark- delegate && datasoure
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataBaseModel.data.count;

    return  self.cellTitleArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JX_MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.dataModel = self.dataBaseModel.data[indexPath.row];

    if (!cell) {
        cell = [[JX_MessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"messageCell"];
    }
    
    DLog(@"消息数 ==== %ld",(long)self.OANoReadCountInt);
  
    cell.OANoReadCountInt = self.OANoReadCountInt;
    cell.titleStr = self.cellTitleArr[indexPath.row];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.contentModelArr[indexPath.row]];
    if (arr.count == 0) {
        cell.dataModel = arr[0];
    }
    else{
         cell.dataModel = arr[arr.count-1];
    }
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JX_SystemMessageViewController *systemVC = [[JX_SystemMessageViewController alloc]init];
    
    //判断是系统消息还是聊天消息-->跳转
    systemVC.navTitleStr = self.cellTitleArr[indexPath.row];    //从数据中提取
//    systemVC.applicationIDStr = self.
    systemVC.navTitleStr = @"行政办公";
    systemVC.descriptionStr = @"OA办公管理系统模板";
    systemVC.logourlStr = @"/app/oa.png";
    [self.navigationController pushViewController:systemVC animated:YES];
    
    
}


//设置可删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//滑动删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.tableView.isEditing)
    {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleDelete ;
}

//滑动删除begin预处理
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
    //PeopleAllCell *cell = (PeopleAllCell *)[tableView cellForRowAtIndexPath:indexPath];
    //[cell.verifyBtn setHidden:YES];
}

//滑动删除结束处理
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
    //PeopleAllCell *cell = (PeopleAllCell *)[tableView cellForRowAtIndexPath:indexPath];
    //[cell.verifyBtn setHidden:NO];
}

//修改左滑的按钮的字
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除";
}

//左滑点击事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) { //删除事件
//        [self.dataListArray removeObjectAtIndex:indexPath.row];//tableview数据源
//        if (![self.dataListArray count]) { //删除此行后数据源为空
//            [self.peopleAllTableView deleteSections: [NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];
//        } else {
//            [self.peopleAllTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        }
        
        
        
        //与前面不同的只有style改为了ActionSheet，表现为上拉菜单
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"是否删除该消息"message:nil  preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {}];
        UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive
                                                             handler:^(UIAlertAction * action) {
                                                                 [self removeOneMessageFormFMDBAtIndexPath:indexPath];//删除某条信息
                                                             }];
        
        
        [alert addAction:cancleAction];
        [alert addAction:deleteAction];
        
        [self presentViewController:alert animated:YES completion:nil];

        
    }
   
}


-(void)removeOneMessageFormFMDBAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.cellTitleArr removeAllObjects];
    [self.OAWorkArr removeAllObjects];
    self.OANoReadCountInt = 0;
    
    //    // 创建数据库 开始存
    //    self.store = [[YTKKeyValueStore alloc] initDBWithName:@"message.db"];
    //    // 创建表格
    //    [self.store createTableWithName:tableName];
    // 创建数据库 开始存
    self.store = [[YTKKeyValueStore alloc] initDBWithName:@"message.db"];
    // 创建表格
    NSString *userIDStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    DLog(@"账号 === %@",userIDStr);
    NSString *tabelNameStr = [NSString stringWithFormat:@"%@%@",userIDStr,tableName];
    [self.store createTableWithName:tabelNameStr];
    
    
    //读取数据
    NSArray *sumArr  = [self.store getAllItemsFromTable:tabelNameStr];
    NSMutableArray *FMDBArr = [self.store getModelArrayByclassName:[GetNotificationModelsData class]fromTable:tabelNameStr arrayCount:sumArr.count];
//#pragma mark 没有消息占位图
//    if (FMDBArr.count == 0) {
//        self.noMessageDataTempView.hidden = NO;
//    }
//    else{
//        self.noMessageDataTempView.hidden = YES;
//
//
//    }
    for (int i=0; i<FMDBArr.count; i++) {
        GetNotificationModelsData *model = FMDBArr[i];
        //        DLog(@"----==== =====   %@",model);
    }
    
    [self.store clearTable:tabelNameStr];
    [FMDBArr removeAllObjects];
    [self.store putModelObjectArray:FMDBArr intoTable:tabelNameStr];
    
        [self setReadMessageNetworking];//再读一次数据库
    
//    [self.tableView reloadData];
    
//
//    DLog(@"数据库的数据数 =====   %lu",(unsigned long)FMDBArr.count);
//
//
//    /****************** 行政办公的消息 (可以添加数组保存其他类型数据) ******************/
//    NSMutableArray *idsArr = [[NSMutableArray alloc]init];
//    NSString *titleStr =@"";
//    NSString *titleTempStr =@"";
//    for (int i=0; i<FMDBArr.count; i++) {
//        GetNotificationModelsData *model = FMDBArr[i];
//
//        [idsArr addObject:model.dataIdentifier];
//
//        //        for (GetNotificationModelsData *model in self.dataBaseModel.data) {
//        titleTempStr = model.module;
//
//        if (![titleStr isEqualToString:titleTempStr]) {
//            titleStr = titleTempStr;
//            [self.cellTitleArr addObject:titleStr];
//        }
//
//        //    DLog(@"消息是否已读(0未读,1已读) === %d",model.read );
//        //行政办公
//        if ([model.module isEqualToString:OAWorkIdentifier]) {
//            if (model.read == 0) {
//                self.OANoReadCountInt++;
//            }
//
//            [self.OAWorkArr addObject:model];
//
//        }
//
//
//
//        //为任务......
//    }
//    //cell的内容Arr
//    [self.contentModelArr addObject:self.OAWorkArr];
}



#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
        DLog(@"self.tableView.contentOffset.y<0  ======= %f",self.tableView.contentOffset.y)

    if (self.tableView.contentOffset.y < -64) {
        
                UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0, IPHONE_WIDTH, 39)];
                tempView.backgroundColor = [UIColor colorWithHexString:@"#efefef"];
                UIButton *searchtabBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                searchtabBtn.frame =  CGRectMake(7, 5, IPHONE_WIDTH-14, 29);;
                searchtabBtn.backgroundColor = [UIColor whiteColor];
                [searchtabBtn setTitle:@"搜索" forState:UIControlStateNormal];
                [searchtabBtn setTitleColor:[UIColor colorWithHexString:@"#808080"] forState:UIControlStateNormal];
                searchtabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 6);
                searchtabBtn.layer.cornerRadius = 2;
                searchtabBtn.layer.masksToBounds = YES;
                [searchtabBtn setFont:[UIFont systemFontOfSize:14]];
                [searchtabBtn addTarget:self action:@selector(DidTapSearchBtnHandle) forControlEvents:UIControlEventTouchUpInside];
                [searchtabBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
                [searchtabBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:10];
                [tempView addSubview:searchtabBtn];
                self.tableView.tableHeaderView = tempView;
            }
}
#pragma mark - 滚动代理


#pragma mark - method
// 点击搜索框跳转搜索界面
-(void)DidTapSearchBtnHandle{
    DLog(@"点击搜索");
//    JX_ContactSeachViewController * testVC = [JX_ContactSeachViewController new];
    JX_SeachResultViewController *testVC = [JX_SeachResultViewController new];
    
    self.definesPresentationContext = YES; //self is presenting view controller
    testVC.view.backgroundColor = [UIColor colorWithHexString:@"#999999" alpha:0.4];
    testVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    testVC.isMessageSeach = YES;
    [self presentViewController:testVC animated:NO completion:nil];
 
}

//去除TabelviewCell中多余的线
- (void)hiddenExtraCellLine: (UITableView *)tableView
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = view;
}
//定时器
-(void)timerAction{
    
    // TODO: 待办  -------->>>>     打开
    
        [self getNetworkingRequest];

    // TODO: 待办  -------->>>>     打开

}


#pragma mark- networking
-(void)getNetworkingRequest{
  
    // 创建数据库 开始存
    self.store = [[YTKKeyValueStore alloc] initDBWithName:@"message.db"];
    // 创建表格
    NSString *userIDStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    DLog(@"账号 === %@",userIDStr);
    NSString *tabelNameStr = [NSString stringWithFormat:@"%@%@",userIDStr,tableName];
    [self.store createTableWithName:tabelNameStr];
    
    
 
        MBProgressHUD *hud;
    if (_isOnceHud == NO) {
    
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = NSLocalizedString(@"", @"HUD loading title");
    }
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"access_token"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
    [parDic setValue:@(1) forKey:@"pageNum"];
    [parDic setValue:@(100) forKey:@"pageSize"];
    
    [HttpClient getWithURL:GETNOTIFICATION_URL parameter:parDic success:^(id JSON) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
        
         DLog(@"获取系统消息 === %@",dict);
        
        self.dataBaseModel = [GetNotificationModelsBaseClass modelObjectWithDictionary:dict];
        
//        [self.OAWorkArr setArray:self.dataBaseModel.data];
        
            /****************** 存入数据库     ******************/

        //取
        NSArray *sumArr  = [self.store getAllItemsFromTable:tabelNameStr];
        NSMutableArray *FMDBArr = [self.store getModelArrayByclassName:[GetNotificationModelsData class]fromTable:tabelNameStr arrayCount:sumArr.count];
//        for (int i=0; i<FMDBArr.count; i++) {
//            GetNotificationModelsData *model = FMDBArr[i];
//            DLog(@"----==== =====   %@",model);
//        }
        
        if (self.dataBaseModel.data.count != 0) {
           
            for (int i=0; i<self.dataBaseModel.data.count; i++) {
                GetNotificationModelsData *model = self.dataBaseModel.data[i];
                [FMDBArr addObject:model];
            }
            //存
            [self.store putModelObjectArray:FMDBArr intoTable:tabelNameStr];
    
    /****************** 使用通知的方式监控是否有新数据,如有则可以在下一页面中实时操作数据库展示数据  ******************/
                [[NSNotificationCenter defaultCenter] postNotificationName:@"haveNewSystemMessageNotification" object:nil];
          /****************** 使用通知的方式监控是否有新数据,如有则可以在下一页面中实时操作数据库展示数据  ******************/ 
        }
        
        /****************** 存入数据库     ******************/

//
//
//            /****************** 行政办公的消息 (可以添加数组保存其他类型数据) ******************/
//
//        NSString *titleStr =@"";
//        NSString *titleTempStr =@"";
//        for (int i=0; i<self.dataBaseModel.data.count; i++) {
//            GetNotificationModelsData *model = self.dataBaseModel.data[i];
////        for (GetNotificationModelsData *model in self.dataBaseModel.data) {
//            titleTempStr = model.module;
//
//            if (![titleStr isEqualToString:titleTempStr]) {
//                titleStr = titleTempStr;
//                [self.cellTitleArr addObject:titleStr];
//            }
//
//            //行政办公
//            if ([model.toUserId isEqualToString:OAWorkIdentifier]) {
//                if (model.read == 0) {
//                    self.OANoReadCountInt++;
//                }
//                [self.OAWorkArr addObject:model];
//
//            }
//            //为任务......
//        }
//        //cell的内容Arr
//        [self.contentModelArr addObject:self.OAWorkArr];
//              /****************** 行政办公的消息  (可以添加数组保存其他类型数据) ******************/
//
//        DLog(@"消息标题 === %ld",self.cellTitleArr.count);
//         DLog(@"消息标题 === %ld",self.OAWorkArr.count);
//
//
        // 保存数据库成功之后请求接口标记为已读（否则下次进来还是会请求到那些数据库中的数据）
        [self setReadMessageNetworking];  //设为已读
        
        dispatch_async(dispatch_get_main_queue(), ^{
//            self.navigationItem.title = [NSString stringWithFormat:@"消息(%ld)",self.OANoReadCountInt];
//            [self.tabBarController.tabBar updateBadge:[NSString stringWithFormat:@"%ld",self.OANoReadCountInt] bgColor:[UIColor redColor] atIndex:0];
//            [self.tabBarController.tabBar updateBadgeTextColor:[UIColor whiteColor] atIndex:0];
            [hud hide:YES];
//            [self.tableView reloadData];
        });
        
        
       
        
        
    } failure:^(NSError *error) {
          [hud hide:YES];
        
//        [ZJCustomHud  showWithError:@"数据加载失败"];
    }];
    
    
}


//保存在数据库后设为已读
- (void)setReadMessageNetworking{
    
    
    [self.cellTitleArr removeAllObjects];
    [self.OAWorkArr removeAllObjects];
    self.OANoReadCountInt = 0;
    
//    // 创建数据库
//    self.store = [[YTKKeyValueStore alloc] initDBWithName:@"message.db"];
//    // 创建表格
//    [self.store createTableWithName:tableName];
    // 创建数据库
    self.store = [[YTKKeyValueStore alloc] initDBWithName:@"message.db"];
    // 创建表格
    NSString *userIDStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    DLog(@"账号 === %@",userIDStr);
    NSString *tabelNameStr = [NSString stringWithFormat:@"%@%@",userIDStr,tableName];
    [self.store createTableWithName:tabelNameStr];
    
    
    //读取数据
    NSArray *sumArr  = [self.store getAllItemsFromTable:tabelNameStr];
    NSMutableArray *FMDBArr = [self.store getModelArrayByclassName:[GetNotificationModelsData class]fromTable:tabelNameStr arrayCount:sumArr.count];
#pragma mark 没有消息占位图
    if (FMDBArr.count == 0) {
        self.noMessageDataTempView.hidden = NO;
    }
    else{
        self.noMessageDataTempView.hidden = YES;
     
        
    }
    for (int i=0; i<FMDBArr.count; i++) {
        GetNotificationModelsData *model = FMDBArr[i];
//        DLog(@"----==== =====   %@",model);
    }
    
    DLog(@"数据库的数据数 =====   %lu",(unsigned long)FMDBArr.count);
    
    
    /****************** 行政办公的消息 (可以添加数组保存其他类型数据) ******************/
    NSMutableArray *idsArr = [[NSMutableArray alloc]init];
    NSString *titleStr =@"";
    NSString *titleTempStr =@"";
    for (int i=0; i<FMDBArr.count; i++) {
        GetNotificationModelsData *model = FMDBArr[i];
        
        [idsArr addObject:model.dataIdentifier];
        
        //        for (GetNotificationModelsData *model in self.dataBaseModel.data) {
        titleTempStr = model.module;
        
        if (![titleStr isEqualToString:titleTempStr]) {
            titleStr = titleTempStr;
            [self.cellTitleArr addObject:titleStr];
        }
        
//    DLog(@"消息是否已读(0未读,1已读) === %d",model.read );
            //行政办公
            if ([model.module isEqualToString:OAWorkIdentifier]) {
                if (model.read == 0) {
                    self.OANoReadCountInt++;
                }
                
                [self.OAWorkArr addObject:model];
                
            }

        
       
        //为任务......
    }
    //cell的内容Arr
    [self.contentModelArr addObject:self.OAWorkArr];
    /****************** 行政办公的消息  (可以添加数组保存其他类型数据) ******************/
    
//    DLog(@"消息标题 === %ld",self.cellTitleArr.count);
//    DLog(@"行政办公的模型数组 === %ld",self.OAWorkArr.count);
//       DLog(@"消息未读数 === %ld",self.OANoReadCountInt);
    
    
    
//    将array数组转换为string字符串
    NSString *idsStr = [idsArr componentsJoinedByString:@","];//--分隔符
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
//    parDic[@"access_token"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
    parDic[@"ids"] = idsStr;
    
    NSString *urlString = [NSString stringWithFormat:@"%@?access_token=", READNOTIFICATION_URL, [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]];
    
    [HttpClient sendPostWithURL:urlString parameter:parDic success:^(id JSON) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
        DLog(@"标记成功 === %@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (self.OANoReadCountInt != 0) {
                self.navigationItem.title = [NSString stringWithFormat:@"消息(%ld)",self.OANoReadCountInt];
                [self.tabBarController.tabBar updateBadge:[NSString stringWithFormat:@"%ld",self.OANoReadCountInt] bgColor:[UIColor redColor] atIndex:0];
                [self.tabBarController.tabBar updateBadgeTextColor:[UIColor whiteColor] atIndex:0];
            }
            else{
                 self.navigationItem.title = [NSString stringWithFormat:@"消息",self.OANoReadCountInt];
                [self.tabBarController.tabBar updateBadge:nil bgColor:[UIColor clearColor] atIndex:0];
            }
            [self.tableView reloadData];

        });
    } failure:^(NSError *error) {
        
        DLog(@" 失败哦 ==== %@",error);
    }];
    
    _isOnceHud = YES;
    
       [self.tableView reloadData];
    
}
#pragma mark- 自动登录
-(void)autoLogin{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    // 发送请求
    parameters[@"domainName"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"domainName"] ;
    parameters[@"username"] =  [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    
    NSData *data = [[[NSUserDefaults standardUserDefaults] objectForKey:@"password"] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    parameters[@"password"] = base64String;
    
    DLog(@"请求体 === %@",parameters);
    [HttpClient sendPostWithURL:USERLOGIN_URL parameter:parameters success:^(id JSON) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
        DLog(@"提交 === %@",dict);
        
        if ([dict[@"status"] integerValue] == 0) {
            
            //存储用户数据
              [[NSUserDefaults standardUserDefaults]  setObject:dict[@"data"][@"avatar"] forKey:@"avatar"];
             [[NSUserDefaults standardUserDefaults]  setObject:dict[@"data"][@"username"] forKey:@"username"];
            [[NSUserDefaults standardUserDefaults]  setObject:dict[@"data"][@"userId"] forKey:@"userId"];
            [[NSUserDefaults standardUserDefaults]  setObject:dict[@"data"][@"access_token"] forKey:@"access_token"];
            
            //数据持久化
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            
            
        }
        else{
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"access_token"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"account"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
            
            JX_NewLoginViewController *loginVC = [[JX_NewLoginViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
            UIWindow * window = [[UIApplication sharedApplication] keyWindow];
            window.rootViewController = nav;
            
        }
        
    } failure:^(NSError *error) {
        
    }];
}


@end
