service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package one2many
  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/slice"
  15. "git.sxidc.com/go-tools/utils/strutils"
  16. "github.com/pkg/errors"
  17. "reflect"
  18. )
  19. func UpdateLeft(leftTableName string, leftDomainCNName string, leftRelationFieldName string, leftRelationColumnName string,
  20. rightTableName string, rightDomainCNName string) binding.ServiceFunc[any] {
  21. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  22. object := objects[0]
  23. if object == nil {
  24. return nil, errors.New("领域实体为空")
  25. }
  26. dbExecutor := i.DBExecutor()
  27. leftEntity, ok := object.(entity.Entity)
  28. if !ok {
  29. return nil, errors.New("领域对象不是实体")
  30. }
  31. // 字段校验
  32. err := entity.CheckFieldID(leftEntity)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // left存在性校验
  37. leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  38. TableName: leftTableName,
  39. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftEntity.GetID()),
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. if !leftExist {
  45. return nil, errors.New(leftEntity.DomainCNName() + "不存在")
  46. }
  47. if !domain.HasField(object, leftRelationFieldName) {
  48. return nil, errors.New("关联字段" + leftRelationFieldName + "不存在")
  49. }
  50. rightIDs, err := domain.Field[[]string](object, leftRelationFieldName)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if rightIDs != nil && len(rightIDs) != 0 {
  55. for _, rightID := range rightIDs {
  56. err := entity.CheckIDTypeValue(leftDomainCNName, leftRelationFieldName, rightID)
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. rightIDs = slice.RemoveRepeatElement(rightIDs)
  62. }
  63. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  64. err := database.Update(tx, &sql.UpdateExecuteParams{
  65. TableName: rightTableName,
  66. TableRow: sql.NewTableRow().Add(leftRelationColumnName, ""),
  67. Conditions: sql.NewConditions().Equal(leftRelationColumnName, leftEntity.GetID()),
  68. })
  69. if err != nil {
  70. return err
  71. }
  72. if rightIDs == nil || len(rightIDs) == 0 {
  73. return nil
  74. }
  75. rightCount, err := database.Count(dbExecutor, &sql.CountExecuteParams{
  76. TableName: rightTableName,
  77. Conditions: sql.NewConditions().In(entity.ColumnID, rightIDs),
  78. })
  79. if err != nil {
  80. return err
  81. }
  82. if int(rightCount) != len(rightIDs) {
  83. return errors.New("部分" + rightDomainCNName + "不存在")
  84. }
  85. err = database.Update(tx, &sql.UpdateExecuteParams{
  86. TableName: rightTableName,
  87. TableRow: sql.NewTableRow().Add(leftRelationColumnName, leftEntity.GetID()),
  88. Conditions: sql.NewConditions().In(entity.ColumnID, rightIDs),
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. return nil, nil
  99. }
  100. }
  101. func QueryLeft[RI any](leftTableName string, leftRelationColumnName string,
  102. rightTableName string) binding.ServiceFunc[response.InfosData[RI]] {
  103. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[RI], error) {
  104. errResponse := response.InfosData[RI]{
  105. Infos: make([]RI, 0),
  106. }
  107. if params == nil {
  108. return errResponse, errors.New("请求参数为空")
  109. }
  110. object := objects[0]
  111. if object == nil {
  112. return errResponse, errors.New("领域实体为空")
  113. }
  114. dbExecutor := i.DBExecutor()
  115. queryParams, ok := params.(request.QueryWithIDRequestParams)
  116. if !ok {
  117. return errResponse, errors.New("请求参数不是Query接口")
  118. }
  119. leftEntity, ok := object.(entity.Entity)
  120. if !ok {
  121. return errResponse, errors.New("领域对象不是实体")
  122. }
  123. // left存在性校验
  124. leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  125. TableName: leftTableName,
  126. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftEntity.GetID()),
  127. })
  128. if err != nil {
  129. return errResponse, err
  130. }
  131. if !leftExist {
  132. return errResponse, errors.New(leftEntity.DomainCNName() + "不存在")
  133. }
  134. rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  135. TableName: rightTableName,
  136. Conditions: sql.NewConditions().Equal(leftRelationColumnName, leftEntity.GetID()),
  137. })
  138. if err != nil && !database.IsErrorDBRecordNotExist(err) {
  139. return errResponse, err
  140. }
  141. infos := make([]RI, 0)
  142. err = sql.ParseSqlResult(rightResults, &infos)
  143. if err != nil {
  144. return errResponse, err
  145. }
  146. return response.InfosData[RI]{
  147. Infos: infos,
  148. TotalCount: totalCount,
  149. PageNo: queryParams.GetPageNo(),
  150. }, nil
  151. }
  152. }
  153. func UpdateRight(rightTableName string, rightRelationFieldName string,
  154. leftTableName string, leftDomainCNName string, leftRelationColumnName string) binding.ServiceFunc[any] {
  155. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  156. object := objects[0]
  157. if object == nil {
  158. return nil, errors.New("领域实体为空")
  159. }
  160. dbExecutor := i.DBExecutor()
  161. rightEntity, ok := object.(entity.Entity)
  162. if !ok {
  163. return nil, errors.New("领域对象不是实体")
  164. }
  165. // 字段校验
  166. err := entity.CheckFieldID(rightEntity)
  167. if err != nil {
  168. return nil, err
  169. }
  170. // right存在性校验
  171. rightExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  172. TableName: rightTableName,
  173. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  174. })
  175. if err != nil {
  176. return nil, err
  177. }
  178. if !rightExist {
  179. return nil, errors.New(rightEntity.DomainCNName() + "不存在")
  180. }
  181. if !domain.HasField(object, rightRelationFieldName) {
  182. return nil, errors.New("关联字段" + rightRelationFieldName + "不存在")
  183. }
  184. leftID, err := domain.Field[string](object, rightRelationFieldName)
  185. if err != nil {
  186. return nil, err
  187. }
  188. if strutils.IsStringNotEmpty(leftID) {
  189. leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  190. TableName: leftTableName,
  191. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
  192. })
  193. if err != nil {
  194. return nil, err
  195. }
  196. if !leftExist {
  197. return nil, errors.New(leftDomainCNName + "不存在")
  198. }
  199. }
  200. err = database.Update(dbExecutor, &sql.UpdateExecuteParams{
  201. TableName: rightTableName,
  202. TableRow: sql.NewTableRow().Add(leftRelationColumnName, leftID),
  203. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  204. })
  205. if err != nil {
  206. return nil, err
  207. }
  208. return nil, nil
  209. }
  210. }
  211. func QueryRight[LI any](rightTableName string, rightRelationFieldName string, leftTableName string) binding.ServiceFunc[LI] {
  212. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (LI, error) {
  213. outputZero := reflectutils.Zero[LI]()
  214. dbExecutor := i.DBExecutor()
  215. object := objects[0]
  216. if object == nil {
  217. return outputZero, errors.New("领域实体为空")
  218. }
  219. rightEntity, ok := object.(entity.Entity)
  220. if !ok {
  221. return outputZero, errors.New("领域对象不是实体")
  222. }
  223. // right存在性校验
  224. existRightResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  225. TableName: rightTableName,
  226. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  227. })
  228. if err != nil {
  229. if database.IsErrorDBRecordNotExist(err) {
  230. return outputZero, errors.New(rightEntity.DomainCNName() + "不存在")
  231. }
  232. return outputZero, err
  233. }
  234. existRightEntity := reflect.New(reflect.TypeOf(object).Elem()).Interface().(domain.Object)
  235. err = sql.ParseSqlResult(existRightResult, existRightEntity)
  236. if err != nil {
  237. return outputZero, err
  238. }
  239. if !domain.HasField(existRightEntity, rightRelationFieldName) {
  240. return outputZero, errors.New("关联字段" + rightRelationFieldName + "不存在")
  241. }
  242. leftID, err := domain.Field[string](existRightEntity, rightRelationFieldName)
  243. if err != nil {
  244. return outputZero, err
  245. }
  246. leftResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  247. TableName: leftTableName,
  248. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
  249. })
  250. if err != nil && !database.IsErrorDBRecordNotExist(err) {
  251. return outputZero, err
  252. }
  253. leftInfo := reflectutils.Zero[LI]()
  254. err = sql.ParseSqlResult(leftResult, &leftInfo)
  255. if err != nil {
  256. return outputZero, err
  257. }
  258. return leftInfo, nil
  259. }
  260. }
  261. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  262. func QueryRightWithLeftInfo[RI any, LI any](rightTableName string, rightFieldCallback ConditionFieldCallback, leftTableName string, leftRelationColumnName string) binding.ServiceFunc[response.InfosData[map[string]any]] {
  263. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[map[string]any], error) {
  264. errResponse := response.InfosData[map[string]any]{
  265. Infos: make([]map[string]any, 0),
  266. }
  267. if params == nil {
  268. return errResponse, errors.New("请求参数为空")
  269. }
  270. object := objects[0]
  271. if object == nil {
  272. return errResponse, errors.New("领域实体为空")
  273. }
  274. dbExecutor := i.DBExecutor()
  275. queryParams, ok := params.(request.QueryRequestParams)
  276. if !ok {
  277. return errResponse, errors.New("请求参数不是Query接口")
  278. }
  279. rightEntity, ok := object.(entity.Entity)
  280. if !ok {
  281. return errResponse, errors.New("领域对象不是实体")
  282. }
  283. conditions := sql.NewConditions()
  284. rightFields, err := sql_mapping.DefaultUsage(rightEntity)
  285. if err != nil {
  286. return errResponse, err
  287. }
  288. for _, rightField := range rightFields {
  289. hasDeal := false
  290. if rightFieldCallback != nil {
  291. hasDeal, err = rightFieldCallback(conditions, rightField.FieldName, rightField.ColumnName, rightField.Value)
  292. if err != nil {
  293. return errResponse, err
  294. }
  295. }
  296. if !hasDeal {
  297. fieldValue := reflect.ValueOf(rightField.Value)
  298. if !fieldValue.IsZero() {
  299. conditions.Equal(rightField.ColumnName, rightField.Value)
  300. }
  301. }
  302. }
  303. rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  304. TableName: rightTableName,
  305. Conditions: conditions,
  306. PageNo: queryParams.GetPageNo(),
  307. PageSize: queryParams.GetPageSize(),
  308. })
  309. if err != nil {
  310. return errResponse, nil
  311. }
  312. if rightResults == nil || len(rightResults) == 0 {
  313. return response.InfosData[map[string]any]{
  314. Infos: make([]map[string]any, 0),
  315. TotalCount: 0,
  316. PageNo: 0,
  317. }, nil
  318. }
  319. leftIDs := make([]string, 0)
  320. for _, rightResult := range rightResults {
  321. leftID := rightResult.ColumnValueString(leftRelationColumnName)
  322. if strutils.IsStringNotEmpty(leftID) {
  323. leftIDs = append(leftIDs, leftID)
  324. }
  325. }
  326. leftResultMap := make(map[string]sql.Result)
  327. if leftIDs != nil && len(leftIDs) != 0 {
  328. leftResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  329. TableName: leftTableName,
  330. Conditions: sql.NewConditions().In(entity.ColumnID, leftIDs),
  331. })
  332. if err != nil {
  333. return errResponse, err
  334. }
  335. for _, leftResult := range leftResults {
  336. leftID := leftResult.ColumnValueString(entity.ColumnID)
  337. leftResultMap[leftID] = leftResult
  338. }
  339. }
  340. infos := make([]map[string]any, len(rightResults))
  341. for index, rightResult := range rightResults {
  342. rightInfo := reflectutils.Zero[RI]()
  343. leftInfo := reflectutils.Zero[LI]()
  344. err = sql.ParseSqlResult(rightResult, &rightInfo)
  345. if err != nil {
  346. return errResponse, err
  347. }
  348. leftID := rightResult.ColumnValueString(leftRelationColumnName)
  349. if strutils.IsStringEmpty(leftID) {
  350. infos[index] = map[string]any{
  351. "self": rightInfo,
  352. "with": leftInfo,
  353. }
  354. continue
  355. }
  356. leftResult, ok := leftResultMap[leftID]
  357. if !ok {
  358. infos[index] = map[string]any{
  359. "self": rightInfo,
  360. "with": leftInfo,
  361. }
  362. continue
  363. }
  364. err = sql.ParseSqlResult(leftResult, &leftInfo)
  365. if err != nil {
  366. return errResponse, err
  367. }
  368. infos[index] = map[string]any{
  369. "self": rightInfo,
  370. "with": leftInfo,
  371. }
  372. }
  373. return response.InfosData[map[string]any]{
  374. Infos: infos,
  375. TotalCount: totalCount,
  376. PageNo: queryParams.GetPageNo(),
  377. }, nil
  378. }
  379. }