package com.example.administrator.zhapp.fragment.xfbwxcgl;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.zhapp.DBModel.LyXcjsInfo;
import com.example.administrator.zhapp.DBModel.LyYhpcInfo;
import com.example.administrator.zhapp.DBModel.LyxcXm;
import com.example.administrator.zhapp.DBModel.LyxcXmJg;
import com.example.administrator.zhapp.DBModel.Lyxcrwqy;
import com.example.administrator.zhapp.DBModel.XfBaxcRwqy;
import com.example.administrator.zhapp.DBModel.XfDjjh;
import com.example.administrator.zhapp.DBModel.XfDjjhRwqy;
import com.example.administrator.zhapp.DBModel.XfXcjsInfo;
import com.example.administrator.zhapp.DBModel.XfXcmhqc;
import com.example.administrator.zhapp.DBModel.XfXcxm;
import com.example.administrator.zhapp.DBModel.XfXcxmjg;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.xfbwxcgl.XxzActivity;
import com.example.administrator.zhapp.adapter.XfXzjhAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.StatusInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.HttpResponseListener;
import com.example.administrator.zhapp.utils.RSAUtils;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;
import com.example.administrator.zhapp.weight.WaitDialog;
import com.orhanobut.logger.Logger;
import com.yolanda.nohttp.FileBinary;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.litepal.crud.DataSupport.where;

/**
 * 消防上传
 */
public class XscFragment extends BaseFragment {

    ListView lv;
    Button btnSc;
    Button btnHf;
    Button btnDel;
    TextView tvNodata;

    private XxzActivity mActivity;
    private View headView;
    private View head;
    private CheckBox cb;
    private XfXzjhAdapter adapter;

    //private List<XfDjjhRwList> rwLists;
    private List<XfDjjh> djjhs;
    private List<XfDjjhRwqy> rwqys;
    private List<XfDjjh> list;
    private List<XfDjjh> removeList;
    private Map<String, List<XfDjjhRwqy>> map;
    //private List<ScDjjhInfo> scs;//上传的集合
    private boolean first = true;

    //private List<XfBaxcRwqyList> xfbarws;
    private List<XfBaxcRwqy> barwqys;
    private List<Lyxcrwqy> lyrwqys;

    private int requestCount = 0;
    private int successCount = 0;
    private WaitDialog dialog;
    private RequestQueue queue;

    private List<XfDjjhRwqy> yjqys;//已检查项目

    public XscFragment() {
    }

