feat 事务关联project到rtk和work
This commit is contained in:
@@ -43,6 +43,16 @@ export function calcUncompactedArea(totalArea?: number, compactedArea?: number)
|
|||||||
return Math.max(0, (totalArea ?? 0) - (compactedArea ?? 0))
|
return Math.max(0, (totalArea ?? 0) - (compactedArea ?? 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeValue(value: any): string {
|
||||||
|
if (value === null || value === undefined)
|
||||||
|
return 'NULL'
|
||||||
|
if (typeof value === 'number')
|
||||||
|
return String(value)
|
||||||
|
if (typeof value === 'boolean')
|
||||||
|
return value ? '1' : '0'
|
||||||
|
return `'${String(value).replace(/'/g, '\'\'')}'`
|
||||||
|
}
|
||||||
|
|
||||||
/** 查询全部项目 */
|
/** 查询全部项目 */
|
||||||
export async function selectAll(): Promise<Project[]> {
|
export async function selectAll(): Promise<Project[]> {
|
||||||
return DB.select(TABLE)
|
return DB.select(TABLE)
|
||||||
@@ -68,7 +78,7 @@ export async function insert(data: Omit<Project, 'id'>): Promise<number> {
|
|||||||
return result?.[0]?.id || 0
|
return result?.[0]?.id || 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 更新项目 */
|
/** 更新项目;标记为已完成(status=1)时清除该项目的作业记录与 RTK 轨迹 */
|
||||||
export async function update(id: number, data: Partial<Project>): Promise<any> {
|
export async function update(id: number, data: Partial<Project>): Promise<any> {
|
||||||
const old = await selectById(id)
|
const old = await selectById(id)
|
||||||
const totalArea = data.total_area !== undefined ? data.total_area : old?.total_area
|
const totalArea = data.total_area !== undefined ? data.total_area : old?.total_area
|
||||||
@@ -76,11 +86,24 @@ export async function update(id: number, data: Partial<Project>): Promise<any> {
|
|||||||
? data.total_compaction_area
|
? data.total_compaction_area
|
||||||
: old?.total_compaction_area
|
: old?.total_compaction_area
|
||||||
|
|
||||||
return DB.update(TABLE, {
|
const payload: Record<string, any> = {
|
||||||
...data,
|
...data,
|
||||||
uncompacted_area: calcUncompactedArea(totalArea, compactedArea),
|
uncompacted_area: calcUncompactedArea(totalArea, compactedArea),
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
}, `id = ${id}`)
|
}
|
||||||
|
|
||||||
|
const becomingCompleted = data.status === 1 && old?.status !== 1
|
||||||
|
if (becomingCompleted) {
|
||||||
|
const sets = Object.keys(payload).map(k => `${k} = ${escapeValue(payload[k])}`)
|
||||||
|
await DB.transaction([
|
||||||
|
`UPDATE ${TABLE} SET ${sets.join(', ')} WHERE id = ${id}`,
|
||||||
|
`DELETE FROM rtk_track WHERE work_record_id IN (SELECT id FROM work_record WHERE project_id = ${id})`,
|
||||||
|
`DELETE FROM work_record WHERE project_id = ${id}`,
|
||||||
|
])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB.update(TABLE, payload, `id = ${id}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 删除项目 */
|
/** 删除项目 */
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class DB {
|
|||||||
/**
|
/**
|
||||||
* 执行 SQL 语句(增、删、改)
|
* 执行 SQL 语句(增、删、改)
|
||||||
*/
|
*/
|
||||||
static executeSql(sql: string | string[]): Promise<any> {
|
static executeSql(sql: string[]): Promise<any> {
|
||||||
return DB.open().then(() => {
|
return DB.open().then(() => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
plus.sqlite.executeSql({
|
plus.sqlite.executeSql({
|
||||||
|
|||||||
Reference in New Issue
Block a user