base.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "reflect"
  7. "dashoo.cn/common_definition/comm_def"
  8. "dashoo.cn/opms_libary/micro_srv"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/os/gtime"
  11. )
  12. var (
  13. // DingTalkSpaceId 钉钉 空间Id。
  14. DingTalkSpaceId = "21077726250"
  15. // CommonUpdateFieldEx UpdateFieldEx 更新过滤字段
  16. CommonUpdateFieldEx = []interface{}{"created_by", "created_name", "created_time"}
  17. UpdateFieldEx = []interface{}{"id", "created_by", "created_name", "created_time"}
  18. )
  19. func Sequence(db gdb.DB, name string) (string, error) {
  20. v, err := db.GetValue("select `nextval`( ? );", name)
  21. if err != nil {
  22. return "", err
  23. }
  24. return v.String(), nil
  25. }
  26. // SetCreatedInfo 插入数据库时设置创建信息
  27. func SetCreatedInfo(entry interface{}, id int, name string) {
  28. v := reflect.ValueOf(entry)
  29. t := reflect.TypeOf(entry)
  30. if t.Kind() == reflect.Map {
  31. data := entry.(map[string]interface{})
  32. data["created_by"] = id
  33. data["created_name"] = name
  34. data["created_time"] = gtime.Now()
  35. return
  36. }
  37. if t.Kind() == reflect.Ptr {
  38. t = t.Elem()
  39. v = v.Elem()
  40. }
  41. if t.Kind() == reflect.Slice {
  42. }
  43. if t.Kind() != reflect.Struct {
  44. log.Println("Check type error not Struct")
  45. return
  46. }
  47. for i := 0; i < t.NumField(); i++ {
  48. fieldName := t.Field(i).Name
  49. if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
  50. switch tag {
  51. case "created_by":
  52. v.FieldByName(fieldName).Set(reflect.ValueOf(id))
  53. case "created_name":
  54. v.FieldByName(fieldName).Set(reflect.ValueOf(name))
  55. case "created_time":
  56. v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
  57. }
  58. }
  59. }
  60. }
  61. // SetUpdatedInfo 插入数据库时设置修改信息
  62. func SetUpdatedInfo(entry interface{}, id int, name string) {
  63. v := reflect.ValueOf(entry)
  64. t := reflect.TypeOf(entry)
  65. if t.Kind() == reflect.Map {
  66. data := entry.(map[string]interface{})
  67. data["updated_by"] = id
  68. data["updated_name"] = name
  69. data["updated_time"] = gtime.Now()
  70. return
  71. }
  72. if t.Kind() == reflect.Ptr {
  73. t = t.Elem()
  74. v = v.Elem()
  75. }
  76. if t.Kind() != reflect.Struct {
  77. log.Println("Check type error not Struct")
  78. return
  79. }
  80. for i := 0; i < t.NumField(); i++ {
  81. fieldName := t.Field(i).Name
  82. if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
  83. switch tag {
  84. case "updated_by":
  85. v.FieldByName(fieldName).Set(reflect.ValueOf(id))
  86. case "updated_name":
  87. v.FieldByName(fieldName).Set(reflect.ValueOf(name))
  88. case "updated_time":
  89. v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
  90. }
  91. }
  92. }
  93. }
  94. // Div 数字转字母
  95. func Div(Num int) string {
  96. var (
  97. Str string = ""
  98. k int
  99. temp []int //保存转化后每一位数据的值,然后通过索引的方式匹配A-Z
  100. )
  101. //用来匹配的字符A-Z
  102. Slice := []string{"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
  103. "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
  104. if Num > 26 { //数据大于26需要进行拆分
  105. for {
  106. k = Num % 26 //从个位开始拆分,如果求余为0,说明末尾为26,也就是Z,如果是转化为26进制数,则末尾是可以为0的,这里必须为A-Z中的一个
  107. if k == 0 {
  108. temp = append(temp, 26)
  109. k = 26
  110. } else {
  111. temp = append(temp, k)
  112. }
  113. Num = (Num - k) / 26 //减去Num最后一位数的值,因为已经记录在temp中
  114. if Num <= 26 { //小于等于26直接进行匹配,不需要进行数据拆分
  115. temp = append(temp, Num)
  116. break
  117. }
  118. }
  119. } else {
  120. return Slice[Num]
  121. }
  122. for _, value := range temp {
  123. Str = Slice[value] + Str //因为数据切分后存储顺序是反的,所以Str要放在后面
  124. }
  125. return Str
  126. }
  127. type GetDictReq struct {
  128. DictType string `p:"dictType" v:"required#字典类型不能为空"`
  129. DefaultValue string `p:"defaultValue"`
  130. }
  131. func GetDictDataByType(ctx context.Context, typ string) (map[string]string, error) {
  132. srv := micro_srv.InitMicroSrvClient("Dict", "micro_srv.auth")
  133. defer srv.Close()
  134. resp := &comm_def.CommonMsg{}
  135. err := srv.Call(ctx, "GetDictDataByType", GetDictReq{
  136. DictType: typ,
  137. }, resp)
  138. if err != nil {
  139. return nil, fmt.Errorf("获取字典 %s %s", typ, err.Error())
  140. }
  141. fmt.Println(resp.Data)
  142. data := resp.Data.(map[string]interface{})["Values"].([]interface{})
  143. fmt.Println(data)
  144. res := map[string]string{}
  145. for _, i := range data {
  146. info := i.(map[string]interface{})
  147. res[info["DictValue"].(string)] = info["DictLabel"].(string)
  148. }
  149. return res, nil
  150. }
  151. func StringSlicecontains(s []string, ele string) bool {
  152. for _, i := range s {
  153. if i == ele {
  154. return true
  155. }
  156. }
  157. return false
  158. }