diff --git a/src/service/index.ts b/src/service/index.ts new file mode 100644 index 0000000..e5eeed0 --- /dev/null +++ b/src/service/index.ts @@ -0,0 +1,32 @@ +import DB from '@/utils/sqlite' + +import { projectTableSql } from './project' +import { rtkTrackTableSql } from './rtkTrack' +import { warningRecordTableSql } from './warningRecord' +import { workRecordTableSql } from './workRecord' + +export { projectTableSql } from './project' +export { rtkTrackTableSql } from './rtkTrack' +export { warningRecordTableSql } from './warningRecord' +export { workRecordTableSql } from './workRecord' + +/** + * 初始化数据库表 + */ +export async function initTables() { + const sqls = [ + projectTableSql, + workRecordTableSql, + rtkTrackTableSql, + warningRecordTableSql, + ] + + try { + await DB.transaction(sqls) + console.log('数据库表初始化成功') + } + catch (err) { + console.error('数据库表初始化失败:', err) + throw err + } +} diff --git a/src/service/project.ts b/src/service/project.ts new file mode 100644 index 0000000..55cb72f --- /dev/null +++ b/src/service/project.ts @@ -0,0 +1,16 @@ +/** + * 项目信息表 + * 字段说明: + * - id:项目唯一标识符 + * - project_name:项目名称 + * - project_type:项目类型 + * - status:项目状态(默认值为 'offline') + */ +export const projectTableSql = `CREATE TABLE IF NOT EXISTS project ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + project_name TEXT NOT NULL, + project_type TEXT, + status TEXT DEFAULT 'offline', + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP +)` diff --git a/src/service/rtkTrack.ts b/src/service/rtkTrack.ts new file mode 100644 index 0000000..ed9ac1b --- /dev/null +++ b/src/service/rtkTrack.ts @@ -0,0 +1,24 @@ +/** + * RTK 轨迹数据表 + * 字段说明: + * - id:轨迹唯一标识符 + * - work_record_id:作业记录 ID + * - timestamp:时间戳 + * - latitude:纬度 + * - longitude:经度 + * - altitude:高度 + * - speed:速度 + * - heading:方向 + */ +export const rtkTrackTableSql = `CREATE TABLE IF NOT EXISTS rtk_track ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + work_record_id INTEGER, + timestamp DATETIME, + latitude REAL, + longitude REAL, + altitude REAL, + speed REAL, + heading REAL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (work_record_id) REFERENCES work_record(id) +)` diff --git a/src/service/warningRecord.ts b/src/service/warningRecord.ts new file mode 100644 index 0000000..a45eefc --- /dev/null +++ b/src/service/warningRecord.ts @@ -0,0 +1,21 @@ +/** + * 告警记录表 + * 字段说明: + * - id:告警记录唯一标识符 + * - project_id:项目 ID + * - warning_type:告警类型 + * - warning_level:告警等级 + * - message:告警信息 + * - is_read:是否已读(0 表示未读,1 表示已读,默认值为 0) + * - created_at:创建时间(默认值为当前时间) + */ +export const warningRecordTableSql = `CREATE TABLE IF NOT EXISTS warning_record ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + project_id INTEGER, + warning_type TEXT, + warning_level TEXT, + message TEXT, + is_read INTEGER DEFAULT 0, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (project_id) REFERENCES project(id) +)` diff --git a/src/service/workRecord.ts b/src/service/workRecord.ts new file mode 100644 index 0000000..69d5925 --- /dev/null +++ b/src/service/workRecord.ts @@ -0,0 +1,30 @@ +/** + * 作业记录表 + * 字段说明: + * -id:作业记录唯一标识符 + * -project_id:所属项目ID + * -start_time:作业开始时间 + * -end_time:作业结束时间 + * -speed:作业速度 + * -frequency:作业频率 + * -amplitude:作业振幅 + * -compaction_value:压实度值 + * -gps_lat:GPS纬度 + * -gps_lng:GPS经度 + * -created_at:记录创建时间(默认值为当前时间) + * -FOREIGN KEY (project_id) REFERENCES project(id):外键约束,关联到项目表中的id字段 + */ +export const workRecordTableSql = `CREATE TABLE IF NOT EXISTS work_record ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + project_id INTEGER, + start_time DATETIME, + end_time DATETIME, + speed REAL, + frequency REAL, + amplitude REAL, + compaction_value REAL, + gps_lat REAL, + gps_lng REAL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (project_id) REFERENCES project(id) +)` diff --git a/src/utils/sqlite.ts b/src/utils/sqlite.ts index fc1b478..8239563 100644 --- a/src/utils/sqlite.ts +++ b/src/utils/sqlite.ts @@ -5,112 +5,30 @@ * ======================== 使用说明 ======================== * * ## 1. 导入 - * - * ```typescript - * import DB, { initTables } from '@/utils/sqlite' - * ``` + * import DB from '@/utils/sqlite' + * import { initTables } from '@/service' * * ## 2. 初始化数据库表 - * - * ```typescript * // 首次使用或表结构变更时调用,仅需执行一次 * await initTables() - * ``` * * ## 3. 插入数据(增) - * - * ```typescript - * // 方式一:使用 insert 方法(推荐,自动拼接 SQL) * await DB.insert('device', { * device_name: '徐工XS65H #01', * device_type: '压路机', * status: 'online', * }) * - * // 方式二:使用原生 SQL - * await DB.executeSql( - * `INSERT INTO device (device_name, device_type, status) - * VALUES ('徐工XS65H #01', '压路机', 'online')` - * ) - * ``` - * * ## 4. 删除数据(删) - * - * ```typescript - * // 按条件删除 * await DB.delete('device', "id = 1") - * // 删除所有 - * await DB.delete('device') - * ``` * * ## 5. 更新数据(改) - * - * ```typescript - * await DB.update('device', - * { status: 'offline', updated_at: new Date().toISOString() }, - * "id = 1" - * ) - * ``` + * await DB.update('device', { status: 'offline' }, "id = 1") * * ## 6. 查询数据(查) - * - * ### 6.1 查询全部 - * ```typescript * const devices = await DB.select('device') - * ``` - * - * ### 6.2 详细查询 —— 按 ID 查询单条 - * ```typescript * const device = await DB.selectById('device', 1) - * // 返回: { id: 1, device_name: '...', ... } 或 null - * ``` - * - * ### 6.3 详细查询 —— 按字段精确匹配 - * ```typescript - * const devices = await DB.selectByField('device', 'status', 'online') - * ``` - * - * ### 6.4 模糊查询 —— LIKE 模糊匹配 - * ```typescript - * // 按设备名称模糊查询 - * const devices = await DB.selectLike('device', 'device_name', '徐工') - * - * // 指定返回字段的模糊查询 - * const devices = await DB.selectLike('device', 'device_name', '徐工', 'id, device_name, status') - * ``` - * - * ### 6.5 条件查询 —— 带排序和分页 - * ```typescript - * const devices = await DB.select('device', '*', "status = 'online'", 'id DESC', 10) - * ``` - * - * ### 6.6 计数查询 - * ```typescript * const count = await DB.count('device', "status = 'online'") - * ``` - * - * ### 6.7 原生 SQL 查询(复杂查询时使用) - * ```typescript - * const result = await DB.selectSql('SELECT * FROM device WHERE status = "online"') - * ``` - * - * ## 7. 事务操作 - * - * ```typescript - * await DB.transaction([ - * "INSERT INTO device (device_name, device_type) VALUES ('设备A', '压路机')", - * "INSERT INTO work_record (device_id, speed) VALUES (1, 5.5)", - * ]) - * ``` - * - * ## 8. 其他 - * - * ```typescript - * // 手动打开数据库 - * await DB.open() - * // 手动关闭数据库 - * await DB.close() - * ``` */ const DB_NAME = 'road_roller' @@ -254,10 +172,6 @@ class DB { /** * 插入一条记录 - * @param table 表名 - * @param data 键值对数据对象 - * @example - * await DB.insert('device', { device_name: '设备A', device_type: '压路机', status: 'online' }) */ static async insert(table: string, data: Record): Promise { const keys = Object.keys(data) @@ -268,11 +182,6 @@ class DB { /** * 更新记录 - * @param table 表名 - * @param data 要更新的键值对 - * @param where 条件子句(不含 WHERE 关键字),为空则更新全部 - * @example - * await DB.update('device', { status: 'offline' }, "id = 1") */ static async update(table: string, data: Record, where?: string): Promise { const sets = Object.keys(data).map(k => `${k} = ${escapeValue(data[k])}`) @@ -284,10 +193,6 @@ class DB { /** * 删除记录 - * @param table 表名 - * @param where 条件子句(不含 WHERE 关键字),为空则删除全部 - * @example - * await DB.delete('device', "id = 1") */ static async delete(table: string, where?: string): Promise { let sql = `DELETE FROM ${table}` @@ -298,13 +203,6 @@ class DB { /** * 查询记录 - * @param table 表名 - * @param columns 返回字段,默认 '*' - * @param where 条件子句(不含 WHERE 关键字),可选 - * @param orderBy 排序子句(不含 ORDER BY 关键字),可选 - * @param limit 返回条数限制,可选 - * @example - * const list = await DB.select('device', '*', "status = 'online'", 'id DESC', 10) */ static async select( table: string, @@ -324,12 +222,7 @@ class DB { } /** - * 按 ID 查询单条记录(详细查询) - * @param table 表名 - * @param id 主键值 - * @returns 记录对象或 null - * @example - * const device = await DB.selectById('device', 1) + * 按 ID 查询单条记录 */ static async selectById(table: string, id: number | string): Promise { const result = await DB.select( @@ -341,13 +234,7 @@ class DB { } /** - * 按字段精确匹配查询(详细查询) - * @param table 表名 - * @param field 字段名 - * @param value 匹配值 - * @param columns 返回字段,默认 '*' - * @example - * const devices = await DB.selectByField('device', 'status', 'online') + * 按字段精确匹配查询 */ static async selectByField( table: string, @@ -360,15 +247,6 @@ class DB { /** * 模糊查询(LIKE %keyword%) - * @param table 表名 - * @param field 要模糊匹配的字段名 - * @param keyword 关键词 - * @param columns 返回字段,默认 '*' - * @example - * // 按设备名称模糊查询 - * const devices = await DB.selectLike('device', 'device_name', '徐工') - * // 指定返回字段 - * const devices = await DB.selectLike('device', 'device_name', '徐工', 'id, device_name') */ static async selectLike( table: string, @@ -382,11 +260,6 @@ class DB { /** * 计数查询 - * @param table 表名 - * @param where 条件子句(不含 WHERE 关键字),可选 - * @returns 记录数量 - * @example - * const count = await DB.count('device', "status = 'online'") */ static async count(table: string, where?: string): Promise { const result = await DB.select(table, 'COUNT(*) as total', where) @@ -394,71 +267,6 @@ class DB { } } -/** - * 初始化数据库表 - */ -export async function initTables() { - const sqls = [ - // 项目信息表 - `CREATE TABLE IF NOT EXISTS project ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - project_name TEXT NOT NULL, - project_type TEXT, - status TEXT DEFAULT 'offline', - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP - )`, - // 作业记录表 - `CREATE TABLE IF NOT EXISTS work_record ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - project_id INTEGER, - start_time DATETIME, - end_time DATETIME, - speed REAL, - frequency REAL, - amplitude REAL, - compaction_value REAL, - gps_lat REAL, - gps_lng REAL, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (project_id) REFERENCES project(id) - )`, - // RTK 轨迹数据表 - `CREATE TABLE IF NOT EXISTS rtk_track ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - work_record_id INTEGER, - timestamp DATETIME, - latitude REAL, - longitude REAL, - altitude REAL, - speed REAL, - heading REAL, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (work_record_id) REFERENCES work_record(id) - )`, - // 告警记录表 - `CREATE TABLE IF NOT EXISTS warning_record ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - project_id INTEGER, - warning_type TEXT, - warning_level TEXT, - message TEXT, - is_read INTEGER DEFAULT 0, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (project_id) REFERENCES project(id) - )`, - ] - - try { - await DB.transaction(sqls) - console.log('数据库表初始化成功') - } - catch (err) { - console.error('数据库表初始化失败:', err) - throw err - } -} - // 兼容旧版按需导出 export const openDatabase = DB.open export const closeDatabase = DB.close