m_file_chunk.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package model
  2. import (
  3. "context"
  4. "gorm.io/gorm"
  5. "gxt-file-server/app/errors"
  6. "gxt-file-server/app/model/entity"
  7. "gxt-file-server/app/schema"
  8. "time"
  9. )
  10. // NewFileChunk 创建demo存储实例
  11. func NewFileChunk(db *gorm.DB) *FileChunk {
  12. return &FileChunk{db}
  13. }
  14. // FileChunk demo存储
  15. type FileChunk struct {
  16. db *gorm.DB
  17. }
  18. func (a *FileChunk) getQueryOption(opts ...schema.FileChunkQueryOptions) schema.FileChunkQueryOptions {
  19. var opt schema.FileChunkQueryOptions
  20. if len(opts) > 0 {
  21. opt = opts[0]
  22. }
  23. return opt
  24. }
  25. // Query 查询数据
  26. func (a *FileChunk) Query(ctx context.Context, params schema.FileChunkQueryParam, opts ...schema.FileChunkQueryOptions) (*schema.FileChunkQueryResult, error) {
  27. db := entity.GetFileChunkDB(ctx, a.db)
  28. if v := params.Hash; v != "" {
  29. db = db.Where("hash = ?", v)
  30. }
  31. if v := params.Current; v > 0 {
  32. db = db.Where("current = ?", v)
  33. }
  34. if v := params.IsClear; v {
  35. //精确到秒 已备定时扫表的定时任务为秒/分钟
  36. timeNowString := time.Now().Format("2006-01-02 15:04:05")
  37. db = db.Where("past_time < ?", timeNowString)
  38. }
  39. //按照块的顺序排序 不可变更 !!! guo
  40. db = db.Order("current ASC")
  41. opt := a.getQueryOption(opts...)
  42. var list entity.FileChunks
  43. pr, err := WrapPageQuery(ctx, db, opt.PageParam, &list)
  44. if err != nil {
  45. return nil, errors.ErrDBServerInternalError
  46. }
  47. qr := &schema.FileChunkQueryResult{
  48. PageResult: pr,
  49. Data: list.ToSchemaFileChunks(),
  50. }
  51. return qr, nil
  52. }
  53. // Get 查询指定数据
  54. func (a *FileChunk) Get(ctx context.Context, recordID string, opts ...schema.FileChunkQueryOptions) (*schema.FileChunk, error) {
  55. db := entity.GetFileChunkDB(ctx, a.db).Where("record_id=?", recordID)
  56. var item entity.FileChunk
  57. ok, err := FindOne(ctx, db, &item)
  58. if err != nil {
  59. return nil, errors.ErrDBServerInternalError
  60. } else if !ok {
  61. return nil, nil
  62. }
  63. return item.ToSchemaFileChunk(), nil
  64. }
  65. // Create 创建数据
  66. func (a *FileChunk) Create(ctx context.Context, item schema.FileChunk) error {
  67. demo := entity.SchemaFileChunk(item).ToFileChunk()
  68. result := entity.GetFileChunkDB(ctx, a.db).Create(demo)
  69. if err := result.Error; err != nil {
  70. return errors.ErrDBServerInternalError
  71. }
  72. return nil
  73. }
  74. // Update 更新数据
  75. func (a *FileChunk) Update(ctx context.Context, recordID string, item schema.FileChunk) error {
  76. demo := entity.SchemaFileChunk(item).ToFileChunk()
  77. result := entity.GetFileChunkDB(ctx, a.db).Where("record_id=?", recordID).Omit("record_id", "creator").Updates(demo)
  78. if err := result.Error; err != nil {
  79. return errors.ErrDBServerInternalError
  80. }
  81. return nil
  82. }
  83. // Delete 真实删除数据
  84. func (a *FileChunk) Delete(ctx context.Context, recordID string) error {
  85. result := entity.GetFileChunkDB(ctx, a.db).Where("record_id=?", recordID).Unscoped().Delete(entity.FileChunk{})
  86. if err := result.Error; err != nil {
  87. return errors.ErrDBServerInternalError
  88. }
  89. return nil
  90. }
  91. // DeleteHash 根据HASH真实删除数据
  92. func (a *FileChunk) DeleteHash(ctx context.Context, hash string) error {
  93. result := entity.GetFileChunkDB(ctx, a.db).Where("hash=?", hash).Unscoped().Delete(entity.FileChunk{})
  94. if err := result.Error; err != nil {
  95. return errors.ErrDBServerInternalError
  96. }
  97. return nil
  98. }
  99. // UpdateStatus 更新状态
  100. func (a *FileChunk) UpdateStatus(ctx context.Context, recordID string, status int) error {
  101. result := entity.GetFileChunkDB(ctx, a.db).Where("record_id=?", recordID).Update("status", status)
  102. if err := result.Error; err != nil {
  103. return errors.ErrDBServerInternalError
  104. }
  105. return nil
  106. }