service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 := domain.CheckField(leftEntity, entity.FieldID, leftEntity.GetFieldMap())
  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, leftQueryOrderBy 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. OrderBy: leftQueryOrderBy,
  138. })
  139. if err != nil && !database.IsErrorDBRecordNotExist(err) {
  140. return errResponse, err
  141. }
  142. infos := make([]RI, 0)
  143. err = sql.ParseSqlResult(rightResults, &infos)
  144. if err != nil {
  145. return errResponse, err
  146. }
  147. return response.InfosData[RI]{
  148. Infos: infos,
  149. TotalCount: totalCount,
  150. PageNo: queryParams.GetPageNo(),
  151. }, nil
  152. }
  153. }
  154. func UpdateRight(rightTableName string, rightRelationFieldName string,
  155. leftTableName string, leftDomainCNName string, leftRelationColumnName string) binding.ServiceFunc[any] {
  156. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  157. object := objects[0]
  158. if object == nil {
  159. return nil, errors.New("领域实体为空")
  160. }
  161. dbExecutor := i.DBExecutor()
  162. rightEntity, ok := object.(entity.Entity)
  163. if !ok {
  164. return nil, errors.New("领域对象不是实体")
  165. }
  166. // 字段校验
  167. err := domain.CheckField(rightEntity, entity.FieldID, rightEntity.GetFieldMap())
  168. if err != nil {
  169. return nil, err
  170. }
  171. // right存在性校验
  172. rightExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  173. TableName: rightTableName,
  174. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  175. })
  176. if err != nil {
  177. return nil, err
  178. }
  179. if !rightExist {
  180. return nil, errors.New(rightEntity.DomainCNName() + "不存在")
  181. }
  182. if !domain.HasField(object, rightRelationFieldName) {
  183. return nil, errors.New("关联字段" + rightRelationFieldName + "不存在")
  184. }
  185. leftID, err := domain.Field[string](object, rightRelationFieldName)
  186. if err != nil {
  187. return nil, err
  188. }
  189. if strutils.IsStringNotEmpty(leftID) {
  190. leftExist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  191. TableName: leftTableName,
  192. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
  193. })
  194. if err != nil {
  195. return nil, err
  196. }
  197. if !leftExist {
  198. return nil, errors.New(leftDomainCNName + "不存在")
  199. }
  200. }
  201. err = database.Update(dbExecutor, &sql.UpdateExecuteParams{
  202. TableName: rightTableName,
  203. TableRow: sql.NewTableRow().Add(leftRelationColumnName, leftID),
  204. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  205. })
  206. if err != nil {
  207. return nil, err
  208. }
  209. return nil, nil
  210. }
  211. }
  212. func QueryRight[LI any](rightTableName string, rightRelationFieldName string, leftTableName string) binding.ServiceFunc[LI] {
  213. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (LI, error) {
  214. outputZero := reflectutils.Zero[LI]()
  215. dbExecutor := i.DBExecutor()
  216. object := objects[0]
  217. if object == nil {
  218. return outputZero, errors.New("领域实体为空")
  219. }
  220. rightEntity, ok := object.(entity.Entity)
  221. if !ok {
  222. return outputZero, errors.New("领域对象不是实体")
  223. }
  224. // right存在性校验
  225. existRightResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  226. TableName: rightTableName,
  227. Conditions: sql.NewConditions().Equal(entity.ColumnID, rightEntity.GetID()),
  228. })
  229. if err != nil {
  230. if database.IsErrorDBRecordNotExist(err) {
  231. return outputZero, errors.New(rightEntity.DomainCNName() + "不存在")
  232. }
  233. return outputZero, err
  234. }
  235. existRightEntity := reflect.New(reflect.TypeOf(object).Elem()).Interface().(domain.Object)
  236. err = sql.ParseSqlResult(existRightResult, existRightEntity)
  237. if err != nil {
  238. return outputZero, err
  239. }
  240. if !domain.HasField(existRightEntity, rightRelationFieldName) {
  241. return outputZero, errors.New("关联字段" + rightRelationFieldName + "不存在")
  242. }
  243. leftID, err := domain.Field[string](existRightEntity, rightRelationFieldName)
  244. if err != nil {
  245. return outputZero, err
  246. }
  247. leftResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  248. TableName: leftTableName,
  249. Conditions: sql.NewConditions().Equal(entity.ColumnID, leftID),
  250. })
  251. if err != nil && !database.IsErrorDBRecordNotExist(err) {
  252. return outputZero, err
  253. }
  254. leftInfo := reflectutils.Zero[LI]()
  255. err = sql.ParseSqlResult(leftResult, &leftInfo)
  256. if err != nil {
  257. return outputZero, err
  258. }
  259. return leftInfo, nil
  260. }
  261. }
  262. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  263. func QueryRightWithLeftInfo[RI any, LI any](rightTableName string, rightFieldCallback ConditionFieldCallback, leftTableName string, leftRelationColumnName string) binding.ServiceFunc[response.InfosData[map[string]any]] {
  264. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[map[string]any], error) {
  265. errResponse := response.InfosData[map[string]any]{
  266. Infos: make([]map[string]any, 0),
  267. }
  268. if params == nil {
  269. return errResponse, errors.New("请求参数为空")
  270. }
  271. object := objects[0]
  272. if object == nil {
  273. return errResponse, errors.New("领域实体为空")
  274. }
  275. dbExecutor := i.DBExecutor()
  276. queryParams, ok := params.(request.QueryRequestParams)
  277. if !ok {
  278. return errResponse, errors.New("请求参数不是Query接口")
  279. }
  280. rightEntity, ok := object.(entity.Entity)
  281. if !ok {
  282. return errResponse, errors.New("领域对象不是实体")
  283. }
  284. conditions := sql.NewConditions()
  285. rightFields, err := sql_mapping.DefaultUsage(rightEntity)
  286. if err != nil {
  287. return errResponse, err
  288. }
  289. for _, rightField := range rightFields {
  290. hasDeal := false
  291. if rightFieldCallback != nil {
  292. hasDeal, err = rightFieldCallback(conditions, rightField.FieldName, rightField.ColumnName, rightField.Value)
  293. if err != nil {
  294. return errResponse, err
  295. }
  296. }
  297. if !hasDeal {
  298. fieldValue := reflect.ValueOf(rightField.Value)
  299. if !fieldValue.IsZero() {
  300. conditions.Equal(rightField.ColumnName, rightField.Value)
  301. }
  302. }
  303. }
  304. rightResults, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  305. TableName: rightTableName,
  306. Conditions: conditions,
  307. PageNo: queryParams.GetPageNo(),
  308. PageSize: queryParams.GetPageSize(),
  309. })
  310. if err != nil {
  311. return errResponse, nil
  312. }
  313. if rightResults == nil || len(rightResults) == 0 {
  314. return response.InfosData[map[string]any]{
  315. Infos: make([]map[string]any, 0),
  316. TotalCount: 0,
  317. PageNo: 0,
  318. }, nil
  319. }
  320. leftIDs := make([]string, 0)
  321. for _, rightResult := range rightResults {
  322. leftID := rightResult.ColumnValueString(leftRelationColumnName)
  323. if strutils.IsStringNotEmpty(leftID) {
  324. leftIDs = append(leftIDs, leftID)
  325. }
  326. }
  327. leftResultMap := make(map[string]sql.Result)
  328. if leftIDs != nil && len(leftIDs) != 0 {
  329. leftResults, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  330. TableName: leftTableName,
  331. Conditions: sql.NewConditions().In(entity.ColumnID, leftIDs),
  332. })
  333. if err != nil {
  334. return errResponse, err
  335. }
  336. for _, leftResult := range leftResults {
  337. leftID := leftResult.ColumnValueString(entity.ColumnID)
  338. leftResultMap[leftID] = leftResult
  339. }
  340. }
  341. infos := make([]map[string]any, len(rightResults))
  342. for index, rightResult := range rightResults {
  343. rightInfo := reflectutils.Zero[RI]()
  344. leftInfo := reflectutils.Zero[LI]()
  345. err = sql.ParseSqlResult(rightResult, &rightInfo)
  346. if err != nil {
  347. return errResponse, err
  348. }
  349. leftID := rightResult.ColumnValueString(leftRelationColumnName)
  350. if strutils.IsStringEmpty(leftID) {
  351. infos[index] = map[string]any{
  352. "self": rightInfo,
  353. "with": leftInfo,
  354. }
  355. continue
  356. }
  357. leftResult, ok := leftResultMap[leftID]
  358. if !ok {
  359. infos[index] = map[string]any{
  360. "self": rightInfo,
  361. "with": leftInfo,
  362. }
  363. continue
  364. }
  365. err = sql.ParseSqlResult(leftResult, &leftInfo)
  366. if err != nil {
  367. return errResponse, err
  368. }
  369. infos[index] = map[string]any{
  370. "self": rightInfo,
  371. "with": leftInfo,
  372. }
  373. }
  374. return response.InfosData[map[string]any]{
  375. Infos: infos,
  376. TotalCount: totalCount,
  377. PageNo: queryParams.GetPageNo(),
  378. }, nil
  379. }
  380. }