import SqlliteDbUtil from '@/utils/sqllitedb'
import table from './sqllite/table.js'

/**
 * 清理过期的巡检列表和日志列表，防止数据过多
 */
export async function cleanTimeoutDataApi() {
	let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
	try {
		const sql =
			`DELETE FROM ${table.inspectionRecordName} WHERE createTime < (strftime('%s', 'now') - 90 * 24 * 60 * 60) * 1000`;
		const ret = await sqllitedb.executeSQL(sql);
		const cleanSql = `
		      DELETE FROM ${table.logListName}
		      WHERE id NOT IN (
		        SELECT id FROM ${table.logListName}
		        ORDER BY time DESC
		        LIMIT 100
		      )
		    `
		const ret1 = await sqllitedb.executeSQL(cleanSql)
		console.log('cleanTimeoutDataApi success', ret, ret1)
	} catch (e) {
		console.error('cleanTimeoutDataApi error', e.message)
	} finally {
		// await sqllitedb.closeDB();
	}
}