service.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package one2one
  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-framework/baize/framework/core/tag/sql/sql_mapping"
  13. "git.sxidc.com/go-tools/utils/reflectutils"
  14. "git.sxidc.com/go-tools/utils/strutils"
  15. "github.com/pkg/errors"
  16. "reflect"
  17. )
  18. func Update(fromTableName string, fromRelationFieldName string, fromRelationColumnName string,
  19. toTableName string, toDomainCNName string, toRelationColumnName string) binding.ServiceFunc[any] {
  20. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  21. object := objects[0]
  22. if object == nil {
  23. return nil, errors.New("领域实体为空")
  24. }
  25. dbExecutor := i.DBExecutor()
  26. fromEntity, ok := object.(entity.Entity)
  27. if !ok {
  28. return nil, errors.New("领域对象不是实体")
  29. }
  30. // 字段校验
  31. err := entity.CheckFieldID(fromEntity)
  32. if err != nil {
  33. return nil, err
  34. }
  35. // from存在性校验
  36. fromResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  37. TableName: fromTableName,
  38. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  39. })
  40. if err != nil {
  41. if database.IsErrorDBRecordNotExist(err) {
  42. return nil, errors.New(fromEntity.DomainCNName() + "不存在")
  43. }
  44. return nil, err
  45. }
  46. if !domain.HasField(object, fromRelationFieldName) {
  47. return nil, errors.New("关联字段" + fromRelationFieldName + "不存在")
  48. }
  49. existFrom := reflect.New(reflect.TypeOf(object).Elem()).Interface().(domain.Object)
  50. err = sql.ParseSqlResult(fromResult, existFrom)
  51. if err != nil {
  52. return nil, err
  53. }
  54. currentToID, err := domain.Field[string](existFrom, fromRelationFieldName)
  55. if err != nil {
  56. return nil, err
  57. }
  58. newToID, err := domain.Field[string](object, fromRelationFieldName)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if strutils.IsStringNotEmpty(newToID) {
  63. // to存在性校验
  64. toExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  65. TableName: toTableName,
  66. Conditions: sql.NewConditions().Equal(entity.ColumnID, newToID),
  67. })
  68. if err != nil {
  69. return nil, err
  70. }
  71. if !toExist {
  72. return nil, errors.New(fromEntity.DomainCNName() + "关联的" + toDomainCNName + "不存在")
  73. }
  74. }
  75. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  76. if strutils.IsStringNotEmpty(fromTableName) {
  77. err := database.Update(tx, &sql.UpdateExecuteParams{
  78. TableName: fromTableName,
  79. TableRow: sql.NewTableRow().Add(toRelationColumnName, newToID),
  80. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. if strutils.IsStringNotEmpty(toTableName) &&
  87. (strutils.IsStringNotEmpty(currentToID) || strutils.IsStringNotEmpty(newToID)) {
  88. fromID := fromEntity.GetID()
  89. if strutils.IsStringEmpty(newToID) {
  90. fromID = ""
  91. }
  92. updateToID := currentToID
  93. if strutils.IsStringEmpty(currentToID) {
  94. updateToID = newToID
  95. }
  96. err := database.Update(tx, &sql.UpdateExecuteParams{
  97. TableName: toTableName,
  98. TableRow: sql.NewTableRow().Add(fromRelationColumnName, fromID),
  99. Conditions: sql.NewConditions().Equal(entity.ColumnID, updateToID),
  100. })
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. })
  107. if err != nil {
  108. return nil, err
  109. }
  110. return nil, nil
  111. }
  112. }
  113. func Query[TI any](fromTableName string, fromRelationFieldName string, toTableName string) binding.ServiceFunc[TI] {
  114. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
  115. outputZero := reflectutils.Zero[TI]()
  116. object := objects[0]
  117. if object == nil {
  118. return outputZero, errors.New("领域实体为空")
  119. }
  120. dbExecutor := i.DBExecutor()
  121. fromEntity, ok := object.(entity.Entity)
  122. if !ok {
  123. return outputZero, errors.New("领域对象不是实体")
  124. }
  125. // from存在性校验
  126. existFromResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  127. TableName: fromTableName,
  128. Conditions: sql.NewConditions().Equal(entity.ColumnID, fromEntity.GetID()),
  129. })
  130. if err != nil {
  131. if database.IsErrorDBRecordNotExist(err) {
  132. return outputZero, errors.New(fromEntity.DomainCNName() + "不存在")
  133. }
  134. return outputZero, err
  135. }
  136. existFromEntity := reflect.New(reflect.TypeOf(object).Elem()).Interface().(domain.Object)
  137. err = sql.ParseSqlResult(existFromResult, existFromEntity)
  138. if err != nil {
  139. return outputZero, err
  140. }
  141. if !domain.HasField(existFromEntity, fromRelationFieldName) {
  142. return outputZero, errors.New("关联字段" + fromRelationFieldName + "不存在")
  143. }
  144. toID, err := domain.Field[string](existFromEntity, fromRelationFieldName)
  145. if err != nil {
  146. return outputZero, err
  147. }
  148. toResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  149. TableName: toTableName,
  150. Conditions: sql.NewConditions().Equal(entity.ColumnID, toID),
  151. })
  152. if err != nil && !database.IsErrorDBRecordNotExist(err) {
  153. return outputZero, err
  154. }
  155. info := reflectutils.Zero[TI]()
  156. err = sql.ParseSqlResult(toResult, &info)
  157. if err != nil {
  158. return outputZero, err
  159. }
  160. return info, nil
  161. }
  162. }
  163. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  164. func QueryWithOtherInfo[FI any, TI any](fromTableName string, fromFieldCallback ConditionFieldCallback, toTableName string, toRelationColumnName string) binding.ServiceFunc[response.InfosData[map[string]any]] {
  165. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[map[string]any], error) {
  166. errResponse := response.InfosData[map[string]any]{
  167. Infos: make([]map[string]any, 0),
  168. }
  169. if params == nil {
  170. return errResponse, errors.New("请求参数为空")
  171. }
  172. object := objects[0]
  173. if object == nil {
  174. return errResponse, errors.New("领域实体为空")
  175. }
  176. dbExecutor := i.DBExecutor()
  177. queryParams, ok := params.(request.QueryRequestParams)
  178. if !ok {
  179. return errResponse, errors.New("请求参数不是Query接口")
  180. }
  181. fromEntity, ok := object.(entity.Entity)
  182. if !ok {
  183. return errResponse, errors.New("领域对象不是实体")
  184. }
  185. conditions := sql.NewConditions()
  186. fromFields, err := sql_mapping.DefaultUsage(fromEntity)
  187. if err != nil {
  188. return errResponse, err
  189. }
  190. for _, fromField := range fromFields {
  191. hasDeal := false
  192. if fromFieldCallback != nil {
  193. hasDeal, err = fromFieldCallback(conditions, fromField.FieldName, fromField.ColumnName, fromField.Value)
  194. if err != nil {
  195. return errResponse, err
  196. }
  197. }
  198. if !hasDeal {
  199. fieldValue := reflect.ValueOf(fromField.Value)
  200. if !fieldValue.IsZero() {
  201. conditions.Equal(fromField.ColumnName, fromField.Value)
  202. }
  203. }
  204. }
  205. fromResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  206. TableName: fromTableName,
  207. Conditions: conditions,
  208. PageNo: queryParams.GetPageNo(),
  209. PageSize: queryParams.GetPageSize(),
  210. })
  211. if err != nil {
  212. return errResponse, nil
  213. }
  214. if fromResults == nil || len(fromResults) == 0 {
  215. return response.InfosData[map[string]any]{
  216. Infos: make([]map[string]any, 0),
  217. TotalCount: 0,
  218. PageNo: 0,
  219. }, nil
  220. }
  221. toIDs := make([]string, 0)
  222. for _, fromResult := range fromResults {
  223. toID := fromResult.ColumnValueString(toRelationColumnName)
  224. if strutils.IsStringNotEmpty(toID) {
  225. toIDs = append(toIDs, toID)
  226. }
  227. }
  228. toResultMap := make(map[string]sql.Result)
  229. if toIDs != nil && len(toIDs) != 0 {
  230. toResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  231. TableName: toTableName,
  232. Conditions: sql.NewConditions().In(entity.ColumnID, toIDs),
  233. })
  234. if err != nil {
  235. return errResponse, err
  236. }
  237. for _, toResult := range toResults {
  238. toID := toResult.ColumnValueString(entity.ColumnID)
  239. toResultMap[toID] = toResult
  240. }
  241. }
  242. infos := make([]map[string]any, len(fromResults))
  243. for index, fromResult := range fromResults {
  244. fromInfo := reflectutils.Zero[FI]()
  245. toInfo := reflectutils.Zero[TI]()
  246. err = sql.ParseSqlResult(fromResult, &fromInfo)
  247. if err != nil {
  248. return errResponse, err
  249. }
  250. toID := fromResult.ColumnValueString(toRelationColumnName)
  251. if strutils.IsStringEmpty(toID) {
  252. infos[index] = map[string]any{
  253. "self": fromInfo,
  254. "with": toInfo,
  255. }
  256. continue
  257. }
  258. toResult, ok := toResultMap[toID]
  259. if !ok {
  260. infos[index] = map[string]any{
  261. "self": fromInfo,
  262. "with": toInfo,
  263. }
  264. continue
  265. }
  266. err = sql.ParseSqlResult(toResult, &toInfo)
  267. if err != nil {
  268. return errResponse, err
  269. }
  270. infos[index] = map[string]any{
  271. "self": fromInfo,
  272. "with": toInfo,
  273. }
  274. }
  275. return response.InfosData[map[string]any]{
  276. Infos: infos,
  277. TotalCount: totalCount,
  278. PageNo: queryParams.GetPageNo(),
  279. }, nil
  280. }
  281. }