base.go 4.4 KB

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