    public static XscFragment newInstance() {
        XscFragment fragment = new XscFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            visible();
        }
    }

    //视图显示时才查询数据库数据
    private void visible() {
        getDataInSQL();
        setListData();
    }

    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        btnSc = view.findViewById(R.id.btn_sc);
        btnHf = view.findViewById(R.id.btn_hf);
        btnDel = view.findViewById(R.id.btn_delete);
        tvNodata = view.findViewById(R.id.tv_nodata);

        dialog = new WaitDialog(context, "上传中...");
        dialog.setCancelable(false);
        lv.setEmptyView(tvNodata);
        mActivity = (XxzActivity) getActivity();
        headView = View.inflate(context, R.layout.xzjh_item, null);
        head = headView.findViewById(R.id.head);
        cb = (CheckBox) headView.findViewById(R.id.cb);
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (cb.isChecked()) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setChecked(true);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setChecked(false);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });

        btnSc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadData();
            }
        });
        btnHf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("恢复");
            }
        });
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<XfDjjh> dels = new ArrayList<>();
                for (XfDjjh djjh : list) {
                    if (djjh.isChecked()) {
                        dels.add(djjh);
                    }
                }
                removeData(dels);
            }
        });

        TextView yjzj = (TextView) headView.findViewById(R.id.tv_jhlx);
        yjzj.setText("已检/总计");
        head.setVisibility(View.VISIBLE);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_xsc;
    }

    public void initData() {

        queue = NoHttp.newRequestQueue(1);
        djjhs = new ArrayList<>();
        rwqys = new ArrayList<>();
        list = new ArrayList<>();
        map = new HashMap<>();
        barwqys = new ArrayList<>();
        removeList = new ArrayList<>();
        lyrwqys = new ArrayList<>();
    }

    /**
     * 获取数据库的数据
     */
    public void getDataInSQL() {

        rwqys.clear();
        djjhs.clear();
        list.clear();
        barwqys.clear();
        lyrwqys.clear();

        //查找已经下载的点检计划
        djjhs.addAll(DataSupport.where("download = ?", "1").find(XfDjjh.class));

        if (djjhs.size() != 0) {
            for (XfDjjh xfdjjh : djjhs) {
                //保安任务
                if (xfdjjh.getXctypes().equals("2")) {
                    barwqys = DataSupport.where("jhid = ?", xfdjjh.getJhid()).find(XfBaxcRwqy.class);
                    int zong = barwqys.size();
                    int wz = 0;//已检总数
                    for (XfBaxcRwqy rwqy : barwqys) {
                        if (rwqy.isChecked()) {
                            wz++;
                        }
                    }
                    XfDjjh djjh = new XfDjjh();
                    djjh.setJhid(xfdjjh.getJhid());
                    djjh.setJhmc(xfdjjh.getJhmc());
                    djjh.setNexttime(wz + "/" + zong);
                    djjh.setXctypes(xfdjjh.getXctypes());
                    list.add(djjh);
                }
                //消防任务
                else if (xfdjjh.getXctypes().equals("1")) {
                    rwqys = DataSupport.where("jhid = ?", xfdjjh.getJhid()).find(XfDjjhRwqy.class);
                    int zong = rwqys.size();
                    int wz = 0;//已检总数
                    for (XfDjjhRwqy rwqy : rwqys) {
                        if (rwqy.isChecked()) {
                            wz++;
                        }
                    }
                    XfDjjh djjh = new XfDjjh();
                    djjh.setJhid(xfdjjh.getJhid());
                    djjh.setJhmc(xfdjjh.getJhmc());
                    djjh.setNexttime(wz + "/" + zong);
                    djjh.setXctypes(xfdjjh.getXctypes());
                    list.add(djjh);
                }
                //楼宇任务
                else if (xfdjjh.getXctypes().equals("3")) {
                    lyrwqys = DataSupport.where("jhid = ?", xfdjjh.getJhid()).find(Lyxcrwqy.class);
                    int yj = 0;
                    for (Lyxcrwqy rwqy : lyrwqys) {
                        if (rwqy.isChecked()) {
                            yj++;
                        }
                    }
                    XfDjjh djjh = new XfDjjh();
                    djjh.setJhid(xfdjjh.getJhid());
                    djjh.setJhmc(xfdjjh.getJhmc());
                    djjh.setXctypes(xfdjjh.getXctypes());
                    djjh.setNexttime(yj + "/" + lyrwqys.size());
                    list.add(djjh);
                }
            }
        }
    }

    private void setListData() {

        if (adapter == null) {
            adapter = new XfXzjhAdapter(context, list, new XfXzjhAdapter.CallBack() {
                @Override
                public void Click(View view) {
                    CheckBox checkBox = (CheckBox) view;
                    int index = (int) checkBox.getTag();
                    list.get(index).setChecked(checkBox.isChecked());
                    int count = 0;
                    for (int a = 0; a < list.size(); a++) {
                        if (list.get(a).isChecked()) {
                            count++;
                        }
                    }
                    cb.setChecked(count == list.size());
                    adapter.notifyDataSetChanged();
                }
            });
            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    list.get(i - 1).setChecked(!list.get(i - 1).isChecked());
                    int count = 0;
                    for (int a = 0; a < list.size(); a++) {
                        if (list.get(a).isChecked()) {
                            count++;
                        }
                    }
                    cb.setChecked(count == list.size());
                    adapter.notifyDataSetChanged();
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    //上传数据
    private void uploadData() {

        final Map<String, List<XfDjjhRwqy>> rwqys = new HashMap<>();
        final Map<String, List<XfXcmhqc>> mhqcs = new HashMap<>();
        final Map<String, List<XfXcxmjg>> xms = new HashMap<>();
        final Map<String, List<XfDjjh>> xfDjjhs = new HashMap<>();
        final Map<String, List<XfBaxcRwqy>> xfBaxcRwqys = new HashMap<>();

        final Map<String, List<XfDjjh>> badjjh = new HashMap<>();
        final Map<String, List<XfDjjh>> lydjjh = new HashMap<>();
        final Map<String, List<Lyxcrwqy>> lyxcrwqies = new HashMap<>();
        final Map<String, List<LyxcXmJg>> lyxcXms = new HashMap<>();

        int checkedCount = 0;
        //已检查的项目
        int select = 0;
        for (int i = 0; i < list.size(); i++) {
//            rwqys.clear();
//            mhqcs.clear();
//            xms.clear();
//            xfDjjhs.clear();
//            xfBaxcRwqys.clear();
//            badjjh.clear();
//            lydjjh.clear();
//            lyxcrwqies.clear();
//            lyxcXms.clear();

            if (list.get(i).isChecked()) {
                if (list.get(i).getXctypes().equals("1")) {
                    //消防需要上传的数据
                    rwqys.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfDjjhRwqy.class));
                    for (XfDjjhRwqy rwqy : rwqys.get(list.get(i).getJhid())) {
                        if (!rwqy.isChecked()) {
                            checkedCount++;
                            break;
                        }
                    }
                    mhqcs.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfXcmhqc.class));
                    xms.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfXcxmjg.class));
                    xfDjjhs.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfDjjh.class));
                    //uploadXfData(rwqys, mhqcs, xms, xfDjjhs, checkedCount);
                }
                if (list.get(i).getXctypes().equals("2")) {
                    //保安需要上传的数据
                    xfBaxcRwqys.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfBaxcRwqy.class));
                    badjjh.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfDjjh.class));

                    for (XfBaxcRwqy rwqy : xfBaxcRwqys.get(list.get(i).getJhid())) {
                        if (!rwqy.isChecked()) {
                            checkedCount++;
                            break;
                        }
                    }
                    //uploadBaData(xfBaxcRwqys, badjjh, checkedCount);
                }
                if (list.get(i).getXctypes().equals("3")) {
                    //楼宇需要上传的数据
                    lydjjh.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(XfDjjh.class));
                    lyxcrwqies.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(Lyxcrwqy.class));
                    lyxcXms.put(list.get(i).getJhid(), where("jhid = ?", list.get(i).getJhid()).find(LyxcXmJg.class));
                    for (Lyxcrwqy qy : lyxcrwqies.get(list.get(i).getJhid())) {
                        //有未检查项目，进行提示
                        if (!qy.isChecked()) {
                            checkedCount++;
                            break;
                        }
                        List<LyxcXm> xm = DataSupport.where("xmid = ?", qy.getXMID()).find(LyxcXm.class);
                        if (qy.getISZC().equals("0")) {
                            //如果不正常，生成隐患排查单
                            LyYhpcInfo yhpcInfo = new LyYhpcInfo();
                            yhpcInfo.setAREACODE(qy.getQYID());
                            yhpcInfo.setFXSJ(qy.getCJSJ());
                            yhpcInfo.setFXR((String) SPUtils.get(context, Contans.USERNAME, ""));
                            yhpcInfo.setWT(qy.getXMNAME() + xm.get(0).getTypename());
                            yhpcInfo.setNR(qy.getCJJG());
                        }
                    }
                    //uploadLyData(lyxcrwqies, lydjjh, lyxcXms, checkedCount);
                }
                removeList.add(list.get(i));
                select++;
            }
        }

        if (select != 0) {
            //已检查项目
            if (checkedCount == 0) {

                for (XfDjjh djjh : list) {

                    StringBuilder xf = new StringBuilder();
                    StringBuilder ba = new StringBuilder();
                    StringBuilder ly = new StringBuilder();

                    if (djjh.isChecked()) {
                        if (rwqys.size() != 0) {

                            List<XfDjjhRwqy> djjhRwqies = rwqys.get(djjh.getJhid());
                            if (djjhRwqies != null) {
                                String str1 = GsonUtils.GsonString(xms.get(djjh.getJhid()));
                                String str2 = GsonUtils.GsonString(mhqcs.get(djjh.getJhid()));
                                String str3 = GsonUtils.GsonString(rwqys.get(djjh.getJhid()));
                                String str4 = GsonUtils.GsonString(xfDjjhs.get(djjh.getJhid()));
                                xf.append("{\"Rows\":[");
                                xf.append("{\"表\":" + str1 + "},");
                                xf.append("{\"表\":" + str2 + "},");
                                xf.append("{\"表\":" + str3 + "},");
                                xf.append("{\"表\":" + str4);
                                xf.append("}],\"total\":4}");
                            }
                        }

                        //保安的JSON数据
                        if (xfBaxcRwqys.size() != 0) {

                            List<XfBaxcRwqy> xfBaxcRwqies = xfBaxcRwqys.get(djjh.getJhid());
                            if (xfBaxcRwqies != null) {
                                String str1 = GsonUtils.GsonString(xfBaxcRwqys.get(djjh.getJhid()));
                                String str2 = GsonUtils.GsonString(badjjh.get(djjh.getJhid()));
                                ba.append("{\"Rows\":[");
                                ba.append("{\"表\":" + str1 + "},");
                                ba.append("{\"表\":" + str2);
                                ba.append("}],\"total\":2}");
                            }
                        }

                        if (lyxcrwqies.size() != 0) {

                            List<Lyxcrwqy> lyxcrwqies1 = lyxcrwqies.get(djjh.getJhid());
                            if (lyxcrwqies1 != null) {
                                String str1 = GsonUtils.GsonString(lyxcrwqies.get(djjh.getJhid()));
                                String str2 = GsonUtils.GsonString(lyxcXms.get(djjh.getJhid()));
                                String str3 = GsonUtils.GsonString(lydjjh.get(djjh.getJhid()));
                                ly.append("{\"Rows\":[");
                                ly.append("{\"表\":" + str2 + "},");
                                ly.append("{\"表\":" + str1 + "},");
                                ly.append("{\"表\":" + str3);
                                ly.append("}],\"total\":3}");
                            }
                        }

                        String bajson = ba.toString();
                        String xfjson = xf.toString();
                        String lyjson = ly.toString();
                        bajson = new String(ba.toString().getBytes(), StandardCharsets.UTF_8);
                        xfjson = new String(xf.toString().getBytes(), StandardCharsets.UTF_8);
                        lyjson = new String(ly.toString().getBytes(), StandardCharsets.UTF_8);
                        Logger.v(bajson);
                        sCData(xfjson, bajson, lyjson);
                    }
                }

            } else {

                //showToast("你还有未检查的项目");
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setTitle("你还有项目未检查，是否上传？");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        for (XfDjjh djjh : list) {
                            if (djjh.isChecked()) {

                                StringBuilder xf = new StringBuilder();
                                StringBuilder ba = new StringBuilder();
                                StringBuilder ly = new StringBuilder();

                                if (rwqys.size() != 0) {
                                    List<XfDjjhRwqy> djjhRwqies = rwqys.get(djjh.getJhid());
                                    if (djjhRwqies != null) {
                                        for (XfDjjhRwqy rwqy : djjhRwqies) {
                                            if (!rwqy.isChecked()) {
                                                rwqy.setCJSJ("");
                                                rwqy.setCJR((String) SPUtils.get(context, Contans.NAME, ""));
                                                rwqy.setSMFX("");
                                                rwqy.setCJJG("");
                                                rwqy.setIszc("0");
                                            }
                                        }
                                        String str1 = GsonUtils.GsonString(xms.get(djjh.getJhid()));
                                        String str2 = GsonUtils.GsonString(mhqcs.get(djjh.getJhid()));
                                        String str3 = GsonUtils.GsonString(rwqys.get(djjh.getJhid()));
                                        String str4 = GsonUtils.GsonString(xfDjjhs.get(djjh.getJhid()));
                                        xf.append("{\"Rows\":[");
                                        xf.append("{\"表\":" + str1 + "},");
                                        xf.append("{\"表\":" + str2 + "},");
                                        xf.append("{\"表\":" + str3 + "},");
                                        xf.append("{\"表\":" + str4);
                                        xf.append("}],\"total\":4}");
                                    }
                                }

                                //保安的JSON数据
                                if (xfBaxcRwqys.size() != 0) {
                                    List<XfBaxcRwqy> xfBaxcRwqies = xfBaxcRwqys.get(djjh.getJhid());
                                    if (xfBaxcRwqies != null) {
                                        for (XfBaxcRwqy qys : xfBaxcRwqies) {
                                            if (!qys.isChecked()) {
                                                qys.setCjr((String) SPUtils.get(context, Contans.NAME, ""));
                                                qys.setCjsj("");
                                            }
                                        }
                                        String str1 = GsonUtils.GsonString(xfBaxcRwqys.get(djjh.getJhid()));
                                        String str2 = GsonUtils.GsonString(badjjh.get(djjh.getJhid()));
                                        ba.append("{\"Rows\":[");
                                        ba.append("{\"表\":" + str1 + "},");
                                        ba.append("{\"表\":" + str2);
                                        ba.append("}],\"total\":2}");
                                    }
                                }

                                if (lyxcrwqies.size() != 0) {
                                    List<Lyxcrwqy> lyxcrwqies1 = lyxcrwqies.get(djjh.getJhid());
                                    if (lyxcrwqies1 != null) {
                                        for (Lyxcrwqy lyxcrwqy : lyxcrwqies1) {
                                            if (!lyxcrwqy.isChecked()) {
                                                lyxcrwqy.setCJR((String) SPUtils.get(context, Contans.NAME, ""));
                                                lyxcrwqy.setCJSJ("");
                                                lyxcrwqy.setISZC("0");
                                                lyxcrwqy.setSMFX("");
                                                lyxcrwqy.setCJSJ("");
                                                lyxcrwqy.setCJJG("");
                                            }
                                        }

                                        if (lyxcXms.size() == 0) {
                                            LyxcXmJg jg = new LyxcXmJg();
                                            jg.setXmid("");
                                            jg.setTypename("");
                                            jg.setType1("");
                                            jg.setType2("");
                                            jg.setJg(false);
                                            jg.setJhid("");
                                            jg.setQyid("");
                                            jg.setSsid("");
                                            lyxcXms.get(djjh.getJhid()).add(jg);
                                        }

                                        String str1 = GsonUtils.GsonString(lyxcrwqies.get(djjh.getJhid()));
                                        String str2 = GsonUtils.GsonString(lyxcXms.get(djjh.getJhid()));
                                        String str3 = GsonUtils.GsonString(lydjjh.get(djjh.getJhid()));
                                        ly.append("{\"Rows\":[");
                                        ly.append("{\"表\":" + str2 + "},");
                                        ly.append("{\"表\":" + str1 + "},");
                                        ly.append("{\"表\":" + str3);
                                        ly.append("}],\"total\":3}");
                                    }
                                }

                                String bajson = ba.toString();
                                String xfjson = xf.toString();
                                String lyjson = ly.toString();
                                bajson = new String(ba.toString().getBytes(), StandardCharsets.UTF_8);
                                xfjson = new String(xf.toString().getBytes(), StandardCharsets.UTF_8);
                                lyjson = new String(ly.toString().getBytes(), StandardCharsets.UTF_8);
                                sCData(xfjson, bajson, lyjson);
                                //removeData();
                            }
                        }
                    }
                });
                builder.show();
            }
        } else {
            showToast("没有可上传计划");
        }
    }

    private void sCData(String xf, String ba, String ly) {
        dialog.show();

        String tokenTemp = (String) SPUtils.get(mActivity, Contans.LOGIN_TOKEN, "");
        if (!xf.isEmpty()) {
            requestCount++;
            Logger.v(xf);
            Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.XFDJJHSC + "?cjscr=" + UiUtlis.encoder((String) SPUtils.get(context, Contans.NAME, "测试")), RequestMethod.POST);
            request.setDefineRequestBodyForJson(UiUtlis.encoder(xf));
            if (!TextUtils.isEmpty(tokenTemp)) {
                String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                String credential = "Bearer " + token;
                request.addHeader("Authorization", credential);
                showLog(request.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
            }
            queue.add(0, request, new HttpResponseListener<>(getActivity(), request, callback, false, false, "加载中..."));
            //NohttpUtils.getInstance().add(getActivity(), 0, request, callback, false, false, "上传中...");
            List<XfXcjsInfo> xcjss = DataSupport.findAll(XfXcjsInfo.class);
            for (XfXcjsInfo info : xcjss) {
                File file = new File(info.getPath());
                if (file.isFile()) {

                    requestCount++;
                    Request<String> xcjs = NoHttp.createStringRequest(Contans.IP + Contans.XFXCJSSC + "?ms=" + UiUtlis.encoder(info.getMs()) +
                            "&jhid=" + UiUtlis.encoder(info.getJhid()) + "&filename=" + UiUtlis.encoder(info.getFilename()) +
                            "&djr=" + UiUtlis.encoder(info.getDjr()) + "&xfid=" + UiUtlis.encoder(info.getXfid()), RequestMethod.POST);

                    xcjs.add(Contans.FILEPS, new FileBinary(new File(info.getPath())));
                    if (!TextUtils.isEmpty(tokenTemp)) {
                        String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                        String credential = "Bearer " + token;
                        xcjs.addHeader("Authorization", credential);
                        showLog(xcjs.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
                    }
                    queue.add(1, xcjs, new HttpResponseListener<>(getActivity(), xcjs, callback, false, false, "加载中..."));
                }
                //NohttpUtils.getInstance().add(getActivity(), 1, xcjs, callback, false, false, "上传中...");
            }
        }
        if (!ba.isEmpty()) {
            requestCount++;
            Logger.v(ba);
            Request<String> baxc = NoHttp.createStringRequest(Contans.IP + Contans.BAXCJHSC + "?cjscr=" + UiUtlis.encoder((String) SPUtils.get(context, Contans.NAME, "测试")), RequestMethod.POST);
            baxc.setDefineRequestBodyForJson(UiUtlis.encoder(ba));
            if (!TextUtils.isEmpty(tokenTemp)) {
                String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                String credential = "Bearer " + token;
                baxc.addHeader("Authorization", credential);
                showLog(baxc.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
            }
            queue.add(2, baxc, new HttpResponseListener<>(getActivity(), baxc, callback, false, false, "加载中..."));
            //NohttpUtils.getInstance().add(getActivity(), 2, baxc, callback, false, false, "上传中...");

        }
        if (!ly.isEmpty()) {
            Logger.v(ly);
//            //上传隐患排查单
            List<LyYhpcInfo> info = DataSupport.findAll(LyYhpcInfo.class);
            if (info.size() != 0) {
                requestCount++;
                Request<String> yhpc = NoHttp.createStringRequest(Contans.IP + Contans.YHPC + "?cjscr=" + UiUtlis.encoder((String) SPUtils.get(context, Contans.NAME, "测试")), RequestMethod.POST);
                String json1 = "{\"Rows\":" + GsonUtils.GsonString(info) + ",\"Total\": " + info.size() + "}";
                yhpc.setDefineRequestBodyForJson(json1);
                if (!TextUtils.isEmpty(tokenTemp)) {
                    String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                    String credential = "Bearer " + token;
                    yhpc.addHeader("Authorization", credential);
                    showLog(yhpc.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
                }
                queue.add(3, yhpc, new HttpResponseListener<>(getActivity(), yhpc, callback, false, false, "加载中..."));
                //NohttpUtils.getInstance().add(getActivity(), 3, yhpc, callback, false, false, "");
            }

            requestCount++;
            Request<String> lyxc = NoHttp.createStringRequest(Contans.IP + Contans.LYXCSC + "?cjscr=" + UiUtlis.encoder((String) SPUtils.get(context, Contans.NAME, "测试")), RequestMethod.POST);
            lyxc.setDefineRequestBodyForJson(UiUtlis.encoder(ly));
            if (!TextUtils.isEmpty(tokenTemp)) {
                String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                String credential = "Bearer " + token;
                lyxc.addHeader("Authorization", credential);
                showLog(lyxc.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
            }
            queue.add(4, lyxc, new HttpResponseListener<>(getActivity(), lyxc, callback, false, false, "加载中..."));
            //NohttpUtils.getInstance().add(getActivity(), 4, lyxc, callback, false, false, "上传中...");

            //楼宇现场记事
            List<LyXcjsInfo> xcjss = DataSupport.findAll(LyXcjsInfo.class);
            for (LyXcjsInfo lyinfo : xcjss) {
                requestCount++;
                Request<String> xcjs = NoHttp.createStringRequest(Contans.IP + Contans.LYXCJSSC + "?ms=" + UiUtlis.encoder(lyinfo.getMs()) +
                        "&jhid=" + UiUtlis.encoder(lyinfo.getJhid()) + "&filename=" + UiUtlis.encoder(lyinfo.getFilename()) +
                        "&djr=" + UiUtlis.encoder(lyinfo.getDjr()) + "&xfid=" + UiUtlis.encoder(lyinfo.getXfid()), RequestMethod.POST);
                xcjs.add(Contans.FILEPS, new FileBinary(new File(lyinfo.getPath())));
                if (!TextUtils.isEmpty(tokenTemp)) {
                    String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
                    String credential = "Bearer " + token;
                    xcjs.addHeader("Authorization", credential);
                    showLog(xcjs.url());
//            if(!TextUtils.isEmpty(token)){
//                showLog(credential);
//                showLog(new Gson().toJson(request.headers()));
//            }
                }
                queue.add(5, xcjs, new HttpResponseListener<>(getActivity(), xcjs, callback, false, false, "加载中..."));
                //NohttpUtils.getInstance().add(getActivity(), 5, xcjs, callback, false, false, "上传中...");
            }
        }
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {
            showLog("消防");
            String result = response.get();
            showLog(result);
            StatusInfo info = GsonUtils.GsonToBean(response.get(), StatusInfo.class);
            if (info != null && info.getTotal() != 0) {
                try {
                    switch (what) {
                        case 0:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("点出错");
                            }
                            break;
                        case 1:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("上传图片错误");
                            }
                            break;

                        case 2:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("上传数据出错");
                            }
                            break;
                        case 3:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("上传数据出错");
                            }
                            break;
                        case 4:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("上传数据出错");
                            }
                            break;
                        case 5:
                            if (info.getRows().get(0).getStatus().equals("1")) {
                                ++successCount;
                            } else {
                                requestCount = 0;
                                successCount = 0;
                                showToast("上传楼宇图片错误");
                            }
                            break;

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    requestCount = 0;
                    successCount = 0;
                    showToast(UiUtlis.getString(context, R.string.data_error));
                }
            } else {
                requestCount = 0;
                successCount = 0;
                showToast(UiUtlis.getString(context, R.string.data_error));
            }
            if (requestCount == successCount) {
                requestCount = 0;
                successCount = 0;
                dialog.dismiss();
                showToast("上传成功");
                //删除本地数据
                removeData(removeList);
            } else {
                dialog.dismiss();
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                requestCount = 0;
                successCount = 0;
            }
        }
    };

    //上传成功，移除数据
    private void removeData(List<XfDjjh> dels) {
        for (XfDjjh djjh : dels) {

            List<XfXcjsInfo> infos = DataSupport.where("jhid = ?", djjh.getJhid()).find(XfXcjsInfo.class);
            for (XfXcjsInfo info : infos) {
                File file = new File(info.getPath());
                if (file.isFile()) {
                    file.delete();
                }
            }

            List<LyXcjsInfo> lyjs = DataSupport.where("jhid = ?", djjh.getJhid()).find(LyXcjsInfo.class);
            for (LyXcjsInfo info : lyjs) {
                File file = new File(info.getPath());
                if (file.isFile()) {
                    file.delete();
                }
            }
            DataSupport.deleteAll(XfDjjh.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(XfDjjhRwqy.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(XfXcjsInfo.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(XfXcxmjg.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(XfXcmhqc.class, "jhid = ?", djjh.getJhid());

            DataSupport.deleteAll(XfBaxcRwqy.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(XfXcxm.class, "jhid = ?", djjh.getJhid());

            DataSupport.deleteAll(Lyxcrwqy.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(LyxcXm.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(LyXcjsInfo.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(LyxcXmJg.class, "jhid = ?", djjh.getJhid());
            DataSupport.deleteAll(LyYhpcInfo.class);
        }
        getDataInSQL();
        cb.setChecked(false);
        adapter.notifyDataSetChanged();
    }
}