CardViewController.m 6.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//
//  CardViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/10/20.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CardViewController.h"
#import "CardCollectionViewCell.h"
曹云霄's avatar
曹云霄 committed
11
#import "CardDetailsViewController.h"
曹云霄's avatar
曹云霄 committed
12
#import "CardAmplificationViewController.h"
曹云霄's avatar
曹云霄 committed
13
#import "LuckyDrawViewController.h"
14

15
@interface CardViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
16 17 18 19 20 21

//** 查询最近10张卡劵 */
@property (nonatomic,strong) RsJingDongECardRequest *requestModel;

//** 数据源 */
@property (nonatomic,strong) NSMutableArray *datasArray;
曹云霄's avatar
曹云霄 committed
22
@property (nonatomic,strong) WYPopoverController *settingsPopoverController;
23 24 25 26 27 28 29 30 31 32 33

@end

@implementation CardViewController

#pragma mark - lazy
- (RsJingDongECardRequest *)requestModel
{
    if (!_requestModel) {
        _requestModel = [[RsJingDongECardRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
34 35
        page.page = ZERO;
        page.rows = KROWS;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        _requestModel.guideIdEquals = [Shoppersmanager manager].Shoppers.employee.fid;
        _requestModel.page = page;
    }
    return _requestModel;
}

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

- (void)viewDidAppear:(BOOL)animated
{
曹云霄's avatar
曹云霄 committed
52
    [super viewDidAppear:animated];
53 54 55 56 57 58 59 60 61 62 63
    [self getCardDatasAction];
}

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

#pragma mark - 查询卡劵
- (void)getCardDatasAction
{
    WS(weakSelf);
64
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(QUERYALLJDECARD)  WithRequestType:ZERO WithParameter:self.requestModel WithReturnValueBlock:^(id returnValue) {
65
        
曹云霄's avatar
曹云霄 committed
66 67
        weakSelf.cardCollectionView.emptyDataSetSource = weakSelf;
        weakSelf.cardCollectionView.emptyDataSetDelegate = weakSelf;
68
        [weakSelf endRefreshingForTableView:weakSelf.cardCollectionView];
69 70
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf.datasArray removeAllObjects];
曹云霄's avatar
曹云霄 committed
71 72 73 74
            
            RsSimpleJingDongECardResponse *cardInformation = [[RsSimpleJingDongECardResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
            for (TOJingdongEcardEntity *eCard in cardInformation.list) {
                [weakSelf.datasArray addObject:eCard];
75 76 77
            }
            [weakSelf.cardCollectionView reloadData];
        }else{
曹云霄's avatar
曹云霄 committed
78
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
79 80
        }
        
81
    }WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
82
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
83 84 85
    }];
}

曹云霄's avatar
曹云霄 committed
86 87 88 89
#pragma mark - 使用京东卡劵
- (void)useJDECard:(NSString *)JDECardNumber
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
90
    [XBLoadingView showHUDViewWithDefault];
91
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(USEJDECARD),JDECardNumber]  WithRequestType:ZERO WithParameter:nil WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
92
        
曹云霄's avatar
曹云霄 committed
93
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
94 95 96
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf deleteUsedJDECard:JDECardNumber];
        }else{
曹云霄's avatar
曹云霄 committed
97
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
98
        }
99
    }WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
100
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
101 102 103 104 105 106 107 108 109
    }];
}

#pragma mark - 区分已经使用的E卡
- (void)deleteUsedJDECard:(NSString *)eCardNumber
{
    ///@property (nonatomic, strong) NSMutableArray<TOJingdongEcardEntity> *eCards;
    /// 手动改为可变数组
    for (int i=0; i<self.datasArray.count; i++) {
110 111 112 113
        TOJingdongEcardEntity *order = self.datasArray[i];
            if ([eCardNumber isEqualToString:order.cardNumber]) {
                order.state = @"used";
                [self.cardCollectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:i inSection:0]]];
曹云霄's avatar
曹云霄 committed
114 115 116 117
        }
    }
}

118
#pragma mark <UICollectionViewDataSource>
119 120
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
121 122 123
    return self.datasArray.count;
}

124 125
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
126
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
127
    cell.Cardmodel = self.datasArray[indexPath.row];
128 129 130
    return cell;
}

曹云霄's avatar
曹云霄 committed
131
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
132
{
曹云霄's avatar
曹云霄 committed
133 134
    WS(weakSelf);
    TOJingdongEcardEntity *model = self.datasArray[indexPath.row];
曹云霄's avatar
曹云霄 committed
135
    if ([[self class] isBlankString:model.orderReceiptUrl]) {
曹云霄's avatar
曹云霄 committed
136
        [self promptCustomerTitle:@"我知道了" withMessage:@"不能查看未激活卡劵,请先上传小票激活卡劵" finish:^{
曹云霄's avatar
曹云霄 committed
137
            CardDetailsViewController *cardDetails = [[[weakSelf class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"CardDetailsViewController"];
曹云霄's avatar
曹云霄 committed
138 139
            [weakSelf.navigationController pushViewController:cardDetails animated:YES];
        }];
140
    }else if ([model.state isEqualToString:CHECK]){
曹云霄's avatar
曹云霄 committed
141
        [XBLoadingView showHUDViewWithText:@"审核中"];
曹云霄's avatar
曹云霄 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }else{
        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];
        cardVC.cardModel = self.datasArray[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];
            }];
        }];
    }
161 162
}

曹云霄's avatar
曹云霄 committed
163
#pragma mark -友好界面
164 165
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
曹云霄's avatar
曹云霄 committed
166
    return [[NSAttributedString alloc]initWithString:@"暂无卡劵记录~" attributes:nil];
167 168
}

169
@end