CardBeenUseViewController.m 9.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  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"

@interface CardBeenUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>


@property (nonatomic,strong) RsJingDongECardRequest *requestModel;
曹云霄's avatar
曹云霄 committed
18
@property (nonatomic,strong) WYPopoverController *settingsPopoverController;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

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

/**
 *  总页数
 */
@property (nonatomic,assign) NSInteger totalPage;
@end

@implementation CardBeenUseViewController

#pragma mark - lazy
- (RsJingDongECardRequest *)requestModel
{
    if (!_requestModel) {
        _requestModel = [[RsJingDongECardRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
39 40
        page.page = ZERO;
        page.rows = KROWS;
41 42
        page.order = @"desc";
        _requestModel.guideIdEquals = [Shoppersmanager manager].Shoppers.employee.fid;
曹云霄's avatar
曹云霄 committed
43
        _requestModel.stateEquals = self.cardState;
44 45 46 47 48 49 50 51 52 53 54 55 56
        _requestModel.page = page;
    }
    return _requestModel;
}

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

曹云霄's avatar
曹云霄 committed
57 58
- (void)viewDidAppear:(BOOL)animated
{
59
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
60 61 62
    [self.beenUseCardCollectionView.mj_header beginRefreshing];
}

63 64 65 66 67 68 69 70 71 72 73 74
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
    [self setupRefreshAction];
}

#pragma mark - 设置刷新
- (void)setupRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
75
        weakSelf.requestModel.page.page = ZERO;
76 77 78 79 80 81 82 83
        [weakSelf.beenUseCardCollectionView.mj_footer resetNoMoreData];
        [weakSelf getCardDatasAction:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.beenUseCardCollectionView.mj_header = headerRefresh;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
84
        if (++ weakSelf.requestModel.page.page > weakSelf.totalPage) {
85 86 87 88 89 90 91 92 93 94
            [weakSelf.beenUseCardCollectionView.mj_footer endRefreshingWithNoMoreData];
        }else
        {
            [weakSelf getCardDatasAction:NO];
        }
    }];
    footer.automaticallyHidden = YES;
    self.beenUseCardCollectionView.mj_footer = footer;
}

95

曹云霄's avatar
曹云霄 committed
96
#pragma mark - 获取卡劵
97 98 99
- (void)getCardDatasAction:(BOOL)isRemove
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
100
    [XBLoadingView showHUDViewWithDefault];
101
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(CARDINFORMATION)  WithRequestType:ZERO WithParameter:self.requestModel WithReturnValueBlock:^(id returnValue) {
102
        
曹云霄's avatar
曹云霄 committed
103
        [XBLoadingView hideHUDViewWithDefault];
104 105 106 107 108 109
        weakSelf.beenUseCardCollectionView.emptyDataSetSource = self;
        weakSelf.beenUseCardCollectionView.emptyDataSetDelegate = self;
        [weakSelf endRefreshingForTableView:weakSelf.beenUseCardCollectionView];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            RsJingDongECardResponse *cardInformation = [[RsJingDongECardResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
            weakSelf.totalPage = cardInformation.totalpages;
110
            if (isRemove) {
曹云霄's avatar
曹云霄 committed
111 112
                [weakSelf.datasArray removeAllObjects];
            }
113 114 115
            [weakSelf.datasArray addObjectsFromArray:cardInformation.list];
            [weakSelf.beenUseCardCollectionView reloadData];
        }else{
曹云霄's avatar
曹云霄 committed
116
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
117
        }
118
    }WithFailureBlock:^(NSError *error) {
119
        [weakSelf endRefreshingForTableView:weakSelf.beenUseCardCollectionView];
120
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
121
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
122 123 124
    }];
}

曹云霄's avatar
曹云霄 committed
125 126 127 128
#pragma mark - 使用京东卡劵
- (void)useJDECard:(NSString *)JDECardNumber
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
129
    [XBLoadingView showHUDViewWithDefault];
130
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(USEJDECARD),JDECardNumber]  WithRequestType:ZERO WithParameter:nil WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
131
        
曹云霄's avatar
曹云霄 committed
132
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
133 134 135
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf deleteUsedJDECard:JDECardNumber];
        }else{
曹云霄's avatar
曹云霄 committed
136
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
137
        }
138
    }WithFailureBlock:^(NSError *error) {
139
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
140
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
141 142 143 144 145 146
    }];
}

#pragma mark - 区分已经使用的E卡
- (void)deleteUsedJDECard:(NSString *)eCardNumber
{
147 148
    //@property (nonatomic, strong) NSMutableArray<TOJingdongEcardEntity> *eCards;
    // 手动改为可变数组
曹云霄's avatar
曹云霄 committed
149 150 151 152 153
    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]) {
154
                ecard.state = USED;
曹云霄's avatar
曹云霄 committed
155 156 157 158 159 160
                [self.beenUseCardCollectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:j inSection:i]]];
            }
        }
    }
}

161 162 163 164 165 166 167 168
#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;
    [self.beenUseCardCollectionView registerNib:[UINib nibWithNibName:@"CardOrderInformationReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView"];
169 170
}

171 172 173

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
174
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
175 176
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cell.Cardmodel = model.eCards[indexPath.row];
177 178 179 180 181 182
    return cell;
}

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

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
188 189 190 191
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    headerView.model = self.datasArray[indexPath.section];
    headerView.uploadReceiptsButton.hidden = true;
    return headerView;
192 193 194 195 196 197 198 199 200 201 202 203 204 205
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 65);
}

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
206 207 208
    if ([self.cardState isEqualToString:CHECK]) {
        return;
    }
曹云霄's avatar
曹云霄 committed
209 210 211 212 213 214 215
    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];
216
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
曹云霄's avatar
曹云霄 committed
217 218 219
    cardVC.cardModel = model.eCards[indexPath.row];
    cardVC.preferredContentSize = CGSizeMake(500, 370);
    [self.settingsPopoverController endThemeUpdates];
220
    // 更新E卡状态
曹云霄's avatar
曹云霄 committed
221 222 223 224 225 226
    WS(weakSelf);
    [cardVC setRefreshJDCardList:^(NSString *cardNumber) {
        [weakSelf.settingsPopoverController dismissPopoverAnimated:YES completion:^{
            [weakSelf useJDECard:cardNumber];
        }];
    }];
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

249 250 251 252
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
253 254 255


@end