//
//  ZJBaseFileModel.m
//  RealEstateManagement
//
//  Created by Javen on 2017/2/13.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "ZJBaseFileModel.h"
#import "UIImageView+WebCache.h"

@interface ZJBaseFileModel()
{
    UIImage *_image;
    NSString *_fileUrl;
    
}

@end
@implementation ZJBaseFileModel

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    return [[self class] modelWithDic:dict];  
}

- (void)setImageView:(UIImageView *)imageView
{
    if (_image != nil) {
        imageView.image = _image;
    }else{
        [self getFileUrlComplelet:^(NSString *fileUrl) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:kPlaceHolderImage];
        }];
    }
}

- (void)getFileUrlComplelet:(void (^)(NSString *fileUrl))fileUrl
{
    static NSCache *cache = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [NSCache new];
    });
    
    
    NSString *url = [cache objectForKey:self.idField];
    if (self.fileUrl) {
        fileUrl(self.fileUrl);
    }else if (url) {
        fileUrl(url);
    }else {
//        [kHttp getAttachmentWithId:self.idField
//                          complete:^(NSString *attachmentUrl) {
//                              _fileUrl = attachmentUrl;
//                              [cache setObject:attachmentUrl forKey:self.idField];
//                              fileUrl(attachmentUrl);
//                          }];
    }
}


@end
