service.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package remote
  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. fromRemote bool, fromTableName string, fromDomainCNName string, fromRelationFieldName string, fromRelationColumnName string,
  17. toRemote bool, 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 := entity.CheckFieldID(fromEntity)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if !fromRemote {
  34. // from存在性校验
  35. fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  36. TableName: fromTableName,
  37. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. if !fromExist {
  43. return nil, errors.New(fromEntity.DomainCNName() + "不存在")
  44. }
  45. }
  46. if !domain.HasField(object, fromRelationFieldName) {
  47. return nil, errors.New("关联字段" + fromRelationFieldName + "不存在")
  48. }
  49. toIDs, err := domain.Field[[]string](object, fromRelationFieldName)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if toIDs != nil && len(toIDs) != 0 {
  54. for _, toID := range toIDs {
  55. err := entity.CheckIDTypeValue(fromDomainCNName, fromRelationFieldName, toID)
  56. if err != nil {
  57. return nil, err
  58. }
  59. }
  60. toIDs = slice.RemoveRepeatElement(toIDs)
  61. }
  62. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  63. err := database.Delete(tx, &sql.DeleteExecuteParams{
  64. TableName: middleTableName,
  65. Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. if toIDs == nil || len(toIDs) == 0 {
  71. return nil
  72. }
  73. if !toRemote {
  74. toCount, err := database.Count(dbExecutor, &sql.CountExecuteParams{
  75. TableName: toTableName,
  76. Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
  77. })
  78. if err != nil {
  79. return err
  80. }
  81. if int(toCount) != len(toIDs) {
  82. return errors.New("部分{{ $toCNName }}不存在")
  83. }
  84. }
  85. tableRows := make([]sql.TableRow, len(toIDs))
  86. for index, toID := range toIDs {
  87. tableRows[index] = *(sql.NewTableRow().
  88. Add(fromRelationColumnName, fromEntity.GetID()).
  89. Add(toRelationColumnName, toID))
  90. }
  91. err = database.InsertBatch(tx, &sql.InsertBatchExecuteParams{
  92. TableName: middleTableName,
  93. TableRowBatch: tableRows,
  94. })
  95. if err != nil {
  96. if database.IsErrorDBRecordHasExist(err) {
  97. return errors.New("关联项已存在")
  98. }
  99. return err
  100. }
  101. return nil
  102. })
  103. if err != nil {
  104. return nil, err
  105. }
  106. return nil, nil
  107. }
  108. }
  109. func QueryToExist[TI any](middleTableName string,
  110. fromRemote bool, fromTableName string, fromRelationColumnName string,
  111. toTableName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[TI]] {
  112. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[TI], error) {
  113. errResponse := response.InfosData[TI]{
  114. Infos: make([]TI, 0),
  115. }
  116. if params == nil {
  117. return errResponse, errors.New("请求参数为空")
  118. }
  119. object := objects[0]
  120. if object == nil {
  121. return errResponse, errors.New("领域实体为空")
  122. }
  123. dbExecutor := i.DBExecutor()
  124. queryParams, ok := params.(request.QueryRequestParams)
  125. if !ok {
  126. return errResponse, errors.New("请求参数不是Query接口")
  127. }
  128. fromEntity, ok := object.(entity.Entity)
  129. if !ok {
  130. return errResponse, errors.New("领域对象不是实体")
  131. }
  132. if !fromRemote {
  133. // from存在性校验
  134. fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  135. TableName: fromTableName,
  136. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  137. })
  138. if err != nil {
  139. return errResponse, err
  140. }
  141. if !fromExist {
  142. return errResponse, errors.New(fromEntity.DomainCNName() + "不存在")
  143. }
  144. }
  145. toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  146. TableName: middleTableName,
  147. SelectClauses: []string{toRelationColumnName},
  148. Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
  149. PageNo: queryParams.GetPageNo(),
  150. PageSize: queryParams.GetPageSize(),
  151. })
  152. if err != nil {
  153. return errResponse, err
  154. }
  155. if toIDResults == nil || len(toIDResults) == 0 {
  156. return response.InfosData[TI]{
  157. Infos: make([]TI, 0),
  158. TotalCount: 0,
  159. PageNo: queryParams.GetPageNo(),
  160. }, nil
  161. }
  162. toIDs := make([]string, 0)
  163. err = sql.ParseSqlResult(toIDResults, &toIDs)
  164. if err != nil {
  165. return errResponse, err
  166. }
  167. toResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  168. TableName: toTableName,
  169. Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
  170. })
  171. if err != nil {
  172. return errResponse, err
  173. }
  174. infos := make([]TI, 0)
  175. err = sql.ParseSqlResult(toResults, &infos)
  176. if err != nil {
  177. return errResponse, err
  178. }
  179. output := response.InfosData[TI]{
  180. Infos: infos,
  181. TotalCount: totalCount,
  182. PageNo: queryParams.GetPageNo(),
  183. }
  184. return output, nil
  185. }
  186. }
  187. func QueryToRemote(middleTableName string, fromRemote bool, fromTableName string, fromRelationColumnName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[string]] {
  188. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[string], error) {
  189. errResponse := response.InfosData[string]{
  190. Infos: make([]string, 0),
  191. }
  192. if params == nil {
  193. return errResponse, errors.New("请求参数为空")
  194. }
  195. object := objects[0]
  196. if object == nil {
  197. return errResponse, errors.New("领域实体为空")
  198. }
  199. dbExecutor := i.DBExecutor()
  200. queryParams, ok := params.(request.QueryRequestParams)
  201. if !ok {
  202. return errResponse, errors.New("请求参数不是Query接口")
  203. }
  204. fromEntity, ok := object.(entity.Entity)
  205. if !ok {
  206. return errResponse, errors.New("领域对象不是实体")
  207. }
  208. if !fromRemote {
  209. // from存在性校验
  210. fromExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  211. TableName: fromTableName,
  212. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  213. })
  214. if err != nil {
  215. return errResponse, err
  216. }
  217. if !fromExist {
  218. return errResponse, errors.New(fromEntity.DomainCNName() + "不存在")
  219. }
  220. }
  221. toIDResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  222. TableName: middleTableName,
  223. SelectClauses: []string{toRelationColumnName},
  224. Conditions: sql.NewConditions().Equal(fromRelationColumnName, fromEntity.GetID()),
  225. PageNo: queryParams.GetPageNo(),
  226. PageSize: queryParams.GetPageSize(),
  227. })
  228. if err != nil {
  229. return errResponse, err
  230. }
  231. if toIDResults == nil || len(toIDResults) == 0 {
  232. return response.InfosData[string]{
  233. Infos: make([]string, 0),
  234. TotalCount: 0,
  235. PageNo: queryParams.GetPageNo(),
  236. }, nil
  237. }
  238. toIDs := make([]string, 0)
  239. err = sql.ParseSqlResult(toIDResults, &toIDs)
  240. if err != nil {
  241. return errResponse, err
  242. }
  243. return response.InfosData[string]{
  244. Infos: toIDs,
  245. TotalCount: totalCount,
  246. PageNo: queryParams.GetPageNo(),
  247. }, nil
  248. }
  249. }