Quellcode durchsuchen

列表页面的数据修改

john vor 1 Jahr
Ursprung
Commit
74b0072ad7

+ 37 - 11
src/pages/DuplicateFile/DuplicateFile.tsx

@@ -19,7 +19,7 @@ import { CopyText } from "@/components/Table/CopyText";
 import type { FixedType } from "rc-table/lib/interface";
 import FileInfoEditer from "./FileInfoEditer";
 import { FileInfoType } from "@/types/files";
-import { get_all_history, get_info_by_path, insertSeletedFileHistory } from "@/services";
+import {get_all_history, get_info_by_path, insertSeletedFileHistory, updateSelectedFileHistory} from "@/services";
 import dayjs from "dayjs";
 import { DEFAULT_TIME_FORMAT } from "@/config";
 
@@ -37,7 +37,7 @@ export default function DuplicateFile() {
   const [usePath, setUsePath] = useState<string>();
   const [historyList, setHistoryList] = useState<historyListType[]>([]);
   const [fileList, setFileList] = useState<FileInfoType[]>([
-    {
+    /*{
       id: 1,
       path: "D:/code/wb_project/bar_association_app",
       time: "2024-01-23",
@@ -54,7 +54,7 @@ export default function DuplicateFile() {
       path: "D:/code/wb_project/bar_association_app",
       time: "2024-01-23",
       progress: 90,
-    },
+    },*/
   ]);
   const [isModalOpen, setIsModalOpen] = useState(false);
   const [fileInfo, setFileInfo] = useState<any>({});
@@ -115,9 +115,9 @@ export default function DuplicateFile() {
       key: "actions",
       fixed: "right" as FixedType,
       width: 220,
-      render: () => (
+      render: (text: string, record: FileInfoType) => (
         <Space size="middle" style={{ width: "220px" }} align="baseline">
-          <Button onClick={() => setIsModalOpen(true)} type="default">
+          <Button onClick={() => openModal(record)} type="default">
             修改
           </Button>
           <Button type="primary">运行</Button>
@@ -133,10 +133,27 @@ export default function DuplicateFile() {
     getFileList()
   }, [current])
 
-  async function handleOk(newFileInfo: FileInfoType) {
-    console.log(180, newFileInfo);
-    const res = await insertSeletedFileHistory(newFileInfo.path, newFileInfo);
-    console.log(133, res);
+  async function handleOk(newFileInfo: FileInfoType, callback?: Function) {
+    try {
+      console.log(180, newFileInfo);
+      let method = insertSeletedFileHistory
+      if(fileInfoSource && JSON.stringify(fileInfoSource) !== '{}') {
+        method = updateSelectedFileHistory
+      }
+      const res = await method(newFileInfo.path, newFileInfo);
+      console.log(133, res);
+      if(res) {
+        message.error(`${res}`)
+        return
+      }
+      setIsModalOpen(false);
+      setFileInfoSource({})
+      setFileList([])
+      await getFileList();
+      callback && callback();
+    } catch (err) {
+    }
+
   }
   function handleCancel() {
     setFileInfo({});
@@ -145,7 +162,17 @@ export default function DuplicateFile() {
 
   async function openModal(info?: FileInfoType) {
     // initDB()
-    // setIsModalOpen(true);
+    setIsModalOpen(true);
+    if(info) {
+      console.log(165, info)
+      setFileInfoSource({
+        ...info,
+        checkedSizeValues: info && info?.checkedSizeValues ? `${info.checkedSizeValues}`.split(',') : [],
+        checkedTypeValues: info && info?.checkedTypeValues ? `${info.checkedTypeValues}`.split(',') : []
+      })
+    }
+    // const [fileInfo, msg] = await get_info_by_path('/Users/honghaitao/Downloads')
+    // console.log(161, fileInfo);
     // const res = await insertSeletedFileHistory('/Users/sysadmin/Downloads');
     // console.log(133, res);
     // const res = await get_info_by_path('/Users/sysadmin/Downloads')
@@ -178,7 +205,6 @@ Object Prototype
 
     */
   }
-
   async function getFileList() {
     console.log(183, current, total);
     const {data, total: localeTotal} = await get_all_history(current - 1, 10);

+ 1 - 0
src/pages/DuplicateFile/FileInfoEditer.tsx

@@ -37,6 +37,7 @@ export default function FileInfoEditer({
   const { TextArea } = Input;
   useEffect(() => {
     setIsModalOpen(showModal);
+    console.log(4040, fileInfoSource)
     if (JSON.stringify(fileInfoSource) !== "{}" && showModal) {
       setFileInfo(fileInfoSource);
     }

+ 120 - 97
src/services/file-service.ts

@@ -1,8 +1,8 @@
-import { table_init } from "@/databases/index";
+import {table_init} from "@/databases/index";
 // import { SQLite } from "@/plugins/tauri-plugin-sqlite";
 import Database from "tauri-plugin-sql-api";
-import { FILE_DB_PATH } from "@/config";
-import { FileInfoType, historyListType, insertSearchFilesPasamsType } from "@/types/files";
+import {FILE_DB_PATH} from "@/config";
+import {FileInfoType, historyListType, insertSearchFilesPasamsType} from "@/types/files";
 
 /**
  * 写入用户选择好的目录和处理规则数据
@@ -11,41 +11,67 @@ import { FileInfoType, historyListType, insertSearchFilesPasamsType } from "@/ty
  * @returns false 表示写入成功
  */
 export async function insertSeletedFileHistory(path?: string, fileInfoParams?: FileInfoType) {
-  /* 
-    addType: ".1231,.kidd"
-    checkboxAll: true
-    checkboxSizeAll: true
-    checkedSizeValues: ["巨大(4GB+)", "特大(1~4GB-)", "大(128MB ~ 1GB-)", "中(1MB ~ 128MB-)", "小(16KB ~ 1MB-)", "微小(1B ~ 16KB-)", "空文件及目录"] (7)
-    checkedTypeValues: ["音频", "视频", "文档", "图片", "应用程序", "压缩包", "其他所有带扩展名的类型", "其他所有无扩展名的类型", "指定", "排除"] (10)
-    passType: ".1231,.2113"
-    path: "/Users/sysadmin/Downloads"
- */
-  try {
-    // await table_init(FILE_DB_PATH, "select_history");
+    /*
+      addType: ".1231,.kidd"
+      checkboxAll: true
+      checkboxSizeAll: true
+      checkedSizeValues: ["巨大(4GB+)", "特大(1~4GB-)", "大(128MB ~ 1GB-)", "中(1MB ~ 128MB-)", "小(16KB ~ 1MB-)", "微小(1B ~ 16KB-)", "空文件及目录"] (7)
+      checkedTypeValues: ["音频", "视频", "文档", "图片", "应用程序", "压缩包", "其他所有带扩展名的类型", "其他所有无扩展名的类型", "指定", "排除"] (10)
+      passType: ".1231,.2113"
+      path: "/Users/sysadmin/Downloads"
+   */
+    try {
+        // await table_init(FILE_DB_PATH, "select_history");
 
-    const DB = await Database.load("sqlite:files.db");
-    // const DB = await SQLite.open(FILE_DB_PATH);
-    await DB.execute(
-      `INSERT INTO select_history (time, name, path, addType, checkboxAll, checkboxSizeAll, checkedSizeValues, checkedTypeValues, passType) VALUES (:time, :name, :path, :addType, :checkboxAll, :checkboxSizeAll, :checkedSizeValues, :checkedTypeValues, :passType)`,
-      {
-        ":time": new Date().getTime(),  // 获取当前时间的时间戳
-        ":name": path,                  // 假设 path 变量是预定义的
-        ":path": path,                  // path 变量用于 name 和 path
-        ":addType": fileInfoParams?.addType || '',
-        ":checkboxAll": fileInfoParams?.checkboxAll ? 1 : 0,
-        ":checkboxSizeAll": fileInfoParams?.checkboxSizeAll ? 1 : 0,
-        ":checkedSizeValues": fileInfoParams?.checkedSizeValues?.toString() || '',
-        ":checkedTypeValues": fileInfoParams?.checkedTypeValues?.toString() || '',
-        ":passType": fileInfoParams?.passType || '',
-      }
-    );
-    return false;
-  } catch (err) {
-    if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
-      return "当前路径重复";
+        const DB = await Database.load("sqlite:files.db");
+        // const DB = await SQLite.open(FILE_DB_PATH);
+        await DB.execute(
+            `INSERT INTO select_history (time, name, path, addType, checkboxAll, checkboxSizeAll, checkedSizeValues, checkedTypeValues, passType) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
+                new Date().getTime(),  // 获取当前时间的时间戳
+                path,                  // 假设 path 变量是预定义的
+                path,                  // path 变量用于 name 和 path
+                fileInfoParams?.addType || '',
+                fileInfoParams?.checkboxAll ? 1 : 0,
+                fileInfoParams?.checkboxSizeAll ? 1 : 0,
+                fileInfoParams?.checkedSizeValues?.toString() || '',
+                fileInfoParams?.checkedTypeValues?.toString() || '',
+                fileInfoParams?.passType || '',
+            ],
+        );
+        return false;
+    } catch (err) {
+        console.log(5454, err)
+        if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
+            return "当前路径重复";
+        }
+        return err;
+    }
+}
+
+export async function updateSelectedFileHistory(path?: string, fileInfoParams?: FileInfoType) {
+    try {
+        const DB = await Database.load("sqlite:files.db");
+        const result = await DB.execute(
+            `UPDATE select_history 
+             SET addType = $1, checkboxAll = $2, checkboxSizeAll = $3, checkedSizeValues = $4, checkedTypeValues = $5, passType = $6 
+             WHERE path = $7;`, [
+                fileInfoParams?.addType || '',
+                fileInfoParams?.checkboxAll ? 1 : 0,
+                fileInfoParams?.checkboxSizeAll ? 1 : 0,
+                fileInfoParams?.checkedSizeValues?.toString() || '',
+                fileInfoParams?.checkedTypeValues?.toString() || '',
+                fileInfoParams?.passType || '',
+                path,                  // 假设 path 变量是预定义的
+            ],
+        );
+        return false;
+    } catch (error) {
+        console.log(595959, error)
+        if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
+            return "当前数据格式异常";
+        }
+        return error;
     }
-    return err;
-  }
 }
 
 /**
@@ -53,27 +79,26 @@ export async function insertSeletedFileHistory(path?: string, fileInfoParams?: F
  * @param path 文件的路径
  * @returns FileInfoType
  */
-export async function get_info_by_path(path: string):Promise<[FileInfoType|boolean, string]>{
-  try {
-    // await table_init(FILE_DB_PATH, "select_history");
-    // const DB = await SQLite.open(FILE_DB_PATH);
-    const DB = await Database.load("sqlite:files.db");
-    const res = await DB.queryWithArgs<Array<FileInfoType>>(
-      "SELECT * FROM select_history WHERE path = :path",
-      { ":path": path }
-    );
-    
-    if(res.length) {
-      return [res[0], ""];
+export async function get_info_by_path(path: string): Promise<[FileInfoType | boolean, string]> {
+    try {
+        // await table_init(FILE_DB_PATH, "select_history");
+        // const DB = await SQLite.open(FILE_DB_PATH);
+        const DB = await Database.load("sqlite:files.db");
+        const res = await DB.select(
+            "SELECT * FROM select_history WHERE path = $1", [path]
+        );
+        if (Array.isArray(res)) {
+            return [res[0], ""];
+        }
+        return [false, "暂无数据"];
+    } catch (err) {
+        if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
+            return [false, "当前路径重复"];
+        }
+        return [false, `${err}`];
     }
-    return [false, "暂无数据"];
-  } catch (err) {
-    if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
-      return [false, "当前路径重复"];
-    }
-    return [false, `${err}`];
-  }
 }
+
 // export async function getSource(path: string) {
 
 // }
@@ -118,55 +143,53 @@ export async function get_info_by_path(path: string):Promise<[FileInfoType|boole
  *          - total: number - 表中的总记录数,用于前端计算总页数。
  */
 export async function get_all_history(page?: number, pageSize?: number): Promise<{
-  [x: string]: any; data: insertSearchFilesPasamsType[], total: number 
+    [x: string]: any; data: insertSearchFilesPasamsType[], total: number
 }> {
-  // await table_init(FILE_DB_PATH, "select_history");
-  const DB = await Database.load("sqlite:files.db");
+    // await table_init(FILE_DB_PATH, "select_history");
+    const DB = await Database.load("sqlite:files.db");
 
-  // 查询总记录数
-  const totalResult = await DB.select("SELECT COUNT(*) AS total FROM select_history");
-  console.log(128, totalResult);
-  // [Log] 128 – {lastInsertId: 0, rowsAffected: 0} (file-service.ts, line 51)
-  const total = totalResult[0].total;  // 获取总记录数
-  // 计算分页偏移量
-  const offset = (page || 1 - 1) * (pageSize || 10);
+    // 查询总记录数
+    const totalResult = await DB.select("SELECT COUNT(*) AS total FROM select_history");
+    console.log(128, totalResult);
+    // [Log] 128 – {lastInsertId: 0, rowsAffected: 0} (file-service.ts, line 51)
+    const total = Array.isArray(totalResult) && totalResult[0].total;  // 获取总记录数
+    // 计算分页偏移量
+    const offset = (page || 1 - 1) * (pageSize || 10);
 
-  // 获取当前页的数据
-  const data = await DB.select(
-    "SELECT * FROM select_history LIMIT ? OFFSET ?", [pageSize, offset]
-  );
-  console.log(138, data, pageSize, offset)
-  DB.close()
-  return { data, total };  // 返回包含数据和总记录数的对象
+    // 获取当前页的数据
+    const data = await DB.select(
+        "SELECT * FROM select_history LIMIT ? OFFSET ?", [pageSize, offset]
+    );
+    console.log(138, data, pageSize, offset)
+    DB.close()
+    return {data: Array.isArray(data) ? data : [], total};  // 返回包含数据和总记录数的对象
 }
 
 
+export async function get_list_by_sourceid(sourceId: number): Promise<[insertSearchFilesPasamsType[] | false, string]> {
+    try {
+        // await table_init(FILE_DB_PATH, "select_history");
+        // const DB = await SQLite.open(FILE_DB_PATH);
+        const DB = await Database.load("sqlite:test.db");
+        const res = await DB.execute(
+            "SELECT * FROM search_files WHERE sourceId = $1", [sourceId]
+        );
+        console.log(969696, sourceId);
 
-export async function get_list_by_sourceid(sourceId: number):Promise<[insertSearchFilesPasamsType[]|false, string]>{
-  try {
-    // await table_init(FILE_DB_PATH, "select_history");
-    // const DB = await SQLite.open(FILE_DB_PATH);
-    const DB = await Database.load("sqlite:test.db");
-    const res = await DB.execute(
-      "SELECT * FROM search_files WHERE sourceId = :sourceId",
-      { ":sourceId": sourceId }
-    );
-    console.log(969696, sourceId);
+        /* const res = await DB.queryWithArgs<Array<insertSearchFilesPasamsType>>(
+          "SELECT * FROM search_files WHERE sourceId = :sourceId GROUP BY hash HAVING COUNT(*) > 1",
+          { ":sourceId": sourceid }
+        ); */
+        console.log(3434, res);
 
-    /* const res = await DB.queryWithArgs<Array<insertSearchFilesPasamsType>>(
-      "SELECT * FROM search_files WHERE sourceId = :sourceId GROUP BY hash HAVING COUNT(*) > 1",
-      { ":sourceId": sourceid }
-    ); */
-    console.log(3434, res);
-
-    if(res.length) {
-      return [res, ""];
-    }
-    return [false, "暂无数据"];
-  } catch (err) {
-    if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
-      return [false, "当前路径重复"];
+        if (Array.isArray(res)) {
+            return [res, ""];
+        }
+        return [false, "暂无数据"];
+    } catch (err) {
+        if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
+            return [false, "当前路径重复"];
+        }
+        return [false, `${err}`];
     }
-    return [false, `${err}`];
-  }
 }