123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- package remote
- 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-tools/utils/slice"
- "github.com/pkg/errors"
- )
- func Update(middleTableName string,
- fromRemote bool, fromTableName string, fromDomainCNName string, fromRelationFieldName string, fromRelationColumnName string,
- toRemote bool, 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]
- if object == nil {
- return nil, errors.New("领域实体为空")
- }
- dbExecutor := i.DBExecutor()
- fromEntity, ok := object.(entity.Entity)
- if !ok {
- return nil, errors.New("领域对象不是实体")
- }
- // 字段校验
- err := entity.CheckFieldID(fromEntity)
- if err != nil {
- return nil, err
- }
- if !fromRemote {
- // from存在性校验
- fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: fromTableName,
- Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
- })
- if err != nil {
- return nil, err
- }
- if !fromExist {
- return nil, errors.New(fromEntity.DomainCNName() + "不存在")
- }
- }
- if !domain.HasField(object, fromRelationFieldName) {
- return nil, errors.New("关联字段" + fromRelationFieldName + "不存在")
- }
- toIDs, err := domain.Field[[]string](object, fromRelationFieldName)
- if err != nil {
- return nil, err
- }
- if toIDs != nil && len(toIDs) != 0 {
- for _, toID := range toIDs {
- err := entity.CheckIDTypeValue(fromDomainCNName, fromRelationFieldName, toID)
- if err != nil {
- return nil, err
- }
- }
- toIDs = slice.RemoveRepeatElement(toIDs)
- }
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- err := database.Delete(tx, &sql.DeleteExecuteParams{
- TableName: middleTableName,
- Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
- })
- if err != nil {
- return err
- }
- if toIDs == nil || len(toIDs) == 0 {
- return nil
- }
- if !toRemote {
- toCount, err := database.Count(dbExecutor, &sql.CountExecuteParams{
- TableName: toTableName,
- Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
- })
- if err != nil {
- return err
- }
- if int(toCount) != len(toIDs) {
- return errors.New("部分{{ $toCNName }}不存在")
- }
- }
- tableRows := make([]sql.TableRow, len(toIDs))
- for index, toID := range toIDs {
- tableRows[index] = *(sql.NewTableRow().
- Add(fromRelationColumnName, fromEntity.GetID()).
- Add(toRelationColumnName, toID))
- }
- err = database.InsertBatch(tx, &sql.InsertBatchExecuteParams{
- TableName: middleTableName,
- TableRowBatch: tableRows,
- })
- if err != nil {
- if database.IsErrorDBRecordHasExist(err) {
- return errors.New("关联项已存在")
- }
- return err
- }
- return nil
- })
- if err != nil {
- return nil, err
- }
- return nil, nil
- }
- }
- func QueryToExist[TI any](middleTableName string,
- fromRemote bool, 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, 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接口")
- }
- fromEntity, ok := object.(entity.Entity)
- if !ok {
- return errResponse, errors.New("领域对象不是实体")
- }
- if !fromRemote {
- // 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, errors.New(fromEntity.DomainCNName() + "不存在")
- }
- }
- toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: middleTableName,
- SelectClauses: []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)
- err = sql.ParseSqlResult(toIDResults, &toIDs)
- if err != nil {
- return errResponse, err
- }
- 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
- }
- }
- func QueryToRemote(middleTableName string, fromRemote bool, fromTableName string, fromRelationColumnName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[string]] {
- return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[string], error) {
- errResponse := response.InfosData[string]{
- Infos: make([]string, 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接口")
- }
- fromEntity, ok := object.(entity.Entity)
- if !ok {
- return errResponse, errors.New("领域对象不是实体")
- }
- if !fromRemote {
- // 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, errors.New(fromEntity.DomainCNName() + "不存在")
- }
- }
- toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: middleTableName,
- SelectClauses: []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[string]{
- Infos: make([]string, 0),
- TotalCount: 0,
- PageNo: queryParams.GetPageNo(),
- }, nil
- }
- toIDs := make([]string, 0)
- err = sql.ParseSqlResult(toIDResults, &toIDs)
- if err != nil {
- return errResponse, err
- }
- return response.InfosData[string]{
- Infos: toIDs,
- TotalCount: totalCount,
- PageNo: queryParams.GetPageNo(),
- }, nil
- }
- }
|