import 'package:flutter/foundation.dart'; import 'package:sqflite/sqflite.dart'; import 'package:translator_lux/entity/translator_history_entity.dart'; class TranslatorHistoryTable { static String tableName = "translatorHistoryTable"; static Database? thDB; static createTable(Database db) async { await db.execute( "CREATE TABLE $tableName (tid TEXT,sourceText TEXT,sourcecountryCode TEXT,sourcelanguageName TEXT,sourcelanguageCode TEXT,targetText TEXT,targetcountryCode TEXT,targetlanguageName TEXT,targetlanguageCode TEXT,translatorTime TEXT,isShow Text)"); await db.execute( 'CREATE INDEX translator_history_tid ON $tableName (tid)', ); debugPrint("TranslatorHistoryTable create success"); } static init(Database db) { thDB = db; } static insertData(TranslatorHistoryEntity historyEntity) { thDB!.insert(tableName, { "tid": historyEntity.tid, "sourceText": historyEntity.sourceText, "sourcecountryCode": historyEntity.sourcecountryCode, "sourcelanguageName": historyEntity.sourcelanguageName, "sourcelanguageCode": historyEntity.sourcelanguageCode, "targetText": historyEntity.targetText, "targetcountryCode": historyEntity.targetcountryCode, "targetlanguageName": historyEntity.targetlanguageName, "targetlanguageCode": historyEntity.targetlanguageCode, "translatorTime": historyEntity.translatorTime, "isShow": historyEntity.isShow, }); } static Future> queryData() async { List responseData = []; List> result = await thDB!.query( tableName, ); for (var historyData in result) { responseData.add( TranslatorHistoryEntity( tid: historyData["tid"].toString(), sourceText: historyData["sourceText"].toString(), sourcecountryCode: historyData["sourcecountryCode"].toString(), sourcelanguageName: historyData["sourcelanguageName"].toString(), sourcelanguageCode: historyData["sourcelanguageCode"].toString(), targetText: historyData["targetText"].toString(), targetcountryCode: historyData["targetcountryCode"].toString(), targetlanguageName: historyData["targetlanguageName"].toString(), targetlanguageCode: historyData["targetlanguageCode"].toString(), translatorTime: historyData["translatorTime"].toString(), isShow: historyData["isShow"].toString(), ), ); } return responseData; } static Future> queryShowData() async { List responseData = []; List> result = await thDB!.query( tableName, where: "isShow = ? ", whereArgs: ["true"], ); for (var historyData in result) { responseData.add( TranslatorHistoryEntity( tid: historyData["tid"].toString(), sourceText: historyData["sourceText"].toString(), sourcecountryCode: historyData["sourcecountryCode"].toString(), sourcelanguageName: historyData["sourcelanguageName"].toString(), sourcelanguageCode: historyData["sourcelanguageCode"].toString(), targetText: historyData["targetText"].toString(), targetcountryCode: historyData["targetcountryCode"].toString(), targetlanguageName: historyData["targetlanguageName"].toString(), targetlanguageCode: historyData["targetlanguageCode"].toString(), translatorTime: historyData["translatorTime"].toString(), isShow: historyData["isShow"].toString(), ), ); } return responseData; } static Future queryShowDataByTid(String tid) async { TranslatorHistoryEntity responseData = TranslatorHistoryEntity(); List> result = await thDB!.query( tableName, where: "tid = ? ", whereArgs: [tid], ); var historyData = result.first; responseData.copyWith( tid: historyData["tid"].toString(), sourceText: historyData["sourceText"].toString(), sourcecountryCode: historyData["sourcecountryCode"].toString(), sourcelanguageName: historyData["sourcelanguageName"].toString(), sourcelanguageCode: historyData["sourcelanguageCode"].toString(), targetText: historyData["targetText"].toString(), targetcountryCode: historyData["targetcountryCode"].toString(), targetlanguageName: historyData["targetlanguageName"].toString(), targetlanguageCode: historyData["targetlanguageCode"].toString(), translatorTime: historyData["translatorTime"].toString(), isShow: historyData["isShow"].toString(), ); return responseData; } static cleanAllShowData() async { await thDB!.update( tableName, { "isShow": "false", }, where: " isShow = ? ", whereArgs: ["true"], ); } static cleanShowDataByTid(String tid) async { await thDB!.update( tableName, { "isShow": "false", }, where: " tid = ? ", whereArgs: [tid], ); } static deleteHistoryDataByTid(String tid) async { await thDB!.delete( tableName, where: " tid = ? ", whereArgs: [tid], ); } }