Ver Fonte

添加过期数据删除

zh há 10 meses atrás
pai
commit
d98134de27

+ 1 - 1
app/build.gradle

@@ -20,7 +20,7 @@ android {
         debug {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
-            buildConfigField "String", "WEB_URL", "\"https://app.baoshi56.com/\""
+            buildConfigField "String", "WEB_URL", "\"http://192.168.2.175:5173/\""
             buildConfigField "String", "APK_URL", "\"https://swms.baoshi56.com/\""
             buildConfigField "String", "APK_CNF", '"piece.cnf"'
             signingConfig signingConfigs.debug

+ 30 - 2
app/src/main/java/com/baoshi/piece/base/BaseWebViewActivity.java

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
 import android.graphics.Bitmap;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.provider.Settings;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -14,6 +15,7 @@ import android.webkit.WebResourceResponse;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
+import android.widget.Toast;
 
 import androidx.annotation.Nullable;
 
@@ -68,10 +70,15 @@ public abstract class BaseWebViewActivity extends BaseActivity<ActivityWebViewBi
         YUtils.showLoading(this, "加载中...");
         deliveryDao = new DeliveryDao(this);
         super.onCreate(savedInstanceState);
+        // 添加焦点管理 - 确保 WebView 获取焦点
+        viewBinding.webView.requestFocus();
+        // 删除旧记录
+        deleteOldRecordsOnStart();
         // 更新检查
         new UpdateUtils().init();
     }
 
+
     // 在活动由不可见变为可见的时候调用
     @Override
     protected void onStart() {
@@ -153,13 +160,11 @@ public abstract class BaseWebViewActivity extends BaseActivity<ActivityWebViewBi
                 @Override
                 public void onPageStarted(WebView view, String url, Bitmap favicon) {
                     super.onPageStarted(view, url, favicon);
-                    LogUtil.d("onPageStarted: " + url);
                 }
 
                 // 完成页面加载
                 @Override
                 public void onPageFinished(WebView view, String url) {
-                    LogUtil.d("onPageFinished: " + url);
                     super.onPageFinished(view, url);
                     YUtils.hideLoading();
                 }
@@ -184,4 +189,27 @@ public abstract class BaseWebViewActivity extends BaseActivity<ActivityWebViewBi
         }
     }
 
+
+    /**
+     * 应用启动时删除旧记录
+     */
+    private void deleteOldRecordsOnStart() {
+        new Thread(() -> {
+            // 创建DAO对象
+            DeliveryDao dao = new DeliveryDao(this);
+
+            // 执行删除操作
+            int deletedCount = dao.deleteOldSuccessfulRecords(7);
+
+            // 在UI线程显示结果
+            runOnUiThread(() -> {
+                if (deletedCount > 0) {
+                    Toast.makeText(this,
+                            "已删除" + deletedCount + "条过期记录",
+                            Toast.LENGTH_SHORT).show();
+                }
+                Log.d("Cleanup", "删除" + deletedCount + "条过期记录");
+            });
+        }).start();
+    }
 }

+ 2 - 2
app/src/main/java/com/baoshi/piece/db/DBHelper.java

