extra_func.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "hash/fnv"
  6. "reflect"
  7. "go-common/app/admin/main/aegis/model/common"
  8. "go-common/library/log"
  9. )
  10. type multransFunc func(context.Context, []int64) (map[int64][]interface{}, error)
  11. /* 批量查询,批量转换
  12. * list []*struct{}
  13. * multrans 转化器,根据ID查出其他值
  14. * ID id字段名称,id字段类型必须是int64
  15. * Names 查出来的各个字段名称
  16. */
  17. func (s *Service) mulIDtoName(c context.Context, list interface{}, multrans multransFunc, ID string, Names ...string) (err error) {
  18. var (
  19. lV, itemI, itemIE, idFiled, nameFiled, valueField reflect.Value
  20. id int64
  21. ids []int64
  22. hashIDName = make(map[int64][]interface{})
  23. )
  24. if lV = reflect.ValueOf(list); !lV.IsValid() || lV.IsNil() || lV.Kind() != reflect.Slice {
  25. return fmt.Errorf("invalid list")
  26. }
  27. count := lV.Len()
  28. for i := 0; i < count; i++ {
  29. if itemI = lV.Index(i); !itemI.IsValid() || itemI.IsNil() || itemI.Kind() != reflect.Ptr {
  30. return fmt.Errorf("invalid itemI")
  31. }
  32. if itemIE = itemI.Elem(); !itemIE.IsValid() || itemIE.Kind() != reflect.Struct {
  33. return fmt.Errorf("invalid itemIE")
  34. }
  35. if idFiled = itemIE.FieldByName(ID); !idFiled.IsValid() || idFiled.Kind() != reflect.Int64 {
  36. return fmt.Errorf("invalid idFiled")
  37. }
  38. for _, name := range Names {
  39. if nameFiled = itemIE.FieldByName(name); !nameFiled.IsValid() || !nameFiled.CanSet() {
  40. return fmt.Errorf("invalid nameFiled")
  41. }
  42. }
  43. if id = idFiled.Int(); id != 0 {
  44. if _, ok := hashIDName[id]; !ok {
  45. hashIDName[id] = []interface{}{}
  46. ids = append(ids, id)
  47. }
  48. }
  49. }
  50. if hashIDName, err = multrans(c, ids); err != nil {
  51. log.Error("multrans error(%v)", ids)
  52. return
  53. }
  54. for i := 0; i < count; i++ {
  55. itemIE = lV.Index(i).Elem()
  56. id = itemIE.FieldByName(ID).Int()
  57. if names, ok := hashIDName[id]; ok && len(names) == len(Names) {
  58. for i, name := range names {
  59. nameFiled = itemIE.FieldByName(Names[i])
  60. valueField = reflect.ValueOf(name)
  61. if nameFiled.Kind() != valueField.Kind() {
  62. log.Error("multrans(%s) return %v while need %v", name, valueField.Kind(), nameFiled.Kind())
  63. continue
  64. }
  65. itemIE.FieldByName(Names[i]).Set(reflect.ValueOf(name))
  66. }
  67. }
  68. }
  69. return
  70. }
  71. func (s *Service) getUserGroup(c context.Context, ids []int64) (group map[int64]*common.Group) {
  72. group = make(map[int64]*common.Group)
  73. for _, id := range ids {
  74. group[id] = s.groupCache[id]
  75. }
  76. return
  77. }
  78. func mergeSlice(arr1 []int64, arr2 []int64) (arr []int64) {
  79. for _, id1 := range arr1 {
  80. for _, id2 := range arr2 {
  81. if id1 == id2 {
  82. arr = append(arr, id1)
  83. }
  84. }
  85. }
  86. return
  87. }
  88. func fnvhash32(s string) uint32 {
  89. h := fnv.New32a()
  90. h.Write([]byte(s))
  91. return h.Sum32()
  92. }
  93. func joinstr(maphash []string, sep string, max int) (msgs []string) {
  94. var msg string
  95. for _, v := range maphash {
  96. if len(msg) == 0 {
  97. msg = v
  98. } else if len(msg)+len(v) < max {
  99. msg += sep + v
  100. } else {
  101. msgs = append(msgs, msg)
  102. msg = v
  103. }
  104. }
  105. msgs = append(msgs, msg)
  106. return
  107. }
  108. //stringset 过滤重复字符串
  109. func stringset(arr []string) (res []string) {
  110. mapHash := make(map[uint32]struct{})
  111. for _, item := range arr {
  112. hf := fnvhash32(item)
  113. if _, ok := mapHash[hf]; ok {
  114. continue
  115. }
  116. mapHash[hf] = struct{}{}
  117. res = append(res, item)
  118. }
  119. return
  120. }