ICRPatrolResultViewController.m 7.33 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
//
//  ICRPatrolResultViewController.m
//  XFFruit
//
//  Created by Xummer on 15/6/8.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRPatrolResultViewController.h"
#import "ICRPatrolPlan.h"
#import "ICRStoreResult.h"
#import "ICRQuestion.h"

@interface ICRPatrolResultViewController ()

@property (strong, nonatomic) IBTTableViewInfo *m_tableViewInfo;

@property (strong, nonatomic) ICRPatrolPlan *m_plan;
@property (copy, nonatomic) NSString * m_storeID;
@property (strong, nonatomic) ICRStoreResult *m_result;

@end

@implementation ICRPatrolResultViewController

#pragma mark - Life Cycle
- (instancetype)initWithPatrolPlan:(id)plan storeID:(NSString *)storeID {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    if ([plan isKindOfClass:[ICRPatrolPlan class]]) {
        self.m_plan = plan;
    }
    
    self.m_storeID = storeID;
    
    return self;
}

- (instancetype)initWithPatrolPlan:(id)plan andStoreResult:(id)result {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    if ([plan isKindOfClass:[ICRPatrolPlan class]]) {
        self.m_plan = plan;
    }
    
    if ([result isKindOfClass:[ICRStoreResult class]]) {
        self.m_result = result;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"巡店结果";
    
    [self initTableViewInfo];
    
    if (!_m_result) {
        [self fetchLocalData];
        
        __weak typeof(self)weakSelf = self;
        void(^succ)(id) = ^(id data) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            [strongSelf fetchLocalData];
        };
        
        void(^fail)(id) = ^(id data) {
            [IBTLoadingView showTips:data];
        };
        
        ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
        [httpCtrl doGetStoreResultWithPlanID:_m_plan.pID storeID:_m_storeID success:succ failure:fail];
    }
    else {
        [self updateTableViewInfo];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Method
- (void)fetchLocalData {
    
    __weak typeof(self)weakSelf = self;
    ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?", [ICRStoreResult TableName], @"uuid"];
        return [db executeQuery:sql,strongSelf.m_plan.pID];
    };
    
    ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.m_result = [fetchedObjects firstObject];
        [strongSelf updateTableViewInfo];
    };
    
    ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
    [dbCtrl runFetchForClass:[ICRStoreResult class]
                  fetchBlock:fetchBlk
           fetchResultsBlock:fetchResultsBlk];
}

- (void)initTableViewInfo {
    self.m_tableViewInfo = [[IBTTableViewInfo alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    IBTTableView *tableV = [_m_tableViewInfo getTableView];
    [self.view addSubview:tableV];
}

- (void)updateTableViewInfo {
    [self.m_tableViewInfo removeSectionAt:0];
    
    if (_m_result) {
        
        NSArray *arrQuestions = _m_plan.questions;
        NSArray *arrAnswers = _m_result.answers;
        
//        if ([arrQuestions count] != [arrAnswers count]) {
//            return;
//        }
        
        IBTTableViewSectionInfo *secInfo = [IBTTableViewSectionInfo sectionInfoDefaut];
        IBTTableViewCellInfo *cellInfo;
        for (NSUInteger i = 0; i < [arrQuestions count]; i++) {
            NSDictionary *dictQ = arrQuestions[ i ];
            NSDictionary *dictA = arrAnswers[ i ];
            
            NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"lineNo" ascending:YES];
            NSArray *arrAnswerDetails = dictA[ @"answerDetails" ];
            
            NSString *nsTitle = [NSString stringWithFormat:@"%@.%@", @( i+1 ), dictQ[ @"title" ]];
            NSString *nsRightValue = nil;
            
            ICRQestionType eType = [dictQ[ @"type" ] integerValue];
            switch (eType) {
                case kICRQestionTypeSingleSel:
                {
                    NSDictionary *dictDetail = [arrAnswerDetails firstObject];
//                    NSUInteger uiAnswer = [dictDetail[ @"numberValue" ] integerValue] - 1;
//                    
//                    NSDictionary *dictOption = [arrOptions safeObjectAtIndex:uiAnswer];
//                    
//                    nsRightValue = dictOption[ @"content" ];
                }
                    break;
                case kICRQestionTypeMultipleSel:
                {
//                    NSArray *arrAnswers = [arrAnswerDetails valueForKeyPath:@"answer"];
                    
//                    NSMutableArray *tmpArr = [NSMutableArray array];
//                    for (NSNumber *answer in arrAnswers) {
//                        NSUInteger uiAnswer = [answer integerValue] - 1;
//                        NSDictionary *dictOption = [arrOptions safeObjectAtIndex:uiAnswer];
//                        [tmpArr safeAddObject:dictOption[ @"content" ]];
//                    }
                    
//                    nsRightValue = [tmpArr componentsJoinedByString:@"/"];
                }
                    break;
                case kICRQestionTypeReply:
                {
                    nsRightValue = dictA[ @"content" ];
                }
                    break;
                case kICRQestionTypeScore:
                {
                    NSNumber *numScore = dictA[ @"score" ];
                    
                    nsRightValue = [NSString stringWithFormat:@"%@分", @( [numScore integerValue])];
                }
                    break;
                case kICRQestionTypeStoreInv:
                {
                    nsRightValue = @"点击查看";
                }
                    break;
                case kICRQestionTypeSpeciesInv:
                {
                    nsRightValue = @"点击查看";
                }
                    break;
                case kICRQestionTypeStarNum:
                {
                    NSNumber *numScore = dictA[ @"score" ];
                    NSNumber *numMaxScore = dictQ[ @"star" ];
                    nsRightValue = [NSString stringWithFormat:@"%@/%@", @( [numScore integerValue]), @([numMaxScore integerValue]) ];
                }
                    break;
                default:
                    break;
            }
            
            cellInfo = [self cellWithLeft:nsTitle right:nsRightValue sel:NULL];
            
            [secInfo addCell:cellInfo];
        }
        
        if ([secInfo getCellCount] > 0) {
            [_m_tableViewInfo addSection:secInfo];
        }
    }
    
    [[self.m_tableViewInfo getTableView] reloadData];
    
}

- (IBTTableViewCellInfo *)cellWithLeft:(NSString *)nsLeftValue
                                 right:(NSString *)nsRightValue sel:(SEL)sel
{
    IBTTableViewCellInfo *cellInfo = [IBTTableViewCellInfo normalCellForSel:sel target:self title:nsLeftValue rightValue:nsRightValue accessoryType:UITableViewCellAccessoryNone];
    
    return cellInfo;
}

@end