fusenapi/server/info/internal/logic/infologic_test.go
2023-09-22 15:42:09 +08:00

181 lines
4.4 KiB
Go

package logic
import (
"encoding/json"
"fmt"
"fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/utils/check"
"log"
"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")
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"
// }
var mquerys map[string]*ModuleQuery = make(map[string]*ModuleQuery)
var metadict map[string]any = make(map[string]any)
for _, module := range []string{"userinfo.profile", "userinfo.profile.logo_selected", "userinfo.profile.logo_selected.logo_url1"} {
if !check.CheckModuleQuery(module) {
return
}
mlist := strings.Split(module, ".")
if len(mlist) < 2 {
return
}
mtable := mlist[0]
tname, ok := ModuleTable[mtable]
if !ok {
return
}
moduleName := mlist[1]
cond := fmt.Sprintf("module = '%s' and %s", moduleName, condUser)
if mquery, ok := mquerys[mtable]; ok {
mquery.ModuleQuery[strings.Join(mlist[2:], ".")] = struct{}{}
} else {
mquery := &ModuleQuery{
TableName: tname,
ModuleName: mtable + "." + moduleName,
Cond: cond,
ModuleQuery: map[string]struct{}{strings.Join(mlist[2:], "."): {}}}
mquerys[mtable] = mquery
}
}
for _, mquery := range mquerys {
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)
raw := conn.Raw(sqlstr)
if raw.Error != nil {
if raw.Error == gorm.ErrRecordNotFound {
continue
} else {
logx.Error(raw.Error)
}
}
var info map[string]any
err := raw.Scan(&info).Error
if err == gorm.ErrRecordNotFound {
continue
}
if err != nil {
logx.Error(err, mquery.EncodeQuery("metadata"))
}
queryjson, ok := info["querydata"].(string)
if !ok {
for k, v := range mquery.EncodeEmpty() {
metadict[k] = v
}
continue
// return resp.SetStatusWithMessage(basic.CodeDbSqlErr, mquery.ModuleName)
}
var querydata map[string]any = make(map[string]any)
err = json.Unmarshal([]byte(queryjson), &querydata)
if err != nil {
logx.Error(info)
}
for k, v := range querydata {
metadict[k] = v
}
}
// 隐含白板用户逻辑
if v, ok := metadict["userinfo.profile"]; ok {
if v == nil {
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 {
info := QueryDefault(conn, "profile", "logo_selected", "fs_user_info")
metadict["userinfo.profile.logo_selected"] = info
}
} 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
}
}
}
}
}
func TestCaseJSON_EXTRACT(t *testing.T) {
userProfile := &gmodel.UserProfile{}
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
log.Println(conn.Model(&gmodel.FsChangeCode{}).Select("id").Where("id = 5").Take(nil).Error)
log.Println(err)
}