diff --git a/app/src/main/java/com/iflytop/profilometer/core/db/helper/MyDatabaseHelper.java b/app/src/main/java/com/iflytop/profilometer/core/db/helper/MyDatabaseHelper.java index b43d400..491ad27 100644 --- a/app/src/main/java/com/iflytop/profilometer/core/db/helper/MyDatabaseHelper.java +++ b/app/src/main/java/com/iflytop/profilometer/core/db/helper/MyDatabaseHelper.java @@ -7,7 +7,7 @@ import android.database.sqlite.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "app.db"; - private static final int DATABASE_VERSION = 4; // 数据库版本升级 + private static final int DATABASE_VERSION = 1; // 升级版本号 // 表名 public static final String TABLE_APP_USER = "app_user"; @@ -15,6 +15,7 @@ public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String TABLE_PROFILE_RECORD_POINT_SET = "profile_record_point_set"; public static final String TABLE_BASE_PROFILE_RECORD_POINT_SET = "base_profile_record_point_set"; public static final String TABLE_SYSTEM_CONFIG = "system_config"; + public static final String TABLE_SYNC_TASK = "sync_task"; // 同步任务表 // 用户表建表语句 private static final String CREATE_TABLE_APP_USER = @@ -77,6 +78,23 @@ public class MyDatabaseHelper extends SQLiteOpenHelper { + "update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + ");"; + // 同步任务表建表语句,表结构与 profile_record_description 相同,增加 syncStatus 字段 + private static final String CREATE_TABLE_SYNC_TASK = + "CREATE TABLE IF NOT EXISTS " + TABLE_SYNC_TASK + " (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "create_time TEXT, " + + "update_time TEXT, " + + "uuid TEXT, " + + "operator_name TEXT, " + + "track_shape_code TEXT, " + + "verification_method_code TEXT, " + + "name TEXT NOT NULL, " + + "line_name TEXT, " + + "location TEXT, " + + "direction TEXT, " + + "sync_status TEXT" + + ");"; + public MyDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @@ -88,6 +106,7 @@ public class MyDatabaseHelper extends SQLiteOpenHelper { db.execSQL(CREATE_TABLE_PROFILE_RECORD_POINT_SET); db.execSQL(CREATE_TABLE_BASE_PROFILE_RECORD_POINT_SET); db.execSQL(CREATE_TABLE_SYSTEM_CONFIG); + db.execSQL(CREATE_TABLE_SYNC_TASK); } @Override @@ -98,6 +117,7 @@ public class MyDatabaseHelper extends SQLiteOpenHelper { db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILE_RECORD_POINT_SET); db.execSQL("DROP TABLE IF EXISTS " + TABLE_BASE_PROFILE_RECORD_POINT_SET); db.execSQL("DROP TABLE IF EXISTS " + TABLE_SYSTEM_CONFIG); + db.execSQL("DROP TABLE IF EXISTS " + TABLE_SYNC_TASK); onCreate(db); } } diff --git a/app/src/main/java/com/iflytop/profilometer/dao/SyncTaskDao.java b/app/src/main/java/com/iflytop/profilometer/dao/SyncTaskDao.java new file mode 100644 index 0000000..817421f --- /dev/null +++ b/app/src/main/java/com/iflytop/profilometer/dao/SyncTaskDao.java @@ -0,0 +1,140 @@ +package com.iflytop.profilometer.dao; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import com.iflytop.profilometer.core.db.helper.MyDatabaseHelper; +import com.iflytop.profilometer.model.entity.SyncTask; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +public class SyncTaskDao { + private final MyDatabaseHelper dbHelper; + + public SyncTaskDao(Context context) { + dbHelper = new MyDatabaseHelper(context); + } + + // 插入同步任务记录 + public long insertSyncTask(SyncTask task) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + ContentValues values = new ContentValues(); + String currentTime = getCurrentTime(); + values.put("create_time", currentTime); + values.put("update_time", currentTime); + values.put("uuid", task.getUuid()); + values.put("operator_name", task.getOperatorName()); + values.put("track_shape_code", task.getTrackShapeCode()); + values.put("verification_method_code", task.getVerificationMethodCode()); + values.put("name", task.getName()); + values.put("line_name", task.getLineName()); + values.put("location", task.getLocation()); + values.put("direction", task.getDirection()); + values.put("sync_status", task.getSyncStatus()); + long id = db.insert(MyDatabaseHelper.TABLE_SYNC_TASK, null, values); + db.close(); + return id; + } + + // 更新同步任务记录 + public int updateSyncTask(SyncTask task) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + ContentValues values = new ContentValues(); + values.put("update_time", getCurrentTime()); + values.put("uuid", task.getUuid()); + values.put("operator_name", task.getOperatorName()); + values.put("track_shape_code", task.getTrackShapeCode()); + values.put("verification_method_code", task.getVerificationMethodCode()); + values.put("name", task.getName()); + values.put("line_name", task.getLineName()); + values.put("location", task.getLocation()); + values.put("direction", task.getDirection()); + values.put("sync_status", task.getSyncStatus()); + int rows = db.update(MyDatabaseHelper.TABLE_SYNC_TASK, values, "id = ?", new String[]{String.valueOf(task.getId())}); + db.close(); + return rows; + } + + // 删除指定的同步任务记录 + public int deleteSyncTask(long id) { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + int rows = db.delete(MyDatabaseHelper.TABLE_SYNC_TASK, "id = ?", new String[]{String.valueOf(id)}); + db.close(); + return rows; + } + + // 清空同步任务表数据 + public int clearSyncTasks() { + SQLiteDatabase db = dbHelper.getWritableDatabase(); + int rows = db.delete(MyDatabaseHelper.TABLE_SYNC_TASK, null, null); + db.close(); + return rows; + } + + // 查询所有同步任务记录 + @SuppressLint("Range") + public List getAllSyncTasks() { + List list = new ArrayList<>(); + SQLiteDatabase db = dbHelper.getReadableDatabase(); + Cursor cursor = db.query(MyDatabaseHelper.TABLE_SYNC_TASK, null, null, null, null, null, "id DESC"); + if (cursor.moveToFirst()) { + do { + SyncTask task = new SyncTask(); + task.setId(cursor.getLong(cursor.getColumnIndex("id"))); + task.setUuid(cursor.getString(cursor.getColumnIndex("uuid"))); + task.setOperatorName(cursor.getString(cursor.getColumnIndex("operator_name"))); + task.setTrackShapeCode(cursor.getString(cursor.getColumnIndex("track_shape_code"))); + task.setVerificationMethodCode(cursor.getString(cursor.getColumnIndex("verification_method_code"))); + task.setName(cursor.getString(cursor.getColumnIndex("name"))); + task.setLineName(cursor.getString(cursor.getColumnIndex("line_name"))); + task.setLocation(cursor.getString(cursor.getColumnIndex("location"))); + task.setDirection(cursor.getString(cursor.getColumnIndex("direction"))); + task.setSyncStatus(cursor.getString(cursor.getColumnIndex("sync_status"))); + task.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time"))); + task.setUpdateTime(cursor.getString(cursor.getColumnIndex("update_time"))); + list.add(task); + } while (cursor.moveToNext()); + cursor.close(); + } + db.close(); + return list; + } + + // 根据 id 查询同步任务记录 + @SuppressLint("Range") + public SyncTask getSyncTaskById(long id) { + SQLiteDatabase db = dbHelper.getReadableDatabase(); + Cursor cursor = db.query(MyDatabaseHelper.TABLE_SYNC_TASK, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null); + SyncTask task = null; + if (cursor.moveToFirst()) { + task = new SyncTask(); + task.setId(cursor.getLong(cursor.getColumnIndex("id"))); + task.setUuid(cursor.getString(cursor.getColumnIndex("uuid"))); + task.setOperatorName(cursor.getString(cursor.getColumnIndex("operator_name"))); + task.setTrackShapeCode(cursor.getString(cursor.getColumnIndex("track_shape_code"))); + task.setVerificationMethodCode(cursor.getString(cursor.getColumnIndex("verification_method_code"))); + task.setName(cursor.getString(cursor.getColumnIndex("name"))); + task.setLineName(cursor.getString(cursor.getColumnIndex("line_name"))); + task.setLocation(cursor.getString(cursor.getColumnIndex("location"))); + task.setDirection(cursor.getString(cursor.getColumnIndex("direction"))); + task.setSyncStatus(cursor.getString(cursor.getColumnIndex("sync_status"))); + task.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time"))); + task.setUpdateTime(cursor.getString(cursor.getColumnIndex("update_time"))); + cursor.close(); + } + db.close(); + return task; + } + + private String getCurrentTime() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + return sdf.format(new Date()); + } +} diff --git a/app/src/main/java/com/iflytop/profilometer/model/entity/SyncTask.java b/app/src/main/java/com/iflytop/profilometer/model/entity/SyncTask.java new file mode 100644 index 0000000..247c791 --- /dev/null +++ b/app/src/main/java/com/iflytop/profilometer/model/entity/SyncTask.java @@ -0,0 +1,70 @@ +package com.iflytop.profilometer.model.entity; + +import com.iflytop.profilometer.common.base.BaseEntity; + +public class SyncTask extends BaseEntity { + private String uuid; + private String operatorName; + private String trackShapeCode; + private String verificationMethodCode; + private String name; + private String lineName; + private String location; + private String direction; + private String syncStatus; // 同步状态字段 + + public String getUuid() { + return uuid; + } + public void setUuid(String uuid) { + this.uuid = uuid; + } + public String getOperatorName() { + return operatorName; + } + public void setOperatorName(String operatorName) { + this.operatorName = operatorName; + } + public String getTrackShapeCode() { + return trackShapeCode; + } + public void setTrackShapeCode(String trackShapeCode) { + this.trackShapeCode = trackShapeCode; + } + public String getVerificationMethodCode() { + return verificationMethodCode; + } + public void setVerificationMethodCode(String verificationMethodCode) { + this.verificationMethodCode = verificationMethodCode; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getLineName() { + return lineName; + } + public void setLineName(String lineName) { + this.lineName = lineName; + } + public String getLocation() { + return location; + } + public void setLocation(String location) { + this.location = location; + } + public String getDirection() { + return direction; + } + public void setDirection(String direction) { + this.direction = direction; + } + public String getSyncStatus() { + return syncStatus; + } + public void setSyncStatus(String syncStatus) { + this.syncStatus = syncStatus; + } +}