base.go 6.5 KB

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