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

#import "PromotionChooseViewController.h"
10 11 12 13 14
#import "PromotionChooseTableViewCell.h"
#import "PromotionWeChatCardModel.h"
#import "PromotionalGoodsModel.h"
#import "PromotionalDeductionModel.h"
#import "PromotionLuckyDrawModel.h"
15

16 17 18 19 20 21 22
@interface PromotionChooseViewController ()<UITableViewDelegate,UITableViewDataSource>


/**
 自定义促销列表
 */
@property (nonatomic,strong) NSMutableArray *customPromotionDatas;
23 24 25 26 27

@end

@implementation PromotionChooseViewController

28

29 30
- (void)viewDidLoad {
    [super viewDidLoad];
31 32
    
    [self setUpTableView];
33 34
}

35 36 37 38 39 40
#pragma mark - 数据源
- (void)setPromotionDatasArray:(NSArray<JSONModel *> *)promotionDatasArray
{
    _promotionDatasArray = promotionDatasArray;
    NSMutableArray *promotionArray = [NSMutableArray array];
    [_promotionDatasArray enumerateObjectsUsingBlock:^(JSONModel * _Nonnull model, NSUInteger index, BOOL * _Nonnull stop) {
曹云霄's avatar
曹云霄 committed
41
        CustomPromotionModel *promotionModel = [[CustomPromotionModel alloc] initCustomPromotionModel:model];
42 43 44 45 46
        [promotionArray addObject:promotionModel];
    }];
    //促销列表按降序排列
    self.customPromotionDatas = [NSMutableArray arrayWithArray:[promotionArray sortedArrayUsingComparator:^NSComparisonResult(CustomPromotionModel *obj1, CustomPromotionModel *obj2) {
        if (obj1.priority < obj2.priority) {
47
            return NSOrderedDescending;
48 49
        }
        if (obj1.priority < obj2.priority) {
50
            return NSOrderedAscending;
51 52 53 54
        }
        return NSOrderedSame;
    }]];
    [self calculatedPromotionConflicts:0];
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#pragma mark - 选择促销后计算是否存在冲突
- (void)calculatedPromotionConflicts:(NSInteger)cellIndex
{
    WS(weakSelf);
    //默认选中最高级别促销及衍生促销
    CustomPromotionModel *promotionModel = self.customPromotionDatas[cellIndex];
    if (promotionModel.isSelected) {
        return;
    }else {
        for (CustomPromotionModel *model in self.customPromotionDatas) {
            model.isSelected = NO;
        }
        promotionModel.isSelected = YES;
    }
    [self.customPromotionDatas enumerateObjectsUsingBlock:^(CustomPromotionModel *model1, NSUInteger index, BOOL * _Nonnull stop) {
        if (![promotionModel.conflicts containsObject:model1.type] && ![model1.conflicts containsObject:promotionModel.type]) {
            for (int i=0;i<weakSelf.customPromotionDatas.count;i++) {
                CustomPromotionModel *model2 = weakSelf.customPromotionDatas[i];
75
                if (![model2.conflicts containsObject:model1.type] && ![model1.conflicts containsObject:model2.type]) {
76
                    model1.isSelected = YES;
77 78 79 80 81 82 83 84
                }
            }
        }else {
            model1.isSelected = NO;
        }
    }];
    [self.promotionChooseTableView reloadData];
}
85

86 87


88 89 90 91
#pragma mark - UITableview
- (void)setUpTableView
{
    self.promotionChooseTableView.tableFooterView = [UIView new];
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

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PromotionChooseTableViewCell *chooseCell = [tableView dequeueReusableCellWithIdentifier:@"PromotionChooseTableViewCell" forIndexPath:indexPath];
    CustomPromotionModel *model = self.customPromotionDatas[indexPath.row];
    chooseCell.promotionModel = model;
    chooseCell.accessoryType = model.isSelected?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
    chooseCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return chooseCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.customPromotionDatas.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self calculatedPromotionConflicts:indexPath.row];
}

#pragma mark - 退出促销条件选择
- (IBAction)dismissPromotionChooseNavigation:(UIBarButtonItem *)sender {
117 118

    WS(weakSelf);
119 120 121 122
    ShowAlertView(@"提示", @"退出后此单将不在享受促销", @[@"我知道了"], UIAlertControllerStyleAlert, ^(NSInteger index) {
        if (index == ONE) {
            return;
        }
123
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
124
    });
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

#pragma mark - 确认促销条件选择
- (IBAction)confirmPromotionChoose:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.promotionDelegate respondsToSelector:@selector(confirmChoosePromotion:)]) {
        NSMutableArray *chooseArray = [NSMutableArray array];
        for (CustomPromotionModel *model in self.customPromotionDatas) {
            if (model.isSelected) {
                [chooseArray addObject:model];
            }
        }
        [self.promotionDelegate confirmChoosePromotion:chooseArray];
    }
}

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

145 146

@end