//
//  AdvisoryHttpCmd+Response.m
//  MobilePortal
//
//  Created by xiejiangbo on 2019/9/14.
//  Copyright © 2019 yin chen. All rights reserved.
//

#import "AdvisoryHttpCmd+Response.h"
#import "AdvisoryHttpResult.h"


@protocol AdvisoryHttpResultDataList;
@interface AdvisoryHttpResultDataList : JSONModel
@property NSString<Optional>*docname;
@property NSString<Optional>*status;
@property NSString<Optional>*title;
@property NSString<Optional>*createby;
@property NSString<Optional>*updateby;
@property NSString<Optional>*tags;
@property NSString<Optional>*image;
@property NSString<Optional>*listimage;
@property NSString<Optional>*sort;
@property NSString<Optional>*kgimage;
@property NSString<Optional>*homeshow;
@property NSString<Optional>*createon;
@property NSString<Optional>*updateon;
@property NSString<Optional>*type;
@property NSString<Optional>*id;
@property NSString<Optional>*createbyname;
@property NSString<Optional>*foldertype;
@property NSString<Optional>*updatebyname;
@property NSString<Optional>*comments;
@property NSString<Optional>*pid;
@property NSString<Optional>*author;
@property NSString<Optional>*publishtime;
@property NSString<Optional>*value1;
@property NSString<Optional>*value2;
@property NSString<Optional>*value3;
@property NSString<Optional>*value4;
@property NSString<Optional>*value5;

@end
@implementation AdvisoryHttpResultDataList
@synthesize description;
@end


@protocol AdvisoryHttpResultData;
@interface AdvisoryHttpResultData : JSONModel
@property NSString<Optional>*isLastPage;
@property NSString<Optional>*pageNum;
@property NSString<Optional>*pages;
@property NSString<Optional>*pageSize;
@property NSString<Optional>*size;
@property NSString<Optional>*status;

@property NSMutableArray<Optional,AdvisoryHttpResultDataList> *list;
@end
@implementation AdvisoryHttpResultData
@synthesize description;
@end


@interface AdvisoryHttpResult()
@property AdvisoryHttpResultData *data;
@end

@implementation AdvisoryHttpCmd (Response)
- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    AdvisoryHttpResult *result = [[AdvisoryHttpResult alloc]init];
    result = (AdvisoryHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[AdvisoryHttpResultData alloc]initWithDictionary:dic error:(NSError **)&error];
        if(error){
            [self onRequestFailed:error];
            return;
        }
    }else{
        NSString *memo = [dic objectForKey:@"error_description"];
        [self.result setResultState:kRequestResultFail];
        [self.result setErrMsg:memo];
        NSLog(@"code :%ld errormsg:%@",(long)code,memo);
    }
}

- (void)onRequestFailed:(NSError *)error{
    NSLog(@"Error===%@",error);
    
    [self.result setErrMsg:[error localizedDescription]];
    [self.result setResultState:kRequestResultFail];
}

