cloud_disk_resource.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package models
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  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. type CloudDiskResourceModifyReq struct {
  85. ResourceId int `description:"文件ID"`
  86. MenuId int `description:"目录ID"`
  87. ResourceName string `description:"文件名称"`
  88. }
  89. // CloudDiskResourceDeleteReq 删除文件请求体
  90. type CloudDiskResourceDeleteReq struct {
  91. ResourceId int `description:"文件ID"`
  92. }
  93. // CloudDiskListResp 云盘列表响应体
  94. type CloudDiskListResp struct {
  95. List []*CloudDiskListItem `description:"列表数据"`
  96. CreateMenuAuth bool `description:"新建文件夹权限"`
  97. UploadResourceAuth bool `description:"上传文件的权限"`
  98. BatchDelAuth bool `description:"批量删除目录/文件的权限(仅管理员有)"`
  99. ListPath []*CloudDiskMenuResourcePath `description:"面包屑"`
  100. }
  101. // CloudDiskListItem 云盘列表数据
  102. type CloudDiskListItem struct {
  103. ItemId int `description:"ID"`
  104. ItemName string `description:"名称"`
  105. ItemType int `description:"类型: 1-目录; 2-文件"`
  106. ParentId int `description:"父级目录ID"`
  107. ParentName string `description:"父级目录文件名"`
  108. Size int64 `description:"大小"`
  109. SizeName string `description:"目录/文件大小(格式化)"`
  110. ResourceUrl string `description:"文件地址"`
  111. ItemIcon string `description:"图标"`
  112. AdminId int `description:"创建人ID"`
  113. AdminName string `description:"创建人"`
  114. CreateTime string `description:"创建时间"`
  115. ButtonAuth *CloudDiskListButton `description:"按钮权限" gorm:"-"`
  116. }
  117. func (m *CloudDiskListItem) AfterFind(db *gorm.DB) (err error) {
  118. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  119. return
  120. }
  121. // CloudDiskListButton 云盘列表按钮权限
  122. type CloudDiskListButton struct {
  123. RenameAuth bool `description:"重命名文件夹的权限"`
  124. DelAuth bool `description:"删除文件夹的权限"`
  125. }
  126. // CloudDiskMenuResourcePath 云盘目录/文件面包屑
  127. type CloudDiskMenuResourcePath struct {
  128. MenuId int `description:"目录ID"`
  129. MenuName string `description:"目录名称"`
  130. ParentId int `description:"父级目录ID"`
  131. Sort int `description:"排序"`
  132. Selected bool `description:"是否选中"`
  133. }
  134. // 分页查询响应结果
  135. type CloudDiskMenuResourcePageResp struct {
  136. List []*CloudDiskResourcePageItem
  137. Paging *paging.PagingItem `description:"分页数据"`
  138. }
  139. // GetCloudDiskMenuAndResourceList UNION获取云盘列表数据
  140. func GetCloudDiskMenuAndResourceList(menuCond, resourceCond string, menuPars, resourcePars []interface{}, orderRule string) (items []*CloudDiskListItem, err error) {
  141. 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
  142. WHERE 1=1 %s
  143. UNION
  144. 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
  145. WHERE 1=1 %s %s`
  146. order := ` ORDER BY item_type ASC `
  147. if orderRule != "" {
  148. order = ` ORDER BY ` + orderRule
  149. }
  150. sql := fmt.Sprintf(base, menuCond, resourceCond, order)
  151. pars := make([]interface{}, 0)
  152. pars = append(pars, menuPars, resourcePars)
  153. sql = utils.ReplaceDriverKeywords("", sql)
  154. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  155. return
  156. }
  157. // GetSizeTotalByMenuIds 获取目录所属文件总大小
  158. func GetSizeTotalByMenuIds(menuIds []int) (sizeTotal int64, err error) {
  159. if len(menuIds) == 0 {
  160. return
  161. }
  162. sql := `SELECT SUM(size) FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `)`
  163. err = global.DEFAULT_DB.Raw(sql, menuIds).Scan(&sizeTotal).Error
  164. return
  165. }
  166. // CloudDiskBatchDeleteReq 批量删除请求体
  167. type CloudDiskBatchDeleteReq struct {
  168. MenuId int `description:"当前目录ID"`
  169. MenuIds string `description:"目录IDs"`
  170. ResourceIds string `description:"文件IDs"`
  171. }
  172. type CloudDiskBatchMoveReq struct {
  173. KeyWord string `description:"关键字"`
  174. ResourceIds string `description:"文件IDs"`
  175. MenuIds string `description:"当前目录ID"`
  176. MoveAll bool `description:"是否全选"`
  177. TargetMenuId int `description:"移动新目录ID"`
  178. }
  179. // BatchDeleteCloudDiskMenuAndResource 批量删除目录及文件
  180. func BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds []int) (err error) {
  181. menuLens := len(menuIds)
  182. resourceLens := len(resourceIds)
  183. if menuLens == 0 && resourceLens == 0 {
  184. return
  185. }
  186. tx := global.DEFAULT_DB.Begin()
  187. defer func() {
  188. if err != nil {
  189. _ = tx.Rollback()
  190. } else {
  191. _ = tx.Commit()
  192. }
  193. }()
  194. if menuLens > 0 {
  195. sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  196. err = tx.Exec(sql, menuIds).Error
  197. if err != nil {
  198. return
  199. }
  200. sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  201. err = tx.Exec(sql, menuIds).Error
  202. if err != nil {
  203. return
  204. }
  205. }
  206. if resourceLens > 0 {
  207. sql := `DELETE FROM cloud_disk_resource WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)`
  208. err = tx.Exec(sql, resourceIds).Error
  209. if err != nil {
  210. return
  211. }
  212. }
  213. return
  214. }
  215. func GetCloudDiskResourcePageCount(resourceCond string, resourcePars []interface{}) (total int, err error) {
  216. sql := fmt.Sprintf(`SELECT COUNT(1) FROM cloud_disk_resource WHERE 1=1 %s `, resourceCond)
  217. err = global.DEFAULT_DB.Raw(sql, resourcePars...).Scan(&total).Error
  218. return
  219. }
  220. func GetCloudDiskResourcePageList(resourceCond string, resourcePars []interface{}, orderRule string, startSize, pageSize int, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem, err error) {
  221. 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)
  222. var ormList []*CloudDiskResource
  223. resourcePars = append(resourcePars, startSize, pageSize)
  224. err = global.DEFAULT_DB.Raw(sql, resourcePars...).Find(&ormList).Error
  225. if err != nil {
  226. return
  227. }
  228. resourceList = toCloudDiskResourcePageList(ormList, menuNameMap)
  229. return
  230. }
  231. type CloudDiskResourcePageItem struct {
  232. ResourceId int
  233. ResourceName string
  234. ResourceSuffix string
  235. MenuId int
  236. MenuPath string
  237. AdminName string
  238. CreateTime string
  239. Size int64
  240. }
  241. func (this *CloudDiskResource) toPageItem(menuNameMap map[int]string) *CloudDiskResourcePageItem {
  242. return &CloudDiskResourcePageItem{
  243. ResourceId: this.ResourceId,
  244. ResourceName: this.ResourceName,
  245. MenuId: this.MenuId,
  246. ResourceSuffix: this.ResourceSuffix,
  247. MenuPath: menuNameMap[this.MenuId],
  248. AdminName: this.AdminName,
  249. CreateTime: this.CreateTime.Format(utils.FormatDateTime),
  250. Size: this.Size,
  251. }
  252. }
  253. func (this *CloudDiskResource) toItem() *CloudDiskResourcePageItem {
  254. return &CloudDiskResourcePageItem{
  255. ResourceId: this.ResourceId,
  256. ResourceName: this.ResourceName,
  257. MenuId: this.MenuId,
  258. ResourceSuffix: this.ResourceSuffix,
  259. AdminName: this.AdminName,
  260. CreateTime: this.CreateTime.Format(utils.FormatDateTime),
  261. Size: this.Size,
  262. }
  263. }
  264. func toCloudDiskResourcePageList(items []*CloudDiskResource, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem) {
  265. for _, item := range items {
  266. resourceList = append(resourceList, item.toPageItem(menuNameMap))
  267. }
  268. return
  269. }
  270. func toCloudDiskResourceList(items []*CloudDiskResource) (resourceList []*CloudDiskResourcePageItem) {
  271. for _, item := range items {
  272. resourceList = append(resourceList, item.toItem())
  273. }
  274. return
  275. }
  276. func GetCloudDiskResourceList(resourceCond string, resourcePars []interface{}) (resourceList []*CloudDiskResourcePageItem, err error) {
  277. 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)
  278. var ormList []*CloudDiskResource
  279. err = global.DEFAULT_DB.Raw(sql, resourcePars).Find(&ormList).Error
  280. if err != nil {
  281. return
  282. }
  283. resourceList = toCloudDiskResourceList(ormList)
  284. return
  285. }
  286. func BatchMoveCloudDiskResource(menuId int, resourceIds []int) (err error) {
  287. resourceLens := len(resourceIds)
  288. if resourceLens == 0 {
  289. return
  290. }
  291. sql := `UPDATE cloud_disk_resource SET menu_id = ? WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)`
  292. err = global.DEFAULT_DB.Exec(sql, menuId, resourceIds).Error
  293. return
  294. }