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

#import "LoginHttpPostCmd+Response.h"
#import "LoginHttpPostResult.h"

@protocol LoginHttpPostResultDataData;
@interface LoginHttpPostResultDataData : JSONModel

@property NSString<Optional>*userid;

@end
@implementation LoginHttpPostResultDataData
@synthesize description;
@end


@protocol LoginHttpPostResultData;
@interface LoginHttpPostResultData : JSONModel
@property NSString<Optional>*status;
@property NSString<Optional>*userid;
@property NSString<Optional>*msg;

@end
@implementation LoginHttpPostResultData
@synthesize description;
@end


@protocol LoginHttpPostResultData1;
@interface LoginHttpPostResultData1 : JSONModel
@property NSMutableArray<Optional,LoginHttpPostResultData> *data;
@property NSString<Optional>*msg;
@property NSString<Optional>*success;
@property NSString<Optional>*code;

@end
@implementation LoginHttpPostResultData1
@synthesize description;
@end


@interface LoginHttpPostResult()
@property LoginHttpPostResultData1 *data;
@end


@implementation LoginHttpPostCmd (Response)

- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    LoginHttpPostResult *result = [[LoginHttpPostResult alloc]init];
    result = (LoginHttpPostResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[LoginHttpPostResultData1 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 LoginHttpPostResult
- (NSMutableArray *)getLoginHttpPostList{
    NSMutableArray *httpArray = [NSMutableArray array];
    if (self.data.data.count == 0) {
        [httpArray addObject:@"请查看当前网络状态！"];
    }
    for (int index = 0; index < self.data.data.count; index ++) {
        LoginHttpPostResultData *data = self.data.data[index];
        [httpArray addObject:data.msg];
        if ([data.msg isEqualToString:@"success"]) {
            httpArray = [NSMutableArray array];
            [AccountInfo sharedInstance].UserId = data.userid;
            [AccountInfo sharedInstance].ChangPassWordUserId = data.userid;
        }
    }
    return httpArray;
}

@end

