123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- package one2many
- import (
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping"
- "git.sxidc.com/go-tools/utils/reflectutils"
- "git.sxidc.com/go-tools/utils/slice"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/pkg/errors"
- "reflect"
- )
- func UpdateLeft(leftTableName string, leftDomainCNName string, leftRelationFieldName string, leftRelationColumnName string,
- rightTableName string, rightDomainCNName string) binding.ServiceFunc[any] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
- object := objects[0]
- if object == nil {
- return nil, errors.New("领域实体为空")
- }
- dbExecutor := i.DBExecutor()
- leftEntity, ok := object.(entity.Entity)
- if !ok {
- return nil, errors.New("领域对象不是实体")
- }
- // 字段校验
- err := domain.CheckField(leftEntity, entity.FieldID, leftEntity.GetFieldMap())
- if err != nil {
- return nil, err
- }
- // left存在性校验
- leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: leftTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, leftEntity.GetID()),
- })
- if err != nil {
- return nil, err
- }
- if !leftExist {
- return nil, errors.New(leftEntity.DomainCNName() + "不存在")
- }
- if !domain.HasField(object, leftRelationFieldName) {
- return nil, errors.New("关联字段" + leftRelationFieldName + "不存在")
- }
- rightIDs, err := domain.Field[[]string](object, leftRelationFieldName)
- if err != nil {
- return nil, err
- }
- if rightIDs != nil && len(rightIDs) != 0 {
- for _, rightID := range rightIDs {
- err := entity.CheckIDTypeValue(leftDomainCNName, leftRelationFieldName, rightID)
- if err != nil {
- return nil, err
- }
- }
- rightIDs = slice.RemoveRepeatElement(rightIDs)
- }
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- err := database.Update(tx, &sql.UpdateExecuteParams{
- TableName: rightTableName,
- TableRow: sql.NewTableRow().Add(leftRelationColumnName, ""),
- Conditions: sql.NewConditions().Equal(leftRelationColumnName, leftEntity.GetID()),
- })
- if err != nil {
- return err
- }
- if rightIDs == nil || len(rightIDs) == 0 {
- return nil
- }
- rightCount, err := database.Count(dbExecutor, &sql.CountExecuteParams{
- TableName: rightTableName,
- Conditions: sql.NewConditions().In(entity.ColumnID, rightIDs),
- })
- if err != nil {
- return err
- }
- if int(rightCount) != len(rightIDs) {
- return errors.New("部分" + rightDomainCNName + "不存在")
- }
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: rightTableName,
- TableRow: sql.NewTableRow().Add(leftRelationColumnName, leftEntity.GetID()),
- Conditions: sql.NewConditions().In(entity.ColumnID, rightIDs),
- })
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- }
- func QueryLeft[RI any](leftTableName string, leftRelationColumnName string,
- rightTableName string) binding.ServiceFunc[response.InfosData[RI]] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[RI], error) {
- errResponse := response.InfosData[RI]{
- Infos: make([]RI, 0),
- }
- if params == nil {
- return errResponse, errors.New("请求参数为空")
- }
- object := objects[0]
- if object == nil {
- return errResponse, errors.New("领域实体为空")
- }
- dbExecutor := i.DBExecutor()
- queryParams, ok := params.(request.QueryWithIDRequestParams)
- if !ok {
- return errResponse, errors.New("请求参数不是Query接口")
- }
- leftEntity, ok := object.(entity.Entity)
- if !ok {
- return errResponse, errors.New("领域对象不是实体")
- }
- // left存在性校验
- leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: leftTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, leftEntity.GetID()),
- })
- if err != nil {
- return errResponse, err
- }
- if !leftExist {
- return errResponse, errors.New(leftEntity.DomainCNName() + "不存在")
- }
- rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: rightTableName,
- Conditions: sql.NewConditions().Equal(leftRelationColumnName, leftEntity.GetID()),
- })
- if err != nil && !database.IsErrorDBRecordNotExist(err) {
- return errResponse, err
- }
- infos := make([]RI, 0)
- err = sql.ParseSqlResult(rightResults, &infos)
- if err != nil {
- return errResponse, err
- }
- return response.InfosData[RI]{
- Infos: infos,
- TotalCount: totalCount,
- PageNo: queryParams.GetPageNo(),
- }, nil
- }
- }
- func UpdateRight(rightTableName string, rightRelationFieldName string,
- leftTableName string, leftDomainCNName string, leftRelationColumnName string) binding.ServiceFunc[any] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
- object := objects[0]
- if object == nil {
- return nil, errors.New("领域实体为空")
- }
- dbExecutor := i.DBExecutor()
- rightEntity, ok := object.(entity.Entity)
- if !ok {
- return nil, errors.New("领域对象不是实体")
- }
- // 字段校验
- err := domain.CheckField(rightEntity, entity.FieldID, rightEntity.GetFieldMap())
- if err != nil {
- return nil, err
- }
- // right存在性校验
- rightExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: rightTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
- })
- if err != nil {
- return nil, err
- }
- if !rightExist {
- return nil, errors.New(rightEntity.DomainCNName() + "不存在")
- }
- if !domain.HasField(object, rightRelationFieldName) {
- return nil, errors.New("关联字段" + rightRelationFieldName + "不存在")
- }
- leftID, err := domain.Field[string](object, rightRelationFieldName)
- if err != nil {
- return nil, err
- }
- if strutils.IsStringNotEmpty(leftID) {
- leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: leftTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
- })
- if err != nil {
- return nil, err
- }
- if !leftExist {
- return nil, errors.New(leftDomainCNName + "不存在")
- }
- }
- err = database.Update(dbExecutor, &sql.UpdateExecuteParams{
- TableName: rightTableName,
- TableRow: sql.NewTableRow().Add(leftRelationColumnName, leftID),
- Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
- })
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- }
- func QueryRight[LI any](rightTableName string, rightRelationFieldName string, leftTableName string) binding.ServiceFunc[LI] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (LI, error) {
- outputZero := reflectutils.Zero[LI]()
- dbExecutor := i.DBExecutor()
- object := objects[0]
- if object == nil {
- return outputZero, errors.New("领域实体为空")
- }
- rightEntity, ok := object.(entity.Entity)
- if !ok {
- return outputZero, errors.New("领域对象不是实体")
- }
- // right存在性校验
- existRightResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
- TableName: rightTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
- })
- if err != nil {
- if database.IsErrorDBRecordNotExist(err) {
- return outputZero, errors.New(rightEntity.DomainCNName() + "不存在")
- }
- return outputZero, err
- }
- existRightEntity := reflect.New(reflect.TypeOf(object).Elem()).Interface().(domain.Object)
- err = sql.ParseSqlResult(existRightResult, existRightEntity)
- if err != nil {
- return outputZero, err
- }
- if !domain.HasField(existRightEntity, rightRelationFieldName) {
- return outputZero, errors.New("关联字段" + rightRelationFieldName + "不存在")
- }
- leftID, err := domain.Field[string](existRightEntity, rightRelationFieldName)
- if err != nil {
- return outputZero, err
- }
- leftResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
- TableName: leftTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
- })
- if err != nil && !database.IsErrorDBRecordNotExist(err) {
- return outputZero, err
- }
- leftInfo := reflectutils.Zero[LI]()
- err = sql.ParseSqlResult(leftResult, &leftInfo)
- if err != nil {
- return outputZero, err
- }
- return leftInfo, nil
- }
- }
- type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
- func QueryRightWithLeftInfo[RI any, LI any](rightTableName string, rightFieldCallback ConditionFieldCallback, leftTableName string, leftRelationColumnName string) binding.ServiceFunc[response.InfosData[map[string]any]] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[map[string]any], error) {
- errResponse := response.InfosData[map[string]any]{
- Infos: make([]map[string]any, 0),
- }
- if params == nil {
- return errResponse, errors.New("请求参数为空")
- }
- object := objects[0]
- if object == nil {
- return errResponse, errors.New("领域实体为空")
- }
- dbExecutor := i.DBExecutor()
- queryParams, ok := params.(request.QueryRequestParams)
- if !ok {
- return errResponse, errors.New("请求参数不是Query接口")
- }
- rightEntity, ok := object.(entity.Entity)
- if !ok {
- return errResponse, errors.New("领域对象不是实体")
- }
- conditions := sql.NewConditions()
- rightFields, err := sql_mapping.DefaultUsage(rightEntity)
- if err != nil {
- return errResponse, err
- }
- for _, rightField := range rightFields {
- hasDeal := false
- if rightFieldCallback != nil {
- hasDeal, err = rightFieldCallback(conditions, rightField.FieldName, rightField.ColumnName, rightField.Value)
- if err != nil {
- return errResponse, err
- }
- }
- if !hasDeal {
- fieldValue := reflect.ValueOf(rightField.Value)
- if !fieldValue.IsZero() {
- conditions.Equal(rightField.ColumnName, rightField.Value)
- }
- }
- }
- rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: rightTableName,
- Conditions: conditions,
- PageNo: queryParams.GetPageNo(),
- PageSize: queryParams.GetPageSize(),
- })
- if err != nil {
- return errResponse, nil
- }
- if rightResults == nil || len(rightResults) == 0 {
- return response.InfosData[map[string]any]{
- Infos: make([]map[string]any, 0),
- TotalCount: 0,
- PageNo: 0,
- }, nil
- }
- leftIDs := make([]string, 0)
- for _, rightResult := range rightResults {
- leftID := rightResult.ColumnValueString(leftRelationColumnName)
- if strutils.IsStringNotEmpty(leftID) {
- leftIDs = append(leftIDs, leftID)
- }
- }
- leftResultMap := make(map[string]sql.Result)
- if leftIDs != nil && len(leftIDs) != 0 {
- leftResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: leftTableName,
- Conditions: sql.NewConditions().In(entity.ColumnID, leftIDs),
- })
- if err != nil {
- return errResponse, err
- }
- for _, leftResult := range leftResults {
- leftID := leftResult.ColumnValueString(entity.ColumnID)
- leftResultMap[leftID] = leftResult
- }
- }
- infos := make([]map[string]any, len(rightResults))
- for index, rightResult := range rightResults {
- rightInfo := reflectutils.Zero[RI]()
- leftInfo := reflectutils.Zero[LI]()
- err = sql.ParseSqlResult(rightResult, &rightInfo)
- if err != nil {
- return errResponse, err
- }
- leftID := rightResult.ColumnValueString(leftRelationColumnName)
- if strutils.IsStringEmpty(leftID) {
- infos[index] = map[string]any{
- "self": rightInfo,
- "with": leftInfo,
- }
- continue
- }
- leftResult, ok := leftResultMap[leftID]
- if !ok {
- infos[index] = map[string]any{
- "self": rightInfo,
- "with": leftInfo,
- }
- continue
- }
- err = sql.ParseSqlResult(leftResult, &leftInfo)
- if err != nil {
- return errResponse, err
- }
- infos[index] = map[string]any{
- "self": rightInfo,
- "with": leftInfo,
- }
- }
- return response.InfosData[map[string]any]{
- Infos: infos,
- TotalCount: totalCount,
- PageNo: queryParams.GetPageNo(),
- }, nil
- }
- }
|