CartViewModel.java
24.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package com.naji.xiaotao.cart.vm;
import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
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.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.CartCookbook;
import com.naji.xiaotao.cart.entity.CartCookbookDetail;
import com.naji.xiaotao.cart.entity.CartEntity;
import com.naji.xiaotao.cart.entity.ClearOutter;
import com.naji.xiaotao.cart.entity.OrderEntity;
import com.naji.xiaotao.cart.entity.RemarkEntity;
import com.naji.xiaotao.cart.entity.RemarkItemEntity;
import com.naji.xiaotao.cart.service.CartApiService;
import com.naji.xiaotao.cart.ui.CartActivity;
import com.naji.xiaotao.cart.ui.ClearDialogFragment;
import com.naji.xiaotao.cart.ui.RemarkActivity;
import com.naji.xiaotao.common.dialog.RightDialog;
import com.naji.xiaotao.cookbook.bean.OuterEntity;
import com.naji.xiaotao.cookbook.ui.CookBookMainActivity;
import com.naji.xiaotao.http.RetrofitClient;
import com.naji.xiaotao.login.entity.SellerConfig;
import com.naji.xiaotao.login.service.LoginApiService;
import com.naji.xiaotao.order.ui.OrderActivity;
import com.naji.xiaotao.order.vm.OrderItemViewModel;
import com.naji.xiaotao.tangshi.service.TangshiApiService;
import com.naji.xiaotao.tangshi.ui.TangShiActivity;
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.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
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 CartViewModel extends BaseViewModel{
public static final String TOKEN_DELETE_ITEM="token_delete_item";//删减一个菜品item
public ObservableField<String> no=new ObservableField<>();//单号
public ObservableField<String> pnum=new ObservableField<>();//当前用餐人数
public ObservableField<String> tableid=new ObservableField<>();//台号
public ObservableInt tableVisibility=new ObservableInt(View.GONE);//是否显示台号
public ObservableField<String> remark=new ObservableField<>();//备注
public ObservableField<String> title=new ObservableField<>();//list标题
private String orderid;//订单id
public OrderEntity orderEntity;
private ArrayList<CartCookbook> cLists=new ArrayList<>();
private OnRequestFocusListener onRequestFocusListener;
private Disposable mSubscription;//订阅者
public TitleViewModel titleViewModel;
private float countPrice;//订单金额
private int countTotal;//订单人数
public String isSetMealFee;//是否设置餐位费
private String meelFee;//餐位费
private float ortherMoney=0.00f;//餐位费
private String fromSource;//加菜
private boolean isFast;//是否快餐
private boolean isWaitingSure;//待确认
private String messageid;//消息id
public CartViewModel(Context context,String orderid,OnRequestFocusListener onRequestFocusListener,String fromSource,boolean isFast,boolean isWaitingSure,String messageid){
super(context);
this.orderid=orderid;
this.onRequestFocusListener=onRequestFocusListener;
this.titleViewModel=new TitleViewModel(context,"购物车");
this.fromSource=fromSource;
this.isFast=isFast;
this.isWaitingSure=isWaitingSure;
this.messageid=messageid;
}
public interface OnRequestFocusListener{
//人数输入框获取焦点
void onPnumRequestFocus();
//备注获取焦点
void onRemardRequestFocus();
}
@Override
public void onStart() {
super.onStart();
getOrderDetail();
}
@Override
public void registerRxBus() {
super.registerRxBus();
//监听总的菜品数量和价格变化
mSubscription=RxBus.getDefault().toObservable(String.class).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
if(s.contains("¥")){
countPrice=Float.parseFloat(s.substring(s.indexOf("¥")+1));
String all= new DecimalFormat("##0.00").format(countPrice+ortherMoney);
title.set(s.substring(0,s.indexOf("¥")+1)+all);
}
}
});
RxSubscriptions.add(mSubscription);
//监听删除掉一个菜品Item
Messenger.getDefault().register(context, TOKEN_DELETE_ITEM, CartItemViewModel.class, new BindingConsumer<CartItemViewModel>() {
@Override
public void call(CartItemViewModel cartItemViewModel) {
int titleIndex=Integer.parseInt(cartItemViewModel.entity.getTag());
KLog.d("titleIndex",titleIndex);
KLog.d("size",observableList.size());
int size=observableList.get(titleIndex).entity.getSize();
observableList.get(titleIndex).title.set(cartItemViewModel.entity.getClass_name()+"("+(size-1)+")");
observableList.get(titleIndex).entity.setSize(--size);
KLog.d("titleIndex:"+titleIndex+"size:"+size);
int groupIndex=cartItemViewModel.entity.getGroupIndex();
if(size==0){//只剩标题,删除标题
cLists.remove(groupIndex);
}else{
cLists.get(groupIndex).getList().remove(cartItemViewModel.entity.getGroupInnerIndex());
}
initCookbookList();
}
});
}
@Override
public void removeRxBus() {
super.removeRxBus();
RxSubscriptions.remove(mSubscription);
Messenger.getDefault().unregister(context,TOKEN_DELETE_ITEM);
}
public ObservableList<CartItemViewModel> observableList=new ObservableArrayList<>();
public OnItemBind<CartItemViewModel> onItemBind=new OnItemBind<CartItemViewModel>() {
@Override
public void onItemBind(ItemBinding itemBinding, int position, CartItemViewModel item) {
itemBinding.set(BR.viewModel,item.entity.isTitle()? R.layout.cart_item_title:R.layout.cart_item_content);
}
};
//修改就餐人数
public BindingCommand onPnumClick=new BindingCommand(new BindingAction() {
@Override
public void call() {
if(onRequestFocusListener!=null){
onRequestFocusListener.onPnumRequestFocus();
}
}
});
//修改备注
public BindingCommand onRemarkClick=new BindingCommand(new BindingAction() {
@Override
public void call() {
Bundle bundle=new Bundle();
bundle.putString("orderid",orderid);
bundle.putString("remarkid",orderEntity.getRemarkid());
bundle.putString("remark",orderEntity.getRemark());
startActivity(RemarkActivity.class,bundle);
}
});
//获取订单详情
private void getOrderDetail(){
HashMap<String,String> params=new HashMap<>();
params.put("orderid",orderid);
if(isWaitingSure){
params.put("status","2");//2 待确认菜品
}else{
params.put("status","0");//0 购物车中菜品 1 已下单菜品
}
RetrofitClient.getInstance().create(CartApiService.class)
.getOrderById(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<CartEntity>>>() {
@Override
public void accept(BaseResponse<List<CartEntity>> listBaseResponse) throws Exception {
dismissDialog();
if(listBaseResponse.getRet()==200){
orderEntity=listBaseResponse.getData().get(0).getOrder().get(0);
no.set("单号:"+orderEntity.getRow_num());
pnum.set(orderEntity.getPnum());
tableid.set("台号:"+orderEntity.getTablename());
if(orderEntity.getTablename().equals("快餐")){
tableVisibility.set(View.GONE);
}else{
tableVisibility.set(View.VISIBLE);
}
StringBuilder builder=new StringBuilder();
if(orderEntity.getRemarkList()!=null&&orderEntity.getRemarkList().size()>0){
for(RemarkItemEntity itemEntity:orderEntity.getRemarkList()){
builder.append(itemEntity.getRemark()+",");
}
}
if(!TextUtils.isEmpty(orderEntity.getRemark())){
builder.append(orderEntity.getRemark()+",");
}
if(builder.length()>0){
remark.set(builder.toString().substring(0,builder.toString().length()-1));
}else{
remark.set("");
}
countTotal=listBaseResponse.getData().get(0).getCountTotal();
countPrice=listBaseResponse.getData().get(0).getCountPirce();
cLists.clear();
cLists.addAll(listBaseResponse.getData().get(0).getCookbooklist());
getSellerConfig();
}else{
ToastUtils.showShort(listBaseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable throwable) throws Exception {
dismissDialog();
ToastUtils.showShort(throwable.msg);
throwable.printStackTrace();
}
});
}
//获取商家配置
private void getSellerConfig(){
Map<String,String> params=new HashMap<>();
params.put("sellerid",SPUtils.getInstance("user_info").getString("sellerid"));
params.put("type","1");//1堂食配置 2外卖配置
RetrofitClient.getInstance().create(LoginApiService.class)
.getSellerConfig(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
}
})
.subscribe(new Consumer<BaseResponse<List<SellerConfig>>>() {
@Override
public void accept(BaseResponse<List<SellerConfig>> listBaseResponse) throws Exception {
if(listBaseResponse.getRet()==200){
KLog.d("sssssssssssss");
isSetMealFee=listBaseResponse.getData().get(0).getIs_set_meal_fee();
meelFee=listBaseResponse.getData().get(0).getMeal_fee();
if(isSetMealFee.equals("1")){
ortherMoney=Float.parseFloat(meelFee)*Integer.parseInt(orderEntity.getPnum());
}
String all= new DecimalFormat("##0.00").format(countPrice+ortherMoney);
title.set("已点"+countTotal+"个菜,共计¥"+all);
initCookbookList();
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable responseThrowable) throws Exception {
}
});
}
private void initCookbookList(){
observableList.clear();
int tag=0;
for(int i=0;i<cLists.size();i++){
CartCookbookDetail head=new CartCookbookDetail();
head.setTitle(true);
head.setClass_name(cLists.get(i).getClass_name());
head.setTitleName(cLists.get(i).getClass_name()+"("+cLists.get(i).getList().size()+")");
head.setTag(String.valueOf(tag));
head.setSize(cLists.get(i).getList().size());
observableList.add(new CartItemViewModel(context,head,orderid,fromSource,isWaitingSure));
int titleIndex=tag;
for(int j=0;j<cLists.get(i).getList().size();j++){
tag++;
CartCookbookDetail content=cLists.get(i).getList().get(j);
content.setTitleName(cLists.get(i).getClass_name()+"("+cLists.get(i).getList().size()+")");
content.setTag(String.valueOf(titleIndex));
content.setGroupIndex(i);
content.setGroupIndex(j);
observableList.add(new CartItemViewModel(context,content,orderid,fromSource,isWaitingSure));
}
tag++;
}
if(isSetMealFee.equals("1")){
CartCookbookDetail head=new CartCookbookDetail();
head.setTitle(true);
head.setClass_name("其它");
head.setTitleName("其它");
observableList.add(new CartItemViewModel(context,head,orderid,fromSource,isWaitingSure));
CartCookbookDetail content=new CartCookbookDetail();
content.setTitleName("其它");
content.setName("餐位费");
content.setPrice(meelFee);
content.setUnit("位");
content.setCount(orderEntity.getPnum());
observableList.add(new CartItemViewModel(context,content,orderid,fromSource,isWaitingSure));
}
}
//修改人数
public BindingCommand textChangeCommand=new BindingCommand(new BindingConsumer<String>() {
@Override
public void call(String s) {
if(!TextUtils.isEmpty(s)){
int pnum=Integer.parseInt(s);
if(isSetMealFee!=null&&isSetMealFee.equals("1")){
ortherMoney=Float.parseFloat(meelFee)* pnum;
String titleText=title.get().toString();
KLog.d("titleText:"+titleText);
String all= new DecimalFormat("##0.00").format(countPrice+ortherMoney);
title.set(titleText.substring(0,titleText.indexOf("¥")+1)+all);
KLog.d(countPrice);
KLog.d(ortherMoney);
KLog.d("title:"+(titleText.substring(0,titleText.indexOf("¥")+1)));
RxBus.getDefault().post(pnum);
}
setOrderById(pnum);
}
}
});
//继续点菜
public BindingCommand onContinueClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
Intent intent=new Intent(context,CookBookMainActivity.class);
intent.putExtra("orderid",orderid);
intent.putExtra("isFast",isFast);
intent.putExtra("fromSource",fromSource);
intent.putExtra("messageid",messageid);
intent.putExtra("isWaitingSure",isWaitingSure);
context.startActivity(intent);
}
});
//点击确认下单按钮
public BindingCommand onSureClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
setClearNum();
}
});
//修改订单人数备注
private void setOrderById(int pnum){
Map<String,String> params=new HashMap<>();
params.put("orderid",orderid);
params.put("pnum",pnum+"");
RetrofitClient.getInstance().create(CartApiService.class)
.setOrderById(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 {
if(listBaseResponse.getRet()==200){
}else{
ToastUtils.showShort(listBaseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable responseThrowable) throws Exception {
responseThrowable.printStackTrace();
ToastUtils.showShort(responseThrowable.msg);
}
});
}
//下单时检测菜品是否充足
private void setClearNum(){
if(TextUtils.isEmpty(pnum.get())){
ToastUtils.showShort("请输入用餐人数");
return;
}
Map<String,String> params=new HashMap<>();
params.put("orderid",orderEntity.getId());
if(TextUtils.isEmpty(fromSource)){
params.put("type","1");//下单1、加菜2
}else{
params.put("type","2");//下单1、加菜2
}
params.put("identity","2");//1 顾客 2 服务员
RetrofitClient.getInstance().create(CartApiService.class)
.setClearNum(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<ClearOutter>>>() {
@Override
public void accept(BaseResponse<List<ClearOutter>> listBaseResponse) throws Exception {
if(listBaseResponse.getRet()==200){
if(listBaseResponse.getData().size()==0){//没有仓储不足菜品直接下单
placeOrder();
}else{
dismissDialog();
ClearDialogFragment dialog=new ClearDialogFragment();
Bundle bundle=new Bundle();
bundle.putParcelable("clearOuter",listBaseResponse.getData().get(0));
bundle.putString("orderid",orderid);
bundle.putString("fromSource",fromSource);
bundle.putBoolean("isFast",isFast);
bundle.putString("messageid",messageid);
dialog.setArguments(bundle);
dialog.show(((CartActivity)context).getSupportFragmentManager(),"clearFragment");
}
}else{
dismissDialog();
ToastUtils.showShort(listBaseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable responseThrowable) throws Exception {
dismissDialog();
responseThrowable.printStackTrace();
ToastUtils.showShort(responseThrowable.msg);
}
});
}
//下单接口
private void placeOrder(){
Map<String,String> params=new HashMap<>();
params.put("sellerid",orderEntity.getSellerid());
params.put("orderid",orderEntity.getId());
if(TextUtils.isEmpty(fromSource)){
params.put("type","1");//下单1、加菜2
}else{
params.put("type","2");//下单1、加菜2
}
params.put("identity","2");//1 顾客 2 服务员
RetrofitClient.getInstance().create(CartApiService.class)
.placeOrder(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){
if(!TextUtils.isEmpty(messageid)){
getAppHandle(messageid);
}
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();
if(isFast){//快速点餐直接跳到订单详情
Intent intent=new Intent(context, OrderActivity.class);
intent.putExtra("orderid",orderid);
context.startActivity(intent);
}else{
startActivity(TangShiActivity.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);
}
});
}
//告诉后台前端消息已处理
private void getAppHandle(String messageid){
Map<String,String> params=new HashMap<>();
params.put("message_id",messageid);
RetrofitClient.getInstance().create(TangshiApiService.class)
.getAppHandle(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
}
})
.subscribe(new Consumer<BaseResponse<String>>() {
@Override
public void accept(BaseResponse<String> stringBaseResponse) throws Exception {
if(stringBaseResponse.getRet()==200){}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
});
}
}