base.go 5.1 KB

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