batchDownloadRepo.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "fwh-toolbox-server/cache"
  6. "fwh-toolbox-server/domain"
  7. "strconv"
  8. "time"
  9. "go.uber.org/zap"
  10. )
  11. var (
  12. bachDownloadCtx = context.Background()
  13. bachDownloadKeyPrefix = "fwhtoolbox:batchDownload:"
  14. callFileKeyPrefix = bachDownloadKeyPrefix + "callFile:"
  15. )
  16. func CreateCallFileTaskList(id string, list map[string]domain.BatchDownloadFileTaskInfo) error {
  17. // 保存数据
  18. var keyList []string
  19. for key, val := range list {
  20. rdsKey := callFileKeyPrefix + id + ":" + key
  21. exp := time.Now().Add(7 * 24 * time.Hour)
  22. fields := map[string]interface{}{
  23. "sn": val.Sn,
  24. "date": val.Date,
  25. "fileType": val.FileType,
  26. "fileSubType": val.FileSupType,
  27. "reason": val.Reason,
  28. }
  29. if err := cache.HSet(bachDownloadCtx, rdsKey, fields); err != nil {
  30. return err
  31. }
  32. _, err := cache.ExpireAt(bachDownloadCtx, rdsKey, exp)
  33. if err != nil {
  34. return err
  35. }
  36. keyList = append(keyList, key)
  37. }
  38. // 保存key list到standby列表
  39. rdsStandbyKey := callFileKeyPrefix + id + ":standby"
  40. members := make([]interface{}, len(keyList))
  41. for i, v := range keyList {
  42. members[i] = v
  43. }
  44. if err := cache.SAdd(bachDownloadCtx, rdsStandbyKey, members); err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func getCallFileTaskList(rdsKey string, listName string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
  50. // 提取key list
  51. subKeySet, err := cache.SMembers(bachDownloadCtx, rdsKey+":"+listName)
  52. if err != nil {
  53. return nil, err
  54. }
  55. // 提取数据
  56. dataList := map[string]domain.BatchDownloadFileTaskInfo{}
  57. for _, key := range subKeySet {
  58. subRdsKey := rdsKey + ":" + key
  59. res, err := cache.HGetAll(bachDownloadCtx, subRdsKey)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if len(res) == 0 {
  64. return nil, fmt.Errorf("key:%s - No Data", subRdsKey)
  65. }
  66. devTypeStr := res["devType"]
  67. var devType int
  68. if devTypeStr != "" {
  69. devType, err = strconv.Atoi(devTypeStr)
  70. if err != nil {
  71. return nil, err
  72. }
  73. }
  74. sn := res["sn"]
  75. date := res["date"]
  76. fileType := res["fileType"]
  77. fileSubType := res["fileSubType"]
  78. resultStr := res["result"]
  79. reason := res["reason"]
  80. var result int
  81. if resultStr != "" {
  82. result, err = strconv.Atoi(resultStr)
  83. if err != nil {
  84. return nil, err
  85. }
  86. }
  87. dataList[key] = domain.BatchDownloadFileTaskInfo{
  88. DevType: devType,
  89. Sn: sn,
  90. Date: date,
  91. FileType: fileType,
  92. FileSupType: fileSubType,
  93. Result: result,
  94. Reason: reason,
  95. }
  96. }
  97. return dataList, nil
  98. }
  99. func GetCallFileTaskStandbyList(id string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
  100. // 提取key list
  101. rdsKey := callFileKeyPrefix + id
  102. return getCallFileTaskList(rdsKey, "standby")
  103. }
  104. func GetCallFileTaskFailedList(id string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
  105. // 提取key list
  106. rdsKey := callFileKeyPrefix + id
  107. return getCallFileTaskList(rdsKey, "failed")
  108. }
  109. func DeleteCallFileTaskList(id string) {
  110. // 提取failed key list
  111. rdsKey := callFileKeyPrefix + id + ":failed"
  112. subKeySet, err := cache.SMembers(bachDownloadCtx, rdsKey)
  113. if err != nil {
  114. zap.L().Error("[DeleteBatchDownloadTaskList]回去key list失败", zap.String("key", rdsKey), zap.String("err", err.Error()))
  115. return
  116. }
  117. for _, key := range subKeySet {
  118. if err := cache.Delete(bachDownloadCtx, key); err != nil {
  119. zap.L().Error("DeleteBatchDownloadTaskList失败", zap.String("key", key), zap.String("err", err.Error()))
  120. }
  121. }
  122. cache.Delete(bachDownloadCtx, rdsKey)
  123. }
  124. // result 1-成功 -1-失败
  125. func UpdateCallFileResult(id string, taskId string, result int, reason string) error {
  126. rdsKey := callFileKeyPrefix + id
  127. subRdsKey := rdsKey + ":" + taskId
  128. rdsStandbyKey := rdsKey + ":standby"
  129. rdsFailedKey := rdsKey + ":failed"
  130. exists, err := cache.SIsMember(bachDownloadCtx, rdsStandbyKey, taskId)
  131. if err != nil {
  132. return err
  133. }
  134. if !exists {
  135. return fmt.Errorf("[UpdateBatchDownloadResult]%s中不存在%s", rdsStandbyKey, taskId)
  136. }
  137. // 如果成功,则删除list, 否则转移至failed list
  138. if result == 1 {
  139. if err := cache.Delete(bachDownloadCtx, subRdsKey); err != nil {
  140. zap.L().Error("[UpdateCallFileResult] 删除成功list失败", zap.String("key", subRdsKey), zap.String("err", err.Error()))
  141. return err
  142. }
  143. } else {
  144. // 添加到Failed key
  145. if err := cache.SAdd(bachDownloadCtx, rdsFailedKey, taskId); err != nil {
  146. zap.L().Error("[UpdateCallFileResult] 添加failed key失败", zap.String("key", rdsFailedKey), zap.String("err", err.Error()))
  147. return err
  148. }
  149. if err := cache.HSet(bachDownloadCtx, subRdsKey,
  150. "result", result,
  151. "reason", reason,
  152. ); err != nil {
  153. return err
  154. }
  155. }
  156. // 删除Standby key
  157. if err := cache.SRem(bachDownloadCtx, rdsStandbyKey, taskId); err != nil {
  158. zap.L().Error("[UpdateCallFileResult] 删除成功key失败", zap.String("key", rdsKey), zap.String("err", err.Error()))
  159. return err
  160. }
  161. return nil
  162. }