CardBeenUseViewController.m 9.48 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);
100
    [self CreateMBProgressHUDLoding];
101
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(CARDINFORMATION) WithCallClass:weakSelf WithRequestType:ZERO WithParameter:self.requestModel WithReturnValueBlock:^(id returnValue) {
102
        
103
        [weakSelf RemoveMBProgressHUDLoding];
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 116 117 118 119
            [weakSelf.datasArray addObjectsFromArray:cardInformation.list];
            [weakSelf.beenUseCardCollectionView reloadData];
        }else{
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
120 121
        [weakSelf endRefreshingForTableView:weakSelf.beenUseCardCollectionView];
        [weakSelf RemoveMBProgressHUDLoding];
122 123
        [weakSelf ErrorMBProgressView:NETWORK];
    } WithFailureBlock:^(NSError *error) {
124 125
        [weakSelf endRefreshingForTableView:weakSelf.beenUseCardCollectionView];
        [weakSelf RemoveMBProgressHUDLoding];
126 127 128 129
        [weakSelf ErrorMBProgressView:error.localizedDescription];
    }];
}

曹云霄's avatar
曹云霄 committed
130 131 132 133 134
#pragma mark - 使用京东卡劵
- (void)useJDECard:(NSString *)JDECardNumber
{
    WS(weakSelf);
    [self CreateMBProgressHUDLoding];
135
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(USEJDECARD),JDECardNumber] WithCallClass:weakSelf WithRequestType:ZERO WithParameter:nil WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        
        [weakSelf RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf deleteUsedJDECard:JDECardNumber];
        }else{
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf ErrorMBProgressView:NETWORK];
    } WithFailureBlock:^(NSError *error) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf ErrorMBProgressView:error.localizedDescription];
    }];
}

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

169 170 171 172 173 174 175 176
#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"];
177 178
}

179 180 181

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
182
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
183 184
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cell.Cardmodel = model.eCards[indexPath.row];
185 186 187 188 189 190
    return cell;
}

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

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
196 197 198 199
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    headerView.model = self.datasArray[indexPath.section];
    headerView.uploadReceiptsButton.hidden = true;
    return headerView;
200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

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

#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];
}

254 255 256 257
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
258 259 260


@end