提交 3419a7e0 authored 作者: zs's avatar zs

表结构更改

上级 691cf0cd
import SqlliteDbUtil from "@/utils/sqllitedb";
import table from "./sqllite/table.js";
import { fixNullVal } from "@/utils/common";
import {
fixNullVal
} from "@/utils/common";
// 巡检
export default {
async selectList() {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let rs = await sqllitedb.selectSQL(
`select * from ${table.inspectionRecordName}`
);
return rs;
},
async selectList() {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let rs = await sqllitedb.selectSQL(
`select * from ${table.inspectionRecordName}`
);
return rs;
},
async selectDataForTime() {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let rs = await sqllitedb.selectSQL(
`SELECT *,strftime( '%Y年%m月',createTime) AS yearMonth FROM ${table.inspectionRecordName} order by createTime desc`
);
return rs;
},
async info(id) {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let sql = `select * from ${table.inspectionRecordName} where id = '${id}'`;
let res = await sqllitedb.selectSQL(sql);
if (res && res.length > 0) {
return res[0];
}
},
async remove(id) {
if (!id) {
return;
}
let sql = `delete from ${table.inspectionRecordName} where id = '${id}'`;
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
await sqllitedb.executeSQL(sql);
},
async saveBatch(list) {
if (list.length === 0) {
return;
}
console.log("开始保存用户信息...." + list.length);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
for (let data of list) {
let column = "";
let values = "";
let idx = 0;
for (let attr in data) {
let dataField = table["inspectionRecord"].find((v) => {
if (v.field === attr) {
return v;
}
});
if (!dataField) {
continue;
}
column += dataField.field + ",";
values += "'" + fixNullVal(data[attr]) + "',";
idx++;
}
async info(id) {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let sql = `select * from ${table.inspectionRecordName} where id = '${id}'`;
let res = await sqllitedb.selectSQL(sql);
if (res && res.length > 0) {
return res[0];
}
},
async remove(id) {
if (!id) {
return;
}
let sql = `delete from ${table.inspectionRecordName} where id = '${id}'`;
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
await sqllitedb.executeSQL(sql);
},
async saveBatch(list) {
if (list.length === 0) {
return;
}
console.log("开始保存用户信息...." + list.length);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
for (let data of list) {
let column = "";
let values = "";
let idx = 0;
for (let attr in data) {
let dataField = table["inspectionRecord"].find((v) => {
if (v.field === attr) {
return v;
}
});
if (!dataField) {
continue;
}
column += dataField.field + ",";
values += "'" + fixNullVal(data[attr]) + "',";
idx++;
}
column = column.endsWith(",") ?
column.substring(0, column.length - 1) :
column;
values = values.endsWith(",") ?
values.substring(0, values.length - 1) :
values;
column = column.endsWith(",")
? column.substring(0, column.length - 1)
: column;
values = values.endsWith(",")
? values.substring(0, values.length - 1)
: values;
let sql = `insert into ${table.inspectionRecordName}(${column}) values(${values})`;
let has = await this.info(data.id);
if (has && has.id) {
await this.remove(data.id);
}
await sqllitedb.executeSQL(sql);
}
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("导入完成...");
},
async save(data) {
console.log("开始保存巡检记录", data);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
let sql = `insert into ${table.inspectionRecordName}(
let sql = `insert into ${table.inspectionRecordName}(${column}) values(${values})`;
let has = await this.info(data.id);
if (has && has.id) {
await this.remove(data.id);
}
await sqllitedb.executeSQL(sql);
}
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("导入完成...");
},
async save(data) {
console.log("开始保存巡检记录", data);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
let sql = `insert into ${table.inspectionRecordName}(
inspectionType,
inspectionCode,
recordName,
......@@ -90,7 +98,7 @@ export default {
createBy,
createTime,
updateTime,
signImg,
signImg,
inspectionData
) values(
'${data.inspectionType}',
......@@ -106,23 +114,26 @@ export default {
'${data.updateTime}',
'${data.signImg}',
'${JSON.stringify(data.inspectionData)}'
)`;
await sqllitedb.executeSQL(sql);
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("导入完成...");
},
async update(data) {
console.log("开始更新巡检记录", data);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
if (!data.id) {
throw new Error("更新操作需要提供 id");
}
let sql = `UPDATE ${table.inspectionRecordName} SET
);
`;
debugger
let result = await sqllitedb.executeReturnDataSQL(sql);
console.log(result, '有有有有有有')
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("导入完成...");
},
async update(data) {
console.log("开始更新巡检记录", data);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
if (!data.id) {
throw new Error("更新操作需要提供 id");
}
let sql = `UPDATE ${table.inspectionRecordName} SET
inspectionType = '${data.inspectionType}',
inspectionCode = '${data.inspectionCode}',
recordName = '${data.recordName}',
......@@ -137,12 +148,12 @@ export default {
updateTime = '${data.updateTime}',
inspectionData = '${JSON.stringify(data.inspectionData)}'
WHERE id = ${data.id}`;
await sqllitedb.executeSQL(sql);
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("更新完成...");
},
};
await sqllitedb.executeSQL(sql);
} catch (e) {
console.log(e.message);
} finally {
await sqllitedb.closeDB();
}
console.log("更新完成...");
},
};
\ No newline at end of file
......@@ -14,13 +14,13 @@ export default {
// app初始化
async init() {
uni.showLoading({
title: '正在初始化...',
})
// uni.showLoading({
// title: '正在初始化...',
// })
// 初始化目录
await this.initDir()
// await this.initDir()
// 初始化数据库
this.initSqlLite()
this.initSqlLite()
let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
let isInit = await SqlliteDbUtil.checkIfDataImported(sqllitedb)
if (!isInit) {
......
......@@ -51,7 +51,7 @@ module.exports = {
inspectionRecordName: "INSPECTION_RECORD",
inspectionRecord: [{
field: "id",
format: "INTEGER PRIMARY KEY AUTOINCREMENT",
format: "INTEGER PRIMARY KEY AUTOINCREMENT",
},
{
field: "inspectionType",
......
......@@ -17,7 +17,7 @@
<text>编号:</text>
<text class="code-num">{{ details.inspectionCode }}</text>
<text>巡检日期:</text>
<text class="code-num">{{ details.submitTime }}</text>
<text class="code-num">{{ details.inspectionTime }}</text>
</view>
</view>
<view class="img">
......
......@@ -12,7 +12,7 @@
<image
class="bg-img"
mode="aspectFill"
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
></image>
</view>
<view class="card-item" @click="toSyncPage">
......
......@@ -33,7 +33,7 @@
<view class="profile-left">
<view class="avatar">
<image
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit"
></image>
<view class="change-password" @click="updatePassword"
......
......@@ -2,7 +2,7 @@
<view class="home-page">
<view class="left-tab">
<view class="user-info">
<image class="user-img" src="@/static/img/add-img/home1.png"></image>
<image class="user-img" src="@/static/img/add-img/defaultAvatar.png"></image>
<text class="text">{{ userName }}</text>
</view>
......
......@@ -22,7 +22,7 @@
<view class="profile-left">
<view class="avatar">
<image
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit"
></image>
</view>
......@@ -164,7 +164,7 @@
<view class="first-row">
<image
class="icon"
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
></image>
<text class="status">{{ card.statusLable }}</text>
</view>
......
......@@ -495,7 +495,7 @@ export default {
const send = dataToSql(data);
const api = this.uid ? inspectApi.update : inspectApi.save;
api(this.uid ? { id: this.uid, ...send } : send).then((res) => {
console.log("保存成功");
console.log("保存成功",res);
});
},
// 提交
......
......@@ -24,7 +24,7 @@
<view class="profile-left">
<view class="avatar">
<image
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit"
></image>
</view>
......
......@@ -12,7 +12,7 @@
<view class="profile-box">
<view class="profile-left">
<view class="avatar">
<image src="@/static/img/add-img/home1.png" mode="aspectFit"></image>
<image src="@/static/img/add-img/defaultAvatar.png" mode="aspectFit"></image>
</view>
<view class="info">
<view class="username">井道巡检</view>
......@@ -54,7 +54,7 @@
</view>
</view>
<view class="tip">
<image class="tip-icon" src="@/static/img/add-img/home1.png" mode="aspectFit"></image>请点击“需巡检井道”执行巡检
<image class="tip-icon" src="@/static/img/add-img/defaultAvatar.png" mode="aspectFit"></image>请点击“需巡检井道”执行巡检
</view>
<view class="tab-content">
<!-- 操作区域 -->
......
......@@ -18,7 +18,7 @@
<view class="profile-left">
<view class="avatar">
<image
src="@/static/img/add-img/home1.png"
src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit"
></image>
</view>
......
import _Path from "@/constant/ioPath";
export default class SqlliteDB {
// 数据库名称
dbName = "inspect";
open = false;
inst = null;
static async initSqlliteDB() {
if (!this.inst) {
this.inst = new SqlliteDB();
await this.inst.openDB();
return this.inst;
}
return this.inst;
}
// 打开数据库 不存在则创建,否则打开
async openDB() {
if (this.isOpen()) {
console.log("isOpen");
return;
}
console.log("db path:" + _Path.getDbPath() + `${this.dbName}.db`);
let that = this;
return new Promise((resolve, reject) => {
plus.sqlite.openDatabase({
name: this.dbName,
path: _Path.getDbPath() + `${this.dbName}.db`,
success(res) {
that.open = true;
resolve();
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 检查数据库
async checkDB() {
if (!this.open) {
if (!this.isOpen()) {
await this.openDB();
}
}
}
// 关闭数据库
async closeDB() {
let open = await this.openDB();
return new Promise((resolve, reject) => {
if (!open) {
resolve();
} else {
plus.sqlite.closeDatabase({
name: this.dbName,
success(res) {
this.inst = null;
this.open = false;
resolve();
},
fail(e) {
reject(e);
},
});
}
});
}
// 是否打开了数据库
isOpen() {
return plus.sqlite.isOpenDatabase({
name: this.dbName,
path: `_doc/db/${this.dbName}.db`,
});
}
// 创建表
async createTable(tableName, filedList) {
if (!tableName || !filedList || filedList.length === 0) {
throw new Error("数据库创建失败");
}
let fieldSql = "";
console.log(filedList, "filedList--------------------->>>>");
filedList.map((val, idx) => {
fieldSql = fieldSql + '"' + val.field + '" ' + val.format;
if (idx !== filedList.length - 1) {
fieldSql = fieldSql + ",";
}
});
return this.executeSQL(
`CREATE TABLE IF NOT EXISTS ${tableName}(${fieldSql})`
);
}
// 移除表
// async removeTable(tableName) {
// return this.executeSQL(`DROP TABLE IF EXISTS ${tableName}`);
// }
// 清空表
async deleteTable(tableName) {
return this.executeSQL(`DELETE FROM ${tableName}`);
}
// 增删改使用
async executeSQL(sql) {
// console.log('excuteSQL:' + sql)
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: this.dbName,
sql: sql,
success(res) {
resolve();
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 查询使用
async selectSQL(sql) {
await this.checkDB();
// console.log('selectSQL:' + sql)
return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
name: this.dbName,
sql: sql,
success(res) {
// console.log("数据", res);
resolve(res);
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 事务
async openTransaction(open) {
let oper = "open";
if (open) {
oper = open;
}
return new Promise((resolve, reject) => {
plus.sqlite.transaction({
name: this.dbName,
operation: oper,
success: function (e) {
resolve();
},
fail: function (e) {
reject(e.message);
},
});
});
}
async rollbackTransaction() {
this.openTransaction("rollback");
}
async commitTransaction() {
this.openTransaction("commit");
}
static async checkIfDataImported(sqllitedb) {
// 假设有一个表或字段来记录数据导入状态
let result = await sqllitedb.selectSQL('SELECT value FROM SYS_CONFIG WHERE key = "data_imported"');
return result && result.length > 0 && result[0].value === 'true';
}
static async markDataAsImported(sqllitedb) {
await sqllitedb.executeSQL('INSERT OR REPLACE INTO SYS_CONFIG (key, value) VALUES ("data_imported", "true")');
}
}
// 数据库名称
dbName = "inspect";
open = false;
inst = null;
static async initSqlliteDB() {
if (!this.inst) {
this.inst = new SqlliteDB();
await this.inst.openDB();
return this.inst;
}
return this.inst;
}
// 打开数据库 不存在则创建,否则打开
async openDB() {
if (this.isOpen()) {
console.log("isOpen");
return;
}
console.log("db path:" + _Path.getDbPath() + `${this.dbName}.db`);
let that = this;
return new Promise((resolve, reject) => {
plus.sqlite.openDatabase({
name: this.dbName,
path: _Path.getDbPath() + `${this.dbName}.db`,
success(res) {
that.open = true;
resolve();
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 检查数据库
async checkDB() {
if (!this.open) {
if (!this.isOpen()) {
await this.openDB();
}
}
}
// 关闭数据库
async closeDB() {
let open = await this.openDB();
return new Promise((resolve, reject) => {
if (!open) {
resolve();
} else {
plus.sqlite.closeDatabase({
name: this.dbName,
success(res) {
this.inst = null;
this.open = false;
resolve();
},
fail(e) {
reject(e);
},
});
}
});
}
// 是否打开了数据库
isOpen() {
return plus.sqlite.isOpenDatabase({
name: this.dbName,
path: `_doc/db/${this.dbName}.db`,
});
}
// 创建表
async createTable(tableName, filedList) {
if (!tableName || !filedList || filedList.length === 0) {
throw new Error("数据库创建失败");
}
let fieldSql = "";
console.log(filedList, "filedList--------------------->>>>");
filedList.map((val, idx) => {
fieldSql = fieldSql + '"' + val.field + '" ' + val.format;
if (idx !== filedList.length - 1) {
fieldSql = fieldSql + ",";
}
});
return this.executeSQL(
`CREATE TABLE IF NOT EXISTS ${tableName}(${fieldSql})`
);
}
// 移除表
// async removeTable(tableName) {
// return this.executeSQL(`DROP TABLE IF EXISTS ${tableName}`);
// }
// 清空表
async deleteTable(tableName) {
return this.executeSQL(`DELETE FROM ${tableName}`);
}
// 增删改使用
async executeSQL(sql) {
// console.log('excuteSQL:' + sql)
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: this.dbName,
sql: sql,
success(res) {
console.log('SQL execution result:', res);
resolve(res); // 暂时直接返回,方便调试
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 增删改返回最后增加的id
async executeReturnDataSQL(sql) {
console.log('Executing SQL:', sql);
let _ = this
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: _.dbName,
sql: sql,
success(res) {
const queryLastInsertIdSql = "SELECT last_insert_rowid() AS lastId";
plus.sqlite.selectSql({
name: _.dbName,
sql: queryLastInsertIdSql,
success(result) {
if (result && result.length > 0) {
let lastInsertId = result[0].lastId;
console.log('Last Insert ID:', lastInsertId);
resolve({ success: true, lastInsertId: lastInsertId });
} else {
resolve({ success: true, lastInsertId: null });
}
},
fail(e) {
reject(e);
},
});
},
fail(e) {
console.error('SQL execution failed:', e.message);
reject(e);
},
});
});
}
// 查询使用
async selectSQL(sql) {
await this.checkDB();
// console.log('selectSQL:' + sql)
return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
name: this.dbName,
sql: sql,
success(res) {
// console.log("数据", res);
resolve(res);
},
fail(e) {
console.log(e.message);
reject(e);
},
});
});
}
// 事务
async openTransaction(open) {
let oper = "open";
if (open) {
oper = open;
}
return new Promise((resolve, reject) => {
plus.sqlite.transaction({
name: this.dbName,
operation: oper,
success: function(e) {
resolve();
},
fail: function(e) {
reject(e.message);
},
});
});
}
async rollbackTransaction() {
this.openTransaction("rollback");
}
async commitTransaction() {
this.openTransaction("commit");
}
static async checkIfDataImported(sqllitedb) {
// 假设有一个表或字段来记录数据导入状态
let result = await sqllitedb.selectSQL('SELECT value FROM SYS_CONFIG WHERE key = "data_imported"');
return result && result.length > 0 && result[0].value === 'true';
}
static async markDataAsImported(sqllitedb) {
await sqllitedb.executeSQL(
'INSERT OR REPLACE INTO SYS_CONFIG (key, value) VALUES ("data_imported", "true")');
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论