add:添加sqlite保存激活记录
This commit is contained in:
129
electron/service/database/activateRecord.ts
Normal file
129
electron/service/database/activateRecord.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import {BasedbService} from './basedb';
|
||||
|
||||
/**
|
||||
* sqlite数据存储
|
||||
* @class
|
||||
*/
|
||||
class ActivateRecordService extends BasedbService {
|
||||
tableName: string;
|
||||
|
||||
constructor() {
|
||||
const options = {
|
||||
dbname: 'pqs9100-tool.db',
|
||||
}
|
||||
super(options);
|
||||
this.tableName = 'activate_record';
|
||||
}
|
||||
|
||||
/*
|
||||
* 初始化表
|
||||
*/
|
||||
init(): void {
|
||||
this._init();
|
||||
|
||||
// 检查表是否存在
|
||||
const masterStmt = this.db.prepare('SELECT * FROM sqlite_master WHERE type=? AND name = ?');
|
||||
let tableExists = masterStmt.get('table', this.tableName);
|
||||
if (!tableExists) {
|
||||
// 创建表
|
||||
const create_table_sql =
|
||||
`CREATE TABLE ${this.tableName}
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
macAddress CHAR(50) NOT NULL,
|
||||
applicationCode CHAR(2000) NOT NULL,
|
||||
module CHAR(200) NOT NULL,
|
||||
activationCode CHAR(2000) NOT NULL,
|
||||
createTime CHAR(32) NOT NULL,
|
||||
remark CHAR(120) NULL
|
||||
);`
|
||||
this.db.exec(create_table_sql);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 增 data (sqlite)
|
||||
*/
|
||||
async save(data: {
|
||||
macAddress: string;
|
||||
applicationCode: string;
|
||||
module: string;
|
||||
activationCode: string;
|
||||
createTime: string;
|
||||
remark: string;
|
||||
}) {
|
||||
const insert = this.db.prepare(`INSERT INTO ${this.tableName} (macAddress, applicationCode, module, activationCode, createTime, remark)
|
||||
VALUES (@macAddress, @applicationCode, @module, @activationCode, @createTime, @remark)`);
|
||||
insert.run(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* 删 data
|
||||
*/
|
||||
async removeById(name: string = ''): Promise<boolean> {
|
||||
const remove = this.db.prepare(`DELETE
|
||||
FROM ${this.tableName}
|
||||
WHERE id = ?`);
|
||||
remove.run(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* 查list data (sqlite)
|
||||
*/
|
||||
async list(macAddress: string = '', modules: string[] = []): Promise<any[]> {
|
||||
let condition = ''
|
||||
if (macAddress) {
|
||||
condition += ` AND macAddress = '${macAddress}'`
|
||||
}
|
||||
if (modules.length > 0) {
|
||||
const moduleConditions = modules.map(module => `module LIKE '%${module}%'`).join(' OR ');
|
||||
condition += ` AND (${moduleConditions})`;
|
||||
}
|
||||
const select = this.db.prepare(`SELECT *
|
||||
FROM ${this.tableName}
|
||||
WHERE 1 = 1 ${condition}
|
||||
order by id desc`);
|
||||
return select.all();
|
||||
}
|
||||
|
||||
/*
|
||||
* all Test data (sqlite)
|
||||
*/
|
||||
async getAllTestDataSqlite(): Promise<any[]> {
|
||||
const selectAllUser = this.db.prepare(`SELECT *
|
||||
FROM ${this.tableName} `);
|
||||
const allUser = selectAllUser.all();
|
||||
return allUser;
|
||||
}
|
||||
|
||||
/*
|
||||
* get data dir (sqlite)
|
||||
*/
|
||||
async getDataDir(): Promise<string> {
|
||||
const dir = this.storage.getDbDir();
|
||||
return dir;
|
||||
}
|
||||
|
||||
/*
|
||||
* set custom data dir (sqlite)
|
||||
*/
|
||||
async setCustomDataDir(dir: string): Promise<void> {
|
||||
if (dir.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.changeDataDir(dir);
|
||||
this.init();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ActivateRecordService.toString = () => '[class ActivateRecordService]';
|
||||
const activateRecordService = new ActivateRecordService();
|
||||
|
||||
export {
|
||||
ActivateRecordService,
|
||||
activateRecordService
|
||||
};
|
||||
52
electron/service/database/basedb.ts
Normal file
52
electron/service/database/basedb.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import {type Database, SqliteStorage} from 'ee-core/storage';
|
||||
import {getDataDir} from 'ee-core/ps';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* sqlite数据存储
|
||||
* @class
|
||||
*/
|
||||
class BasedbService {
|
||||
dbname: string;
|
||||
db!: Database;
|
||||
storage!: SqliteStorage;
|
||||
|
||||
constructor(options: { dbname: string }) {
|
||||
const { dbname } = options;
|
||||
this.dbname = dbname;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 初始化
|
||||
*/
|
||||
protected _init(): void {
|
||||
// 定义数据文件
|
||||
const dbFile = path.join(getDataDir(), "db", this.dbname);
|
||||
const sqliteOptions = {
|
||||
timeout: 6000,
|
||||
verbose: console.log
|
||||
}
|
||||
this.storage = new SqliteStorage(dbFile, sqliteOptions);
|
||||
this.db = this.storage.db;
|
||||
}
|
||||
|
||||
/*
|
||||
* change data dir (sqlite)
|
||||
*/
|
||||
changeDataDir(dir: string): void {
|
||||
// the absolute path of the db file
|
||||
const dbFile = path.join(dir, this.dbname);
|
||||
const sqliteOptions = {
|
||||
timeout: 6000,
|
||||
verbose: console.log
|
||||
}
|
||||
this.storage = new SqliteStorage(dbFile, sqliteOptions);
|
||||
this.db = this.storage.db;
|
||||
}
|
||||
}
|
||||
BasedbService.toString = () => '[class BasedbService]';
|
||||
|
||||
export {
|
||||
BasedbService,
|
||||
}
|
||||
Reference in New Issue
Block a user