file-service.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {table_init} from "@/databases/index";
  2. // import { SQLite } from "@/plugins/tauri-plugin-sqlite";
  3. import Database from "tauri-plugin-sql-api";
  4. import {FILE_DB_PATH} from "@/config";
  5. import {FileInfoType, historyListType, insertSearchFilesPasamsType} from "@/types/files";
  6. /**
  7. * 写入用户选择好的目录和处理规则数据
  8. * @param path 需要处理的文件夹路径
  9. * @param fileInfoParams 配置好的文件信息
  10. * @returns false 表示写入成功
  11. */
  12. export async function insertSeletedFileHistory(path?: string, fileInfoParams?: FileInfoType) {
  13. /*
  14. addType: ".1231,.kidd"
  15. checkboxAll: true
  16. checkboxSizeAll: true
  17. checkedSizeValues: ["巨大(4GB+)", "特大(1~4GB-)", "大(128MB ~ 1GB-)", "中(1MB ~ 128MB-)", "小(16KB ~ 1MB-)", "微小(1B ~ 16KB-)", "空文件及目录"] (7)
  18. checkedTypeValues: ["音频", "视频", "文档", "图片", "应用程序", "压缩包", "其他所有带扩展名的类型", "其他所有无扩展名的类型", "指定", "排除"] (10)
  19. passType: ".1231,.2113"
  20. path: "/Users/sysadmin/Downloads"
  21. */
  22. try {
  23. // await table_init(FILE_DB_PATH, "select_history");
  24. const DB = await Database.load("sqlite:files.db");
  25. // const DB = await SQLite.open(FILE_DB_PATH);
  26. await DB.execute(
  27. `INSERT INTO select_history (time, name, path, addType, checkboxAll, checkboxSizeAll, checkedSizeValues, checkedTypeValues, passType) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
  28. new Date().getTime(), // 获取当前时间的时间戳
  29. path, // 假设 path 变量是预定义的
  30. path, // path 变量用于 name 和 path
  31. fileInfoParams?.addType || '',
  32. fileInfoParams?.checkboxAll ? 1 : 0,
  33. fileInfoParams?.checkboxSizeAll ? 1 : 0,
  34. fileInfoParams?.checkedSizeValues?.toString() || '',
  35. fileInfoParams?.checkedTypeValues?.toString() || '',
  36. fileInfoParams?.passType || '',
  37. ],
  38. );
  39. return false;
  40. } catch (err) {
  41. console.log(5454, err)
  42. if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
  43. return "当前路径重复";
  44. }
  45. return err;
  46. }
  47. }
  48. export async function updateSelectedFileHistory(path?: string, fileInfoParams?: FileInfoType) {
  49. try {
  50. const DB = await Database.load("sqlite:files.db");
  51. const result = await DB.execute(
  52. `UPDATE select_history
  53. SET addType = $1, checkboxAll = $2, checkboxSizeAll = $3, checkedSizeValues = $4, checkedTypeValues = $5, passType = $6
  54. WHERE path = $7;`, [
  55. fileInfoParams?.addType || '',
  56. fileInfoParams?.checkboxAll ? 1 : 0,
  57. fileInfoParams?.checkboxSizeAll ? 1 : 0,
  58. fileInfoParams?.checkedSizeValues?.toString() || '',
  59. fileInfoParams?.checkedTypeValues?.toString() || '',
  60. fileInfoParams?.passType || '',
  61. path, // 假设 path 变量是预定义的
  62. ],
  63. );
  64. return false;
  65. } catch (error) {
  66. console.log(595959, error)
  67. if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
  68. return "当前数据格式异常";
  69. }
  70. return error;
  71. }
  72. }
  73. /**
  74. *
  75. * @param path 文件的路径
  76. * @returns FileInfoType
  77. */
  78. export async function get_info_by_path(path: string): Promise<[FileInfoType | boolean, string]> {
  79. try {
  80. // await table_init(FILE_DB_PATH, "select_history");
  81. // const DB = await SQLite.open(FILE_DB_PATH);
  82. const DB = await Database.load("sqlite:files.db");
  83. const res = await DB.select(
  84. "SELECT * FROM select_history WHERE path = $1", [path]
  85. );
  86. if (Array.isArray(res)) {
  87. return [res[0], ""];
  88. }
  89. return [false, "暂无数据"];
  90. } catch (err) {
  91. if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
  92. return [false, "当前路径重复"];
  93. }
  94. return [false, `${err}`];
  95. }
  96. }
  97. // export async function getSource(path: string) {
  98. // }
  99. // export async function insertSearchFiles({
  100. // path,
  101. // sourceId,
  102. // type,
  103. // name,
  104. // hash
  105. // }: insertSearchFilesPasamsType) {
  106. // try {
  107. // await table_init(FILE_DB_PATH, "search_files");
  108. // const DB = await SQLite.open(FILE_DB_PATH);
  109. // await DB.execute(
  110. // `INSERT INTO search_files (time,sourceId,name,type,path,hash) VALUES (:time,:sourceId,:name,:type,:path,:hash)`,
  111. // {
  112. // ":time": new Date().getTime(),
  113. // ":sourceId": sourceId,
  114. // ":path": path,
  115. // ":type": type,
  116. // ":name": name,
  117. // ":hash": hash,
  118. // }
  119. // );
  120. // return Promise.resolve([true, ""]);
  121. // } catch (err) {
  122. // if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
  123. // return Promise.resolve([false, "当前路径重复"]);
  124. // }
  125. // return Promise.resolve([false, err]);
  126. // }
  127. // }
  128. /**
  129. * 获取“select_history”表中的历史记录,并进行分页。
  130. * 此函数首先计算总记录数,然后根据提供的页码和页面大小参数返回相应的记录。
  131. *
  132. * @param page 当前请求的页码,代表用户想要访问的数据页。
  133. * @param pageSize 每页展示的记录数量,决定了每次查询返回的数据条数。
  134. * @returns 返回一个对象,其中包含两个属性:
  135. * - data: FileInfoType[] - 当前页的记录数据数组。
  136. * - total: number - 表中的总记录数,用于前端计算总页数。
  137. */
  138. export async function get_all_history(page?: number, pageSize?: number): Promise<{
  139. [x: string]: any; data: insertSearchFilesPasamsType[], total: number
  140. }> {
  141. // await table_init(FILE_DB_PATH, "select_history");
  142. const DB = await Database.load("sqlite:files.db");
  143. // 查询总记录数
  144. const totalResult = await DB.select("SELECT COUNT(*) AS total FROM select_history");
  145. console.log(128, totalResult);
  146. // [Log] 128 – {lastInsertId: 0, rowsAffected: 0} (file-service.ts, line 51)
  147. const total = Array.isArray(totalResult) && totalResult[0].total; // 获取总记录数
  148. // 计算分页偏移量
  149. const offset = (page || 1 - 1) * (pageSize || 10);
  150. // 获取当前页的数据
  151. const data = await DB.select(
  152. "SELECT * FROM select_history LIMIT ? OFFSET ?", [pageSize, offset]
  153. );
  154. console.log(138, data, pageSize, offset)
  155. DB.close()
  156. return {data: Array.isArray(data) ? data : [], total}; // 返回包含数据和总记录数的对象
  157. }
  158. export async function get_list_by_sourceid(sourceId: number): Promise<[insertSearchFilesPasamsType[] | false, string]> {
  159. try {
  160. // await table_init(FILE_DB_PATH, "select_history");
  161. // const DB = await SQLite.open(FILE_DB_PATH);
  162. const DB = await Database.load("sqlite:test.db");
  163. const res = await DB.execute(
  164. "SELECT * FROM search_files WHERE sourceId = $1", [sourceId]
  165. );
  166. console.log(969696, sourceId);
  167. /* const res = await DB.queryWithArgs<Array<insertSearchFilesPasamsType>>(
  168. "SELECT * FROM search_files WHERE sourceId = :sourceId GROUP BY hash HAVING COUNT(*) > 1",
  169. { ":sourceId": sourceid }
  170. ); */
  171. console.log(3434, res);
  172. if (Array.isArray(res)) {
  173. return [res, ""];
  174. }
  175. return [false, "暂无数据"];
  176. } catch (err) {
  177. if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
  178. return [false, "当前路径重复"];
  179. }
  180. return [false, `${err}`];
  181. }
  182. }