service.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package many2many
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  12. "git.sxidc.com/go-tools/utils/slice"
  13. "github.com/pkg/errors"
  14. )
  15. func Update(middleTableName string,
  16. fromTableName string, fromDomainCNName string, fromRelationFieldName string, fromRelationColumnName string,
  17. toTableName string, toRelationColumnName string) binding.ServiceFunc[any] {
  18. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  19. object := objects[0]
  20. if object == nil {
  21. return nil, errors.New("领域实体为空")
  22. }
  23. dbExecutor := i.DBExecutor()
  24. fromEntity, ok := object.(entity.Entity)
  25. if !ok {
  26. return nil, errors.New("领域对象不是实体")
  27. }
  28. // 字段校验
  29. err := domain.CheckField(fromEntity, entity.FieldID, fromEntity.GetFieldMap())
  30. if err != nil {
  31. return nil, err
  32. }
  33. // from存在性校验
  34. fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  35. TableName: fromTableName,
  36. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  37. })
  38. if err != nil {
  39. return nil, err
  40. }
  41. if !fromExist {
  42. return nil, errors.New(fromEntity.DomainCNName() + "不存在")
  43. }
  44. if !domain.HasField(object, fromRelationFieldName) {
  45. return nil, errors.New("关联字段" + fromRelationFieldName + "不存在")
  46. }
  47. toIDs, err := domain.Field[[]string](object, fromRelationFieldName)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if toIDs != nil && len(toIDs) != 0 {
  52. for _, toID := range toIDs {
  53. err := entity.CheckIDTypeValue(fromDomainCNName, fromRelationFieldName, toID)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. toIDs = slice.RemoveRepeatElement(toIDs)
  59. }
  60. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  61. err := database.Delete(tx, &sql.DeleteExecuteParams{
  62. TableName: middleTableName,
  63. Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. if toIDs == nil || len(toIDs) == 0 {
  69. return nil
  70. }
  71. toCount, err := database.Count(dbExecutor, &sql.CountExecuteParams{
  72. TableName: toTableName,
  73. Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. if int(toCount) != len(toIDs) {
  79. return errors.New("部分{{ $toCNName }}不存在")
  80. }
  81. tableRows := make([]sql.TableRow, len(toIDs))
  82. for index, toID := range toIDs {
  83. tableRows[index] = *(sql.NewTableRow().
  84. Add(fromRelationColumnName, fromEntity.GetID()).
  85. Add(toRelationColumnName, toID))
  86. }
  87. err = database.InsertBatch(tx, &sql.InsertBatchExecuteParams{
  88. TableName: middleTableName,
  89. TableRowBatch: tableRows,
  90. })
  91. if err != nil {
  92. if database.IsErrorDBRecordHasExist(err) {
  93. return errors.New("关联项已存在")
  94. }
  95. return err
  96. }
  97. return nil
  98. })
  99. if err != nil {
  100. return nil, err
  101. }
  102. return nil, nil
  103. }
  104. }
  105. func Query[TI any](middleTableName string,
  106. fromTableName string, fromRelationColumnName string,
  107. toTableName string, toRelationColumnName string, orderBy string) binding.ServiceFunc[response.InfosData[TI]] {
  108. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[TI], error) {
  109. errResponse := response.InfosData[TI]{
  110. Infos: make([]TI, 0),
  111. }
  112. if params == nil {
  113. return errResponse, errors.New("请求参数为空")
  114. }
  115. object := objects[0]
  116. if object == nil {
  117. return errResponse, errors.New("领域实体为空")
  118. }
  119. dbExecutor := i.DBExecutor()
  120. queryParams, ok := params.(request.QueryWithIDRequestParams)
  121. if !ok {
  122. return errResponse, errors.New("请求参数不是Query接口")
  123. }
  124. fromEntity, ok := object.(entity.Entity)
  125. if !ok {
  126. return errResponse, errors.New("领域对象不是实体")
  127. }
  128. // from存在性校验
  129. fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  130. TableName: fromTableName,
  131. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  132. })
  133. if err != nil {
  134. return errResponse, err
  135. }
  136. if !fromExist {
  137. return errResponse, errors.New(fromEntity.DomainCNName() + "不存在")
  138. }
  139. toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  140. TableName: middleTableName,
  141. SelectClauses: []string{toRelationColumnName},
  142. Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
  143. PageNo: queryParams.GetPageNo(),
  144. PageSize: queryParams.GetPageSize(),
  145. })
  146. if err != nil {
  147. return errResponse, err
  148. }
  149. if toIDResults == nil || len(toIDResults) == 0 {
  150. return response.InfosData[TI]{
  151. Infos: make([]TI, 0),
  152. TotalCount: 0,
  153. PageNo: queryParams.GetPageNo(),
  154. }, nil
  155. }
  156. toIDs := make([]string, 0)
  157. err = sql.ParseSqlResult(toIDResults, &toIDs)
  158. if err != nil {
  159. return errResponse, err
  160. }
  161. toResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  162. TableName: toTableName,
  163. Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
  164. OrderBy: orderBy,
  165. })
  166. if err != nil {
  167. return errResponse, err
  168. }
  169. infos := make([]TI, 0)
  170. err = sql.ParseSqlResult(toResults, &infos)
  171. if err != nil {
  172. return errResponse, err
  173. }
  174. return response.InfosData[TI]{
  175. Infos: infos,
  176. TotalCount: totalCount,
  177. PageNo: queryParams.GetPageNo(),
  178. }, nil
  179. }
  180. }