SoftDeleteFile 执行软删除:目标标记主动删除,未主动删除的子级批量被动删除
(eid int64, fileID int64, userID int64)
| 1824 | |
| 1825 | // SoftDeleteFile 执行软删除:目标标记主动删除,未主动删除的子级批量被动删除 |
| 1826 | func SoftDeleteFile(eid int64, fileID int64, userID int64) error { |
| 1827 | now := time.Now().UTC().UnixMilli() |
| 1828 | |
| 1829 | file, err := GetFileByID(eid, fileID) |
| 1830 | if err != nil { |
| 1831 | return err |
| 1832 | } |
| 1833 | |
| 1834 | tx := DB.Begin() |
| 1835 | defer func() { |
| 1836 | if r := recover(); r != nil { |
| 1837 | tx.Rollback() |
| 1838 | } |
| 1839 | }() |
| 1840 | |
| 1841 | // 标记目标为主动删除 |
| 1842 | if err := tx.Model(&File{}). |
| 1843 | Where("eid = ? AND id = ? AND is_deleted = ?", eid, fileID, false). |
| 1844 | Updates(map[string]interface{}{ |
| 1845 | "is_deleted": true, |
| 1846 | "is_active_deleted": true, |
| 1847 | "deleted_by": userID, |
| 1848 | "deleted_at": now, |
| 1849 | }).Error; err != nil { |
| 1850 | tx.Rollback() |
| 1851 | return err |
| 1852 | } |
| 1853 | |
| 1854 | // 标记未主动删除的子级为被动删除(仅当前未删除的) |
| 1855 | if err := tx.Model(&File{}). |
| 1856 | Where("eid = ? AND library_id = ? AND path LIKE ? AND is_deleted = ?", eid, file.LibraryID, file.Path+"/%", false). |
| 1857 | Updates(map[string]interface{}{ |
| 1858 | "is_deleted": true, |
| 1859 | "is_active_deleted": false, |
| 1860 | "deleted_by": userID, |
| 1861 | "deleted_at": now, |
| 1862 | }).Error; err != nil { |
| 1863 | tx.Rollback() |
| 1864 | return err |
| 1865 | } |
| 1866 | |
| 1867 | var affectedFileIDs []int64 |
| 1868 | if err := tx.Model(&File{}). |
| 1869 | Select("id"). |
| 1870 | Where("eid = ? AND library_id = ? AND is_deleted = ? AND (id = ? OR path LIKE ?)", eid, file.LibraryID, true, fileID, file.Path+"/%"). |
| 1871 | Pluck("id", &affectedFileIDs).Error; err != nil { |
| 1872 | tx.Rollback() |
| 1873 | return err |
| 1874 | } |
| 1875 | if len(affectedFileIDs) > 0 { |
| 1876 | if err := tx.Model(&EntityChunkRelation{}). |
| 1877 | Where("eid = ? AND file_id IN ? AND status <> ?", eid, affectedFileIDs, EntityRelationStatusDeleted). |
| 1878 | Update("status", EntityRelationStatusDeleted).Error; err != nil { |
| 1879 | tx.Rollback() |
| 1880 | return err |
| 1881 | } |
| 1882 | } |
| 1883 |
nothing calls this directly
no test coverage detected