@end
@implementation AdvisoryHttpResult
- (NSMutableArray *)getAdvisoryHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    AdvisoryHttpListModelData *data = [[AdvisoryHttpListModelData alloc]init];
    data.pageSize = [self getNilString:self.data.pageSize];
    data.isLastPage = [self getNilString:self.data.isLastPage];
    data.pageNum = [self getNilString:self.data.pageNum];
    data.size = [self getNilString:self.data.size];
    data.pages = [self getNilString:self.data.pages];
    data.pageSize = [self getNilString:self.data.pageSize];
    if (self.data.list.count > 0) {
        AdvisoryHttpResultDataList *httpModel = self.data.list[0];
        if ([[AdvisoryDBHelper SharedAdvisoryDBHelper] hasAdvisoryHttpListItemModel:httpModel.type]) {
            [[AdvisoryDBHelper SharedAdvisoryDBHelper] deleteAdvisoryHttpListItemModel:httpModel.type AndBlock:^(BOOL ruselt) {
                [self setModel:data.httpModelArray AndHttpArray:self.data.list];
            }];
        } else {
            [self setModel:data.httpModelArray AndHttpArray:self.data.list];
        }
    }
    [httpArray addObject:data];
    return httpArray;
}
-(void)setModel:(NSMutableArray *)array AndHttpArray:(NSMutableArray *)httpArray{
    for (int index = 0; index < httpArray.count; index ++) {
        AdvisoryHttpListItemModel *model = [[AdvisoryHttpListItemModel alloc]init];
        AdvisoryHttpResultDataList *httpModel = httpArray[index];
        model.docname = [self getNilString:httpModel.docname];
        model.status = [self getNilString:httpModel.status];
        model.title = [self getNilString:httpModel.title];
        model.createby = [self getNilString:httpModel.createby];
        model.updateby = [self getNilString:httpModel.updateby];
        model.tags = [self getNilString:httpModel.tags];
        model.image = [self getNilString:httpModel.image];
        model.imageUrl = [self getImageUrl:model.image];
        model.listimage = [self getNilString:httpModel.listimage];
        model.sort = [self getNilString:httpModel.sort];
        model.kgimage = [self getNilString:httpModel.kgimage];
        model.homeshow = [self getNilString:httpModel.homeshow];
        model.createon = [self getNilString:httpModel.createon];
        model.updateon = [self getNilString:httpModel.updateon];
        model.type = [self getNilString:httpModel.type];
        model.id = [self getNilString:httpModel.id];
        model.createbyname = [self getNilString:httpModel.createbyname];
        model.foldertype = [self getNilString:httpModel.foldertype];
        model.updatebyname = [self getNilString:httpModel.updatebyname];
        model.comments = [self getNilString:httpModel.comments];
        model.pid = [self getNilString:httpModel.pid];
        model.author = [self getNilString:httpModel.author];
        model.value1 = [self getNilString:httpModel.value1];
        model.value2 = [self getNilString:httpModel.value2];
        model.value3 = [self getNilString:httpModel.value3];
        model.value4 = [self getNilString:httpModel.value4];
        model.value5 = [self getNilString:httpModel.value5];
        model.publishtime = [self getNilString:httpModel.publishtime];
        [[AdvisoryDBHelper SharedAdvisoryDBHelper] saveAdvisoryHttpListItemModel:model callBack:^(BOOL ruselt) {
            
        }];
        [array addObject:model];
    }
}
-(NSMutableArray *)getCacheData:(NSString *)type{
    NSMutableArray *httpArray = [NSMutableArray array];
    AdvisoryHttpListModelData *data = [[AdvisoryHttpListModelData alloc]init];
    if (![[AdvisoryDBHelper SharedAdvisoryDBHelper] hasAdvisoryHttpListItemModel:type]) {
        [httpArray addObject:data];
        return httpArray;
    }
    NSMutableArray *array = [[AdvisoryDBHelper SharedAdvisoryDBHelper] allAdvisoryHttpListItemModel:type];
    for (int index = 0; index < array.count; index ++) {
        AdvisoryHttpListItemModel *model = [[AdvisoryHttpListItemModel alloc]init];
        AdvisoryHttpListItemModel *httpModel = array[index];
        model.docname = [self getNilString:httpModel.docname];
        model.status = [self getNilString:httpModel.status];
        model.title = [self getNilString:httpModel.title];
        model.createby = [self getNilString:httpModel.createby];
        model.updateby = [self getNilString:httpModel.updateby];
        model.tags = [self getNilString:httpModel.tags];
        model.image = [self getNilString:httpModel.image];
        model.imageUrl = [self getNilString:httpModel.imageUrl];
        model.listimage = [self getNilString:httpModel.listimage];
        model.sort = [self getNilString:httpModel.sort];
        model.kgimage = [self getNilString:httpModel.kgimage];
        model.homeshow = [self getNilString:httpModel.homeshow];
        model.createon = [self getNilString:httpModel.createon];
        model.updateon = [self getNilString:httpModel.updateon];
        model.type = [self getNilString:httpModel.type];
        model.id = [self getNilString:httpModel.id];
        model.createbyname = [self getNilString:httpModel.createbyname];
        model.foldertype = [self getNilString:httpModel.foldertype];
        model.updatebyname = [self getNilString:httpModel.updatebyname];
        model.comments = [self getNilString:httpModel.comments];
        model.pid = [self getNilString:httpModel.pid];
        model.author = [self getNilString:httpModel.author];
        model.value1 = [self getNilString:httpModel.value1];
        model.value2 = [self getNilString:httpModel.value2];
        model.value3 = [self getNilString:httpModel.value3];
        model.value4 = [self getNilString:httpModel.value4];
        model.value5 = [self getNilString:httpModel.value5];
        model.publishtime = [self getNilString:httpModel.publishtime];
        [data.httpModelArray addObject:model];
    }
    [httpArray addObject:data];
    return httpArray;
}
@end
