2023-09-01 07:05:42 +00:00
package logic
import (
"encoding/json"
"fmt"
"fusenapi/initalize"
"fusenapi/utils/check"
"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-01 09:03:27 +00:00
condUser := "user_id = 105"
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-01 09:03:27 +00:00
for _ , module := range [ ] string { "material.logo.logo_url" , "userinfo.profile" } {
2023-09-01 07:05:42 +00:00
if ! check . CheckModuleQuery ( module ) {
}
mlist := strings . Split ( module , "." )
2023-09-01 09:03:27 +00:00
if len ( mlist ) < 2 {
2023-09-01 07:05:42 +00:00
}
mtable := mlist [ 0 ]
tname , ok := ModuleTable [ mtable ]
if ! ok {
}
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-01 09:03:27 +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
Cond : cond ,
ModuleName : mtable + "." + moduleName ,
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-01 09:03:27 +00:00
sqlstr := fmt . Sprintf ( "select id, module, %s as querydata from %s where %s order by ctime asc 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 )
}
}
var info map [ string ] any = make ( map [ string ] any )
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
logx . Error ( info )
queryjson , ok := info [ "querydata" ] . ( string )
if ! ok {
return
}
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
}
}
}