时间按照【年-月份】分组#

时间按照【年-月份】分组#

DDWaterBillBaseModel.h

#import "BaseModel.h"
@class DDWaterBillCustomReassemblyDataModel;

NS_ASSUME_NONNULL_BEGIN

@interface DDWaterBillBaseListModel : NSObject

@property(nonatomic,copy)NSString *amount;
@property(nonatomic,copy)NSString *ID;
@property(nonatomic,copy)NSString *createTime;
@property(nonatomic,copy)NSNumber *inOutType;
@property(nonatomic,copy)NSString *moneyType;

@end

@interface DDWaterBillBaseModel : BaseModel

@property(nonatomic,strong)NSArray <DDWaterBillBaseListModel *>*list;

-(DDWaterBillCustomReassemblyDataModel *)reassemblyData;

@end

@interface DDWaterBillCustomReassemblyDataModel : NSObject

@property(nonatomic,strong)NSMutableArray <NSString *>*tempSectionDataMutArr;//section的头数据
@property(nonatomic,strong)NSMutableArray <NSMutableArray *>*tempSectionAndRowDataMutArr;//具体的有多少行 每行多少列的数据源
@property(nonatomic,strong)NSMutableArray <DDWaterBillBaseListModel *>*list;
@property(nonatomic,strong)NSMutableArray <NSMutableArray <DDWaterBillBaseListModel *>*>*reassemblyDataMutArr;

@end

NS_ASSUME_NONNULL_END
//数据格式:
/*
 
 {
     endRow = "8";
     hasNextPage = 0;
     pages = 1;
     pageNum = 1;
     navigatepageNums = (
         1
     );
     isLastPage = 1;
     total = "8";
     nextPage = 0;
     navigatePages = 8;
     size = 8;
     hasPreviousPage = 0;
     navigateFirstPage = 1;
     startRow = "1";
     navigateLastPage = 1;
     prePage = 0;
     list = (
         {
             amount = "100.00";
             id = "1370300051760058370";
             createTime = "2021-03-12 17:06:06";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367703729198505985";
             createTime = "2021-03-05 13:09:14";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367703182382923777";
             createTime = "2021-03-05 13:07:04";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367702966913138689";
             createTime = "2021-03-05 13:06:13";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367686887339917314";
             createTime = "2021-03-05 12:02:17";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367683276945981441";
             createTime = "2021-03-05 11:47:58";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1367680617543286785";
             createTime = "2021-03-05 11:37:24";
             inOutType = 0;
             moneyType = "5";
         },
         {
             amount = "100.00";
             id = "1366944841494929410";
             createTime = "2021-03-03 10:53:41";
             inOutType = 0;
             moneyType = "5";
         }
     );
     isFirstPage = 1;
     pageSize = 10;
 }
 
 
 */

DDWaterBillBaseModel.m

#import "DDWaterBillBaseModel.h"

@implementation DDWaterBillBaseListModel

+(NSDictionary *)mj_replacedKeyFromPropertyName{
    /* 返回的字典,key为模型属性名,value为转化的字典的多级key */
    return @{
        @"ID" : @"id"
    };
}

@end

@implementation DDWaterBillBaseModel

+(NSDictionary *)mj_objectClassInArray{
    return @{
        @"list" : DDWaterBillBaseListModel.class
    };
}

-(DDWaterBillCustomReassemblyDataModel *)reassemblyData{
    NSMutableArray <NSString *>*tempSectionDataMutArr = NSMutableArray.array;//section 的头部:年/月
    NSMutableArray <NSMutableArray *>*tempSectionAndRowDataMutArr = NSMutableArray.array;//每个section的row,具体月数的数据列表
    
    NSMutableArray <DDWaterBillBaseListModel *>*tempModel = NSMutableArray.array;
    NSMutableArray <NSMutableArray <DDWaterBillBaseListModel *>*>*tempListModel = NSMutableArray.array;
    
    for (DDWaterBillBaseListModel *waterBillBaseListModel in self.list) {//轮询所有数据源
        // createTime = "2021-03-05 12:02:17";
        // waterBillBaseListModel.createTime;//全时间
        //NSString *ymd = [waterBillBaseListModel.createTime substringToIndex:10];//年月日
        NSString *ym = [waterBillBaseListModel.createTime substringToIndex:7];//字符串截取得出年月
        if (!tempSectionDataMutArr.count) {//第一个数据加进去,不需要判断
            [tempSectionDataMutArr addObject:ym];
            NSMutableArray <NSString *>*fullTimeMutArr = NSMutableArray.array;
            [fullTimeMutArr addObject:waterBillBaseListModel.createTime];
            [tempSectionAndRowDataMutArr addObject:fullTimeMutArr];
            
            [tempModel addObject:waterBillBaseListModel];
            [tempListModel addObject:tempModel];
        }else{//第二个数据开始就要判断了
            
            if ([tempSectionDataMutArr containsObject:ym]) {//存在就直接取,不需要额外创建
                NSInteger index = [tempSectionDataMutArr indexOfObject:ym];//得出已经有的数组的下标
                NSMutableArray <NSString *>*fullTimeMutArr = tempSectionAndRowDataMutArr[index];//同一个月份就取用
                NSMutableArray <DDWaterBillBaseListModel *>*tempModel = tempListModel[index];
                if (![fullTimeMutArr containsObject:waterBillBaseListModel.createTime]) {//相同时间忽略不计
                    [fullTimeMutArr addObject:waterBillBaseListModel.createTime];
                    [tempModel addObject:waterBillBaseListModel];
                }
            }else{
                [tempSectionDataMutArr addObject:ym];
                NSMutableArray <NSString *>*fullTimeMutArr = NSMutableArray.array;//不同月份就创建
                [fullTimeMutArr addObject:waterBillBaseListModel.createTime];
                [tempSectionAndRowDataMutArr addObject:fullTimeMutArr];
                
                NSMutableArray <DDWaterBillBaseListModel *>*tempModel = NSMutableArray.array;
                [tempModel addObject:waterBillBaseListModel];
                [tempListModel addObject:tempModel];
            }
        }
    }
    
    DDWaterBillCustomReassemblyDataModel *waterBillCustomReassemblyDataModel = DDWaterBillCustomReassemblyDataModel.new;
    waterBillCustomReassemblyDataModel.tempSectionAndRowDataMutArr = tempSectionAndRowDataMutArr;
    waterBillCustomReassemblyDataModel.tempSectionDataMutArr = tempSectionDataMutArr;
    waterBillCustomReassemblyDataModel.reassemblyDataMutArr = tempListModel;
    
    return waterBillCustomReassemblyDataModel;
}

@end

@implementation DDWaterBillCustomReassemblyDataModel

@end