| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package service
- import (
- "context"
- "dashoo.cn/opms_libary/myerrors"
- "fmt"
- "github.com/gogf/gf/frame/g"
- "github.com/smallnest/rpcx/share"
- "log"
- "reflect"
- "dashoo.cn/common_definition/comm_def"
- "dashoo.cn/opms_libary/micro_srv"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/gtime"
- )
- var (
- // DingTalkSpaceId 钉钉 空间Id。
- DingTalkSpaceId = "21077726250"
- // CommonUpdateFieldEx UpdateFieldEx 更新过滤字段
- CommonUpdateFieldEx = []interface{}{"created_by", "created_name", "created_time"}
- UpdateFieldEx = []interface{}{"id", "created_by", "created_name", "created_time"}
- )
- func Sequence(db gdb.DB, name string) (string, error) {
- v, err := db.GetValue("select `nextval`( ? );", name)
- if err != nil {
- return "", err
- }
- return v.String(), nil
- }
- // SetCreatedInfo 插入数据库时设置创建信息
- func SetCreatedInfo(entry interface{}, id int, name string) {
- v := reflect.ValueOf(entry)
- t := reflect.TypeOf(entry)
- if t.Kind() == reflect.Map {
- data := entry.(map[string]interface{})
- data["created_by"] = id
- data["created_name"] = name
- data["created_time"] = gtime.Now()
- return
- }
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- v = v.Elem()
- }
- if t.Kind() == reflect.Slice {
- }
- if t.Kind() != reflect.Struct {
- log.Println("Check type error not Struct")
- return
- }
- for i := 0; i < t.NumField(); i++ {
- fieldName := t.Field(i).Name
- if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
- switch tag {
- case "created_by":
- v.FieldByName(fieldName).Set(reflect.ValueOf(id))
- case "created_name":
- v.FieldByName(fieldName).Set(reflect.ValueOf(name))
- case "created_time":
- v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
- }
- }
- }
- }
- // SetUpdatedInfo 插入数据库时设置修改信息
- func SetUpdatedInfo(entry interface{}, id int, name string) {
- v := reflect.ValueOf(entry)
- t := reflect.TypeOf(entry)
- if t.Kind() == reflect.Map {
- data := entry.(map[string]interface{})
- data["updated_by"] = id
- data["updated_name"] = name
- data["updated_time"] = gtime.Now()
- return
- }
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- v = v.Elem()
- }
- if t.Kind() != reflect.Struct {
- log.Println("Check type error not Struct")
- return
- }
- for i := 0; i < t.NumField(); i++ {
- fieldName := t.Field(i).Name
- if tag, ok := t.Field(i).Tag.Lookup("orm"); ok {
- switch tag {
- case "updated_by":
- v.FieldByName(fieldName).Set(reflect.ValueOf(id))
- case "updated_name":
- v.FieldByName(fieldName).Set(reflect.ValueOf(name))
- case "updated_time":
- v.FieldByName(fieldName).Set(reflect.ValueOf(gtime.Now()))
- }
- }
- }
- }
- // Div 数字转字母
- func Div(Num int) string {
- var (
- Str string = ""
- k int
- temp []int //保存转化后每一位数据的值,然后通过索引的方式匹配A-Z
- )
- //用来匹配的字符A-Z
- Slice := []string{"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
- "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
- if Num > 26 { //数据大于26需要进行拆分
- for {
- k = Num % 26 //从个位开始拆分,如果求余为0,说明末尾为26,也就是Z,如果是转化为26进制数,则末尾是可以为0的,这里必须为A-Z中的一个
- if k == 0 {
- temp = append(temp, 26)
- k = 26
- } else {
- temp = append(temp, k)
- }
- Num = (Num - k) / 26 //减去Num最后一位数的值,因为已经记录在temp中
- if Num <= 26 { //小于等于26直接进行匹配,不需要进行数据拆分
- temp = append(temp, Num)
- break
- }
- }
- } else {
- return Slice[Num]
- }
- for _, value := range temp {
- Str = Slice[value] + Str //因为数据切分后存储顺序是反的,所以Str要放在后面
- }
- return Str
- }
- type GetDictReq struct {
- DictType string `p:"dictType" v:"required#字典类型不能为空"`
- DefaultValue string `p:"defaultValue"`
- }
- func GetDictDataByType(ctx context.Context, typ string) (map[string]string, error) {
- srv := micro_srv.InitMicroSrvClient("Dict", "micro_srv.auth")
- defer srv.Close()
- resp := &comm_def.CommonMsg{}
- err := srv.Call(ctx, "GetDictDataByType", GetDictReq{
- DictType: typ,
- }, resp)
- if err != nil {
- return nil, fmt.Errorf("获取字典 %s %s", typ, err.Error())
- }
- fmt.Println(resp.Data)
- data := resp.Data.(map[string]interface{})["Values"].([]interface{})
- fmt.Println(data)
- res := map[string]string{}
- for _, i := range data {
- info := i.(map[string]interface{})
- res[info["DictValue"].(string)] = info["DictLabel"].(string)
- }
- return res, nil
- }
- func StringSlicecontains(s []string, ele string) bool {
- for _, i := range s {
- if i == ele {
- return true
- }
- }
- return false
- }
- func CreateSystemMessage(msg g.MapStrStr) error {
- srv := micro_srv.InitMicroSrvClient("SystemMessage", "micro_srv.auth")
- defer srv.Close()
- resp := &comm_def.CommonMsg{}
- tenant := g.Config().GetString("micro_srv.tenant")
- ctx := context.WithValue(context.TODO(), share.ReqMetaDataKey, map[string]string{"tenant": tenant})
- err := srv.Call(ctx, "Create", msg, resp)
- if err != nil {
- g.Log().Error(err)
- return myerrors.MicroCallError("系统创建消息失败")
- }
- fmt.Println(resp.Data)
- return nil
- }
|