package com.bjzc.zjyxdxj.activity.sbxj;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.bjzc.zjyxdxj.DBModel.UploadLocation;
import com.bjzc.zjyxdxj.DBModel.XSJJHDataBean;
import com.bjzc.zjyxdxj.DBModel.XSJJHXZDataBean;
import com.bjzc.zjyxdxj.R;
import com.bjzc.zjyxdxj.adapter.CommonAdapter;
import com.bjzc.zjyxdxj.adapter.ViewHolder;
import com.bjzc.zjyxdxj.base.BaseActivity3;
import com.bjzc.zjyxdxj.base.MipcaActivityCapture;
import com.bjzc.zjyxdxj.bean.PointCheckedPersion;
import com.bjzc.zjyxdxj.bean.XsRequestInfo;
import com.bjzc.zjyxdxj.contans.Contans;
import com.bjzc.zjyxdxj.utils.GsonUtils;
import com.bjzc.zjyxdxj.utils.HttpListener;
import com.bjzc.zjyxdxj.utils.NoProgresshttpUtils;
import com.bjzc.zjyxdxj.utils.RSAUtils;
import com.bjzc.zjyxdxj.utils.SPUtils;
import com.bjzc.zjyxdxj.weight.BaCheckDialog;
import com.bjzc.zjyxdxj.weight.toastviewbymyself;
import com.google.gson.Gson;
import com.yolanda.nohttp.Connection;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.bjzc.zjyxdxj.utils.GsonUtils.GsonToBean;
import static org.litepal.crud.DataSupport.where;


public class SxgzActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    TextView total_point;
    TextView total_no_check;
    TextView total_checked;
    LinearLayout ll_nocheck;
    LinearLayout ll_check;

    private View headView;
    private TextView tv_djrw;
    private static final int CAMERA_REQUEST_CODE = 3;//请求码
    private CommonAdapter<XSJJHXZDataBean> adapter;
    private List<XSJJHXZDataBean> xsjjhxzDataBeanList = new ArrayList<>();
    private List<XSJJHXZDataBean> checkedList = new ArrayList<>();
    private List<XSJJHXZDataBean> noCheckeList = new ArrayList<>();
    private List<XSJJHXZDataBean> checkedListBA = new ArrayList<>();
    private List<XSJJHXZDataBean> noCheckeListBA = new ArrayList<>();
    private List<String> qybh = new ArrayList<>();
    private ArrayList<XSJJHDataBean> xsjjhDataBeanArrayList = new ArrayList<>();//点检记录列表
    String nfcValue = null;
    String lastNfcbm = null;
    private String type = null;
    private String username;
    BaCheckDialog auditDialog = null;

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Contans.ACTION_DELETE_REFRESHPLANDOWNLOAD.equals(action)) {
                //刷新页面
                getDataInSqlite();
                adapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_xsgz;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (CAMERA_REQUEST_CODE == requestCode) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //扫码
                goScan();
            } else {
                //权限拒绝
                Toast.makeText(this, "你拒绝了权限申请，无法打开相机扫码哟！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void goScan() {
        Intent intent = new Intent(SxgzActivity.this, MipcaActivityCapture.class);
        startActivityForResult(intent, 1);
    }

    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        total_point = findViewById(R.id.total_point);
        total_no_check = findViewById(R.id.total_no_check);
        total_checked = findViewById(R.id.total_checked);
        ll_nocheck = findViewById(R.id.ll_nocheck);
        ll_check = findViewById(R.id.ll_check);

        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        String UserIDTemp = (String) SPUtils.get(context, Contans.USERID, "");
        username = RSAUtils.decryptBASE64StrLocal(UserIDTemp);

        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        tv_djrw = headView.findViewById(R.id.tv_djrw);
        if (type != null && type.equals("BA")) {
            tv_djrw.setText("已检/未检");
        }

        IntentFilter filter = new IntentFilter();
        filter.addAction(Contans.ACTION_DELETE_REFRESHPLANDOWNLOAD);
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);

        auditDialog = new BaCheckDialog(context, "", new BaCheckDialog.AuditDialogListener() {
            @Override
            public void confirm(String notice) {

            }
        });
        //NFC初始化
        initNFC();

