Эх сурвалжийг харах

支持重复文件数据的排序

john 1 жил өмнө
parent
commit
8d7b7ee489

+ 125 - 97
src/pages/DuplicateFile/CalculateListPage.tsx

@@ -49,8 +49,7 @@ export default function CalculateListPage() {
   const [total, setTotal] = useState(0)
   const [pageSize, setPageSize] = useState(10)
   const [userSelectedRowKeys, setUserSelectedRowKeys] = useState<string[]>([]);
-  const [tableKey, setTableKey] = useState('123456')
-  const [sorterOrder, setSorterOrder] = useState('')
+  const [sorterOrder, setSorterOrder] = useState('');
   const [sorterColumnKey, setSorterColumnKey] = useState({})
   interface FileItem {
     sourceId: number;
@@ -65,77 +64,64 @@ export default function CalculateListPage() {
   async function getFileList() {
     setLoading(true);
     setTip('加载数据中~');
-    const sorters = [];
-    Object.keys(sorterColumnKey).forEach(key => {
-      sorters.push({
-        column: key,
-        order: sorterColumnKey[key]
-      })
-    })
-    const res = await getDuplicateFiles_v2({
-      searchParams: {
-        sourceId: `${fileId}`,
-        keywords: {},
-        sorters: sorters
-      },
-      page,
-      pageSize
-    })
-    console.log(69696969, sorterColumnKey);
-    const newData: any[] = [];
-    const allids = []
-    setTotal(res.total)
-    if(Array.isArray(res.data) && res.data.length) {
-      await res.data.reduce(
-        async (prevPromise: any, currentFile: any) => {
-          const ids = currentFile.ids.split(",");
-          const otherItems = await Promise.allSettled(
-            ids
-              .map((id: string) => {
-                if (id === ids[0]) {
-                  return false;
-                }
-                return get_fileInfo_by_id(id, `${fileId}`);
-              })
-              .filter((elm: any) => elm)
-          );
-          const children = otherItems
-            .map((elm) => {
-              if (elm.status === "fulfilled" && !elm.value[1]) {
-                allids.push(`${ids[0]}_${elm.value[0].id}`)
-                return {
-                  id: `${ids[0]}_${elm.value[0].id}`,
-                  key: `${ids[0]}_${elm.value[0].id}`,
-                  ...elm.value[0]
-                };
-              }
-              return false;
-            })
-            .filter((elm: any) => elm)
-          allids.push(ids[0]);
-          newData.push({
-            ...currentFile,
-            id: ids[0],
-            key: ids[0],
-            children: children,
-            otherItems: children,
-          });
-          return Promise.resolve(0)
-        }
-      ), Promise.resolve(0)
-      setData(newData);
-      setTableKey(`${new Date().getTime()}`);
-      setTimeout(() => {
+
+    try {
+        const sorters = Object.keys(sorterColumnKey).map(key => ({
+            column: key,
+            order: sorterColumnKey[key]
+        }));
+
+        const res = await getDuplicateFiles_v2({
+            searchParams: {
+                sourceId: `${fileId}`,
+                keywords: {},
+                sorters: sorters
+            },
+            page: page - 1,
+            pageSize
+        });
+
+        setTotal(res.total);
+
+        const newData = await processFiles(res.data, fileId);
+
+        setTimeout(() => {
+            setData(newData);
+            setLoading(false);
+            setTip('');
+        }, 300);
+    } catch (error) {
+        console.error('Error loading file list:', error);
+        setData([]);
         setLoading(false);
         setTip('');
-      }, 300)
-    } else {
-      setData([]);
-      setTableKey(`${new Date().getTime()}`);
-      setLoading(false);
-      setTip('');
     }
-  }
+}
+
+async function processFiles(data, fileId) {
+    if (!Array.isArray(data) || !data.length) {
+        return [];
+    }
+    const results = [];
+    for (const file of data) {
+        const otherItems = await Promise.allSettled(
+            file.ids.split(",").map(id => get_fileInfo_by_id(id, fileId))
+        );
+        results.push({
+            ...file,
+            key: file.id,
+            children: otherItems
+            .filter(result => result.status === "fulfilled" && !result.value[1])
+            .map(result => ({
+                id: `${file.id}_${result.value[0].id}`,
+                key: `${file.id}_${result.value[0].id}`,
+                ...result.value[0]
+            }))
+        });
+    }
+    return results;
+}
+
   const appendData = async () => {
     setLoading(true);
     setRemoveList([]);
@@ -279,6 +265,7 @@ export default function CalculateListPage() {
   }
 
   useEffect(() => {
+    console.log(281, page, pageSize, sorterColumnKey, sorterOrder);
     getFileList();
   }, [page, pageSize, sorterColumnKey, sorterOrder])
 
@@ -287,13 +274,19 @@ export default function CalculateListPage() {
       title: '文件名称',
       dataIndex: 'name',
       key: 'name',
+      fixed: 'left',
       render: (text: string, record: { name?: string, path?: string }) => (
         <Row>
           <Col span={2}>
             <FolderOpenOutlined onClick={() => openFileShowInExplorer('/Users/sysadmin/Library/Application Support/com.hht.com/files_3.db')} />
           </Col>
           <Col span={22}>
-            {record.name}
+            <CopyText
+            width="300px"
+            ellipsisLine={1}
+            color="#333"
+            name={record.name || ""}
+          ></CopyText>
           </Col>
         </Row>
       )
@@ -303,16 +296,13 @@ export default function CalculateListPage() {
       dataIndex: 'path',
       key: 'path',
       render: (text: string, record: { path?: string }) => (
-        <Row>
-          
-          <CopyText
-            width="300px"
-            ellipsisLine={1}
-            color="#333"
-            name={record.path || ""}
-          ></CopyText>
-        </Row>
-        ),
+        <CopyText
+          width="300px"
+          ellipsisLine={1}
+          color="#333"
+          name={record.path || ""}
+        ></CopyText>
+      ),
     },
     {
       title: '文件大小',
@@ -320,26 +310,55 @@ export default function CalculateListPage() {
       key: 'file_size',
       sorter: true,
       render: (text: string, record: { file_size?: string }) => (
-          <CopyText
-            width="150px"
-            ellipsisLine={1}
-            color="#333"
-            name={formatFileSize(record.file_size)}
-          ></CopyText>
-        ),
+        <CopyText
+          width="150px"
+          ellipsisLine={1}
+          color="#333"
+          name={formatFileSize(record.file_size)}
+        ></CopyText>
+      ),
+    },
+    {
+      title: '生成时间',
+      dataIndex: 'create_time',
+      key: 'create_time',
+      sorter: true,
+      render: (text: string, record: { create_time?: string }) => (
+        <CopyText
+          width="160px"
+          ellipsisLine={1}
+          color="#333"
+          name={dayjs.unix(record.create_time / 1000).format('YYYY-MM-DD HH:mm:ss') }
+        ></CopyText>
+      ),
+    },
+      {
+      title: '创建时间',
+      dataIndex: 'creation_time',
+      key: 'creation_time',
+      sorter: true,
+      render: (text: string, record: { creation_time?: string }) => (
+        <CopyText
+          width="160px"
+          ellipsisLine={1}
+          color="#333"
+          name={dayjs.unix(record.creation_time).format('YYYY-MM-DD HH:mm:ss') }
+        ></CopyText>
+      ),
     },
     {
       title: '修改时间',
       dataIndex: 'modified_time',
       key: 'modified_time',
+      sorter: true,
       render: (text: string, record: { modified_time?: string }) => (
-          <CopyText
-            width="160px"
-            ellipsisLine={1}
-            color="#333"
-            name={dayjs.unix(record.modified_time).format('YYYY-MM-DD HH:mm:ss') }
-          ></CopyText>
-        ),
+        <CopyText
+          width="160px"
+          ellipsisLine={1}
+          color="#333"
+          name={dayjs.unix(record.modified_time).format('YYYY-MM-DD HH:mm:ss') }
+        ></CopyText>
+      ),
     },
   ];
 
@@ -382,7 +401,7 @@ export default function CalculateListPage() {
       setPage(1);
       if(sorter.order) {
         setSorterColumnKey({
-          ...sorterColumnKey,
+          // ...sorterColumnKey,
           [sorter.columnKey]: sorter.order === 'ascend' ? 'ASC' : 'DESC'
         })
       } else {
@@ -407,6 +426,10 @@ export default function CalculateListPage() {
         <Button type="primary">导出</Button>
       </Space>
       <Table
+        style={{
+          width: 'calc(100% - 48px)',
+
+        }}
         columns={columns}
         loading={loading}
         pagination={{
@@ -417,7 +440,12 @@ export default function CalculateListPage() {
         }}
         rowSelection={{ ...rowSelection}}
         expandable={{
-          defaultExpandAllRows: false
+          defaultExpandAllRows: false,
+
+        }}
+        scroll={{
+          scrollToFirstRowOnChange: true,
+           x: true
         }}
         dataIndex="id"
         dataSource={data}

+ 1 - 16
src/services/file-service.ts

@@ -50,7 +50,6 @@ export async function insertSeletedFileHistory(
     );
     return false;
   } catch (err) {
-    console.log(5454, err);
     if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前路径重复";
     }
@@ -82,7 +81,6 @@ export async function updateSelectedFileHistory(
     );
     return false;
   } catch (error) {
-    console.log(595959, error);
     if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前数据格式异常";
     }
@@ -109,7 +107,6 @@ export async function updateSelectedFileHistoryFiles(
     );
     return false;
   } catch (error) {
-    console.log(595959, error);
     if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前数据格式异常";
     }
@@ -215,7 +212,6 @@ export async function insertSearchFiles({
     );
     return Promise.resolve([true, ""]);
   } catch (err) {
-    // console.log(145, err);
     if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
       return Promise.resolve([false, "当前路径重复"]);
     }
@@ -249,7 +245,6 @@ export async function get_all_history(
   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; // 获取总记录数
   // 计算分页偏移量
@@ -260,7 +255,6 @@ export async function get_all_history(
     "SELECT * FROM select_history LIMIT ? OFFSET ?",
     [pageSize, offset]
   );
-  console.log(138, data, pageSize, offset);
   DB.close();
   return { data: Array.isArray(data) ? data : [], total }; // 返回包含数据和总记录数的对象
 }
@@ -276,8 +270,6 @@ export async function get_list_by_sourceid(
       "SELECT * FROM search_files WHERE sourceId = $1 AND (hash = '' OR hash IS NULL)",
       [sourceId]
     );
-    console.log(969696, sourceId);
-    console.log(3434, res);
 
     if (Array.isArray(res)) {
       return [res, ""];
@@ -300,10 +292,8 @@ export async function delSelectedFileHistory(path?: string) {
         path, // 假设 path 变量是预定义的
       ]
     );
-    console.log(206, result);
     return false;
   } catch (error) {
-    console.log(595959, error);
     if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前数据格式异常";
     }
@@ -366,7 +356,6 @@ HAVING COUNT(*) > 1;
     );
     return Promise.resolve([true, res]);
   } catch (err) {
-    // console.log(145, err);
     if (err && `${err}`.indexOf("UNIQUE constraint failed") > -1) {
       return Promise.resolve([false, "当前路径重复"]);
     }
@@ -414,7 +403,6 @@ export async function updateFileHsah(
     );
     return false;
   } catch (error) {
-    console.log(595959, error);
     if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前数据格式异常";
     }
@@ -478,7 +466,6 @@ export async function del_file_by_id(path: string, sourceId: string) {
     );
     return Promise.resolve(false);
   } catch (error) {
-    console.log(595959, error);
     if (error && `${error}`.indexOf("UNIQUE constraint failed") > -1) {
       return "当前数据格式异常";
     }
@@ -646,8 +633,6 @@ async function fetchData() {
   // 调用函数获取数据
   try {
     const result = await getDuplicateFiles({ page, pageSize, searchParams });
-    console.log('Fetched Data:', result.data);
-    console.log('Total Records:', result.total);
   } catch (error) {
     console.error('Error fetching duplicate files:', error);
   }
@@ -707,7 +692,7 @@ export async function getDuplicateFiles_v2({
     });
 
     // 计算分页偏移量
-    const offset = (page - 1) * pageSize;
+    const offset = page * pageSize;
 
     // 动态构建排序条件
     const orderByClauses = searchParams.sorters.map(sorter => `${sorter.column} ${sorter.order}`).join(', ');