管灌系统巡查员智能手机App
app/src/main/java/com/dayu/pipirrapp/tool/InspectionUtils.java
@@ -1,7 +1,10 @@
package com.dayu.pipirrapp.tool;
import static com.dayu.pipirrapp.net.Constants.BASE_URL;
import android.content.Context;
import android.location.Location;
import android.text.TextUtils;
import android.util.Log;
import androidx.room.Transaction;
@@ -10,10 +13,18 @@
import com.dayu.pipirrapp.bean.db.InspectionBean;
import com.dayu.pipirrapp.bean.db.InspectionLocationBean;
import com.dayu.pipirrapp.bean.db.LatLonBean;
import com.dayu.pipirrapp.bean.net.InsectionResult;
import com.dayu.pipirrapp.bean.net.InspectionRequest;
import com.dayu.pipirrapp.dao.DaoSingleton;
import com.dayu.pipirrapp.fragment.MapFragment;
import com.dayu.pipirrapp.net.ApiManager;
import com.dayu.pipirrapp.net.BaseResponse;
import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
import com.dayu.pipirrapp.utils.DateUtils;
import com.dayu.pipirrapp.utils.MyLog;
import com.tencent.bugly.crashreport.CrashReport;
import java.util.List;
import java.util.UUID;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
@@ -44,16 +55,20 @@
     * @param fragment
     * @return
     */
    public static String getInspectionId(MapFragment fragment) {
    public static void getInspectionId(MapFragment fragment) {
        DaoSingleton.getAsynchInstance(fragment.getContext()).inspectionDao().getMostRecentInspectionWithNoStopTime()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).subscribe(inspectionBean -> {
                });
        return "";
    }
    /**
     * 继续显示异常关闭之前的巡检坐标
     *
     * @param fragment
     */
    public static void aginShowLocation(MapFragment fragment) {
        //查询当前未关闭的巡检记录
        DaoSingleton.getAsynchInstance(fragment.getContext()).inspectionDao().getMostRecentInspectionWithNoStopTime()
@@ -80,6 +95,8 @@
        InspectionBean inspectionBean = new InspectionBean();
        inspectionBean.setmInspectId(UUID.randomUUID().toString());
        inspectionBean.setStartTime(DateUtils.getNowDateStr());
        inspectionBean.setId(UUID.randomUUID().toString());
        inspectionBean.setInspectorId(MyApplication.myApplication.userId);
//        inspectionBean.
        // 异步插入到数据库
        DaoSingleton.getAsynchInstance(context)
@@ -90,7 +107,7 @@
                .subscribe(() -> {
                    Log.i(TAG, "Inspection started and inserted successfully.");
                }, throwable -> {
                    Log.e(TAG, "Error inserting inspection data: "+throwable);
                    Log.e(TAG, "Error inserting inspection data: " + throwable);
                });
        // 获取Dao并执行插入操作
        return inspectionBean;  // 插入完成后切换到主线程
@@ -115,6 +132,88 @@
        ;
    }
    //上传因网络问题产生的未上传数据
    public static void aginPutInspectionData(Context context) {
        //查询没有上传的所有本地数据库的巡检id
        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().getUnpostedMInspectIds()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(strings -> {
                    //循环查询所有id
                    for (String data : strings) {
                        if (!TextUtils.isEmpty(data)) {
                            DaoSingleton.getAsynchInstance(context).inspectionDao().findBymInspectId(data)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(Schedulers.io())
                                    .subscribe(inspectionBeans -> {
                                        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().findByNoPostAndInspectId(data)
                                                .subscribeOn(Schedulers.io())
                                                .observeOn(Schedulers.io())
                                                .subscribe(inspectionLocationBeans -> {
                                                    if (inspectionLocationBeans != null && inspectionLocationBeans.size() > 0) {
                                                        postInspectionData(context, inspectionBeans, inspectionLocationBeans);
                                                    }
                                                });
                                    });
                        }
                    }
                });
    }
    /**
     * 上传未上传成功的坐标
     *
     * @param inspectionBean
     * @param inspectionLocationBeans
     */
    private static void postInspectionData(Context context, InspectionBean inspectionBean, List<InspectionLocationBean> inspectionLocationBeans) {
        MyLog.d("postInspectionData>>>上传未上传成功的数据");
        InspectionRequest inspectionRequest = new InspectionRequest();
        inspectionRequest.setInspectId(inspectionBean.getInspectId());
        inspectionRequest.setInspectorId(inspectionBean.getInspectorId());
        inspectionRequest.setStartTime(inspectionBean.getStartTime());
        if (!TextUtils.isEmpty(inspectionBean.getStopTime())) {
            inspectionRequest.setStopTime(inspectionBean.getStopTime());
        }
        for (InspectionLocationBean inspectionLocationBean : inspectionLocationBeans) {
            InspectionRequest.Track track = new InspectionRequest.Track();
            track.setLat(inspectionLocationBean.getLat());
            track.setLng(inspectionLocationBean.getLng());
            track.setLocateTime(inspectionLocationBean.getLocateTime());
            inspectionRequest.addTracks(track);
        }
        ApiManager.getInstance().requestPostHideLoading(context, BASE_URL + "/app/inspect/save", InsectionResult.class, inspectionRequest.toMap(inspectionRequest), new SubscriberListener<BaseResponse<List<InsectionResult>>>() {
            @Override
            public void onNext(BaseResponse<List<InsectionResult>> t) {
                try {
                    if (t.isSuccess()) {
                        MyLog.d("postInspectionData>>>上传未上传成功的数据》》》》成功");
                        if (t.getContent() != null) {
                            inspectionBean.setInspectId(String.valueOf(t.getContent().get(0).getInspectId()));
                            InspectionUtils.upataInspectionData(context, inspectionBean);
                        }
                        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().updataByInspectIdSetIsPost(inspectionBean.getmInspectId()).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(() -> {
                            // 插入成功的回调
                            Log.d(TAG, "updataByInspectIdSetIsPost数据插入成功");
                        }, throwable -> {
                            // 处理错误
                            Log.e(TAG, "updataByInspectIdSetIsPost数据插入失败", throwable);
                        });
                    } else {
                        MyLog.d("postInspectionData>>>上传未上传成功的数据》》》》失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    CrashReport.postCatchedException(e);
                }
            }
        });
    }
    /**
     * 修改巡检记录坐标
     *
@@ -132,6 +231,21 @@
                    Log.e(TAG, "updateInspectionLocationData数据插入失败", throwable);
                });
        ;
    }
    /**
     * 修改巡检信息
     *
     * @param context
     * @param inspectionBean
     */
    public static void upataInspectionData(Context context, InspectionBean inspectionBean) {
        DaoSingleton.getAsynchInstance(context).inspectionDao().update(inspectionBean).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
            // 插入成功的回调
            Log.d(TAG, "uupataInspectionData数据插入成功");
        }, throwable -> {
            Log.e(TAG, "upataInspectionData数据插入失败", throwable);
        });
    }
    /**
@@ -167,7 +281,6 @@
        inspectionLocationBean.setmInspectId(mInspectionBean.getmInspectId());
        inspectionLocationBean.setLocateTime(DateUtils.getNowDateStr());
        inspectionLocationBean.setPost(false);
        inspectionLocationBean.setInspectorId(MyApplication.myApplication.userId);
        inspectionLocationBean.setLng(String.valueOf(latLonBean.getLongitude()));
        inspectionLocationBean.setLat(String.valueOf(latLonBean.getLatitude()));
        return inspectionLocationBean;