|
|
@@ -3,6 +3,7 @@ package many2many
|
|
|
import (
|
|
|
"git.sxidc.com/go-framework/baize/convenient/binding"
|
|
|
"git.sxidc.com/go-framework/baize/convenient/binding/request"
|
|
|
+ "git.sxidc.com/go-framework/baize/convenient/binding/response"
|
|
|
"git.sxidc.com/go-framework/baize/framwork/api"
|
|
|
"git.sxidc.com/go-framework/baize/framwork/domain"
|
|
|
"git.sxidc.com/go-framework/baize/framwork/domain/entity"
|
|
|
@@ -13,7 +14,8 @@ import (
|
|
|
"git.sxidc.com/service-supports/fserr"
|
|
|
)
|
|
|
|
|
|
-func Update(middleTableName string, fromTableName string, fromDomainCNName string, fromRelationFieldName string, fromRelationColumnName string,
|
|
|
+func Update(middleTableName string,
|
|
|
+ fromTableName string, fromDomainCNName string, fromRelationFieldName string, fromRelationColumnName string,
|
|
|
toTableName string, toRelationColumnName string) binding.ServiceFunc[any] {
|
|
|
return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
object := objects[0]
|
|
|
@@ -117,9 +119,92 @@ func Update(middleTableName string, fromTableName string, fromDomainCNName strin
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Query[TI any](tableName string) binding.ServiceFunc[TI] {
|
|
|
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
|
|
|
- var info TI
|
|
|
- return info, nil
|
|
|
+func Query[TI any](middleTableName string,
|
|
|
+ fromTableName string, fromRelationColumnName string,
|
|
|
+ toTableName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[TI]] {
|
|
|
+ return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[TI], error) {
|
|
|
+ errResponse := response.InfosData[TI]{
|
|
|
+ Infos: make([]TI, 0),
|
|
|
+ }
|
|
|
+
|
|
|
+ if params == nil {
|
|
|
+ return errResponse, fserr.New("请求参数为空")
|
|
|
+ }
|
|
|
+
|
|
|
+ object := objects[0]
|
|
|
+ if object == nil {
|
|
|
+ return errResponse, fserr.New("领域实体为空")
|
|
|
+ }
|
|
|
+
|
|
|
+ dbExecutor := i.DBExecutor()
|
|
|
+
|
|
|
+ queryParams, ok := params.(request.Query)
|
|
|
+ if !ok {
|
|
|
+ return errResponse, fserr.New("请求参数不是Query接口")
|
|
|
+ }
|
|
|
+
|
|
|
+ fromEntity, ok := object.(entity.Entity)
|
|
|
+ if !ok {
|
|
|
+ return errResponse, fserr.New("领域对象不是实体")
|
|
|
+ }
|
|
|
+
|
|
|
+ // from存在性校验
|
|
|
+ fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
|
|
|
+ TableName: fromTableName,
|
|
|
+ Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if !fromExist {
|
|
|
+ return errResponse, fserr.New(fromEntity.DomainCNName() + "不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
|
|
|
+ TableName: middleTableName,
|
|
|
+ SelectColumns: []string{toRelationColumnName},
|
|
|
+ Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
|
|
|
+ PageNo: queryParams.GetPageNo(),
|
|
|
+ PageSize: queryParams.GetPageSize(),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if toIDResults == nil || len(toIDResults) == 0 {
|
|
|
+ return response.InfosData[TI]{
|
|
|
+ Infos: make([]TI, 0),
|
|
|
+ TotalCount: 0,
|
|
|
+ PageNo: queryParams.GetPageNo(),
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ toIDs := make([]string, 0)
|
|
|
+ for _, toIDResult := range toIDResults {
|
|
|
+ toIDs = append(toIDs, toIDResult.ColumnValueString(toRelationColumnName))
|
|
|
+ }
|
|
|
+
|
|
|
+ toResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
|
|
|
+ TableName: toTableName,
|
|
|
+ Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ infos := make([]TI, 0)
|
|
|
+ err = sql.ParseSqlResult(toResults, &infos)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, err
|
|
|
+ }
|
|
|
+
|
|
|
+ output := response.InfosData[TI]{
|
|
|
+ Infos: infos,
|
|
|
+ TotalCount: totalCount,
|
|
|
+ PageNo: queryParams.GetPageNo(),
|
|
|
+ }
|
|
|
+
|
|
|
+ return output, nil
|
|
|
}
|
|
|
}
|