@@ -7,7 +7,7 @@ import android.util.Log;
 
 public class DBHelper extends SQLiteOpenHelper {
     private static final String DB_NAME = "delivery_db.db";
-    private static final int DB_VERSION = 1;
+    private static final int DB_VERSION = 2;
     // 送货记录表结构
     private static final String CREATE_TABLE_DELIVERY =
             "CREATE TABLE delivery (" +
@@ -17,7 +17,7 @@ public class DBHelper extends SQLiteOpenHelper {
                     "operator TEXT NOT NULL," +          // 操作员
                     "operatorName TEXT NOT NULL," +      // 操作员名称
                     "operationTime TEXT NOT NULL," +     // 操作时间(ISO8601格式)
-                    "isPush INTEGER DEFAULT 1)";         // 0=待上传, 1=已上传
+                    "isPush INTEGER DEFAULT 0)";         // 0=待上传, 1=已上传, 2=上传失败
 
     public DBHelper(Context context) {
         super(context, DB_NAME, null, DB_VERSION);

+ 61 - 2
app/src/main/java/com/baoshi/piece/db/dao/DeliveryDao.java

@@ -11,8 +11,12 @@ import com.baoshi.piece.db.DBHelper;
 import com.baoshi.piece.db.po.DeliveryRecord;
 import com.baoshi.piece.utils.TimeUtils;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
 
 public class DeliveryDao {
     private final DBHelper dbHelper;
@@ -21,6 +25,8 @@ public class DeliveryDao {
         dbHelper = new DBHelper(context);
     }
 
+    private static final int UPLOAD_SUCCESS = 1; // 已上传状态值
+
     // 从Cursor解析记录(复用方法)
     private DeliveryRecord parseRecordFromCursor(Cursor cursor) {
         DeliveryRecord record = new DeliveryRecord(
@@ -50,7 +56,7 @@ public class DeliveryDao {
         values.put("operator", record.getOperator());
         values.put("operatorName", record.getOperatorName());
         values.put("operationTime", record.getOperationTime());
-        values.put("isPush", record.isPush() ? 1 : 0); // 布尔转INT
+        values.put("isPush", record.isPush()); // 布尔转INT
 
         try {
             long id = db.insertOrThrow("delivery", null, values);
@@ -152,7 +158,7 @@ public class DeliveryDao {
     public int updateDeliveryPushFail(String deliveryNo) {
         SQLiteDatabase db = dbHelper.getWritableDatabase();
         ContentValues values = new ContentValues();
-        values.put("isPush", 0); // 标记为已上传
+        values.put("isPush", 2); // 标记为已上传
 
         return db.update(
                 "delivery",
@@ -300,4 +306,57 @@ public class DeliveryDao {
             dbHelper.close();
         }
     }
+
+    /**
+     * 删除一周前的已上传成功记录
+     * @param retentionDays 保留天数
+     * @return 删除的记录数量
+     */
+    public int deleteOldSuccessfulRecords(int retentionDays) {
+        SQLiteDatabase db = dbHelper.getWritableDatabase();
+        try {
+            // 获取截止日期
+            String cutoffDate = getCutoffDate(retentionDays);
+
+            // 执行删除
+            return db.delete(
+                    "delivery",
+                    "isPush = ? AND operationTime < ?",
+                    new String[]{String.valueOf(UPLOAD_SUCCESS), cutoffDate}
+            );
+        } finally {
+            db.close();
+        }
+    }
+
+    /**
+     * 获取待删除记录数
+     */
+    public int getOldRecordsCount(int retentionDays) {
+        String cutoffDate = getCutoffDate(retentionDays);
+        SQLiteDatabase db = dbHelper.getReadableDatabase();
+
+        try (Cursor cursor = db.rawQuery(
+                "SELECT COUNT(*) FROM delivery WHERE isPush = ? AND operationTime < ?",
+                new String[]{String.valueOf(UPLOAD_SUCCESS), cutoffDate}
+        )) {
+            if (cursor != null && cursor.moveToFirst()) {
+                return cursor.getInt(0);
+            }
+            return 0;
+        }
+    }
+
+    /**
+     * 获取一周前的北京时间
+     */
+    private String getCutoffDate(int retentionDays) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
+        calendar.add(Calendar.DAY_OF_YEAR, -retentionDays);
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
+        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
+        return sdf.format(calendar.getTime());
+    }
 }

+ 5 - 5
app/src/main/java/com/baoshi/piece/db/po/DeliveryRecord.java

@@ -11,7 +11,7 @@ public class DeliveryRecord {
     private String operator;
     private String operatorName;
     private String operationTime;
-    private Boolean isPush;
+    private int isPush;
 
     // 构造方法
     public DeliveryRecord(String deliveryNo, String machine, String operator, String operatorName) {
@@ -19,7 +19,7 @@ public class DeliveryRecord {
         this.machine = machine;
         this.operator = operator;
         this.operatorName = operatorName;
-        this.isPush = true;
+        this.isPush = 0; // 待上传
         this.operationTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                                 .format(new Date());
     }
@@ -30,7 +30,7 @@ public class DeliveryRecord {
         this.machine = machine;
         this.operator = operator;
         this.operatorName = operatorName;
-        this.isPush = isPush == 1;
+        this.isPush = isPush;
         this.operationTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                 .format(new Date());
     }
@@ -54,11 +54,11 @@ public class DeliveryRecord {
         this.operatorName = operatorName;
     }
 
-    public Boolean isPush() {
+    public int isPush() {
         return isPush;
     }
 
-    public void setPushed(Boolean push) {
+    public void setPushed(int push) {
         isPush = push;
     }
 }

+ 2 - 3
app/src/main/java/com/baoshi/piece/utils/JavaScriptInterface.java

@@ -62,9 +62,7 @@ public class JavaScriptInterface {
      */
     @JavascriptInterface
     public String readMacAddress() {
-        String s = MacAddressUtils.readMacAddress(activity);
-        Log.i("JavaScriptInterface", s);
-        return s;
+        return MacAddressUtils.readMacAddress(activity);
     }
 
     /**
@@ -129,6 +127,7 @@ public class JavaScriptInterface {
      */
     @JavascriptInterface
     public void scanRepeatVib() {
+        Log.d("TAG", "scanRepeatVib");
         TipActionUtils.getInstance().scanRepeatTip(activity);
     }
 

+ 0 - 15
app/src/main/res/layout/activity_main.xml

@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
-    xmlns:tools="http://schemas.android.com/tools"
-    android:id="@+id/main"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    tools:context=".MainActivity">
-    <TextView
-        android:id="@+id/tv_message"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:textSize="24sp"/>
-</LinearLayout>

BIN
app/src/main/res/raw/scanner_repeat.wav