1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// PromotionChooseViewController.m
// Lighting
//
// Created by 曹云霄 on 2016/11/14.
// Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
#import "PromotionChooseViewController.h"
#import "PromotionChooseTableViewCell.h"
#import "PromotionWeChatCardModel.h"
#import "PromotionalGoodsModel.h"
#import "PromotionalDeductionModel.h"
#import "PromotionLuckyDrawModel.h"
@interface PromotionChooseViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
自定义促销列表
*/
@property (nonatomic,strong) NSMutableArray *customPromotionDatas;
@end
@implementation PromotionChooseViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpTableView];
}
#pragma mark - 数据源
- (void)setPromotionDatasArray:(NSArray<JSONModel *> *)promotionDatasArray
{
_promotionDatasArray = promotionDatasArray;
NSMutableArray *promotionArray = [NSMutableArray array];
[_promotionDatasArray enumerateObjectsUsingBlock:^(JSONModel * _Nonnull model, NSUInteger index, BOOL * _Nonnull stop) {
CustomPromotionModel *promotionModel = [[CustomPromotionModel alloc]initCustomPromotionModel:model];
[promotionArray addObject:promotionModel];
}];
//促销列表按降序排列
self.customPromotionDatas = [NSMutableArray arrayWithArray:[promotionArray sortedArrayUsingComparator:^NSComparisonResult(CustomPromotionModel *obj1, CustomPromotionModel *obj2) {
if (obj1.priority < obj2.priority) {
return NSOrderedDescending;
}
if (obj1.priority < obj2.priority) {
return NSOrderedAscending;
}
return NSOrderedSame;
}]];
[self calculatedPromotionConflicts:0];
}
#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];
if (![model2.conflicts containsObject:model1.type] && ![model1.conflicts containsObject:model2.type]) {
for (int i=0;i<weakSelf.customPromotionDatas.count;i++) {
CustomPromotionModel *model3 = weakSelf.customPromotionDatas[i];
if ([model3.conflicts containsObject:model1.type]) {
break;
}
if (i == weakSelf.customPromotionDatas.count-1) {
model1.isSelected = YES;
}
}
}
}
}else {
model1.isSelected = NO;
}
}];
[self.promotionChooseTableView reloadData];
}
#pragma mark - UITableview
- (void)setUpTableView
{
self.promotionChooseTableView.tableFooterView = [UIView new];
}
#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 {
[self dismissViewControllerAnimated:YES completion:nil];
}
#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];
}
@end