130 lines
3.7 KiB
TypeScript
130 lines
3.7 KiB
TypeScript
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
|
|
};
|