package com.phlx.wool.ui.sync.vm;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.google.gson.GsonBuilder;
import com.phlx.wool.Configs;
import com.phlx.wool.data.Repository;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.db.gen.EntryRegistrationEntityDao;
import com.phlx.wool.db.gen.HarmlessDao;
import com.phlx.wool.db.gen.HarmlessDetailDao;
import com.phlx.wool.db.gen.MarkingDao;
import com.phlx.wool.db.gen.QuarantineDao;
import com.phlx.wool.db.gen.QuarantineDetailDao;
import com.phlx.wool.db.gen.VillusBuyEntityDao;
import com.phlx.wool.db.gen.VillusGaugeEntityDao;
import com.phlx.wool.db.interf.DbIDUCallBack;
import com.phlx.wool.db.interf.DbQueryCallBack;
import com.phlx.wool.entity.EntryRegistrationEntity;
import com.phlx.wool.entity.Harmless;
import com.phlx.wool.entity.HarmlessDetail;
import com.phlx.wool.entity.Marking;
import com.phlx.wool.entity.Quarantine;
import com.phlx.wool.entity.QuarantineDetail;
import com.phlx.wool.entity.VillusBuyEntity;
import com.phlx.wool.entity.VillusGaugeEntity;
import com.phlx.wool.params.CattleResponse;
import com.phlx.wool.params.UploadParams;
import com.phlx.wool.ui.base.BackBarVM;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/29
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class UploadVM extends BackBarVM<Repository> {

    private String toastStr = "";

    public ObservableField<String> markingLabelNum = new ObservableField<>("打标记录");
    public ObservableField<String> harmlessLabelNum = new ObservableField<>("无害化处理");
    public ObservableField<String> quarantineLabelNum = new ObservableField<>("检疫记录");
    public ObservableField<String> villusBuyLabelNum = new ObservableField<>("绒毛购买");
    public ObservableField<String> villusGaugeLabelNum = new ObservableField<>("绒毛测量");
    public ObservableField<String> slaughterEntryLabelNum = new ObservableField<>("屠宰进场");

    public ObservableField<Boolean> isUploadMarking = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadHarmless = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadQuarantine = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadVillusBuy = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadVillusGauge = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadSlaughterEntry = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isUploadAll = new ObservableField<Boolean>(false);
    public ObservableInt uploadDataVisibility = new ObservableInt();

    private boolean isMarkingUpload, isHarmlessUpload, isQuarantineUpload, isVillusBuyUpload, isVillusGaugeUpload, isSlaughterEntryUpload;

    public UploadVM(@NonNull Application application, Repository repository) {
        super(application, repository);
        if (Configs.isOnLine) {
            uploadDataVisibility.set(View.VISIBLE);
        } else {
            uploadDataVisibility.set(View.GONE);
        }
    }

    //初始化ToolBar
    public void initToolbar() {
        setBackTitleText("上传");
    }

    public BindingCommand<Boolean> onUploadMarkingCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadMarking.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadHarmlessCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadHarmless.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadQuarantineCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadQuarantine.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadVillusBuyCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadVillusBuy.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadVillusGaugeCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadVillusGauge.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadSlaughterEntryCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadSlaughterEntry.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onUploadAllCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isUploadAll.set(isChecked);
                    isAllSelected(isChecked);
                }
            });

    private void isAllSelected(boolean isSelected) {
        isUploadMarking.set(isSelected);
        isUploadHarmless.set(isSelected);
        isUploadQuarantine.set(isSelected);
        isUploadVillusBuy.set(isSelected);
        isUploadVillusGauge.set(isSelected);
        isUploadSlaughterEntry.set(isSelected);
    }

    //查询每项未上传记录数量
    public void refreshUploadCount() {

        markingLabelNum.set("打标记录 " + "(" + DbUtil.getInstance().count(Marking.class) + ")");
        harmlessLabelNum.set("无害化处理 " + "(" + DbUtil.getInstance().count(Harmless.class) + ")");
        quarantineLabelNum.set("检疫记录 " + "(" + DbUtil.getInstance().count(Quarantine.class) + ")");

//        villusBuyLabelNum.set("绒毛购买 " + "(" + DbUtil.getInstance().count(VillusBuyEntity.class) + ")");

        QueryBuilder<VillusBuyEntity> builder =
                DbUtil.getInstance().getQueryBuilder(VillusBuyEntity.class)
                        .where(VillusBuyEntityDao.Properties.Status.eq(1));
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<VillusBuyEntity>() {
            @Override
            public void onSuccess(List<VillusBuyEntity> result) {
                if (result != null) {
                    villusBuyLabelNum.set("绒毛购买 " + "(" + result.size() + ")");
                } else {
                    villusBuyLabelNum.set("绒毛购买 " + "(0)");
                }
            }

            @Override
            public void onFailed() {
                villusBuyLabelNum.set("绒毛购买 " + "(" + "-" + ")");
            }
        }).queryAsyncAll(VillusBuyEntity.class, builder);

        villusGaugeLabelNum.set("绒毛测量 " + "(" + DbUtil.getInstance().count(VillusGaugeEntity.class) + ")");
        slaughterEntryLabelNum.set("屠宰进场 " + "(" + DbUtil.getInstance().count(EntryRegistrationEntity.class) + ")");

    }

    private void uploadNext() {

        if (isUploadMarking.get() && !isMarkingUpload) {
            uploadMarking();
        } else if (isUploadHarmless.get() && !isHarmlessUpload) {
            uploadHarmless();
        } else if (isUploadQuarantine.get() && !isQuarantineUpload) {
            uploadQuarantine();
        } else if (isUploadVillusBuy.get() && !isVillusBuyUpload) {
            uploadVillusBuy();
        } else if (isUploadVillusGauge.get() && !isVillusGaugeUpload) {
            uploadVillusGauge();
        } else if (isUploadSlaughterEntry.get() && !isSlaughterEntryUpload) {
            uploadSlaughterEntry();
        } else {
            refreshUploadCount();
            if (!isUploadMarking.get() && !isUploadHarmless.get() && !isUploadQuarantine.get() && !isUploadVillusBuy.get() && !isUploadVillusGauge.get()
                    && !isUploadSlaughterEntry.get()) {
                ToastUtils.showShort("请选择上传的项目");
            } else if (!"".equals(toastStr)) {
                ToastUtils.showShort(toastStr);
            } else {
                ToastUtils.showShort("上传成功");
                finish();
            }
        }

    }

    public BindingCommand onUploadClickCommand = new BindingCommand(() -> {

        isMarkingUpload = false;
        isHarmlessUpload = false;
        isQuarantineUpload = false;
        isVillusBuyUpload = false;
        isVillusGaugeUpload = false;
        isSlaughterEntryUpload = false;
        toastStr = "";

        uploadNext();

    });

    private void uploadMarking() {

        QueryBuilder<Marking> builder =
                DbUtil.getInstance().getQueryBuilder(Marking.class).
                        where(MarkingDao.Properties.Status.eq(1)).limit(100);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Marking>() {
            @Override
            public void onSuccess(List<Marking> result) {

                isMarkingUpload = true;
                if (result != null && result.size() > 0) {
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<Marking>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitMarking(body)
                                    .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                                    .compose(RxUtils.schedulersTransformer())
                                    .compose(RxUtils.exceptionTransformer())
                                    .doOnSubscribe(disposable -> showDialog("正在上传marking数据..."))
                                    .subscribe((Consumer<CattleResponse<List<Marking>>>) response -> {
                                        KLog.e(response.getCode() + response.getMsg());
                                        if (response.getCode() == 0) {
//                                            for (int i = 0; i < result.size(); i++) {
//                                                result.get(i).setStatus(0);
//                                            }
//                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
//                                        @Override
//                                        public void onNotification(boolean result) {
//                                            if (result) {
//                                                dismissDialog();
//                                            } else {
//                                                ToastUtils.showShort("删除marking数据失败");
//                                            }
//                                            uploadNext();
//                                        }
//                                    }).deleteAsyncBatch(Marking.class, result);

                                            DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                                @Override
                                                public void onNotification(boolean result) {
                                                    if (result) {
                                                        uploadMarking();
                                                    } else {
                                                        dismissDialog();
                                                        toastStr += "删除标签数据失败；";
                                                        uploadNext();
                                                    }
                                                }
                                            }).deleteAsyncBatch(Marking.class, result);
                                        } else {
                                            dismissDialog();
                                            toastStr += "marking上传失败;";
                                            ToastUtils.showShort("marking上传失败");
                                            uploadNext();
                                        }
                                    }, (Consumer<ResponseThrowable>) throwable -> {
                                        dismissDialog();
                                        ToastUtils.showShort(throwable.message);
                                        uploadNext();
                                    })
                    );
                } else {
                    dismissDialog();
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("marking数据库查询失败");
            }
        }).queryAsyncAll(Marking.class, builder);


    }


    /**
     * 上传检疫数据
     */
    private void uploadQuarantine() {
        QueryBuilder<Quarantine> builder =
                DbUtil.getInstance().getQueryBuilder(Quarantine.class).
                        where(QuarantineDao.Properties.Status.eq(1)).limit(20);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Quarantine>() {
            @Override
            public void onSuccess(List<Quarantine> result) {
                isQuarantineUpload = true;
                if (result != null && result.size() > 0) {
                    List<QuarantineDetail> quarantineDetails = new ArrayList<>();
                    KLog.e(result.toString());
                    for (Quarantine quarantine : result) {
                        if (quarantine.getList() != null && quarantine.getList().size() > 0) {
                            quarantine.setList(null);
                        }
                        QueryBuilder<QuarantineDetail> builder =
                                DbUtil.getInstance().getQueryBuilder(QuarantineDetail.class).
                                        where(QuarantineDetailDao.Properties.QuarantineId.eq(quarantine.getId()));
                        ;
                        quarantineDetails.addAll(DbUtil.getInstance().queryAll(QuarantineDetail.class, builder));
                        quarantine.setList(quarantineDetails);
                    }
                    KLog.e(result.toString());
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<Quarantine>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitQuarantine(body)
                            .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                            .compose(RxUtils.schedulersTransformer())
                            .compose(RxUtils.exceptionTransformer())
                            .doOnSubscribe(disposable -> showDialog("正在上传检疫数据..."))
                            .subscribe((Consumer<CattleResponse<List<Quarantine>>>) response -> {
                                KLog.e(response.getCode() + response.getMsg());
                                dismissDialog();
                                if (response.getCode() == 0) {
                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                        @Override
                                        public void onNotification(boolean result) {
                                            if (result) {
                                                if (quarantineDetails != null && quarantineDetails.size() > 0) {
                                                    DbUtil.getInstance().deleteList(quarantineDetails);
                                                    uploadQuarantine();
                                                }
                                            } else {
                                                ToastUtils.showShort("删除检疫数据失败");
                                                uploadNext();
                                            }
                                        }
                                    }).deleteAsyncBatch(Quarantine.class, result);
                                } else {
                                    toastStr += "上传检疫数据失败;";
                                    ToastUtils.showShort("上传检疫数据失败");
                                    uploadNext();
                                }
                            }, (Consumer<ResponseThrowable>) throwable -> {
                                dismissDialog();
                                ToastUtils.showShort(throwable.message);
                                uploadNext();
                            })
                    );
                } else {
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("检疫数据库查询失败");
            }
        }).queryAsyncAll(Quarantine.class, builder);
    }


    /**
     * 上传数据
     */
    private void uploadHarmless() {
        QueryBuilder<Harmless> builder =
                DbUtil.getInstance().getQueryBuilder(Harmless.class).
                        where(HarmlessDao.Properties.Status.eq(1)).limit(20);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Harmless>() {
            @Override
            public void onSuccess(List<Harmless> result) {
                isHarmlessUpload = true;
                if (result != null && result.size() > 0) {
                    KLog.e(result.toString());
                    List<HarmlessDetail> harmlessDetails = new ArrayList<>();
                    for (Harmless harmless : result) {
                        if (harmless.getList() != null && harmless.getList().size() > 0) {
                            harmless.setList(null);
                        }
                        QueryBuilder<HarmlessDetail> builder =
                                DbUtil.getInstance().getQueryBuilder(HarmlessDetail.class).
                                        where(HarmlessDetailDao.Properties.HarmlessId.eq(harmless.getId()));
                        harmlessDetails.addAll(
                                DbUtil.getInstance().queryAll(HarmlessDetail.class, builder));
                        harmless.setList(harmlessDetails);
                    }
                    KLog.e(result.toString());
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<Harmless>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitHarmless(body)
                            .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                            .compose(RxUtils.schedulersTransformer())
                            .compose(RxUtils.exceptionTransformer())
                            .doOnSubscribe(disposable -> showDialog("正在上传无害化数据..."))
                            .subscribe((Consumer<CattleResponse<List<Harmless>>>) response -> {
                                KLog.e(response.getCode() + response.getMsg());
                                dismissDialog();
                                if (response.getCode() == 0) {
                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                        @Override
                                        public void onNotification(boolean result) {
                                            if (result) {
                                                if (harmlessDetails != null && harmlessDetails.size() > 0) {
                                                    DbUtil.getInstance().deleteList(harmlessDetails);
                                                    uploadHarmless();
                                                }
                                            } else {
                                                dismissDialog();
                                                ToastUtils.showShort("删除无害化数据失败");
                                                uploadNext();
                                            }
                                        }
                                    }).deleteAsyncBatch(Harmless.class, result);
                                } else {
                                    dismissDialog();
                                    toastStr += "上传无害化失败;";
                                    ToastUtils.showShort("上传无害化失败");
                                    uploadNext();
                                }
                            }, (Consumer<ResponseThrowable>) throwable -> {
                                dismissDialog();
                                ToastUtils.showShort(throwable.message);
                                uploadNext();
                            })
                    );
                } else {
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("无害化数据库查询失败");
            }
        }).queryAsyncAll(Harmless.class, builder);
    }

    /**
     * 上传绒毛购买数据
     */
    private void uploadVillusBuy() {

        QueryBuilder<VillusBuyEntity> builder =
                DbUtil.getInstance().getQueryBuilder(VillusBuyEntity.class).
                        where(VillusBuyEntityDao.Properties.Status.eq(1)).limit(100);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<VillusBuyEntity>() {
            @Override
            public void onSuccess(List<VillusBuyEntity> result) {
                isVillusBuyUpload = true;
                if (result != null && result.size() > 0) {
                    KLog.e(result.toString());
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<VillusBuyEntity>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitVillusBuy(body)
                            .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                            .compose(RxUtils.schedulersTransformer())
                            .compose(RxUtils.exceptionTransformer())
                            .doOnSubscribe(disposable -> showDialog("正在上传绒毛购买数据..."))
                            .subscribe((Consumer<CattleResponse<List<VillusBuyEntity>>>) response -> {
                                KLog.e(response.getCode() + response.getMsg());
                                dismissDialog();
                                if (response.getCode() == 0) {
                                    for (VillusBuyEntity villusBuyEntity : result) {
                                        villusBuyEntity.setStatus(0);
                                    }
                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                        @Override
                                        public void onNotification(boolean result) {
                                            if (result) {
                                                uploadVillusBuy();
                                            } else {
                                                ToastUtils.showShort("更新绒毛购买数据失败");
                                                uploadNext();
                                            }
                                        }
                                    }).updateAsyncBatch(VillusBuyEntity.class, result);
                                } else {
                                    toastStr += "上传绒毛购买数据失败;";
                                    ToastUtils.showShort("上传绒毛购买数据失败");
                                    uploadNext();
                                }
                            }, (Consumer<ResponseThrowable>) throwable -> {
                                dismissDialog();
                                ToastUtils.showShort(throwable.message);
                                uploadNext();
                            })
                    );
                } else {
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("查询绒毛购买数据失败");
            }
        }).queryAsyncAll(VillusBuyEntity.class, builder);
    }

    /**
     * 上传绒毛测量数据
     */
    private void uploadVillusGauge() {
        QueryBuilder<VillusGaugeEntity> builder =
                DbUtil.getInstance().getQueryBuilder(VillusGaugeEntity.class).
                        where(VillusGaugeEntityDao.Properties.Status.eq(1)).limit(100);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<VillusGaugeEntity>() {
            @Override
            public void onSuccess(List<VillusGaugeEntity> result) {
                isVillusGaugeUpload = true;
                if (result != null && result.size() > 0) {
                    KLog.e(result.toString());
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<VillusGaugeEntity>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitVillusGauge(body)
                            .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                            .compose(RxUtils.schedulersTransformer())
                            .compose(RxUtils.exceptionTransformer())
                            .doOnSubscribe(disposable -> showDialog("正在上传绒毛测量数据..."))
                            .subscribe((Consumer<CattleResponse<List<VillusGaugeEntity>>>) response -> {
                                KLog.e(response.getCode() + response.getMsg());
                                dismissDialog();
                                if (response.getCode() == 0) {
//                                    for (VillusGaugeEntity villusGaugeEntity : result) {
//                                        villusGaugeEntity.setStatus(0);
//                                    }
                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                        @Override
                                        public void onNotification(boolean result) {
                                            if (result) {
                                                uploadVillusGauge();
                                            } else {
                                                ToastUtils.showShort("删除绒毛测量数据失败");
                                                uploadNext();
                                            }
                                        }
                                    }).deleteAsyncBatch(VillusGaugeEntity.class, result);
                                } else {
                                    toastStr += "上传绒毛测量数据失败;";
                                    ToastUtils.showShort("上传绒毛测量数据失败");
                                    uploadNext();
                                }
                            }, (Consumer<ResponseThrowable>) throwable -> {
                                dismissDialog();
                                ToastUtils.showShort(throwable.message);
                                uploadNext();
                            })
                    );
                } else {
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("查询绒毛测量数据失败");
            }
        }).queryAsyncAll(VillusGaugeEntity.class, builder);
    }

    /**
     * 上传屠宰进场数据
     */
    private void uploadSlaughterEntry() {
        QueryBuilder<EntryRegistrationEntity> builder =
                DbUtil.getInstance().getQueryBuilder(EntryRegistrationEntity.class).
                        where(EntryRegistrationEntityDao.Properties.Status.eq(1)).limit(100);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<EntryRegistrationEntity>() {
            @Override
            public void onSuccess(List<EntryRegistrationEntity> result) {
                isSlaughterEntryUpload = true;
                if (result != null && result.size() > 0) {
                    KLog.e(result.toString());
                    String postInfoStr =
                            new GsonBuilder().serializeNulls()
//                                    .excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(new UploadParams<List<EntryRegistrationEntity>>(result));
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postInfoStr);
                    KLog.e(postInfoStr);
                    addSubscribe(model.submitSlaughterEntry(body)
                            .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                            .compose(RxUtils.schedulersTransformer())
                            .compose(RxUtils.exceptionTransformer())
                            .doOnSubscribe(disposable -> showDialog("正在上传屠宰进场数据..."))
                            .subscribe((Consumer<CattleResponse<List<EntryRegistrationEntity>>>) response -> {
                                KLog.e(response.getCode() + response.getMsg());
                                dismissDialog();
                                if (response.getCode() == 0) {
//                                    for (EntryRegistrationEntity entryRegistrationEntity : result) {
//                                        entryRegistrationEntity.setStatus(0);
//                                    }
                                    DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
                                        @Override
                                        public void onNotification(boolean result) {
                                            if (result) {
                                                uploadSlaughterEntry();
                                            } else {
                                                ToastUtils.showShort("删除屠宰进场数据失败");
                                                uploadNext();
                                            }
                                        }
                                    }).deleteAsyncBatch(EntryRegistrationEntity.class, result);
                                } else {
                                    toastStr += "上传屠宰进场数据失败;";
                                    ToastUtils.showShort("上传屠宰进场数据失败");
                                    uploadNext();
                                }
                            }, (Consumer<ResponseThrowable>) throwable -> {
                                dismissDialog();
                                ToastUtils.showShort(throwable.message);
                                uploadNext();
                            })
                    );
                } else {
                    uploadNext();
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("查询屠宰进场数据失败");
            }
        }).queryAsyncAll(EntryRegistrationEntity.class, builder);
    }

}
