CNLiveShopCouponViewModel.swift
13.9 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// CNLiveShopCouponViewModel.swift
// metaCode
//
// Created by zx on 2023/12/12.
//
import Foundation
import CNLiveRequestBastKit
class CNLiveShopCouponViewModel:NSObject{
class func responseCouponCreateWithModel(listModel:CNLiveShopCouponListModel?) -> [[CNLiveShopCouponModel]] {
let model = CNLiveShopCouponListModel()
model.title = listModel?.title ?? ""
if (listModel?.filled != nil){
if listModel!.filled!.count > 0{
model.filled = listModel!.filled
}
}else{
model.filled = ""
}
if (listModel?.discount != nil){
if listModel!.discount!.count > 0{
model.discount = listModel!.discount
}
}else{
model.discount = ""
}
model.num = listModel?.num ?? ""
model.starttime = listModel?.starttime ?? ""
model.endtime = listModel?.endtime ?? ""
model.sort = (listModel?.sort != nil && (listModel?.sort != 1)) ? 2 : 1
model.goods_title = listModel?.goods_title ?? ""
model.state = (listModel?.state != nil && listModel?.state != 1) ? 2 : 1
model.receive_number = (listModel?.receive_number != nil && (listModel?.receive_number != "1")) ? "2" : "1"
model.is_show = (listModel?.is_show != nil && (listModel?.is_show != "1")) ? "2" : "1"
let arr = [[["id":1,"type":1,"title":"名称","subTitle":"请输入(20字以内)","section":1,"message":model.title!],["id":2,"type":2,"title":"满减折扣","subTitle":"","section":1,"filled":model.filled,"disCount":model.discount],["id":3,"type":1,"title":"数量","subTitle":"请输入数量","section":1,"message":model.num],["id":4,"type":3,"title":"开始时间","subTitle":"请选择开始时间","section":1,"message":model.starttime],["id":5,"type":3,"title":"结束时间","subTitle":"请选择结束时间","section":1,"message":model.endtime]],[["id":6,"type":4,"title":"所属类型","subTitle":"","section":model.sort],["id":7,"type":5,"title":"优惠商品","subTitle":"请选择","section":1,"message":model.goods_title],["id":8,"type":4,"title":"领取状态","subTitle":"","section":model.state],["id":9,"type":4,"title":"领取次数","subTitle":"","section":model.receive_number],["id":10,"type":6,"title":"显示状态","subTitle":"","section":model.is_show]]]
// let arr = [[["id":1,"type":1,"title":"名称","subTitle":"请输入(20字以内)","section":1,"message":model.title ?? ""],["id":2,"type":2,"title":"满减折扣","subTitle":"","section":1,"filled":(model.filled>0 ? String(format: "%ld", model.filled) : ""),"disCount":(model.discount>0 ? String(format: "%ld",model.discount) : "")],["id":3,"type":1,"title":"数量","subTitle":"请输入数量","section":1,"message":model.num ?? ""],["id":4,"type":3,"title":"开始时间","subTitle":"请选择开始时间","section":1,"message":model.starttime ?? ""],["id":5,"type":3,"title":"结束时间","subTitle":"请选择结束时间","section":1,"message":model.endtime ?? ""]],[["id":6,"type":4,"title":"所属类型","subTitle":"","section":(model.sort != nil && (model.sort != "1")) ? 2 : 1],["id":7,"type":5,"title":"优惠商品","subTitle":"请选择","section":1,"message":model.goods_title ?? ""],["id":8,"type":4,"title":"领取状态","subTitle":"","section":(model.state != nil && (model.state != "1")) ? 2 : 1],["id":9,"type":4,"title":"领取次数","subTitle":"","section":(model.receive_number != nil && (model.receive_number != "1")) ? 2 : 1],["id":10,"type":6,"title":"显示状态","subTitle":"","section":(model.is_show != nil && (model.is_show != "1")) ? 2 : 1]]]
//
var dataArr = [[CNLiveShopCouponModel]]() // 修改为二维数组
for subArr in arr {
var dataSubArr = [CNLiveShopCouponModel]() // 修改为数组
for (_,mdl) in subArr.enumerated() {
let model = newDictionaryToModel(mdl, CNLiveShopCouponModel.self) as? CNLiveShopCouponModel
if model != nil {
dataSubArr.append(model!) // 如果转换成功,添加到数组中
}
}
dataArr.append(dataSubArr) // 将子数组添加到主数组中
}
return dataArr
}
class func selectTitles(id:Int) -> Array<String>{
var selectTitles = Array<String>()
switch id{
case 6:
selectTitles = ["店铺全场","单个商品"]
break
case 8:
selectTitles = ["正常","冻结"]
break
case 9:
selectTitles = ["不限","限一次"]
break
default: break
}
return selectTitles
}
/// 发布优惠券
/// @param title 优惠券标题
/// @param filled 满多少
/// @param discount 减多少
/// @param startTime 开始时间戳
/// @param endTime 结束时间戳
/// @param state 是否开启 1 开启 2 关闭
/// @param isShow 是否显示 1 对外显示 2 不对外
/// @param receiveNum 领取次数 1 不限制 2 限领一次
/// @param totalNum 优惠券数量 -1 为不限量
/// @param type 所属类型 1全场 2 单品,判断需要
/// @param goodsId 商品id 单品优惠券传此值
class func requestCreateCouponWithTitle(title:String,filled:String,discount:String,startTime:String,endTime:String,state:String,isShow:String,receiveNum:String,totalNum:String,type:Int,goodsId:String,resultBlock: @escaping downSlideResultBlock){
if title == ""{
showMessage(message:"优惠券名称不能为空")
return
}
if filled == "" || discount == ""{
showMessage(message:"优惠券满减金额不能为空")
return
}
if (discount as NSString).floatValue > (filled as NSString).floatValue{
showMessage(message: "减免金额不可大于总金额")
return
}
if totalNum == ""{
showMessage(message:"优惠券总数量不能为空")
return
}
if (totalNum as NSString).integerValue <= 0{
showMessage(message: "优惠券总数量必须大于0")
return
}
if startTime == ""{
showMessage(message:"优惠券开始时间不能为空")
return
}
if endTime == ""{
showMessage(message:"优惠券结束时间不能为空")
return
}
if (startTime as NSString).floatValue >= (endTime as NSString).floatValue{
showMessage(message: "结束时间必须大于开始时间")
return
}
if type == 2 && goodsId == ""{
showMessage(message: "请选择需要优惠的商品")
return
}
let param = NSMutableDictionary()
param.setValue(DSUserInfoManager.shared.dsUserInfo.uid, forKey: "uid")
param.setValue(title, forKey: "title")
param.setValue(filled, forKey: "filled")
param.setValue(discount, forKey: "discount")
param.setValue(startTime, forKey: "starttime")
param.setValue(endTime, forKey: "endtime")
param.setValue(state, forKey: "state")
param.setValue(isShow, forKey: "is_show")
param.setValue(receiveNum, forKey: "receive_number")
param.setValue(totalNum, forKey: "num")
param.setValue(goodsId, forKey: "goods_id")
let custom_param = NSMutableDictionary()
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId_wjj, forKey: "appId")
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPBaseWjjBUrl+"/api/v1/addCoupon", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
resultBlock(result as Any,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
/// 商家优惠券列表
class func requestCouponListResponse(resultBlock: @escaping downSlideResultBlock){
let param = NSMutableDictionary()
param.setValue(DSUserInfoManager.shared.dsUserInfo.uid, forKey: "uid")
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
let custom_param = NSMutableDictionary()
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId_wjj, forKey: "appId")
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.requestNetwork(with: .GET, urlString: APPBaseWjjBUrl+"/api/v1/shopCouponList", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
let resDic = (result as! Dictionary<String, Any>)["data"] as! Dictionary<String, Any>
let model = newDictionaryToModel(resDic, CNLiveShopCouponDataListModel.self) as! CNLiveShopCouponDataListModel
resultBlock(model.list!,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
/// 优惠券显示隐藏
/// @param couponId 优惠券Id
class func requestEditCouponAtterWithId(couponId:String,resultBlock: @escaping downSlideResultBlock){
let param = NSMutableDictionary()
param.setValue(couponId, forKey: "id")
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
let custom_param = NSMutableDictionary()
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId_wjj, forKey: "appId")
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPBaseWjjBUrl+"/api/v1/editCouponAttr", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
resultBlock(result as Any,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
/// 编辑优惠券
/// @param model 信息model
/// @param title 标题
/// @param state 是否开启
/// @param isShow 是否显示
/// @param receiveNum 领取次数
class func requestEditCouponWithModel(model:CNLiveShopCouponListModel,title:String,state:String,isShow:String,receiveNum:String,resultBlock: @escaping downSlideResultBlock){
if title == ""{
showMessage(message: "优惠券名称不能为空")
return
}
let param = NSMutableDictionary()
param.setValue(DSUserInfoManager.shared.dsUserInfo.uid, forKey: "uid")
param.setValue(model.id, forKey: "id")
param.setValue(title, forKey: "title")
param.setValue(model.filled, forKey: "filled")
param.setValue(model.discount, forKey: "discount")
param.setValue(model.starttime, forKey: "starttime")
param.setValue(model.endtime, forKey: "endtime")
param.setValue(state, forKey: "state")
param.setValue(isShow, forKey: "is_show")
param.setValue(receiveNum, forKey: "receive_number")
param.setValue(model.num, forKey: "num")
param.setValue(model.goods_id, forKey: "goods_id")
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
let custom_param = NSMutableDictionary()
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId_wjj, forKey: "appId")
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPBaseWjjBUrl+"/api/v1/editCoupon", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
resultBlock(result as Any,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
/// 删除优惠券
/// @param couponDic 优惠券数组
class func requestDeleteCoupon(couponDic:NSMutableDictionary,resultBlock: @escaping downSlideResultBlock){
var idStr = ""
for couponID in couponDic.allValues{
let couponIDStr = couponID as! String
idStr = idStr.appendingFormat("%@,", couponIDStr)
}
let param = NSMutableDictionary()
param.setValue(idStr, forKey: "id")
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
let custom_param = NSMutableDictionary()
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appId_wjj, forKey: "appId")
custom_param.setValue(CNLiveMCEnvironmentManager.shared.appKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPBaseWjjBUrl+"/api/v1/delCoupon", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
resultBlock(result as Any,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
}