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

#import "AllpriceTableViewCell.h"
10

11

12 13 14 15 16 17 18
@implementation AllpriceTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

勾芒's avatar
勾芒 committed
19 20 21 22
#pragma mark -赋值
- (void)setGoodsAllprice:(NSArray *)goodsAllprice
{
    _goodsAllprice = goodsAllprice;
勾芒's avatar
勾芒 committed
23
    NSUInteger allNumber = 0;
勾芒's avatar
勾芒 committed
24
    CGFloat allPrice = 0;
勾芒's avatar
勾芒 committed
25 26
    for (ShopcarModel *model in _goodsAllprice) {
        allNumber += model.goodsNum;
勾芒's avatar
勾芒 committed
27
        allPrice += [model.costPrice floatValue] *model.goodsNum;
勾芒's avatar
勾芒 committed
28
    }
曹云霄's avatar
曹云霄 committed
29
    self.goodsAllNumber.text = [NSString stringWithFormat:@"%ld",allNumber];
勾芒's avatar
勾芒 committed
30
    self.goodsAllPrice.text = [NSString stringWithFormat:@"¥%.2f",allPrice];
勾芒's avatar
勾芒 committed
31 32 33
}


勾芒's avatar
勾芒 committed
34 35 36 37 38
#pragma mark -数据源赋值
- (void)setGoodsArray:(NSArray *)goodsArray
{
    _goodsArray = goodsArray;
    NSInteger allNumber = 0;//总数量
勾芒's avatar
勾芒 committed
39
    CGFloat allPrice = 0;//总价格
40 41 42
    NSDictionary *deductionPrice = nil;//抵扣金额
    NSDictionary *weChatPrice = nil;//微信卡劵
    NSDictionary *drawPrice = nil;//转盘抽奖
勾芒's avatar
勾芒 committed
43
    for (TOOrderdetailEntity *model in _goodsArray) {
勾芒's avatar
勾芒 committed
44
        allNumber += [model.goodsNum integerValue];
45
        allPrice += [model.goodsPrice floatValue]*[model.goodsNum floatValue];
46
    }
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
     //促销列表
    for (JSONModel *model in self.promotionalArray) {
            //微信卡劵
        if ([model isMemberOfClass:[PromotionWeChatCardModel class]]) {
            PromotionWeChatCardModel *newWeChatModel = (PromotionWeChatCardModel *)model;
            weChatPrice = @{@"price":[NSString stringWithFormat:@"%ld",self.weChatModel.wxcardDenomation],@"priority":@(newWeChatModel.priority)};

        }else if ([model isMemberOfClass:[PromotionalDeductionModel class]]) {
            //抵扣
            PromotionalDeductionModel *deductionModel = (PromotionalDeductionModel *)model;
            deductionPrice = @{@"price":[NSString stringWithFormat:@"%ld",deductionModel.total],@"priority":@(deductionModel.priority)};

        }else if ([model isMemberOfClass:[PromotionLuckyDrawModel class]]) {
            //抽奖
            PromotionLuckyDrawModel *drawModel = (PromotionLuckyDrawModel *)model;
            drawPrice = @{@"price":[NSString stringWithFormat:@"%@",self.model.number],@"priority":@(drawModel.priority)};

        }else if ([model isMemberOfClass:[PromotionalGoodsModel class]]) {
            //送商品
//            PromotionalGoodsModel *goodsModel = (PromotionalGoodsModel *)model;
67 68
        }
    }
69
    NSString *goodsAllPriceString = [self promotionSorting:deductionPrice andWeChatPrice:weChatPrice andDrawPrice:drawPrice andAllPrice:allPrice];
勾芒's avatar
勾芒 committed
70
    self.goodsAllNumber.text = [NSString stringWithFormat:@"%ld",allNumber];
71
    self.goodsAllPrice.text = goodsAllPriceString;
勾芒's avatar
勾芒 committed
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#pragma mark - 通过优先级排列促销项
- (NSString *)promotionSorting:(NSDictionary *)deductionPrice andWeChatPrice:(NSDictionary *)weChatPrice andDrawPrice:(NSDictionary *)drawPrice andAllPrice:(CGFloat)allPrice
{
    NSMutableArray *array = [NSMutableArray array];
    if (deductionPrice) {
        [array addObject:deductionPrice];
    }
    if (weChatPrice) {
        [array addObject:weChatPrice];
    }
    if (drawPrice) {
        [array addObject:drawPrice];
    }
    //安装权限降序排列
    NSArray *chooseArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
89
        if ([obj1[@"priority"] integerValue] < [obj2[@"priority"] integerValue]) {
90 91
            return NSOrderedDescending;
        }
92 93 94
        if ([obj1[@"priority"] integerValue] < [obj2[@"priority"] integerValue]) {
            return NSOrderedAscending;
        }
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 148
        return NSOrderedSame;
    }];
    //计算促销后的价格
    CGFloat newPrice = allPrice;
    NSMutableString *priceString = [NSMutableString string];
    for (NSDictionary *dict in chooseArray) {
        if ([dict isEqual:deductionPrice]) {
            CGFloat deduction = [dict[@"price"] floatValue];
            newPrice = newPrice - deduction;
            [priceString appendString:[NSString stringWithFormat:@" - %.2f",deduction]];
            
        }else if ([dict isEqual:weChatPrice]){
           CGFloat weChat = [dict[@"price"] floatValue];
           newPrice = newPrice - weChat;
            [priceString appendString:[NSString stringWithFormat:@" - %.2f",weChat]];
            
        }else if ([dict isEqual:drawPrice]){
            CGFloat draw = [self.model.number floatValue]/100.0;
            newPrice = newPrice * draw;
            [priceString appendString:[NSString stringWithFormat:@" x %@",self.model.descriptionString]];
        }
    }
    //判断促销是否为空
    if (chooseArray.count) {
        if (newPrice < 0) {
            newPrice = 0;
        }
        [priceString insertString:[NSString stringWithFormat:@"¥%.2f(%.2f",newPrice,allPrice] atIndex:0];
        [priceString appendString:@")"];
    }else{
        priceString = [NSMutableString stringWithString:[NSString stringWithFormat:@"¥%.2f",allPrice]];
    }
    return priceString;
}




















勾芒's avatar
勾芒 committed
149

150
@end