| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package redis
- import (
- "context"
- "fmt"
- "fwh-toolbox-server/cache"
- "fwh-toolbox-server/domain"
- "strconv"
- "time"
- "go.uber.org/zap"
- )
- var (
- bachDownloadCtx = context.Background()
- bachDownloadKeyPrefix = "fwhtoolbox:batchDownload:"
- callFileKeyPrefix = bachDownloadKeyPrefix + "callFile:"
- )
- func CreateCallFileTaskList(id string, list map[string]domain.BatchDownloadFileTaskInfo) error {
- // 保存数据
- var keyList []string
- for key, val := range list {
- rdsKey := callFileKeyPrefix + id + ":" + key
- exp := time.Now().Add(7 * 24 * time.Hour)
- fields := map[string]interface{}{
- "sn": val.Sn,
- "date": val.Date,
- "fileType": val.FileType,
- "fileSubType": val.FileSupType,
- "reason": val.Reason,
- }
- if err := cache.HSet(bachDownloadCtx, rdsKey, fields); err != nil {
- return err
- }
- _, err := cache.ExpireAt(bachDownloadCtx, rdsKey, exp)
- if err != nil {
- return err
- }
- keyList = append(keyList, key)
- }
- // 保存key list到standby列表
- rdsStandbyKey := callFileKeyPrefix + id + ":standby"
- members := make([]interface{}, len(keyList))
- for i, v := range keyList {
- members[i] = v
- }
- if err := cache.SAdd(bachDownloadCtx, rdsStandbyKey, members); err != nil {
- return err
- }
- return nil
- }
- func getCallFileTaskList(rdsKey string, listName string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
- // 提取key list
- subKeySet, err := cache.SMembers(bachDownloadCtx, rdsKey+":"+listName)
- if err != nil {
- return nil, err
- }
- // 提取数据
- dataList := map[string]domain.BatchDownloadFileTaskInfo{}
- for _, key := range subKeySet {
- subRdsKey := rdsKey + ":" + key
- res, err := cache.HGetAll(bachDownloadCtx, subRdsKey)
- if err != nil {
- return nil, err
- }
- if len(res) == 0 {
- return nil, fmt.Errorf("key:%s - No Data", subRdsKey)
- }
- devTypeStr := res["devType"]
- var devType int
- if devTypeStr != "" {
- devType, err = strconv.Atoi(devTypeStr)
- if err != nil {
- return nil, err
- }
- }
- sn := res["sn"]
- date := res["date"]
- fileType := res["fileType"]
- fileSubType := res["fileSubType"]
- resultStr := res["result"]
- reason := res["reason"]
- var result int
- if resultStr != "" {
- result, err = strconv.Atoi(resultStr)
- if err != nil {
- return nil, err
- }
- }
- dataList[key] = domain.BatchDownloadFileTaskInfo{
- DevType: devType,
- Sn: sn,
- Date: date,
- FileType: fileType,
- FileSupType: fileSubType,
- Result: result,
- Reason: reason,
- }
- }
- return dataList, nil
- }
- func GetCallFileTaskStandbyList(id string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
- // 提取key list
- rdsKey := callFileKeyPrefix + id
- return getCallFileTaskList(rdsKey, "standby")
- }
- func GetCallFileTaskFailedList(id string) (map[string]domain.BatchDownloadFileTaskInfo, error) {
- // 提取key list
- rdsKey := callFileKeyPrefix + id
- return getCallFileTaskList(rdsKey, "failed")
- }
- func DeleteCallFileTaskList(id string) {
- // 提取failed key list
- rdsKey := callFileKeyPrefix + id + ":failed"
- subKeySet, err := cache.SMembers(bachDownloadCtx, rdsKey)
- if err != nil {
- zap.L().Error("[DeleteBatchDownloadTaskList]回去key list失败", zap.String("key", rdsKey), zap.String("err", err.Error()))
- return
- }
- for _, key := range subKeySet {
- if err := cache.Delete(bachDownloadCtx, key); err != nil {
- zap.L().Error("DeleteBatchDownloadTaskList失败", zap.String("key", key), zap.String("err", err.Error()))
- }
- }
- cache.Delete(bachDownloadCtx, rdsKey)
- }
- // result 1-成功 -1-失败
- func UpdateCallFileResult(id string, taskId string, result int, reason string) error {
- rdsKey := callFileKeyPrefix + id
- subRdsKey := rdsKey + ":" + taskId
- rdsStandbyKey := rdsKey + ":standby"
- rdsFailedKey := rdsKey + ":failed"
- exists, err := cache.SIsMember(bachDownloadCtx, rdsStandbyKey, taskId)
- if err != nil {
- return err
- }
- if !exists {
- return fmt.Errorf("[UpdateBatchDownloadResult]%s中不存在%s", rdsStandbyKey, taskId)
- }
- // 如果成功,则删除list, 否则转移至failed list
- if result == 1 {
- if err := cache.Delete(bachDownloadCtx, subRdsKey); err != nil {
- zap.L().Error("[UpdateCallFileResult] 删除成功list失败", zap.String("key", subRdsKey), zap.String("err", err.Error()))
- return err
- }
- } else {
- // 添加到Failed key
- if err := cache.SAdd(bachDownloadCtx, rdsFailedKey, taskId); err != nil {
- zap.L().Error("[UpdateCallFileResult] 添加failed key失败", zap.String("key", rdsFailedKey), zap.String("err", err.Error()))
- return err
- }
- if err := cache.HSet(bachDownloadCtx, subRdsKey,
- "result", result,
- "reason", reason,
- ); err != nil {
- return err
- }
- }
- // 删除Standby key
- if err := cache.SRem(bachDownloadCtx, rdsStandbyKey, taskId); err != nil {
- zap.L().Error("[UpdateCallFileResult] 删除成功key失败", zap.String("key", rdsKey), zap.String("err", err.Error()))
- return err
- }
- return nil
- }
|