package models import ( "eta/eta_api/global" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "gorm.io/gorm" "strings" "time" ) // 文件格式图标 const ( CloudDiskResourceFileTypeIconMenu = "https://hzstatic.hzinsights.com/static/icon/file_type_menu.png" CloudDiskResourceFileTypeIconUnknown = "https://hzstatic.hzinsights.com/static/icon/file_type_unknown.png" ) type CloudDiskResource struct { ResourceId int `gorm:"column:resource_id;primaryKey;autoIncrement" description:"资源ID"` MenuId int `description:"目录ID"` ResourceName string `description:"文件名"` ResourceSuffix string `description:"文件后缀名"` ResourceUrl string `description:"文件地址"` ResourceIcon string `description:"文件类型图标"` AdminId int `description:"创建人ID"` AdminName string `description:"创建人名称"` Size int64 `description:"目录大小"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` } func (m *CloudDiskResource) TableName() string { return "cloud_disk_resource" } func (m *CloudDiskResource) Create() (err error) { err = global.DEFAULT_DB.Create(m).Error return } func (m *CloudDiskResource) Update(cols []string) (err error) { err = global.DEFAULT_DB.Select(cols).Updates(m).Error return } func (m *CloudDiskResource) Delete() (err error) { sql := `DELETE FROM cloud_disk_resource WHERE resource_id = ? LIMIT 1` err = global.DEFAULT_DB.Exec(sql, m.ResourceId).Error return } func (m *CloudDiskResource) GetItemById(id int) (err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE resource_id = ? LIMIT 1`, m.TableName()) err = global.DEFAULT_DB.Raw(sql, id).Find(&m).Error return } func (m *CloudDiskResource) GetItemByCondition(condition string, pars []interface{}) (err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition) err = global.DEFAULT_DB.Raw(sql, pars...).Find(&m).Error return } func (m *CloudDiskResource) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CloudDiskResource, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error return } // CloudDiskCheckResourceRepeatReq 校验文件重名请求体 type CloudDiskCheckResourceRepeatReq struct { MenuId int `description:"目录ID"` FileNames []string `description:"文件名"` } // CloudDiskCheckResourceRepeatResp 校验文件重名响应体 type CloudDiskCheckResourceRepeatResp struct { OriginName string `description:"原文件名(包含后缀名)"` IsRepeat bool `description:"是否重复"` AvailableName string `description:"可用名"` } // CloudDiskResourceRenameReq 重命名文件请求体 type CloudDiskResourceRenameReq struct { ResourceId int `description:"文件ID"` ResourceName string `description:"文件名称"` } type CloudDiskResourceModifyReq struct { ResourceId int `description:"文件ID"` MenuId int `description:"目录ID"` ResourceName string `description:"文件名称"` } // CloudDiskResourceDeleteReq 删除文件请求体 type CloudDiskResourceDeleteReq struct { ResourceId int `description:"文件ID"` } // CloudDiskListResp 云盘列表响应体 type CloudDiskListResp struct { List []*CloudDiskListItem `description:"列表数据"` CreateMenuAuth bool `description:"新建文件夹权限"` UploadResourceAuth bool `description:"上传文件的权限"` BatchDelAuth bool `description:"批量删除目录/文件的权限(仅管理员有)"` ListPath []*CloudDiskMenuResourcePath `description:"面包屑"` } // CloudDiskListItem 云盘列表数据 type CloudDiskListItem struct { ItemId int `description:"ID"` ItemName string `description:"名称"` ItemType int `description:"类型: 1-目录; 2-文件"` ParentId int `description:"父级目录ID"` ParentName string `description:"父级目录文件名"` Size int64 `description:"大小"` SizeName string `description:"目录/文件大小(格式化)"` ResourceUrl string `description:"文件地址"` ItemIcon string `description:"图标"` AdminId int `description:"创建人ID"` AdminName string `description:"创建人"` CreateTime string `description:"创建时间"` ButtonAuth *CloudDiskListButton `description:"按钮权限" gorm:"-"` } func (m *CloudDiskListItem) AfterFind(db *gorm.DB) (err error) { m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime) return } // CloudDiskListButton 云盘列表按钮权限 type CloudDiskListButton struct { RenameAuth bool `description:"重命名文件夹的权限"` DelAuth bool `description:"删除文件夹的权限"` } // CloudDiskMenuResourcePath 云盘目录/文件面包屑 type CloudDiskMenuResourcePath struct { MenuId int `description:"目录ID"` MenuName string `description:"目录名称"` ParentId int `description:"父级目录ID"` Sort int `description:"排序"` Selected bool `description:"是否选中"` } // 分页查询响应结果 type CloudDiskMenuResourcePageResp struct { List []*CloudDiskResourcePageItem Paging *paging.PagingItem `description:"分页数据"` } // GetCloudDiskMenuAndResourceList UNION获取云盘列表数据 func GetCloudDiskMenuAndResourceList(menuCond, resourceCond string, menuPars, resourcePars []interface{}, orderRule string) (items []*CloudDiskListItem, err error) { base := `SELECT menu_id AS item_id, menu_name AS item_name, 1 AS item_type, parent_id, size, '' AS resource_url, '' AS item_icon, admin_id, admin_name, create_time FROM cloud_disk_menu WHERE 1=1 %s UNION SELECT resource_id, CONCAT(resource_name,resource_suffix), 2, menu_id, size, resource_url, resource_icon, admin_id, admin_name, create_time FROM cloud_disk_resource WHERE 1=1 %s %s` order := ` ORDER BY item_type ASC ` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(base, menuCond, resourceCond, order) pars := make([]interface{}, 0) pars = append(pars, menuPars, resourcePars) sql = utils.ReplaceDriverKeywords("", sql) err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error return } // GetSizeTotalByMenuIds 获取目录所属文件总大小 func GetSizeTotalByMenuIds(menuIds []int) (sizeTotal int64, err error) { if len(menuIds) == 0 { return } sql := `SELECT SUM(size) FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `)` err = global.DEFAULT_DB.Raw(sql, menuIds).Scan(&sizeTotal).Error return } // CloudDiskBatchDeleteReq 批量删除请求体 type CloudDiskBatchDeleteReq struct { MenuId int `description:"当前目录ID"` MenuIds string `description:"目录IDs"` ResourceIds string `description:"文件IDs"` } type CloudDiskBatchMoveReq struct { KeyWord string `description:"关键字"` ResourceIds string `description:"文件IDs"` MenuIds string `description:"当前目录ID"` MoveAll bool `description:"是否全选"` TargetMenuId int `description:"移动新目录ID"` } // BatchDeleteCloudDiskMenuAndResource 批量删除目录及文件 func BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds []int) (err error) { menuLens := len(menuIds) resourceLens := len(resourceIds) if menuLens == 0 && resourceLens == 0 { return } tx := global.DEFAULT_DB.Begin() defer func() { if err != nil { _ = tx.Rollback() } else { _ = tx.Commit() } }() if menuLens > 0 { sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)` err = tx.Exec(sql, menuIds).Error if err != nil { return } sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)` err = tx.Exec(sql, menuIds).Error if err != nil { return } } if resourceLens > 0 { sql := `DELETE FROM cloud_disk_resource WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)` err = tx.Exec(sql, resourceIds).Error if err != nil { return } } return } func GetCloudDiskResourcePageCount(resourceCond string, resourcePars []interface{}) (total int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM cloud_disk_resource WHERE 1=1 %s `, resourceCond) err = global.DEFAULT_DB.Raw(sql, resourcePars...).Scan(&total).Error return } func GetCloudDiskResourcePageList(resourceCond string, resourcePars []interface{}, orderRule string, startSize, pageSize int, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem, err error) { sql := fmt.Sprintf(`SELECT resource_id , resource_name, resource_suffix, menu_id, size, admin_name, create_time FROM cloud_disk_resource WHERE 1=1 %s order by %s LIMIT ?,?`, resourceCond, orderRule) var ormList []*CloudDiskResource resourcePars = append(resourcePars, startSize, pageSize) err = global.DEFAULT_DB.Raw(sql, resourcePars...).Find(&ormList).Error if err != nil { return } resourceList = toCloudDiskResourcePageList(ormList, menuNameMap) return } type CloudDiskResourcePageItem struct { ResourceId int ResourceName string ResourceSuffix string MenuId int MenuPath string AdminName string CreateTime string Size int64 } func (this *CloudDiskResource) toPageItem(menuNameMap map[int]string) *CloudDiskResourcePageItem { return &CloudDiskResourcePageItem{ ResourceId: this.ResourceId, ResourceName: this.ResourceName, MenuId: this.MenuId, ResourceSuffix: this.ResourceSuffix, MenuPath: menuNameMap[this.MenuId], AdminName: this.AdminName, CreateTime: this.CreateTime.Format(utils.FormatDateTime), Size: this.Size, } } func (this *CloudDiskResource) toItem() *CloudDiskResourcePageItem { return &CloudDiskResourcePageItem{ ResourceId: this.ResourceId, ResourceName: this.ResourceName, MenuId: this.MenuId, ResourceSuffix: this.ResourceSuffix, AdminName: this.AdminName, CreateTime: this.CreateTime.Format(utils.FormatDateTime), Size: this.Size, } } func toCloudDiskResourcePageList(items []*CloudDiskResource, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem) { for _, item := range items { resourceList = append(resourceList, item.toPageItem(menuNameMap)) } return } func toCloudDiskResourceList(items []*CloudDiskResource) (resourceList []*CloudDiskResourcePageItem) { for _, item := range items { resourceList = append(resourceList, item.toItem()) } return } func GetCloudDiskResourceList(resourceCond string, resourcePars []interface{}) (resourceList []*CloudDiskResourcePageItem, err error) { sql := fmt.Sprintf(`SELECT resource_id , resource_name, resource_suffix, menu_id, size, admin_name, create_time FROM cloud_disk_resource WHERE 1=1 %s `, resourceCond) var ormList []*CloudDiskResource err = global.DEFAULT_DB.Raw(sql, resourcePars).Find(&ormList).Error if err != nil { return } resourceList = toCloudDiskResourceList(ormList) return } func BatchMoveCloudDiskResource(menuId int, resourceIds []int) (err error) { resourceLens := len(resourceIds) if resourceLens == 0 { return } sql := `UPDATE cloud_disk_resource SET menu_id = ? WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)` err = global.DEFAULT_DB.Exec(sql, menuId, resourceIds).Error return }