import path from 'path'; import { app as electronApp, dialog } from 'electron'; import { activateRecordService } from "../service/database/activateRecord"; class ActivateRecordController { async list(args: { macAddress: string, modules: string[] }): Promise { const { macAddress, modules } = args; return activateRecordService.list(macAddress, modules); } async save(args: { applicant: string, macAddress: string, applicationCode: string, modules: string[], activationCode: string, createTime: string, remark: string }): Promise { const { modules } = args; return activateRecordService.save({ ...args, module: modules.join(',') }); } async removeById(args: { id: number }): Promise { const { id } = args; return activateRecordService.removeById(id); } async clear(): Promise { return activateRecordService.clear(); } async backup(): Promise { const defaultFileName = `activate-record-backup-${this.getTimestamp()}.json`; const filePath = dialog.showSaveDialogSync({ title: '导出备份', defaultPath: path.join(electronApp.getPath('documents'), defaultFileName), filters: [ { name: 'JSON', extensions: ['json'] } ] }); if (!filePath) { return { canceled: true }; } const count = await activateRecordService.backup(filePath); return { canceled: false, count, filePath }; } async importBackup(): Promise { const filePaths = dialog.showOpenDialogSync({ title: '导入备份', properties: ['openFile'], filters: [ { name: 'JSON', extensions: ['json'] } ] }); if (!filePaths || filePaths.length === 0) { return { canceled: true }; } const filePath = filePaths[0]; const count = await activateRecordService.importBackup(filePath); return { canceled: false, count, filePath }; } private getTimestamp(): string { const now = new Date(); const year = now.getFullYear(); const month = `${now.getMonth() + 1}`.padStart(2, '0'); const day = `${now.getDate()}`.padStart(2, '0'); const hour = `${now.getHours()}`.padStart(2, '0'); const minute = `${now.getMinutes()}`.padStart(2, '0'); const second = `${now.getSeconds()}`.padStart(2, '0'); return `${year}${month}${day}-${hour}${minute}${second}`; } } ActivateRecordController.toString = () => '[class ActivateRecordController]'; export default ActivateRecordController;