PayViewModel.java
17.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package com.naji.xiaotao.pay.vm;
import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableFloat;
import android.databinding.ObservableInt;
import android.databinding.ObservableList;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.naji.xiaotao.BR;
import com.naji.xiaotao.R;
import com.naji.xiaotao.cart.entity.OrderEntity;
import com.naji.xiaotao.cart.vm.CartItemViewModel;
import com.naji.xiaotao.common.dialog.RightDialog;
import com.naji.xiaotao.history.service.HistoryApiService;
import com.naji.xiaotao.history.ui.HistoryActivity;
import com.naji.xiaotao.http.RetrofitClient;
import com.naji.xiaotao.pay.entity.PaymentEntity;
import com.naji.xiaotao.pay.service.PayApiService;
import com.naji.xiaotao.pay.ui.HyyhDialog;
import com.naji.xiaotao.pay.ui.PayActivity;
import com.naji.xiaotao.pay.ui.ZddzActivity;
import com.naji.xiaotao.pay.ui.ZdyyhActivity;
import com.naji.xiaotao.tangshi.ui.TangShiActivity;
import com.naji.xiaotao.util.DialogUtil;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.TitleViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;
public class PayViewModel extends BaseViewModel{
public static final String TOKEN_SELECT_ITEM="token_slect_item";
public static final String TOKEN_ZDDZ="token_zddz";//整单打折
public static final String TOKEN_ZDYYH="token_zdyyh";//自定义优惠
public ObservableField<String> countPrice=new ObservableField<>();//订单金额
public ObservableField<String> lastPrice=new ObservableField<>();//应收金额
private String paymentid;//支付方式id
public ObservableField<String> payment=new ObservableField<>();//支付方式
public ObservableInt paymentVisibility=new ObservableInt(View.GONE);//支付结账显示隐藏
public ObservableInt yhIndex=new ObservableInt(-1);//选中的优惠index
private OnItemSelectListener onItemSelectListener;
public TitleViewModel titleViewModel;
private String orderid;//订单id
private float memberPrice;//会员价格
public ObservableInt youhuiVisibility=new ObservableInt(View.GONE);
public ObservableField<String> youhuiTitle=new ObservableField<>();//优惠方式
public ObservableField<String> youhuiMoney=new ObservableField<>();//优惠金额
public ObservableField<String> zhe=new ObservableField<>();//折
public ObservableInt zheVisibility=new ObservableInt();//折是否可见
private String reason;//优惠说明
private String reverseReason;//反结账原因
public PayViewModel(Context context,List<PaymentEntity> payLists,float countPrice,float memberPrice,String orderid,String reverseReason,OnItemSelectListener onItemSelectListener){
super(context);
titleViewModel=new TitleViewModel(context,"结账");
String all= new DecimalFormat("##0.00").format(countPrice);
this.countPrice.set(all);
this.memberPrice=memberPrice;
this.lastPrice.set(all);
this.orderid=orderid;
this.reverseReason=reverseReason;
this.onItemSelectListener=onItemSelectListener;
}
public interface OnItemSelectListener{
void onItemSelect(int type,int position);//type 0优惠方式 1收款方式
}
@Override
public void onCreate() {
super.onCreate();
getPayList();
}
@Override
public void registerRxBus() {
super.registerRxBus();
Messenger.getDefault().register(context, TOKEN_SELECT_ITEM, PayItemViewModel.class, new BindingConsumer<PayItemViewModel>() {
@Override
public void call(PayItemViewModel payItemViewModel) {
paymentid=payItemViewModel.entity.getId();
payment.set(payItemViewModel.entity.getPayment()+"记账");
paymentVisibility.set(View.VISIBLE);
if(onItemSelectListener!=null){
for(int i=0;i<payObservableList.size();i++){
PayItemViewModel viewModel=payObservableList.get(i);
if(i==Integer.parseInt(payItemViewModel.entity.getTag())){
viewModel.entity.setSelected(true);
}else{
viewModel.entity.setSelected(false);
}
}
onItemSelectListener.onItemSelect(1,Integer.parseInt(payItemViewModel.entity.getTag()));
}
}
});
//账单打折
Messenger.getDefault().register(context, TOKEN_ZDDZ, ZddzViewModel.class, new BindingConsumer<ZddzViewModel>() {
@Override
public void call(ZddzViewModel zddzViewModel) {
yhIndex.set(0);
youhuiTitle.set("整单打折");
zhe.set(zddzViewModel.zhe.get()+"折");
zheVisibility.set(View.VISIBLE);
reason=zddzViewModel.reason.get();
float last_money=Float.parseFloat(countPrice.get())*(Float.parseFloat(zddzViewModel.zhe.get())/10);
String last_money_str=new DecimalFormat("##0.00").format(last_money);
lastPrice.set(last_money_str);
Float youhui_money=Float.parseFloat(countPrice.get())-Float.parseFloat(countPrice.get())*(Float.parseFloat(zddzViewModel.zhe.get())/10);
String money=new DecimalFormat("##0.00").format(youhui_money);
youhuiMoney.set("-¥"+money);
youhuiVisibility.set(View.VISIBLE);
}
});
//自定义优惠
Messenger.getDefault().register(context, TOKEN_ZDYYH, ZdyyhViewModel.class, new BindingConsumer<ZdyyhViewModel>() {
@Override
public void call(ZdyyhViewModel zdyyhViewModel) {
yhIndex.set(2);
youhuiTitle.set("自定义优惠");
reason=zdyyhViewModel.youhuiReason.get();
zheVisibility.set(View.INVISIBLE);
float last_money=Float.parseFloat(countPrice.get())-Float.parseFloat(zdyyhViewModel.youhuiMoney.get());
String last_money_str=new DecimalFormat("##0.00").format(last_money);
lastPrice.set(last_money_str);
Float youhui_money=Float.parseFloat(zdyyhViewModel.youhuiMoney.get());
String money=new DecimalFormat("##0.00").format(youhui_money);
youhuiMoney.set("-¥"+money);
youhuiVisibility.set(View.VISIBLE);
}
});
}
//优惠撤销
public BindingCommand youhuiCancel=new BindingCommand(new BindingAction() {
@Override
public void call() {
youhuiVisibility.set(View.GONE);
yhIndex.set(-1);
lastPrice.set(countPrice.get());
}
});
public ObservableList<PayItemViewModel> payObservableList=new ObservableArrayList<>();
public ItemBinding<PayItemViewModel> payItemBinding=ItemBinding.of(BR.viewModel,R.layout.layout_pay_item);
//支付商家支持支付方式
private void getPayList(){
Map<String,String> params=new HashMap<>();
params.put("sellerid", SPUtils.getInstance("user_info").getString("sellerid"));
RetrofitClient.getInstance().create(PayApiService.class)
.getPaymentList(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
showDialog();
}
})
.subscribe(new Consumer<BaseResponse<List<List<PaymentEntity>>>>() {
@Override
public void accept(BaseResponse<List<List<PaymentEntity>>> listBaseResponse) throws Exception {
dismissDialog();
if (listBaseResponse.getRet()==200){
payObservableList.clear();
for(int i=0;i<listBaseResponse.getData().get(0).size();i++){
PaymentEntity entity=listBaseResponse.getData().get(0).get(i);
entity.setTag(String.valueOf(i));;
payObservableList.add(new PayItemViewModel(context,entity));
}
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable responseThrowable) throws Exception {
dismissDialog();
responseThrowable.printStackTrace();
ToastUtils.showShort(responseThrowable.msg);
}
});
}
//整单打折
public BindingCommand OnZddzClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
Bundle bundle=new Bundle();
bundle.putString("count_price",countPrice.get());
startActivity(ZddzActivity.class,bundle);
}
});
//会员优惠
public BindingCommand OnHyyhClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
HyyhDialog dialog=new HyyhDialog();
dialog.setCallback(new HyyhDialog.OnSureCallback() {
@Override
public void onSureClick() {
yhIndex.set(1);
youhuiTitle.set("会员优惠");
zheVisibility.set(View.INVISIBLE);
String last_money_str=new DecimalFormat("##0.00").format(memberPrice);
lastPrice.set(last_money_str);
Float youhui_money=Float.parseFloat(countPrice.get())-memberPrice;
String money=new DecimalFormat("##0.00").format(youhui_money);
youhuiMoney.set("-¥"+money);
youhuiVisibility.set(View.VISIBLE);
}
});
dialog.show(((AppCompatActivity)context).getSupportFragmentManager(),"HyyhDialog");
}
});
//自定义优惠
public BindingCommand OnZdyyhClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
Bundle bundle=new Bundle();
bundle.putString("count_price",countPrice.get());
startActivity(ZdyyhActivity.class,bundle);
}
});
//支付方式撤销
public BindingCommand paymentCancelCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
paymentid="";
paymentVisibility.set(View.GONE);
if(onItemSelectListener!=null){
onItemSelectListener.onItemSelect(1,-1);
}
}
});
//结账
public BindingCommand onPayClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
checkoutOrder();
}
});
//结账
private void checkoutOrder(){
if(TextUtils.isEmpty(paymentid)){
MaterialDialogUtils.showBasicDialogNoCancel(context,"提示","请选择记账方式").build().show();
return;
}
Map<String,String> params=new HashMap<>();
params.put("sellerid",SPUtils.getInstance("user_info").getString("sellerid"));
params.put("orderid",orderid);
params.put("paymentid",paymentid);
params.put("price",countPrice.get());//订单金额
params.put("receivable_price",lastPrice.get());//应付金额
if(yhIndex.get()!=-1){
params.put("discount_type",yhIndex.get()+"");//优惠方式 0:整单折扣 1:会员优惠 2:自定义
if(yhIndex.get()==0||yhIndex.get()==2){
params.put("discount_desc",TextUtils.isEmpty(reason)?"":reason);//优惠说明
}
if(yhIndex.get()==0){
params.put("discount",zhe.get().substring(0,zhe.get().length()-1));//折扣
}
params.put("discount_price",youhuiMoney.get().substring(2));//优惠金额
}
RetrofitClient.getInstance().create(PayApiService.class)
.checkoutOrder(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
showDialog();
}
})
.subscribe(new Consumer<BaseResponse<List<String>>>() {
@Override
public void accept(BaseResponse<List<String>> listBaseResponse) throws Exception {
if(listBaseResponse.getRet()==200){
if(TextUtils.isEmpty(reverseReason)){
dismissDialog();
final RightDialog dialog=new RightDialog();
Bundle bundle=new Bundle();
bundle.putString("msg","结账成功");
dialog.setArguments(bundle);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
startActivity(TangShiActivity.class);
}
}, 1000);
dialog.show(((AppCompatActivity)context).getSupportFragmentManager(),"rightDialog");
}else{
backOrderPay();
}
}else{
ToastUtils.showShort(listBaseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable throwable) throws Exception {
dismissDialog();
throwable.printStackTrace();
ToastUtils.showShort(throwable.msg);
}
});
}
//反结账
private void backOrderPay(){
Map<String,String> params=new HashMap<>();
params.put("orderid",orderid);
params.put("back_desc",reverseReason);
RetrofitClient.getInstance().create(HistoryApiService.class)
.backOrderPay(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
}
})
.subscribe(new Consumer<BaseResponse<List<String>>>() {
@Override
public void accept(BaseResponse<List<String>> listBaseResponse) throws Exception {
dismissDialog();
if(listBaseResponse.getRet()==200){
final RightDialog dialog=new RightDialog();
Bundle bundle=new Bundle();
bundle.putString("msg","反结账成功");
dialog.setArguments(bundle);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
startActivity(HistoryActivity.class);
}
}, 1000);
dialog.show(((AppCompatActivity)context).getSupportFragmentManager(),"rightDialog");
}else{
ToastUtils.showShort(listBaseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable responseThrowable) throws Exception {
dismissDialog();
responseThrowable.printStackTrace();
ToastUtils.showShort(responseThrowable.msg);
}
});
}
}