|
|
@@ -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());
|
|
|
+ }
|
|
|
}
|