generated from pkg/go-template
137 lines
2.8 KiB
Go
137 lines
2.8 KiB
Go
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
|
|
ParamCheck ParamCheck
|
|
GrammarCheck GrammarCheck
|
|
total int64
|
|
}
|
|
|
|
func (s *Script) GetScripts(ctx context.Context, queryMap *murders.QueryCondition) (*script.Scripts, error) {
|
|
s.queryMap = queryMap
|
|
|
|
t := new(Tag)
|
|
err := pkg.Run(1*time.Second, ctx, s, t)
|
|
if err != nil {
|
|
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)
|
|
num := int64(0)
|
|
i := new([]model.Scripts)
|
|
|
|
db, err := orm.GetContextDB(ctx)
|
|
if err != nil {
|
|
pkg.SafeSend(finishChan, pkg.Finish{
|
|
IsDone: false,
|
|
Err: err,
|
|
})
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|