generated from pkg/go-template
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
type (
|
||||
Category struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid string `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del"`
|
||||
Tags *[]Tag `json:"tags,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:CategoryUid;References:Uuid;JoinReferences:TagUid" valid:"no_empty"`
|
||||
}
|
||||
CategoriesTagDto struct {
|
||||
TIds []string `json:"tag_ids" valid:"no_empty"`
|
||||
Cid string `json:"category_id" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
@@ -11,9 +11,10 @@ type (
|
||||
)
|
||||
|
||||
type Scripts struct {
|
||||
ScriptName string `json:"script_name" gorm:"column:script_name"`
|
||||
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
|
||||
ScriptTag ints `json:"script_tag" gorm:"column:script_tag"`
|
||||
ScriptName string `json:"script_name" gorm:"column:script_name"`
|
||||
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
|
||||
//type json型
|
||||
ScriptTag ints `json:"script_tag" gorm:"column:script_tag" type:"json"`
|
||||
ScriptScore float64 `json:"script_score" gorm:"column:script_score"`
|
||||
GroupDuration int `json:"group_duration" gorm:"column:group_duration"`
|
||||
ScriptCoverUrl string `json:"script_cover_url" gorm:"column:script_cover_url"`
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
type (
|
||||
Tag struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid int `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del" value:"1|0"`
|
||||
Cs *[]Category `json:"categories,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:TagUid;References:Uuid;JoinReferences:CategoryUid" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (m *Tag) TableName() string {
|
||||
return "tag"
|
||||
}
|
||||
@@ -1 +1,189 @@
|
||||
package pkg
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -3,33 +3,91 @@ package servesr
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/model"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/pkg"
|
||||
"git.icechen.cn/monorepo/backend/pkg/orm"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders/script"
|
||||
"github.com/jinzhu/copier"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
ParamCheck struct {
|
||||
kv pkg.Kv
|
||||
}
|
||||
|
||||
GrammarCheck struct {
|
||||
kv pkg.Kv
|
||||
}
|
||||
)
|
||||
|
||||
func (g GrammarCheck) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
go pkg.Watcher(ctx, finishChan)
|
||||
|
||||
}
|
||||
|
||||
func (p ParamCheck) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
|
||||
}
|
||||
|
||||
type Script struct {
|
||||
murders.UnimplementedMurdersServer
|
||||
pkg.WorkerInterFace
|
||||
queryMap *murders.QueryCondition
|
||||
scriptModel *[]model.Scripts
|
||||
queryMap *murders.QueryCondition
|
||||
scriptModel *[]model.Scripts
|
||||
ParamCheck ParamCheck
|
||||
GrammarCheck GrammarCheck
|
||||
total int64
|
||||
}
|
||||
|
||||
func (s *Script) GetScripts(ctx context.Context, queryMap *murders.QueryCondition) (*script.Scripts, error) {
|
||||
s.queryMap = queryMap
|
||||
err := pkg.Run(1*time.Second, ctx, s)
|
||||
|
||||
t := new(Tag)
|
||||
err := pkg.Run(1*time.Second, ctx, s, t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
|
||||
m := make(map[int]string)
|
||||
for _, v := range *t.tagModel {
|
||||
m[v.Uuid] = v.Value
|
||||
}
|
||||
|
||||
scripts := make([]*script.Script, len(*s.scriptModel))
|
||||
|
||||
for k, v := range *s.scriptModel {
|
||||
tags := make([]string, len(v.ScriptTag))
|
||||
for k, v := range v.ScriptTag {
|
||||
if s, ok := m[v]; ok {
|
||||
tags[k] = s
|
||||
} else {
|
||||
return nil, errors.New(fmt.Sprintf("tag %v 没有对应value", k))
|
||||
}
|
||||
}
|
||||
s2 := new(script.Script)
|
||||
err = copier.Copy(s2, v)
|
||||
s2.ScriptTag = tags
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scripts[k] = s2
|
||||
}
|
||||
|
||||
s2 := new(script.Scripts)
|
||||
s2.Scripts = scripts
|
||||
s2.Total = s.total
|
||||
return s2, err
|
||||
}
|
||||
|
||||
func (s *Script) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
go pkg.Watcher(ctx, finishChan)
|
||||
db, err := orm.GetContextDB(ctx, orm.DB)
|
||||
num := int64(0)
|
||||
i := new([]model.Scripts)
|
||||
|
||||
db, err := orm.GetContextDB(ctx)
|
||||
if err != nil {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
@@ -37,15 +95,40 @@ func (s *Script) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
})
|
||||
}
|
||||
|
||||
i := new([]model.Scripts)
|
||||
if num := db.Find(i).RowsAffected; num < 0 {
|
||||
page, limit, query := pkg.GetParam(s.queryMap)
|
||||
queryMap, err := pkg.GenerateKv(query)
|
||||
|
||||
if err != nil {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: err,
|
||||
})
|
||||
}
|
||||
|
||||
for k, v := range queryMap {
|
||||
if v.Rule == pkg.Normal {
|
||||
db = db.Where(fmt.Sprintf("%v = ?", k), v.Value[0])
|
||||
}
|
||||
if v.Rule == pkg.JsonArray {
|
||||
db = db.Where(v.Value[0])
|
||||
}
|
||||
if v.Rule == pkg.Array {
|
||||
db = db.Where(fmt.Sprintf("%v IN ?", k), v.Value)
|
||||
}
|
||||
}
|
||||
if num = db.Find(i).RowsAffected; num < 0 {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: errors.New("RowsAffected < 0"),
|
||||
})
|
||||
}
|
||||
if limit > 0 {
|
||||
db = db.Limit(limit).Offset((page - 1) * limit)
|
||||
}
|
||||
db.Find(i)
|
||||
|
||||
s.scriptModel = i
|
||||
s.total = num
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: true,
|
||||
Err: nil,
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func RpcServer() {
|
||||
lis, err := net.Listen("tcp", ":3000")
|
||||
lis, err := net.Listen("tcp", ":3001")
|
||||
if err != nil {
|
||||
ctxLogger.Info(nil, "brahma server start up error", zap.String("error", err.Error()))
|
||||
}
|
||||
|
||||
@@ -1 +1,49 @@
|
||||
package servesr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/model"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/pkg"
|
||||
"git.icechen.cn/monorepo/backend/pkg/orm"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders/tag"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
murders.UnimplementedMurdersServer
|
||||
pkg.WorkerInterFace
|
||||
queryMap *murders.QueryCondition
|
||||
tagModel *[]model.Tag
|
||||
}
|
||||
|
||||
func (Tag) GetTags(ctx context.Context, q *murders.QueryCondition) (*tag.Tag, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetTags not implemented")
|
||||
}
|
||||
|
||||
func (w *Tag) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
go pkg.Watcher(ctx, finishChan)
|
||||
db, err := orm.GetContextDB(ctx)
|
||||
if err != nil {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: err,
|
||||
})
|
||||
}
|
||||
|
||||
t := new([]model.Tag)
|
||||
if err = db.Where("type = ?", "script").Find(t).Error; err != nil {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: err,
|
||||
})
|
||||
}
|
||||
|
||||
w.tagModel = t
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: true,
|
||||
Err: nil,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user