fusenapi/server/info/internal/logic/infologic.go

149 lines
3.8 KiB
Go
Raw Normal View History

2023-08-31 08:17:32 +00:00
package logic
import (
"encoding/json"
"fmt"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-09-01 02:48:00 +00:00
"fusenapi/utils/check"
2023-08-31 08:17:32 +00:00
"strings"
"context"
"fusenapi/server/info/internal/svc"
"fusenapi/server/info/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type InfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
return &InfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *InfoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 这个与表名强关联
var ModuleTable map[string]string = map[string]string{
"userinfo": "fs_user_info",
"material": "fs_user_material",
}
type InfoType struct {
2023-09-01 06:04:39 +00:00
Id int64 `gorm:"id"`
Module string `gorm:"module"`
QueryData string `gorm:"querydata"`
2023-08-31 08:17:32 +00:00
// CreateAt time.Time `json:"ctime"`
}
2023-09-01 02:48:00 +00:00
type ModuleQuery struct {
2023-09-01 05:30:34 +00:00
TableName string
2023-09-01 02:48:00 +00:00
ModuleName string
2023-09-01 04:22:05 +00:00
ModuleQuery map[string]struct{}
}
func (mquery *ModuleQuery) EncodeQuery(field string) string {
var qstr []byte = []byte("JSON_OBJECT(")
for query := range mquery.ModuleQuery {
2023-09-01 05:28:35 +00:00
qstr = append(qstr, []byte(fmt.Sprintf("'%s.%s',%s ->> '$.%s',", mquery.ModuleName, query, field, query))...)
2023-09-01 04:22:05 +00:00
}
if qstr[len(qstr)-1] == ',' {
qstr[len(qstr)-1] = ')'
} else {
qstr = append(qstr, ')')
}
return string(qstr)
2023-09-01 02:48:00 +00:00
}
2023-08-31 08:17:32 +00:00
func (l *InfoLogic) Info(req *types.UserInfoRequest, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
var cond string
switch userinfo.GetIdType() {
case auth.IDTYPE_User:
cond = fmt.Sprintf("user_id = %d", userinfo.UserId)
case auth.IDTYPE_Guest:
cond = fmt.Sprintf("guest_id = %d", userinfo.GuestId)
default:
cond = "user_id = 0 and guest_id = 0"
}
2023-09-01 04:22:05 +00:00
var mquerys map[string]*ModuleQuery = make(map[string]*ModuleQuery)
2023-08-31 08:17:32 +00:00
var metadict map[string]any = make(map[string]any)
for _, module := range req.Module {
2023-09-01 02:48:00 +00:00
if !check.CheckModuleQuery(module) {
return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format is error", module))
}
2023-08-31 08:17:32 +00:00
mlist := strings.Split(module, ".")
if len(mlist) == 0 {
return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error", module))
}
mtable := mlist[0]
tname, ok := ModuleTable[mtable]
if !ok {
return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error, table %s not found", module, tname))
}
2023-09-01 04:22:05 +00:00
if mquery, ok := mquerys[mtable]; ok {
mquery.ModuleQuery[strings.Join(mlist[1:], ",")] = struct{}{}
} else {
mquery := &ModuleQuery{
2023-09-01 05:32:17 +00:00
TableName: tname,
2023-09-01 04:22:05 +00:00
ModuleName: mtable,
ModuleQuery: map[string]struct{}{strings.Join(mlist[1:], ","): {}}}
mquerys[mtable] = mquery
}
2023-09-01 02:48:00 +00:00
}
for _, mquery := range mquerys {
2023-09-01 05:30:34 +00:00
sqlstr := fmt.Sprintf("select id, module, %s as querydata from %s where %s ", mquery.EncodeQuery("metadata"), mquery.TableName, cond)
2023-08-31 08:17:32 +00:00
var info InfoType
err := l.svcCtx.MysqlConn.Raw(sqlstr).Scan(&info).Error
if err == gorm.ErrRecordNotFound {
continue
}
if err != nil {
2023-09-01 05:24:40 +00:00
logx.Error(err, mquery.EncodeQuery("metadata"))
2023-08-31 08:17:32 +00:00
return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error())
}
2023-09-01 04:22:05 +00:00
var querydata map[string]any = make(map[string]any)
err = json.Unmarshal([]byte(info.QueryData), &querydata)
2023-08-31 08:17:32 +00:00
if err != nil {
2023-09-01 05:36:05 +00:00
logx.Error(err, info)
2023-09-01 05:34:29 +00:00
return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()+":"+info.QueryData)
2023-08-31 08:17:32 +00:00
}
2023-09-01 04:22:05 +00:00
for k, v := range querydata {
metadict[k] = v
}
2023-08-31 08:17:32 +00:00
}
return resp.SetStatus(basic.CodeOK, metadict)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *InfoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }