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
148
//
// KWMShopCartData.m
// iCemarose
//
// Created by 陈荣科 on 16/9/22.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMShopCartData.h"
#define KWM_MAX_PRODUCT_COUNT 9999
@implementation KWMShopCartData
-(void)syncCartProducts:(NSArray *)items {
[self removeAllItems];
for (KWMShopCartModel *item in items) {
[self inocreaseItem:item];
}
}
//添加购物商品
- (void) inocreaseItem:(KWMShopCartModel *)shopCartModel{
KWMShopCartItem *shopCartItem = [self toShopCartProduct:shopCartModel];
// NSInteger finalValue = shopCartItem.quantity+1;
NSInteger finalValue = shopCartItem.quantity;
if (finalValue > KWM_MAX_PRODUCT_COUNT) {
finalValue = KWM_MAX_PRODUCT_COUNT;
}
// shopCartItem.quantity = finalValue;
[self saveAll];
}
//删除购物商品
- (void) decreaseItem:(KWMShopCartModel *)shopCartModel{
KWMShopCartItem *shopCartItem = [self toShopCartProduct:shopCartModel];
NSInteger finalValue = shopCartItem.quantity - 1;
if (finalValue < 0) {
finalValue = 0;
[self removeItem:shopCartModel];
return;
}
shopCartItem.quantity = finalValue;
[self saveAll];
}
- (void)saveAll {
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
//移除商品
- (void) removeItem:(KWMShopCartItem *)shopCartItem{
NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartItem.identifier];
NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
if(existsArray && existsArray.count > 0){
KWMShopCartItem *exists = existsArray.firstObject;
KWMShopCartModel *model = [self toProduct:exists];
[exists MR_deleteEntity];
[self saveAll];
}
}
//移除所有商品
- (void) removeAllItems{
NSArray *shopCartArr = [self getALLItems];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1 == 1"];
for (KWMShopCartModel *object in shopCartArr) {
KWMShopCartItem *item = [self toShopCartProduct:object];
[item MR_deleteEntity];
}
// [KWMShopCartItem MR_deleteAllMatchingPredicate:predicate];
[self saveAll];
}
// 获取所有商品
- (NSArray *) getALLItems{
NSArray *shopCartArr = [KWMShopCartItem MR_findAll];
if (shopCartArr) {
NSMutableArray *modelArr = [[NSMutableArray alloc] initWithCapacity:100];
for (NSInteger i = 0; i< shopCartArr.count; i++) {
KWMShopCartItem *item = [shopCartArr objectAtIndex:i];
KWMShopCartModel *model = [self toProduct:item];
[modelArr addObject:model];
}
return [[NSArray alloc] initWithArray:modelArr];
}else{
return [[NSArray alloc]init];
}
return shopCartArr;
}
//修改商品个数
- (void) changeNumberShopcart:(KWMShopCartModel *)shopCartModel{
KWMShopCartItem *result;
NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartModel.identifier];
NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
if(existsArray && existsArray.count > 0){
result = existsArray.firstObject;
result.quantity = shopCartModel.quantity;
}
[self saveAll];
}
//商品转购物车
- (KWMShopCartItem *)toShopCartProduct:(KWMShopCartModel *)shopCartModel {
KWMShopCartItem *result;
NSPredicate *filterById = [NSPredicate predicateWithFormat:@"identifier == %@", shopCartModel.identifier];
NSArray *existsArray = [KWMShopCartItem MR_findAllWithPredicate:filterById];
if(existsArray && existsArray.count > 0){
result = existsArray.firstObject;
result.quantity += shopCartModel.quantity;
}else{
result = [KWMShopCartItem MR_createEntity];
result.identifier = shopCartModel.identifier;
result.imageStr = shopCartModel.imageStr;
result.name = shopCartModel.name;
result.brand = shopCartModel.brand;
result.size = shopCartModel.size;
result.quantity = shopCartModel.quantity;
result.price = shopCartModel.price;
result.line_price = shopCartModel.line_price;
result.product_id = shopCartModel.product_id.stringValue;
result.shopCartDict = shopCartModel.shopCartDict;
}
return result;
}
//购物车转商品
- (KWMShopCartModel *)toProduct:(KWMShopCartItem *)shopCartItem {
KWMShopCartModel *result = [[KWMShopCartModel alloc]init];
result.identifier = shopCartItem.identifier;
result.name = shopCartItem.name;
result.imageStr = shopCartItem.imageStr;
result.price = shopCartItem.price;
result.brand = shopCartItem.brand;
result.size = shopCartItem.size;
result.quantity = shopCartItem.quantity;
result.line_price = shopCartItem.line_price;
result.shopCartDict = shopCartItem.shopCartDict;
result.product_id = @(shopCartItem.product_id.integerValue);
return result;
}
@end