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

190 lines
4.7 KiB
Go
Raw Normal View History

2023-09-01 07:05:42 +00:00
package logic
import (
"encoding/json"
"fmt"
"fusenapi/initalize"
2023-09-07 10:39:31 +00:00
"fusenapi/model/gmodel"
2023-09-01 07:05:42 +00:00
"fusenapi/utils/check"
2023-09-26 09:16:10 +00:00
"fusenapi/utils/fssql"
2023-09-07 10:39:31 +00:00
"log"
2023-09-01 07:05:42 +00:00
"strings"
"testing"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
func TestMain(t *testing.T) {
conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen")
2023-09-21 10:12:22 +00:00
condUser := "user_id = 134"
// var condUser string
// switch userinfo.GetIdType() {
// case auth.IDTYPE_User:
// condUser = fmt.Sprintf("user_id = %d", userinfo.UserId)
// case auth.IDTYPE_Guest:
// condUser = fmt.Sprintf("guest_id = %d", userinfo.GuestId)
// default:
// condUser = "user_id = 0 and guest_id = 0"
// }
2023-09-01 07:05:42 +00:00
var mquerys map[string]*ModuleQuery = make(map[string]*ModuleQuery)
var metadict map[string]any = make(map[string]any)
2023-09-21 10:12:22 +00:00
for _, module := range []string{"userinfo.profile", "userinfo.profile.logo_selected", "userinfo.profile.logo_selected.logo_url1"} {
2023-09-01 07:05:42 +00:00
2023-09-21 10:12:22 +00:00
if !check.CheckModuleQuery(module) {
return
2023-09-01 07:05:42 +00:00
}
mlist := strings.Split(module, ".")
2023-09-01 09:03:27 +00:00
if len(mlist) < 2 {
2023-09-21 10:12:22 +00:00
return
2023-09-01 07:05:42 +00:00
}
mtable := mlist[0]
tname, ok := ModuleTable[mtable]
if !ok {
2023-09-21 10:12:22 +00:00
return
2023-09-01 07:05:42 +00:00
}
2023-09-01 09:03:27 +00:00
moduleName := mlist[1]
cond := fmt.Sprintf("module = '%s' and %s", moduleName, condUser)
2023-09-01 07:05:42 +00:00
if mquery, ok := mquerys[mtable]; ok {
2023-09-21 10:12:22 +00:00
mquery.ModuleQuery[strings.Join(mlist[2:], ".")] = struct{}{}
2023-09-01 07:05:42 +00:00
} else {
mquery := &ModuleQuery{
TableName: tname,
2023-09-01 09:03:27 +00:00
ModuleName: mtable + "." + moduleName,
2023-09-21 10:12:22 +00:00
Cond: cond,
ModuleQuery: map[string]struct{}{strings.Join(mlist[2:], "."): {}}}
2023-09-01 07:05:42 +00:00
mquerys[mtable] = mquery
}
}
for _, mquery := range mquerys {
2023-09-21 10:12:22 +00:00
log.Println(mquery.ModuleName, mquery.ModuleQuery)
sqlstr := fmt.Sprintf("select id, module, %s as querydata from %s where %s order by ctime DESC limit 1", mquery.EncodeQuery("metadata"), mquery.TableName, mquery.Cond)
2023-09-01 07:05:42 +00:00
raw := conn.Raw(sqlstr)
if raw.Error != nil {
if raw.Error == gorm.ErrRecordNotFound {
continue
} else {
logx.Error(raw.Error)
2023-09-21 10:12:22 +00:00
2023-09-01 07:05:42 +00:00
}
}
2023-09-21 10:12:22 +00:00
var info map[string]any
2023-09-01 07:05:42 +00:00
err := raw.Scan(&info).Error
if err == gorm.ErrRecordNotFound {
continue
}
if err != nil {
logx.Error(err, mquery.EncodeQuery("metadata"))
}
2023-09-01 09:03:27 +00:00
queryjson, ok := info["querydata"].(string)
if !ok {
2023-09-21 10:12:22 +00:00
for k, v := range mquery.EncodeEmpty() {
metadict[k] = v
}
continue
// return resp.SetStatusWithMessage(basic.CodeDbSqlErr, mquery.ModuleName)
2023-09-01 09:03:27 +00:00
}
2023-09-01 07:05:42 +00:00
var querydata map[string]any = make(map[string]any)
2023-09-01 09:03:27 +00:00
err = json.Unmarshal([]byte(queryjson), &querydata)
2023-09-01 07:05:42 +00:00
if err != nil {
2023-09-01 09:03:27 +00:00
logx.Error(info)
2023-09-01 07:05:42 +00:00
}
for k, v := range querydata {
metadict[k] = v
}
}
2023-09-21 10:12:22 +00:00
// 隐含白板用户逻辑
if v, ok := metadict["userinfo.profile"]; ok {
if v == nil {
2023-09-22 05:35:05 +00:00
2023-09-21 10:12:22 +00:00
info := QueryDefault(conn, "profile", "logo_selected", "fs_user_info")
metadict["userinfo.profile"] = info
} else {
profileDict := v.(map[string]any)
if _, ok := profileDict["logo_selected"]; !ok {
info := QueryDefault(conn, "profile", "logo_selected", "fs_user_info")
profileDict["logo_selected"] = info["logo_selected"]
}
}
} else if v, ok := metadict["userinfo.profile.logo_selected"]; ok {
if v == nil {
2023-09-22 04:09:41 +00:00
info := QueryDefault(conn, "profile", "logo_selected", "fs_user_info")
metadict["userinfo.profile.logo_selected"] = info
2023-09-21 10:12:22 +00:00
}
2023-09-22 05:35:05 +00:00
} else {
var info map[string]any
for k, v := range metadict {
if v == nil {
if strings.HasPrefix(k, "userinfo.profile.logo_selected") {
if info == nil {
info = QueryDefault(conn, "profile", "logo_selected", "fs_user_info")
}
curValue, err := GetMapValueByKey(info, strings.Split(k, ".")[2:])
if err != nil {
logx.Error(err, info)
continue
// return resp.SetStatus(basic.CodeOK, metadict)
}
metadict[k] = curValue
// curValue
}
}
}
2023-09-21 10:12:22 +00:00
}
}
2023-09-07 10:39:31 +00:00
func TestCaseJSON_EXTRACT(t *testing.T) {
2023-09-22 07:42:09 +00:00
userProfile := &gmodel.UserProfile{}
2023-09-07 10:39:31 +00:00
metadata, err := json.Marshal(userProfile)
if err != nil {
panic(err)
}
updatesql := `UPDATE fusen.fs_change_code
SET metadata = CASE
WHEN metadata IS NULL THEN '%s'
ELSE JSON_MERGE_PATCH(metadata, '%s')
END
WHERE id = ?;`
updatesql = fmt.Sprintf(updatesql, metadata, metadata)
log.Println(string(updatesql))
conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen")
// err = conn.Exec(updatesql, 6).Error
2023-09-26 09:16:10 +00:00
info := gmodel.UserProfileBase{}
2023-09-26 03:21:43 +00:00
2023-09-26 09:16:10 +00:00
var baseinfo map[string]any
tname := fssql.GetGormTableName(conn, gmodel.FsUserInfo{})
rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$.base') as base from %s where user_id = ? and module = ? order by ctime DESC limit 1", tname)
tx := conn.Raw(rawsql, 162, "profile").Take(&baseinfo)
log.Println(tx.Error)
json.Unmarshal([]byte(baseinfo["base"].(string)), &info)
2023-09-26 03:21:43 +00:00
2023-09-07 10:39:31 +00:00
log.Println(err)
}