|
|
@@ -195,12 +195,112 @@ func Query[TI any](fromTableName string, toTableName string, toRelationColumnNam
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func QueryWithOtherInfo[FI any, TI any](tableName string) binding.ServiceFunc[map[string]any] {
|
|
|
+func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string, toRelationColumnName string) binding.ServiceFunc[map[string]any] {
|
|
|
return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
|
|
|
- info := new(struct {
|
|
|
+ var outputFromZero FI
|
|
|
+ var outputToZero TI
|
|
|
+
|
|
|
+ outputFromZeroValue := reflect.New(reflect.TypeOf(outputFromZero)).Elem()
|
|
|
+ if outputFromZeroValue.Kind() == reflect.Pointer {
|
|
|
+ outputFromZeroValue.Set(reflect.New(outputFromZeroValue.Type().Elem()))
|
|
|
+ }
|
|
|
+
|
|
|
+ outputFromZero = outputFromZeroValue.Interface().(FI)
|
|
|
+
|
|
|
+ outputToZeroValue := reflect.New(reflect.TypeOf(outputToZero)).Elem()
|
|
|
+ if outputToZeroValue.Kind() == reflect.Pointer {
|
|
|
+ outputToZeroValue.Set(reflect.New(outputToZeroValue.Type().Elem()))
|
|
|
+ }
|
|
|
+
|
|
|
+ outputToZero = outputToZeroValue.Interface().(TI)
|
|
|
+
|
|
|
+ zeroInfo := &struct {
|
|
|
Self FI `json:"self"`
|
|
|
With TI `json:"with"`
|
|
|
+ }{Self: outputFromZero, With: outputToZero}
|
|
|
+
|
|
|
+ zeroInfoJson, err := json.Marshal(zeroInfo)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ zeroRetMap := make(map[string]any)
|
|
|
+ err = json.Unmarshal(zeroInfoJson, &zeroRetMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ dbExecutor := i.DBExecutor()
|
|
|
+
|
|
|
+ object := objects[0]
|
|
|
+ if object == nil {
|
|
|
+ return zeroRetMap, fserr.New("领域实体为空")
|
|
|
+ }
|
|
|
+
|
|
|
+ fromEntity, ok := object.(domain.Entity)
|
|
|
+ if !ok {
|
|
|
+ return zeroRetMap, fserr.New("领域对象不是实体")
|
|
|
+ }
|
|
|
+
|
|
|
+ // from存在性校验
|
|
|
+ fromResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
+ TableName: fromTableName,
|
|
|
+ Conditions: sql.NewConditions().Equal(domain.ColumnID, fromEntity.GetID()),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ if database.IsErrorDBRecordNotExist(err) {
|
|
|
+ return zeroRetMap, fserr.New(fromEntity.DomainCNName() + "不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ return zeroRetMap, err
|
|
|
+ }
|
|
|
+
|
|
|
+ existFrom := reflect.New(reflect.TypeOf(object).Elem()).Interface()
|
|
|
+ err = sql.ParseSqlResult(fromResult, existFrom)
|
|
|
+ if err != nil {
|
|
|
+ return zeroRetMap, err
|
|
|
+ }
|
|
|
+
|
|
|
+ existFromEntity := existFrom.(domain.Entity)
|
|
|
+
|
|
|
+ toResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
+ TableName: toTableName,
|
|
|
+ Conditions: sql.NewConditions().Equal(toRelationColumnName, existFromEntity.GetID()),
|
|
|
})
|
|
|
+ if err != nil {
|
|
|
+ return zeroRetMap, err
|
|
|
+ }
|
|
|
+
|
|
|
+ var fromInfo FI
|
|
|
+ var fromInfoPointer any
|
|
|
+
|
|
|
+ fromInfoPointer = &fromInfo
|
|
|
+ if outputFromZeroValue.Kind() == reflect.Pointer {
|
|
|
+ fromInfoPointer = fromInfo
|
|
|
+ }
|
|
|
+
|
|
|
+ err = sql.ParseSqlResult(fromResult, fromInfoPointer)
|
|
|
+ if err != nil {
|
|
|
+ return zeroRetMap, err
|
|
|
+ }
|
|
|
+
|
|
|
+ var toInfo TI
|
|
|
+ var toInfoPointer any
|
|
|
+
|
|
|
+ toInfoPointer = &toInfo
|
|
|
+ if outputToZeroValue.Kind() == reflect.Pointer {
|
|
|
+ toInfoPointer = toInfo
|
|
|
+ }
|
|
|
+
|
|
|
+ err = sql.ParseSqlResult(toResult, toInfoPointer)
|
|
|
+ if err != nil {
|
|
|
+ return zeroRetMap, err
|
|
|
+ }
|
|
|
+
|
|
|
+ info := &struct {
|
|
|
+ Self FI `json:"self"`
|
|
|
+ With TI `json:"with"`
|
|
|
+ }{Self: fromInfo, With: toInfo}
|
|
|
|
|
|
infoJson, err := json.Marshal(info)
|
|
|
if err != nil {
|