//        lv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                nfcValue = null;
//            }
//        });
        ll_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(SxgzActivity.this, CheckedListActivity.class);
//                bundle.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
//                bundle.putBoolean("edit", false);
//                bundle.putInt(Contans.KEY_ITEM, 0);
//                bundle.putString("LX", "Click");
//                bundle.putString("LXResult", "LXResult");
//                bundle.putString("qybh", xsjjhxzDataBeanList.get(postion - 1).getQybh());
                intent.putExtra("type", type);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        ll_nocheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(SxgzActivity.this, NoCheckedListActivity.class);
//                bundle.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
//                bundle.putBoolean("edit", false);
//                bundle.putInt(Contans.KEY_ITEM, 0);
//                bundle.putString("LX", "Click");
//                bundle.putString("LXResult", "LXResult");
//                bundle.putString("qybh", xsjjhxzDataBeanList.get(postion - 1).getQybh());
                intent.putExtra("type", type);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        initToolbar("巡检工作(请使用NFC)", "扫码", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //动态权限申请
                if (ContextCompat.checkSelfPermission(SxgzActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(SxgzActivity.this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
                } else {
                    //扫码
                    goScan();
                }
            }
        });
    }

    public void initData() {
        getDataInSqlite();
        setListData();
    }

    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        handleNfcCheck(result);
        //handleNfcCheck("042734F2257381");
    }

    //加载数据库中已经下载的计划
    private void getDataInSqlite() {
        xsjjhxzDataBeanList.clear();
        checkedListBA.clear();
        noCheckeListBA.clear();
        checkedList.clear();
        noCheckeList.clear();
        List<XSJJHXZDataBean> planTemp = DataSupport.where("mk = ? and username = ?", type, username).find(XSJJHXZDataBean.class);

        if (planTemp != null && planTemp.size() > 0) {
            Collections.reverse(planTemp);
        }
        //showLog(new Gson().toJson(planTemp));
        //showLog(String.valueOf(planTemp.size()));
        //DataSupport.findAll(XSJJHXZDataBean.class);
        List<XSJJHXZDataBean> planTemp1 = this.removeDuplicate(planTemp);
        if (planTemp1 != null && planTemp1.size() > 0) {
            Collections.reverse(planTemp1);
        }

//        for(XSJJHXZDataBean item :planTemp){
//            showLog(new Gson().toJson(item));
//        }

        //List<XSJJHXZDataBean> planTemp1 = planTemp;

        xsjjhxzDataBeanList.addAll(planTemp1);
        //Log.i("app", String.valueOf(xsjjhxzDataBeanList.size()));
        for (int i = 0; i < xsjjhxzDataBeanList.size(); i++) {
            XSJJHXZDataBean item = xsjjhxzDataBeanList.get(i);
            if (type != null && type.equals("BA")) {
                if (item.isBAChecked()) {
                    checkedListBA.add(item);
                } else {
                    noCheckeListBA.add(item);
                }
            } else {
                int checkedCount = 0;
                List<XSJJHDataBean> xsjjhdatabean = DataSupport.where("qybh = ? and mk = ? and username = ?", item.getQybh(), type, username).find(XSJJHDataBean.class);
                for (int j = 0; j < xsjjhdatabean.size(); j++) {
                    if (xsjjhdatabean.get(j).isChecked()) {
                        checkedCount++;
                    }
                }
                int a = checkedCount;
                int b = xsjjhdatabean.size();
                if (a == b) {
                    checkedList.add(item);
                } else {
                    noCheckeList.add(item);
                }
            }
        }
        total_point.setText(String.valueOf(xsjjhxzDataBeanList.size()));
        if (type != null && type.equals("BA")) {
            total_no_check.setText(String.valueOf(noCheckeListBA.size()));
            total_checked.setText(String.valueOf(checkedListBA.size()));
        } else {
            total_no_check.setText(String.valueOf(noCheckeList.size()));
            total_checked.setText(String.valueOf(checkedList.size()));
        }
    }

    public List<XSJJHXZDataBean> removeDuplicate(List<XSJJHXZDataBean> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getQybh().equals(list.get(i).getQybh())) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    private void setListData() {

        if (adapter == null) {
            adapter = new CommonAdapter<XSJJHXZDataBean>(context, R.layout.xsgz_item, xsjjhxzDataBeanList) {
                @Override
                protected void convert(ViewHolder viewHolder, XSJJHXZDataBean item, int position) {
                    if (type != null && type.equals("BA")) {
                        viewHolder.setText(R.id.tv_xh, String.valueOf(position + 1));
                        viewHolder.setText(R.id.tv_qymc, item.getQymc());
                        if (item.isBAChecked()) {
                            viewHolder.setText(R.id.tv_djrw, "已检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                        } else {
                            viewHolder.setText(R.id.tv_djrw, "未检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#FF0000"));//未检查完成红色
                        }
                    } else {
                        int checkedCount = 0;
                        List<XSJJHDataBean> xsjjhdatabean = DataSupport.where("qybh = ? and mk = ? and username = ?", item.getQybh(), type, username).find(XSJJHDataBean.class);
                        for (int i = 0; i < xsjjhdatabean.size(); i++) {
                            if (xsjjhdatabean.get(i).isChecked()) {
                                checkedCount++;
                            }
                        }
                        viewHolder.setText(R.id.tv_xh, String.valueOf(position + 1));
                        viewHolder.setText(R.id.tv_qymc, item.getQymc());
                        viewHolder.setText(R.id.tv_djrw, checkedCount + "/" + xsjjhdatabean.size());
                        int a = checkedCount;
                        int b = xsjjhdatabean.size();
                        if (a == b) {
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                        } else {
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#FF0000"));//未检查完成红色
                        }
                    }
                }
            };


            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener((adapterView, view, postion, l) -> {
                if (type != null && type.equals("BA")) {
                    //showToast("请使用NFC进行巡检");
                    //showToast(String.valueOf(postion));
                    //保安巡检
                    XSJJHXZDataBean itemQY = xsjjhxzDataBeanList.get(postion - 1);
                    if (itemQY != null) {
                        String content = null;
                        String notice = null;
                        content = itemQY.getXjnr();
                        notice = itemQY.getFxnr();
                        if (content == null) {
                            content = "";
                        }
                        if (notice == null) {
                            notice = "";
                        }

                        BaCheckDialog auditDialog = new BaCheckDialog(context, content, new BaCheckDialog.AuditDialogListener() {
                            @Override
                            public void confirm(String notice) {

                            }
                        });
                        //auditDialog.hideNotice();
                        auditDialog.setTvTitle("巡检内容");
                        auditDialog.setNotice(notice);
                        auditDialog.hideNotice(notice);
                        auditDialog.setCancelable(true);
                        auditDialog.hideBottomClick();
                        auditDialog.show();
                    }


                } else {
                    showToast("请使用NFC进行巡检");
                    nfcValue = "click";
                    xsjjhDataBeanArrayList.clear();
                    //List<XSJJHDataBean> xsjjhdatabean = DataSupport.where("qybh = ?",xsjjhxzDataBeanList.get(postion - 1).getQybh()).find( XSJJHDataBean.class);
                    xsjjhDataBeanArrayList.addAll(where("qybh = ? and mk = ? and username = ?", xsjjhxzDataBeanList.get(postion - 1).getQybh(), type, username).find(XSJJHDataBean.class));
                    Log.i("app", String.valueOf(xsjjhDataBeanArrayList.size()));
                    Log.i("app", String.valueOf(postion));
                    if (xsjjhDataBeanArrayList.size() != 0) {
                        Bundle bundle = new Bundle();
                        Intent intent = new Intent(SxgzActivity.this, XjYulActivity.class);
                        bundle.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
                        bundle.putBoolean("edit", false);
//                    bundle.putBoolean("edit", true);
//                    if (xsjjhDataBeanArrayList.get(0).getSecond() != null) {
//                        if (xsjjhDataBeanArrayList.get(0).getSecond().equals("YES")) {//如果曾经扫描过二维码或者NFC进入到区域 则下次可以直接点击进入编辑
//                            bundle.putBoolean("edit", true);
//                        } else {
//                            bundle.putBoolean("edit", false);
//                        }
//                    }
                        bundle.putInt(Contans.KEY_ITEM, 0);
                        bundle.putInt("itemposition", postion - 1);
                        bundle.putString("LX", "Click");
                        bundle.putString("LXResult", "LXResult");
                        bundle.putString("qybh", xsjjhxzDataBeanList.get(postion - 1).getQybh());
                        intent.putExtra("type", type);
                        bundle.putInt("from", 0);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }


    //处理NFC的数据
    public void handleNfcCheck(String result) {
        if (type != null && type.equals("BA")) {
            //保安巡检
            List<XSJJHXZDataBean> planNFC = where("nfcbm = ? and mk = ? and username = ?", result, type, username).find(XSJJHXZDataBean.class);
            if (planNFC != null && planNFC.size() > 0) {
                XSJJHXZDataBean itemQY = planNFC.get(0);
                String content = null;
                String notice = null;
                content = itemQY.getXjnr();
                notice = itemQY.getFxnr();
                if (content == null) {
                    content = "";
                }
                if (notice == null) {
                    notice = "";
                }
                ContentValues values = new ContentValues();
                values.put("isBAChecked", true);
                values.put("smfs", "N");
//                if(notice!=null){
//                    values.put("fxnr", notice);
//                }
                values.put("djsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                values.put("DATE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                DataSupport.updateAll(XSJJHXZDataBean.class, values, "nfcbm = ?  and mk = ? and username = ?", result, type, username);
                initData();
                //toastviewbymyself.makeText(SxgzActivity.this, "有数据", Toast.LENGTH_SHORT).show();
                auditDialog.setTvMsg(content);
                auditDialog.setNotice(notice);
                auditDialog.setTvTitle("巡检内容");
                auditDialog.setCancelable(true);
                auditDialog.show();
            } else {
                nfcValue = null;
                //getLastUploadUser(result);
                toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
            }
        } else {
            //运行，燃料巡检
            List<XSJJHDataBean> xsjjhDataBeen = DataSupport.where("nfcbm = ? and mk = ? and username = ?", result, type, username).find(XSJJHDataBean.class);
            xsjjhDataBeanArrayList.clear();
            xsjjhDataBeanArrayList.addAll(xsjjhDataBeen);
            if (xsjjhDataBeen.size() == 0) {
                nfcValue = null;
                //getLastUploadUser(result);
                toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
            } else {
                Bundle bundle3 = new Bundle();
                Intent intent = new Intent(SxgzActivity.this, XjYulActivity.class);
                bundle3.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
                bundle3.putBoolean("edit", true);
                bundle3.putInt(Contans.KEY_ITEM, 0);
                bundle3.putInt("itemposition", 0);
                bundle3.putString("LX", "NFC");
                bundle3.putString("LXResult", result);
                intent.putExtra("type", type);
                intent.putExtra("smfs", "N");
                bundle3.putInt("from", 0);
                intent.putExtras(bundle3);
//            ContentValues values = new ContentValues();
//            values.put("second", "YES");
                startActivity(intent);
            }
        }
        //上传当前位置
//        if(lastNfcbm==null){
//            Log.i("app","==null");
//            this.lastNfcbm=result;
//            String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//            List<UploadLocation> uploadLocations = where("nfcbm = ? and username = ?", result, username).find(UploadLocation.class);
//            List<UploadLocation> currentLocations = new ArrayList<>();
//            if(uploadLocations.size()>0){
////                Log.i("app","----------------");
////                Log.i("app",String.valueOf(uploadLocations.size()));
//                currentLocations.clear();
//                DataSupport.deleteAll(UploadLocation.class, "nfcbm = ? and username = ?",result,username);
//                UploadLocation uploadLocatio = new UploadLocation();
//                uploadLocatio.setDjsj(now);
//                uploadLocatio.setNfcbm(result);
//                uploadLocatio.setUsername(username);
//                currentLocations.add(uploadLocatio);
//                DataSupport.saveAll(currentLocations);
//            }else{
//                currentLocations.clear();
//                UploadLocation uploadLocatio = new UploadLocation();
//                uploadLocatio.setDjsj(now);
//                uploadLocatio.setNfcbm(result);
//                uploadLocatio.setUsername(username);
//                currentLocations.add(uploadLocatio);
//                DataSupport.saveAll(currentLocations);
//            }
//            //上传数据
//            for(UploadLocation item : currentLocations){
//                uploadLocationByNFC(item);
//            }
//        }else{
//            Log.i("app","!=null");
//            if(!lastNfcbm.equals(result)){
//                Log.i("app","lastNfcbm.equals(result)");
//                String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                List<UploadLocation> uploadLocations = where("nfcbm = ? and username = ?", result, username).find(UploadLocation.class);
//                List<UploadLocation> currentLocations = new ArrayList<>();
//                if(uploadLocations.size()>0){
////                    Log.i("app","----------------");
////                    Log.i("app",String.valueOf(uploadLocations.size()));
//                    currentLocations.clear();
//                    DataSupport.deleteAll(UploadLocation.class, "nfcbm = ? and username = ?",result,username);
//                    UploadLocation uploadLocatio = new UploadLocation();
//                    uploadLocatio.setDjsj(now);
//                    uploadLocatio.setNfcbm(result);
//                    uploadLocatio.setUsername(username);
//                    currentLocations.add(uploadLocatio);
//                    DataSupport.saveAll(currentLocations);
//                }else{
//                    currentLocations.clear();
//                    UploadLocation uploadLocatio = new UploadLocation();
//                    uploadLocatio.setDjsj(now);
//                    uploadLocatio.setNfcbm(result);
//                    uploadLocatio.setUsername(username);
//                    currentLocations.add(uploadLocatio);
//                    DataSupport.saveAll(currentLocations);
//                }
//                //上传数据
//                for(UploadLocation item : currentLocations){
//                    uploadLocationByNFC(item);
//                }
//            }
//        }

        //保安巡检
//        List<UploadLocation> uploadLocations = where("nfcbm = ? and username = ?", result, username).find(UploadLocation.class);
//        Log.i("app","----------------");
//        Log.i("app",String.valueOf(uploadLocations.size()));
    }


    //处理二维码的数据
    public void handleTwoCodeCheck(String result) {
        if (type != null && type.equals("BA")) {
            //保安巡检
            List<XSJJHXZDataBean> planNFC = where("txm = ? and txm_zt = 1 and mk = ? and username = ?", result, type, username).find(XSJJHXZDataBean.class);
            if (planNFC != null && planNFC.size() > 0) {
                XSJJHXZDataBean itemQY = planNFC.get(0);
                String content = null;
                String notice = null;
                content = itemQY.getXjnr();
                notice = itemQY.getFxnr();
                if (content == null) {
                    content = "";
                }
                if (notice == null) {
                    notice = "";
                }
                ContentValues values = new ContentValues();
                values.put("isBAChecked", true);
                values.put("smfs", "E");
//                if(notice!=null){
//                    values.put("fxnr", notice);
//                }
                values.put("DATE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                values.put("djsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                DataSupport.updateAll(XSJJHXZDataBean.class, values, "txm = ? and txm_zt = 1 and mk = ? and username = ?", result, type, username);
                initData();

                BaCheckDialog auditDialog = new BaCheckDialog(context, content, new BaCheckDialog.AuditDialogListener() {
                    @Override
                    public void confirm(String notice) {

                    }
                });
                auditDialog.setNotice(notice);
                auditDialog.setTvTitle("巡检内容");
                auditDialog.setCancelable(true);
                auditDialog.show();
            } else {
                nfcValue = null;
                toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
            }
        } else {
            List<XSJJHXZDataBean> scranTwoCodeList = where("txm = ? and txm_zt = 1 and mk = ? and username = ?", result, type, username).find(XSJJHXZDataBean.class);
            if (scranTwoCodeList != null && scranTwoCodeList.size() <= 0) {
                showToast("无数据请联系管理员");
                return;
            }
            //运行，燃料巡检
            List<XSJJHDataBean> xsjjhDataBeen = DataSupport.where("qybh = ? and mk = ? and username = ?", result, type, username).find(XSJJHDataBean.class);
            xsjjhDataBeanArrayList.clear();
            xsjjhDataBeanArrayList.addAll(xsjjhDataBeen);
            if (xsjjhDataBeen.size() == 0) {
                nfcValue = null;
                toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
            } else {
                Bundle bundle3 = new Bundle();
                Intent intent = new Intent(SxgzActivity.this, XjYulActivity.class);
                bundle3.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
                bundle3.putBoolean("edit", true);
                bundle3.putInt(Contans.KEY_ITEM, 0);
                bundle3.putInt("itemposition", 0);
                bundle3.putString("LX", "EWM");
                bundle3.putString("LXResult", result);
                intent.putExtra("type", type);
                intent.putExtra("smfs", "E");
                bundle3.putInt("from", 0);
                intent.putExtras(bundle3);
//            ContentValues values = new ContentValues();
//            values.put("second", "YES");
                startActivity(intent);
            }
        }
    }

    private void uploadLocationByNFC(UploadLocation uploadLocatio) {
        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.XJ_UPLOAD_LOCATION_CHECKED, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJsonByUploadLocation(uploadLocatio));
        NoProgresshttpUtils.getInstance().add(this, 1, requestxs, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

//                Log.i("app","----------------");
//                Log.i("app",response.get());
                PointCheckedPersion bean = GsonToBean(response.get(), PointCheckedPersion.class);
                if (bean.getState() == 1) {
                    DataSupport.deleteAll(UploadLocation.class, "nfcbm = ? and username = ?", uploadLocatio.getNfcbm(), uploadLocatio.getUsername());
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    private String createJsonByUploadLocation(UploadLocation uploadLocatio) {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XJ_LOCATION_CHECKED");
        info.setNfcbm(uploadLocatio.getNfcbm());
        info.setDjsj(uploadLocatio.getDjsj());
        info.setUsername(uploadLocatio.getUsername());
        String json = GsonUtils.GsonString(info);
        //Log.i("app",json);
        return json;
    }

    private void getLastUploadUser(String nfcbm) {

        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.LAST_UPLOAD_POINT_CHECKED, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJsonByLastUploadUser(nfcbm));
        NoProgresshttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

//                Log.i("app","----------------");
//                Log.i("app",response.get());
                PointCheckedPersion bean = GsonToBean(response.get(), PointCheckedPersion.class);
                if (bean.getState() == 1) {
                    if (bean.getData().size() > 0) {
                        String msg = "";
                        PointCheckedPersion.DataBean item = bean.getData().get(0);
                        if (item.getScr() != null) {
                            msg = "当前点已被别人巡检过，最后巡检人是：" + item.getScr();
                            toastviewbymyself.makeText(SxgzActivity.this, msg, Toast.LENGTH_SHORT).show();
                        } else {
                            if (item.getDjsj() != null) {
                                msg = "当前点已被别人巡检过，最后巡检时间是：" + item.getDjsj();
                                toastviewbymyself.makeText(SxgzActivity.this, msg, Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
                }

                //DataSupport.deleteAll(Xjjh.class, "download = 0 and username = ?",username);
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private String createJsonByLastUploadUser(String nfcbm) {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XJ_LAST_UPLOAD_POINT_CHECKED");
        info.setNfcbm(nfcbm);
        info.setType(type);
        String json = GsonUtils.GsonString(info);
        //Log.i("app",json);
        return json;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    String ewm = bundle.getString("result");
                    if (ewm != null) {
                        //Log.i("app",ewm);
                        handleTwoCodeCheck(ewm);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getDataInSqlite();
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        nfcValue = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
    }
}

//8a9481e48a43fa9a018a4a2d90e82327
//8a9481e48a43fa9a018a4a2d890e1fe0
//8a9481e48a43fa9a018a4a2d890e1fe0
//8a9481e48a43fa9a018a4a2d890e1fe0