backend/app/brahma/service/token/internal/server/script.go

137 lines
2.8 KiB
Go
Raw Normal View History

2022-01-19 19:39:04 +08:00
package servesr
import (
"context"
"errors"
2022-01-23 14:48:44 +08:00
"fmt"
2022-01-19 19:39:04 +08:00
"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"
2022-01-23 14:48:44 +08:00
"github.com/jinzhu/copier"
2022-01-19 19:39:04 +08:00
"time"
)
2022-01-23 14:48:44 +08:00
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) {
}
2022-01-19 19:39:04 +08:00
type Script struct {
murders.UnimplementedMurdersServer
pkg.WorkerInterFace
2022-01-23 14:48:44 +08:00
queryMap *murders.QueryCondition
scriptModel *[]model.Scripts
ParamCheck ParamCheck
GrammarCheck GrammarCheck
total int64
2022-01-19 19:39:04 +08:00
}
func (s *Script) GetScripts(ctx context.Context, queryMap *murders.QueryCondition) (*script.Scripts, error) {
s.queryMap = queryMap
2022-01-23 14:48:44 +08:00
t := new(Tag)
err := pkg.Run(1*time.Second, ctx, s, t)
2022-01-19 19:39:04 +08:00
if err != nil {
return nil, err
}
2022-01-23 14:48:44 +08:00
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
2022-01-19 19:39:04 +08:00
}
func (s *Script) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
go pkg.Watcher(ctx, finishChan)
2022-01-23 14:48:44 +08:00
num := int64(0)
i := new([]model.Scripts)
db, err := orm.GetContextDB(ctx)
2022-01-19 19:39:04 +08:00
if err != nil {
pkg.SafeSend(finishChan, pkg.Finish{
IsDone: false,
Err: err,
})
}
2022-01-23 14:48:44 +08:00
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 {
2022-01-19 19:39:04 +08:00
pkg.SafeSend(finishChan, pkg.Finish{
IsDone: false,
Err: errors.New("RowsAffected < 0"),
})
}
2022-01-23 14:48:44 +08:00
if limit > 0 {
db = db.Limit(limit).Offset((page - 1) * limit)
}
db.Find(i)
2022-01-19 19:39:04 +08:00
s.scriptModel = i
2022-01-23 14:48:44 +08:00
s.total = num
2022-01-19 19:39:04 +08:00
pkg.SafeSend(finishChan, pkg.Finish{
IsDone: true,
Err: nil,
})
}