cloud_disk_resource.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package models
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "gorm.io/gorm"
  9. "strings"
  10. "time"
  11. )
  12. // 文件格式图标
  13. const (
  14. CloudDiskResourceFileTypeIconMenu = "https://hzstatic.hzinsights.com/static/icon/file_type_menu.png"
  15. CloudDiskResourceFileTypeIconUnknown = "https://hzstatic.hzinsights.com/static/icon/file_type_unknown.png"
  16. )
  17. type CloudDiskResource struct {
  18. ResourceId int `gorm:"column:resource_id;primaryKey;autoIncrement" description:"资源ID"`
  19. MenuId int `description:"目录ID"`
  20. ResourceName string `description:"文件名"`
  21. ResourceSuffix string `description:"文件后缀名"`
  22. ResourceUrl string `description:"文件地址"`
  23. ResourceIcon string `description:"文件类型图标"`
  24. AdminId int `description:"创建人ID"`
  25. AdminName string `description:"创建人名称"`
  26. Size int64 `description:"目录大小"`
  27. CreateTime time.Time `description:"创建时间"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. }
  30. func (m *CloudDiskResource) TableName() string {
  31. return "cloud_disk_resource"
  32. }
  33. func (m *CloudDiskResource) Create() (err error) {
  34. err = global.DEFAULT_DB.Create(m).Error
  35. return
  36. }
  37. func (m *CloudDiskResource) Update(cols []string) (err error) {
  38. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  39. return
  40. }
  41. func (m *CloudDiskResource) Delete() (err error) {
  42. sql := `DELETE FROM cloud_disk_resource WHERE resource_id = ? LIMIT 1`
  43. err = global.DEFAULT_DB.Exec(sql, m.ResourceId).Error
  44. return
  45. }
  46. func (m *CloudDiskResource) GetItemById(id int) (err error) {
  47. sql := fmt.Sprintf(`SELECT * FROM %s WHERE resource_id = ? LIMIT 1`, m.TableName())
  48. err = global.DEFAULT_DB.Raw(sql, id).Find(&m).Error
  49. return
  50. }
  51. func (m *CloudDiskResource) GetItemByCondition(condition string, pars []interface{}) (err error) {
  52. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  53. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&m).Error
  54. return
  55. }
  56. func (m *CloudDiskResource) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*CloudDiskResource, err error) {
  57. fields := strings.Join(fieldArr, ",")
  58. if len(fieldArr) == 0 {
  59. fields = `*`
  60. }
  61. order := ``
  62. if orderRule != "" {
  63. order = ` ORDER BY ` + orderRule
  64. }
  65. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  66. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  67. return
  68. }
  69. // CloudDiskCheckResourceRepeatReq 校验文件重名请求体
  70. type CloudDiskCheckResourceRepeatReq struct {
  71. MenuId int `description:"目录ID"`
  72. FileNames []string `description:"文件名"`
  73. }
  74. // CloudDiskCheckResourceRepeatResp 校验文件重名响应体
  75. type CloudDiskCheckResourceRepeatResp struct {
  76. OriginName string `description:"原文件名(包含后缀名)"`
  77. IsRepeat bool `description:"是否重复"`
  78. AvailableName string `description:"可用名"`
  79. }
  80. // CloudDiskResourceRenameReq 重命名文件请求体
  81. type CloudDiskResourceRenameReq struct {
  82. ResourceId int `description:"文件ID"`
  83. ResourceName string `description:"文件名称"`
  84. }
  85. type CloudDiskResourceModifyReq struct {
  86. ResourceId int `description:"文件ID"`
  87. MenuId int `description:"目录ID"`
  88. ResourceName string `description:"文件名称"`
  89. }
  90. // CloudDiskResourceDeleteReq 删除文件请求体
  91. type CloudDiskResourceDeleteReq struct {
  92. ResourceId int `description:"文件ID"`
  93. }
  94. // CloudDiskListResp 云盘列表响应体
  95. type CloudDiskListResp struct {
  96. List []*CloudDiskListItem `description:"列表数据"`
  97. CreateMenuAuth bool `description:"新建文件夹权限"`
  98. UploadResourceAuth bool `description:"上传文件的权限"`
  99. BatchDelAuth bool `description:"批量删除目录/文件的权限(仅管理员有)"`
  100. ListPath []*CloudDiskMenuResourcePath `description:"面包屑"`
  101. }
  102. // CloudDiskListItem 云盘列表数据
  103. type CloudDiskListItem struct {
  104. ItemId int `description:"ID"`
  105. ItemName string `description:"名称"`
  106. ItemType int `description:"类型: 1-目录; 2-文件"`
  107. ParentId int `description:"父级目录ID"`
  108. ParentName string `description:"父级目录文件名"`
  109. Size int64 `description:"大小"`
  110. SizeName string `description:"目录/文件大小(格式化)"`
  111. ResourceUrl string `description:"文件地址"`
  112. ItemIcon string `description:"图标"`
  113. AdminId int `description:"创建人ID"`
  114. AdminName string `description:"创建人"`
  115. CreateTime string `description:"创建时间"`
  116. ButtonAuth *CloudDiskListButton `description:"按钮权限" gorm:"-"`
  117. }
  118. func (m *CloudDiskListItem) AfterFind(db *gorm.DB) (err error) {
  119. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  120. return
  121. }
  122. // CloudDiskListButton 云盘列表按钮权限
  123. type CloudDiskListButton struct {
  124. RenameAuth bool `description:"重命名文件夹的权限"`
  125. DelAuth bool `description:"删除文件夹的权限"`
  126. }
  127. // CloudDiskMenuResourcePath 云盘目录/文件面包屑
  128. type CloudDiskMenuResourcePath struct {
  129. MenuId int `description:"目录ID"`
  130. MenuName string `description:"目录名称"`
  131. ParentId int `description:"父级目录ID"`
  132. Sort int `description:"排序"`
  133. Selected bool `description:"是否选中"`
  134. }
  135. // 分页查询响应结果
  136. type CloudDiskMenuResourcePageResp struct {
  137. List []*CloudDiskResourcePageItem
  138. Paging *paging.PagingItem `description:"分页数据"`
  139. }
  140. // GetCloudDiskMenuAndResourceList UNION获取云盘列表数据
  141. func GetCloudDiskMenuAndResourceList(menuCond, resourceCond string, menuPars, resourcePars []interface{}, orderRule string) (items []*CloudDiskListItem, err error) {
  142. 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
  143. WHERE 1=1 %s
  144. UNION
  145. 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
  146. WHERE 1=1 %s %s`
  147. order := ` ORDER BY item_type ASC `
  148. if orderRule != "" {
  149. order = ` ORDER BY ` + orderRule
  150. }
  151. sql := fmt.Sprintf(base, menuCond, resourceCond, order)
  152. pars := make([]interface{}, 0)
  153. pars = append(pars, menuPars, resourcePars)
  154. sql = utils.ReplaceDriverKeywords("", sql)
  155. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  156. return
  157. }
  158. // GetSizeTotalByMenuIds 获取目录所属文件总大小
  159. func GetSizeTotalByMenuIds(menuIds []int) (sizeTotal int64, err error) {
  160. if len(menuIds) == 0 {
  161. return
  162. }
  163. sql := `SELECT SUM(size) FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `)`
  164. err = global.DEFAULT_DB.Raw(sql, menuIds).Scan(&sizeTotal).Error
  165. return
  166. }
  167. // CloudDiskBatchDeleteReq 批量删除请求体
  168. type CloudDiskBatchDeleteReq struct {
  169. MenuId int `description:"当前目录ID"`
  170. MenuIds string `description:"目录IDs"`
  171. ResourceIds string `description:"文件IDs"`
  172. }
  173. type CloudDiskBatchMoveReq struct {
  174. KeyWord string `description:"关键字"`
  175. ResourceIds string `description:"文件IDs"`
  176. MenuIds string `description:"当前目录ID"`
  177. MoveAll bool `description:"是否全选"`
  178. TargetMenuId int `description:"移动新目录ID"`
  179. }
  180. // BatchDeleteCloudDiskMenuAndResource 批量删除目录及文件
  181. func BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds []int) (err error) {
  182. menuLens := len(menuIds)
  183. resourceLens := len(resourceIds)
  184. if menuLens == 0 && resourceLens == 0 {
  185. return
  186. }
  187. tx := global.DEFAULT_DB.Begin()
  188. defer func() {
  189. if err != nil {
  190. _ = tx.Rollback()
  191. } else {
  192. _ = tx.Commit()
  193. }
  194. }()
  195. if menuLens > 0 {
  196. sql := `DELETE FROM cloud_disk_menu WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  197. err = tx.Exec(sql, menuIds).Error
  198. if err != nil {
  199. return
  200. }
  201. sql = `DELETE FROM cloud_disk_resource WHERE menu_id IN (` + utils.GetOrmInReplace(menuLens) + `)`
  202. err = tx.Exec(sql, menuIds).Error
  203. if err != nil {
  204. return
  205. }
  206. }
  207. if resourceLens > 0 {
  208. sql := `DELETE FROM cloud_disk_resource WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)`
  209. err = tx.Exec(sql, resourceIds).Error
  210. if err != nil {
  211. return
  212. }
  213. }
  214. return
  215. }
  216. func GetCloudDiskResourcePageCount(resourceCond string, resourcePars []interface{}) (total int, err error) {
  217. sql := fmt.Sprintf(`SELECT COUNT(1) FROM cloud_disk_resource WHERE 1=1 %s `, resourceCond)
  218. err = global.DEFAULT_DB.Raw(sql, resourcePars...).Scan(&total).Error
  219. return
  220. }
  221. func GetCloudDiskResourcePageList(resourceCond string, resourcePars []interface{}, orderRule string, startSize, pageSize int, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem, err error) {
  222. 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)
  223. var ormList []*CloudDiskResource
  224. resourcePars = append(resourcePars, startSize, pageSize)
  225. err = global.DEFAULT_DB.Raw(sql, resourcePars...).Find(&ormList).Error
  226. if err != nil {
  227. return
  228. }
  229. resourceList = toCloudDiskResourcePageList(ormList, menuNameMap)
  230. return
  231. }
  232. type CloudDiskResourcePageItem struct {
  233. ResourceId int
  234. ResourceName string
  235. ResourceSuffix string
  236. MenuId int
  237. MenuPath string
  238. AdminName string
  239. CreateTime string
  240. Size int64
  241. }
  242. func (this *CloudDiskResource) toPageItem(menuNameMap map[int]string) *CloudDiskResourcePageItem {
  243. return &CloudDiskResourcePageItem{
  244. ResourceId: this.ResourceId,
  245. ResourceName: this.ResourceName,
  246. MenuId: this.MenuId,
  247. ResourceSuffix: this.ResourceSuffix,
  248. MenuPath: menuNameMap[this.MenuId],
  249. AdminName: this.AdminName,
  250. CreateTime: this.CreateTime.Format(utils.FormatDateTime),
  251. Size: this.Size,
  252. }
  253. }
  254. func (this *CloudDiskResource) toItem() *CloudDiskResourcePageItem {
  255. return &CloudDiskResourcePageItem{
  256. ResourceId: this.ResourceId,
  257. ResourceName: this.ResourceName,
  258. MenuId: this.MenuId,
  259. ResourceSuffix: this.ResourceSuffix,
  260. AdminName: this.AdminName,
  261. CreateTime: this.CreateTime.Format(utils.FormatDateTime),
  262. Size: this.Size,
  263. }
  264. }
  265. func toCloudDiskResourcePageList(items []*CloudDiskResource, menuNameMap map[int]string) (resourceList []*CloudDiskResourcePageItem) {
  266. for _, item := range items {
  267. resourceList = append(resourceList, item.toPageItem(menuNameMap))
  268. }
  269. return
  270. }
  271. func toCloudDiskResourceList(items []*CloudDiskResource) (resourceList []*CloudDiskResourcePageItem) {
  272. for _, item := range items {
  273. resourceList = append(resourceList, item.toItem())
  274. }
  275. return
  276. }
  277. func GetCloudDiskResourceList(resourceCond string, resourcePars []interface{}) (resourceList []*CloudDiskResourcePageItem, err error) {
  278. //o := orm.NewOrm()
  279. 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)
  280. var ormList []*CloudDiskResource
  281. err = global.DEFAULT_DB.Raw(sql, resourcePars).Find(&ormList).Error
  282. if err != nil {
  283. return
  284. }
  285. resourceList = toCloudDiskResourceList(ormList)
  286. return
  287. }
  288. func BatchMoveCloudDiskResource(menuId int, resourceIds []int) (err error) {
  289. resourceLens := len(resourceIds)
  290. if resourceLens == 0 {
  291. return
  292. }
  293. sql := `UPDATE cloud_disk_resource SET menu_id = ? WHERE resource_id IN (` + utils.GetOrmInReplace(resourceLens) + `)`
  294. err = global.DEFAULT_DB.Exec(sql, menuId, resourceIds).Error
  295. return
  296. }