cloud_disk_resource.go 7.6 KB

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