162 lines
5.0 KiB
Plaintext
162 lines
5.0 KiB
Plaintext
import { InitializeTheCentralTableType, ResponseDataType, TableDefinition, User } from '../../model';
|
|
import DB, { ColumnInfo, ColumnType } from '../DbSql';
|
|
import { CenterMap, ParameterPlatform, RemappingTableName } from './Relationship';
|
|
import { BusinessError } from '@ohos.base';
|
|
import Prompt from '@system.prompt';
|
|
import { initialization } from '../../api';
|
|
import FileUtil from '../../common/utils/File';
|
|
import { GlobalConfig } from '../../config';
|
|
|
|
// 建表操作
|
|
export async function InitTable() {
|
|
Object.keys(ParameterPlatform).forEach(async (item) => {
|
|
await DB.executeSql(ParameterPlatform[item].sqlCreate);
|
|
});
|
|
}
|
|
|
|
// 插表操作
|
|
export function SqlInsertTable(tableName: string, data: Array<User>, delFlag = true): Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
data.forEach((element, index) => {
|
|
element.id = !delFlag ? `${index + data.length}` : `${index}`
|
|
})
|
|
DB.clearTable(tableName).then((res) => {
|
|
const columns = Object.keys(data[0]).join(', ');
|
|
const values = data
|
|
.map((item) => {
|
|
const rowValues = Object.values(item)
|
|
.map((value: string | number) => (typeof value === 'string' ? `'${value}'` : value))
|
|
return `(${rowValues})`;
|
|
})
|
|
.join(', ');
|
|
let INSERT_SQL = "INSERT INTO " + tableName
|
|
+ " (" + columns + ") VALUES " + values
|
|
DB.executeSql(INSERT_SQL).then(() => {
|
|
console.log('sql insert,', res, 'tableName', tableName)
|
|
resolve(true)
|
|
}).catch((err: BusinessError) => {
|
|
console.log('sql insert err,', JSON.stringify(err), 'tableName', tableName)
|
|
reject(err)
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
// 参数平台
|
|
|
|
|
|
// TODO 后续废弃这个方法,直接调用SqlInsertTable
|
|
// 依据数组和表名更新sql表
|
|
export function UpdateTableByArray(tableName: string, arr: Array<User>): Promise<boolean> {
|
|
return SqlInsertTable(tableName, arr)
|
|
}
|
|
|
|
|
|
//依据表名同步获取数据
|
|
export async function GetSyncData<T>(tableName: string): Promise<T[]> {
|
|
return new Promise((resolve, reject) => {
|
|
const columns: ColumnInfo[] = ParameterPlatform[tableName].columns.map((res: string) => {
|
|
return {
|
|
name: res,
|
|
columnName: res,
|
|
type: ColumnType.STRING
|
|
} as ColumnInfo
|
|
});
|
|
DB.queryListBySql<T>(`select * from ${tableName}`, columns).then((res: T[]) => {
|
|
resolve(res);
|
|
}).catch((err: BusinessError) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
// TODO 后续废弃这个方法,直接调用DB.deleteByName
|
|
// 删除表
|
|
export async function DeleteSyncTable(tableName: string): Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
DB.deleteByName(tableName).then(() => {
|
|
resolve(true)
|
|
}).catch(() => {
|
|
reject(false)
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
// 初始化中心表
|
|
export async function InitializeTheCentralTable(params: InitializeTheCentralTableType): Promise<boolean> {
|
|
return new Promise(async (resolve, reject) => {
|
|
const fileUtil = new FileUtil(params.context)
|
|
let flag = false
|
|
// 单机模式
|
|
if (params.singlePlay) {
|
|
const tableList =
|
|
['MA_SYSSET', 'MA_SYSTEMPARM', 'MA_MARKRULE', 'MA_MARKRULESET', 'ES_CARINFO', 'MA_MAP_ROAD', 'MA_MAP_ROAD_LANE',
|
|
'MAP_SUBITEM', 'MA_T_CARPARMSET', 'MA_MAP_ITEMCLASS', 'MA_MAP_POINT', 'MA_MAP_POINT_ITEM'];
|
|
for (let i = 0; i <= tableList.length - 1; i++) {
|
|
const data =
|
|
await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + `/config/tableList/${tableList[i]}.txt`);
|
|
flag = await SqlInsertTable(tableList[i], JSON.parse(data) || [])
|
|
}
|
|
if (!flag) {
|
|
Prompt.showToast({
|
|
message: '本地文件初始化数据库失败'
|
|
})
|
|
}
|
|
resolve(flag)
|
|
}
|
|
// 非单机模式
|
|
if (params.carId) {
|
|
resolve(false)
|
|
}
|
|
const str: InitializeTheCentralTableType = {
|
|
carId: params?.carId,
|
|
examinationRoomId: params.examinationRoomId,
|
|
videoVersion: '1.0',
|
|
judgeVersion: params.judgeVersion,
|
|
shellVersion: params.shellVersion,
|
|
host: params.host
|
|
}
|
|
const res: ResponseDataType = await initialization(str)
|
|
if (!res || res.initializationRsp.head.resultCode == "1") {
|
|
resolve(false)
|
|
}
|
|
for (const key of Object.keys(res.initializationRsp.body)) {
|
|
if (CenterMap[key]) {
|
|
continue
|
|
}
|
|
// TODO 后续替换类型
|
|
let arrList: ESObject[] = []
|
|
if (res.initializationRsp.body[key] instanceof Array) {
|
|
arrList = res.initializationRsp.body[key]
|
|
} else {
|
|
arrList.push(res.initializationRsp.body[key])
|
|
}
|
|
const folderPath = await fileUtil.initFolder(`/config/tableList`);
|
|
fileUtil.addFile(`${folderPath}/${RemappingTableName[key]}.txt`, JSON.stringify(arrList))
|
|
try {
|
|
const result = await SqlInsertTable(RemappingTableName[key], arrList)
|
|
if (!result) {
|
|
Prompt.showToast({
|
|
message: '初始化数据库失败'
|
|
})
|
|
}
|
|
resolve(result)
|
|
} catch (e) {
|
|
reject(e)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|