cloud_disk_resource.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "strings"
  9. "time"
  10. )
  11. // 文件格式图标
  12. const (
  13. CloudDiskResourceFileTypeIconMenu = "https://hzstatic.hzinsights.com/static/icon/file_type_menu.png"
  14. CloudDiskResourceFileTypeIconUnknown = "https://hzstatic.hzinsights.com/static/icon/file_type_unknown.png"
  15. )
  16. type CloudDiskResource struct {
  17. ResourceId int `gorm:"column:resource_id;primaryKey;autoIncrement" description:"资源ID"`
  18. MenuId int `description:"目录ID"`
  19. ResourceName string `description:"文件名"`
  20. ResourceSuffix string `description:"文件后缀名"`
  21. ResourceUrl string `description:"文件地址"`
  22. ResourceIcon string `description:"文件类型图标"`
  23. AdminId int `description:"创建人ID"`
  24. AdminName string `description:"创建人名称"`
  25. Size int64 `description:"目录大小"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ModifyTime time.Time `description:"修改时间"`
  28. }
  29. func (m *CloudDiskResource) TableName() string {
  30. return "cloud_disk_resource"
  31. }
  32. func (m *CloudDiskResource) Create() (err error) {
  33. err = global.DEFAULT_DB.Create(m).Error
  34. return
  35. }
  36. func (m *CloudDiskResource) Update(cols []string) (err error) {
  37. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  38. return
  39. }
  40. func (m *CloudDiskResource) Delete() (err error) {
  41. sql := `DELETE FROM cloud_disk_resource WHERE resource_id = ? LIMIT 1`
  42. err = global.DEFAULT_DB.Exec(sql, m.ResourceId).Error
  43. return
  44. }
  45. func (m *CloudDiskResource) GetItemById(id int) (err error) {
  46. sql := fmt.Sprintf(`SELECT * FROM %s WHERE resource_id = ? LIMIT 1`, m.TableName())
  47. err = global.DEFAULT_DB.Raw(sql, id).Find(&m).Error
  48. return
  49. }
  50. func (m *CloudDiskResource) GetItemByCondition(condition string, pars []interface{}) (err error) {
  51. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  52. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&m).Error
  53. return
  54. }
  55. func (m *CloudDiskResource) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CloudDiskResource, err error) {
  56. fields := strings.Join(fieldArr, ",")
  57. if len(fieldArr) == 0 {
  58. fields = `*`
  59. }
  60. order := ``
  61. if orderRule != "" {
  62. order = ` ORDER BY ` + orderRule
  63. }
  64. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  65. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  66. return
  67. }
  68. // CloudDiskCheckResourceRepeatReq 校验文件重名请求体
  69. type CloudDiskCheckResourceRepeatReq struct {
  70. MenuId int `description:"目录ID"`
  71. FileNames []string `description:"文件名"`
  72. }
  73. // CloudDiskCheckResourceRepeatResp 校验文件重名响应体
  74. type CloudDiskCheckResourceRepeatResp struct {
  75. OriginName string `description:"原文件名(包含后缀名)"`
  76. IsRepeat bool `description:"是否重复"`
  77. AvailableName string `description:"可用名"`
  78. }
  79. // CloudDiskResourceRenameReq 重命名文件请求体
  80. type CloudDiskResourceRenameReq struct {
  81. ResourceId int `description:"文件ID"`
  82. ResourceName string `description:"文件名称"`
  83. }
  84. // CloudDiskResourceDeleteReq 删除文件请求体
  85. type CloudDiskResourceDeleteReq struct {
  86. ResourceId int `description:"文件ID"`
  87. }
  88. // CloudDiskListResp 云盘列表响应体
  89. type CloudDiskListResp struct {
  90. List []*CloudDiskListItem `description:"列表数据"`
  91. CreateMenuAuth bool `description:"新建文件夹权限"`
  92. UploadResourceAuth bool `description:"上传文件的权限"`
  93. BatchDelAuth bool `description:"批量删除目录/文件的权限(仅管理员有)"`
  94. ListPath []*CloudDiskMenuResourcePath `description:"面包屑"`
  95. }
  96. // CloudDiskListItem 云盘列表数据
  97. type CloudDiskListItem struct {
  98. ItemId int `description:"ID"`
  99. ItemName string `description:"名称"`
  100. ItemType int `description:"类型: 1-目录; 2-文件"`
  101. ParentId int `description:"父级目录ID"`
  102. ParentName string `description:"父级目录文件名"`
  103. Size int64 `description:"大小"`
  104. SizeName string `description:"目录/文件大小(格式化)"`
  105. ResourceUrl string `description:"文件地址"`
  106. ItemIcon string `description:"图标"`
  107. AdminId int `description:"创建人ID"`
  108. AdminName string `description:"创建人"`
  109. CreateTime string `description:"创建时间"`
  110. ButtonAuth *CloudDiskListButton `description:"按钮权限" gorm:"-"`
  111. }
  112. func (m *CloudDiskListItem) AfterFind(db *gorm.DB) (err error) {
  113. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  114. return
  115. }
  116. // CloudDiskListButton 云盘列表按钮权限
  117. type CloudDiskListButton struct {
  118. RenameAuth bool `description:"重命名文件夹的权限"`
  119. DelAuth bool `description:"删除文件夹的权限"`
  120. }
  121. // CloudDiskMenuResourcePath 云盘目录/文件面包屑
  122. type CloudDiskMenuResourcePath struct {
  123. MenuId int `description:"目录ID"`
  124. MenuName string `description:"目录名称"`
  125. ParentId int `description:"父级目录ID"`
  126. Sort int `description:"排序"`
  127. Selected bool `description:"是否选中"`
  128. }
  129. // GetCloudDiskMenuAndResourceList UNION获取云盘列表数据
  130. func GetCloudDiskMenuAndResourceList(menuCond, resourceCond string, menuPars, resourcePars []interface{}, orderRule string) (items []*CloudDiskListItem, err error) {
  131. 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
  132. WHERE 1=1 %s
  133. UNION
  134. 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
  135. WHERE 1=1 %s %s`
  136. order := ` ORDER BY item_type ASC `
  137. if orderRule != "" {
  138. order = ` ORDER BY ` + orderRule
  139. }
  140. sql := fmt.Sprintf(base, menuCond, resourceCond, order)
  141. pars := make([]interface{}, 0)
  142. pars = append(pars, menuPars, resourcePars)
  143. sql = utils.ReplaceDriverKeywords("", sql)
  144. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  145. return
  146. }
  147. // GetSizeTotalByMenuIds 获取目录所属文件总大小
  148. func GetSizeTotalByMenuIds(menuIds []int) (sizeTotal int64, err error) {
  149. if len(menuIds) == 0 {
  150. return
  151. }
  152. var sizeTotalNull sql2.NullInt64
  153. sql := `SELECT SUM(size) FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `)`
  154. err = global.DEFAULT_DB.Raw(sql, menuIds).Scan(&sizeTotalNull).Error
  155. if err != nil {
  156. return
  157. }
  158. if sizeTotalNull.Valid {
  159. sizeTotal = sizeTotalNull.Int64
  160. }
  161. return
  162. }
  163. // CloudDiskBatchDeleteReq 批量删除请求体
  164. type CloudDiskBatchDeleteReq struct {
  165. MenuId int `description:"当前目录ID"`
  166. MenuIds string `description:"目录IDs"`
  167. ResourceIds string `description:"文件IDs"`
  168. }
  169. // BatchDeleteCloudDiskMenuAndResource 批量删除目录及文件
  170. func BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds []int) (err error) {
  171. menuLens := len(menuIds)
  172. resourceLens := len(resourceIds)
  173. if menuLens == 0 && resourceLens == 0 {
  174. return
  175. }
  176. tx := global.DEFAULT_DB.Begin()
  177. defer func() {
  178. if err != nil {
  179. _ = tx.Rollback()
  180. } else {
  181. _ = tx.Commit()
  182. }
  183. }()
  184. if menuLens > 0 {
  185. sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  186. err = tx.Exec(sql, menuIds).Error
  187. if err != nil {
  188. return
  189. }
  190. sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  191. err = tx.Exec(sql, menuIds).Error
  192. if err != nil {
  193. return
  194. }
  195. }
  196. if resourceLens > 0 {
  197. sql := `DELETE FROM cloud_disk_resource WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)`
  198. err = tx.Exec(sql, resourceIds).Error
  199. if err != nil {
  200. return
  201. }
  202. }
  203. return
  204. }