CardBeenUseViewController.m 7.38 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
//
//  CardBeenUseViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/10/19.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CardBeenUseViewController.h"
#import "CardCollectionViewCell.h"
#import "CardOrderInformationReusableView.h"
#import "CardAmplificationViewController.h"

14
@interface CardBeenUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
曹云霄's avatar
曹云霄 committed
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


@property (nonatomic,strong) RsJingDongECardRequest *requestModel;
@property (nonatomic,strong) WYPopoverController *settingsPopoverController;

/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *datasArray;

@end

@implementation CardBeenUseViewController

#pragma mark - lazy
- (RsJingDongECardRequest *)requestModel
{
    if (!_requestModel) {
        _requestModel = [[RsJingDongECardRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ZERO;
        page.rows = KROWS;
        page.order = @"desc";
        _requestModel.guideIdEquals = [Shoppersmanager manager].shoppers.employee.fid;
        _requestModel.stateEquals = self.cardState;
        _requestModel.page = page;
    }
    return _requestModel;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
57
    self.tempIndex = @"0";
曹云霄's avatar
曹云霄 committed
58 59
}

60 61
#pragma mark -获取数据
- (void)loadWebDataSource
曹云霄's avatar
曹云霄 committed
62 63
{
    WS(weakSelf);
64
    if (self.pullPageIndex == ZERO) {
65 66 67 68 69 70 71 72
        [self.datasArray removeAllObjects];
    }
    self.requestModel.page.page = self.pullPageIndex;
    [self getCardDatasActionCompleted:^(RsJingDongECardResponse *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
73 74 75 76 77 78
        }
    }];
}


#pragma mark - 获取卡劵
79
- (void)getCardDatasActionCompleted:(void(^)(RsJingDongECardResponse *result))completed
曹云霄's avatar
曹云霄 committed
80 81 82 83 84
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(CARDINFORMATION)  withRequestType:ZERO withParameter:self.requestModel withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            RsJingDongECardResponse *cardInformation = [[RsJingDongECardResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
85
            completed(cardInformation);
曹云霄's avatar
曹云霄 committed
86
            [weakSelf.datasArray addObjectsFromArray:cardInformation.list];
87
            [weakSelf.collectionView reloadData];
曹云霄's avatar
曹云霄 committed
88 89 90 91
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
92
        [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
93 94 95 96
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

97

曹云霄's avatar
曹云霄 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#pragma mark - 使用京东卡劵
- (void)useJDECard:(NSString *)JDECardNumber
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(USEJDECARD),JDECardNumber]  withRequestType:ZERO withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [weakSelf deleteUsedJDECard:JDECardNumber];
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

116

曹云霄's avatar
曹云霄 committed
117 118 119 120 121 122 123 124 125 126 127
#pragma mark - 区分已经使用的E卡
- (void)deleteUsedJDECard:(NSString *)eCardNumber
{
    //@property (nonatomic, strong) NSMutableArray<TOJingdongEcardEntity> *eCards;
    // 手动改为可变数组
    for (int i=0; i<self.datasArray.count; i++) {
        GroupByOrderNumber *order = self.datasArray[i];
        for (int j=0; j<order.eCards.count; j++) {
            TOJingdongEcardEntity *ecard = order.eCards[j];
            if ([eCardNumber isEqualToString:ecard.cardNumber]) {
                ecard.state = USED;
128
                [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:j inSection:i]]];
曹云霄's avatar
曹云霄 committed
129 130 131 132 133 134 135 136 137 138 139 140
            }
        }
    }
}

#pragma mark - UICollectionView
- (void)setupCollectionView
{
    self.beenUseLayout.itemSize = CGSizeMake((ScreenWidth-48-20*4)/3, 200);
    self.beenUseLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.beenUseLayout.minimumLineSpacing = 20;
    self.beenUseLayout.minimumInteritemSpacing = 20;
141
    [self.collectionView registerNib:[UINib nibWithNibName:@"CardOrderInformationReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView"];
曹云霄's avatar
曹云霄 committed
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
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cell.Cardmodel = model.eCards[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    GroupByOrderNumber *model = self.datasArray[section];
    return model.eCards.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    headerView.model = self.datasArray[indexPath.section];
    headerView.uploadReceiptsButton.hidden = true;
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
169
    return CGSizeMake(0, 80);
曹云霄's avatar
曹云霄 committed
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
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.datasArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.cardState isEqualToString:CHECK]) {
        return;
    }
    CardAmplificationViewController *cardVC = [[CardAmplificationViewController alloc]init];
    self.settingsPopoverController = [[WYPopoverController alloc] initWithContentViewController:cardVC];
    self.settingsPopoverController.theme.fillBottomColor = [UIColor clearColor];
    self.settingsPopoverController.theme.fillTopColor = [UIColor clearColor];
    [self.settingsPopoverController presentPopoverAsDialogAnimated:YES
                                                           options:WYPopoverAnimationOptionFadeWithScale];
    [self.settingsPopoverController beginThemeUpdates];
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cardVC.cardModel = model.eCards[indexPath.row];
    cardVC.preferredContentSize = CGSizeMake(500, 370);
    [self.settingsPopoverController endThemeUpdates];
    // 更新E卡状态
    WS(weakSelf);
    [cardVC setRefreshJDCardList:^(NSString *cardNumber) {
        [weakSelf.settingsPopoverController dismissPopoverAnimated:YES completion:^{
            [weakSelf useJDECard:cardNumber];
        }];
    }];
}


- (void)dealloc
{
    [Notification removeObserver:self];
}


@end