backend/app/brahma/service/token/internal/pkg/query.go

190 lines
3.4 KiB
Go
Raw Normal View History

2022-01-19 19:39:04 +08:00
package pkg
2022-01-23 14:48:44 +08:00
import (
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/model"
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
ctxLogger "github.com/luizsuper/ctxLoggers"
"github.com/pkg/errors"
"reflect"
"strings"
)
const (
Normal = "Normal"
JsonArray = "JsonArray"
Array = "Array"
)
const (
script = "script"
)
type (
entityName string
entityKey string
keyTag string
entityAttrMap map[entityName]nameAttr
nameAttr map[entityKey]keyTag
)
var (
m1 entityAttrMap
attr nameAttr
)
func init() {
m1 = make(entityAttrMap)
m2 := make(map[entityName]interface{})
m2[script] = new(model.Scripts)
attr = make(nameAttr)
getAttr(m2)
}
type (
RuleType struct {
Rule string
Value []string
}
QueryMap map[string]RuleType
Kv map[string]string
)
func GenerateKv(str string) (QueryMap, error) {
kv := make(QueryMap)
s := []byte(str)
i := make([]string, 0)
spilt := make([]string, 0)
if str != "" {
spilt = strings.Split(string(s[1:len(s)-1]), ",")
}
for _, values := range spilt {
if i = strings.Split(values, "="); len(i) != 2 {
return nil, errors.New("无效字符串")
}
s := keyTag("")
ok := true
if s, ok = attr[entityKey(i[0])]; !ok {
return nil, errors.New(fmt.Sprintf("无效的key:%v", i[0]))
}
switch s {
case JsonArray:
jsonArr := ""
isArr := true
if jsonArr, isArr = processJsonArr(i); !isArr {
return nil, errors.New(fmt.Sprintf("无效的jsonArr:%v", i))
}
kv[i[0]] = RuleType{
Rule: JsonArray,
Value: []string{
jsonArr,
},
}
case Array:
i2 := make([]string, 0)
i2 = processArr(i[1])
kv[i[0]] = RuleType{
Rule: Array,
Value: i2,
}
case Normal:
kv[i[0]] = RuleType{
Rule: Normal,
Value: []string{
i[1],
},
}
}
}
return kv, nil
}
func GetParam(queryMap *murders.QueryCondition) (page, limit int, query string) {
if queryMap == nil {
return
}
if q := queryMap.Query; q != nil {
query = *q
}
if p := queryMap.Page; p != nil {
page = int(*p)
}
if l := queryMap.Size; l != nil {
limit = int(*l)
}
return
}
func getAttr(entity map[entityName]interface{}) {
for name, body := range entity {
m1[name] = attr
typ := reflect.TypeOf(body)
val := reflect.ValueOf(body)
if val.Kind().String() != reflect.Ptr.String() {
ctxLogger.Error(nil, "is not ptr")
panic(errors.New("is not ptr"))
}
if val.IsNil() {
ctxLogger.Error(nil, "nil ptr")
panic(errors.New("nil ptr"))
}
num := val.Elem().NumField()
for i := 0; i < num; i++ {
field := typ.Elem().Field(i)
tag := field.Tag.Get("type")
json := field.Tag.Get("json")
attr[entityKey(json)] = keyTag(tag)
if tag == "" {
attr[entityKey(json)] = Normal
}
}
}
}
func processJsonArr(pair []string) (string, bool) {
value := fmt.Sprintf("JSON_CONTAINS(%v,JSON_ARRAY(", pair[0])
s := []byte(pair[1])
//处理括号,遍历元素
arr := strings.Split(string(s[1:len(s)-1]), "|")
for _, v := range arr {
//如果大括号里面没有元素
if v == "" {
return "", false
}
value = fmt.Sprintf("%v%v,", value, v)
}
s1 := []byte(value)
value = string(s1[0:len(s1)-1]) + "))"
return value, true
}
func processArr(str string) []string {
s := []byte(str)
arr := strings.Split(string(s[1:len(s)-1]), "|")
i := make([]string, len(arr))
for k, v := range i {
i[k] = v
}
return i
}