diff --git a/src/service/index.ts b/src/service/index.ts index 8cce39d..785fd27 100644 --- a/src/service/index.ts +++ b/src/service/index.ts @@ -28,7 +28,7 @@ const tableSqls = [ /** 当前代码期望的各表字段(用于检测旧库结构) */ const expectedColumns: Record = { - project: ['id', 'project_name', 'stake_range', 'total_area', 'start_time', 'end_time', 'total_duration', 'total_compaction_area', 'total_pass_count', 'status', 'created_at', 'updated_at'], + project: ['id', 'project_name', 'stake_range', 'total_area', 'start_time', 'end_time', 'total_duration', 'total_compaction_area', 'total_pass_count', 'qualified_rate', 'progress', 'uncompacted_area', 'status', 'created_at', 'updated_at'], work_record: ['id', 'project_id', 'start_time', 'end_time', 'duration', 'compaction_area', 'pass_count', 'exception_count', 'created_at'], rtk_track: ['id', 'work_record_id', 'latitude', 'longitude', 'speed', 'heading', 'steel_wheel_temp', 'compaction_value', 'created_at'], warning_record: ['id', 'project_id', 'device_name', 'exception_type', 'exception_level', 'occurred_at', 'site_description', 'disposal_record', 'disposal_type', 'is_read', 'created_at'], diff --git a/src/service/project.ts b/src/service/project.ts index b5be3a4..3084951 100644 --- a/src/service/project.ts +++ b/src/service/project.ts @@ -12,6 +12,9 @@ export const projectTableSql = `CREATE TABLE IF NOT EXISTS project ( total_duration REAL DEFAULT 0, total_compaction_area REAL DEFAULT 0, total_pass_count INTEGER DEFAULT 0, + qualified_rate REAL DEFAULT 0, + progress REAL DEFAULT 0, + uncompacted_area REAL DEFAULT 0, status INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP @@ -27,11 +30,19 @@ export interface Project { total_duration?: number // 目前作业进行总时长,默认 0 total_compaction_area?: number // 当前总共的压实面积,默认 0 total_pass_count?: number // 当前总共的碾压遍数,默认 0 + qualified_rate?: number // 压实合格率,默认 0 + progress?: number // 完成进度,默认 0 + uncompacted_area?: number // 未压实面积 = 总面积 - 压实面积,默认 0 status: 0 | 1 | 2 // 项目状态,0:未完成,1:已完成,2:作业中, 默认0 created_at?: string // 创建时间,默认当前时间 updated_at?: string // 更新时间,默认当前时间 } +/** 未压实面积 = max(0, 总面积 - 压实面积) */ +export function calcUncompactedArea(totalArea?: number, compactedArea?: number) { + return Math.max(0, (totalArea ?? 0) - (compactedArea ?? 0)) +} + /** 查询全部项目 */ export async function selectAll(): Promise { return DB.select(TABLE) @@ -49,14 +60,27 @@ export async function selectByStatus(status: 0 | 1): Promise { /** 新增项目 */ export async function insert(data: Omit): Promise { - await DB.insert(TABLE, data) + await DB.insert(TABLE, { + ...data, + uncompacted_area: calcUncompactedArea(data.total_area, data.total_compaction_area), + }) const result = await DB.selectSql('SELECT last_insert_rowid() as id') return result?.[0]?.id || 0 } /** 更新项目 */ export async function update(id: number, data: Partial): Promise { - return DB.update(TABLE, { ...data, updated_at: new Date().toISOString() }, `id = ${id}`) + const old = await selectById(id) + const totalArea = data.total_area !== undefined ? data.total_area : old?.total_area + const compactedArea = data.total_compaction_area !== undefined + ? data.total_compaction_area + : old?.total_compaction_area + + return DB.update(TABLE, { + ...data, + uncompacted_area: calcUncompactedArea(totalArea, compactedArea), + updated_at: new Date().toISOString(), + }, `id = ${id}`) } /** 删除项目 */ diff --git a/src/service/workRecord.ts b/src/service/workRecord.ts index 2a5e86e..b8ff5e5 100644 --- a/src/service/workRecord.ts +++ b/src/service/workRecord.ts @@ -64,6 +64,7 @@ function buildProjectDeltaSql( total_duration = COALESCE(total_duration, 0) + (${durationDelta}), total_compaction_area = COALESCE(total_compaction_area, 0) + (${compactionDelta}), total_pass_count = COALESCE(total_pass_count, 0) + (${passDelta}), + uncompacted_area = MAX(0, COALESCE(total_area, 0) - (COALESCE(total_compaction_area, 0) + (${compactionDelta}))), updated_at = ${now} WHERE id = ${projectId}` }