banner.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "erpims/application/domain/banner"
  4. "erpims/oss"
  5. "git.sxidc.com/go-framework/baize/convenient/entity_crud"
  6. "git.sxidc.com/go-framework/baize/framework/binding"
  7. "git.sxidc.com/go-framework/baize/framework/core/api"
  8. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  9. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  10. "git.sxidc.com/go-framework/baize/framework/core/application"
  11. "git.sxidc.com/go-framework/baize/framework/core/domain"
  12. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  13. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  14. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  15. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  16. "git.sxidc.com/go-tools/utils/strutils"
  17. "github.com/pkg/errors"
  18. )
  19. var bannerService = &BannerService{}
  20. type BannerService struct{}
  21. func (svc *BannerService) Init(appInstance *application.App) error {
  22. svc.v1(appInstance)
  23. return nil
  24. }
  25. func (svc *BannerService) Destroy() error {
  26. return nil
  27. }
  28. func (svc *BannerService) v1(appInstance *application.App) {
  29. v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
  30. appInstance.Infrastructure())
  31. entity_crud.BindSimple[banner.Info](v1Binder,
  32. &entity_crud.Simple[banner.Info]{
  33. Entity: &banner.Entity{},
  34. Schema: dbSchema,
  35. CreateJsonBody: &banner.CreateBannerJsonBody{},
  36. DeleteQueryParams: &banner.DeleteBannerQueryParams{},
  37. UpdateJsonBody: &banner.UpdateBannerJsonBody{},
  38. QueryQueryParams: &banner.GetBannersQueryParams{},
  39. GetByIDQueryParams: &banner.GetBannerQueryParams{},
  40. },
  41. entity_crud.WithCreateTx(),
  42. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  43. Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  44. requestParams, err := request.ToConcrete[*banner.CreateBannerJsonBody](params)
  45. if err != nil {
  46. return err
  47. }
  48. info, err := domain.ToConcrete[*banner.Entity](e)
  49. if err != nil {
  50. return err
  51. }
  52. //图片处理
  53. if strutils.IsStringNotEmpty(requestParams.PicUrl) {
  54. newPicture, err := oss.MultipleFileMv(requestParams.PicUrl)
  55. if err != nil {
  56. return err
  57. }
  58. info.PicUrl = newPicture
  59. }
  60. return nil
  61. },
  62. }),
  63. entity_crud.WithUpdateTx(),
  64. entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  65. Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  66. requestParams, err := request.ToConcrete[*banner.UpdateBannerJsonBody](params)
  67. info, err := domain.ToConcrete[*banner.Entity](e)
  68. if err != nil {
  69. return err
  70. }
  71. if strutils.IsStringNotEmpty(requestParams.PicUrl) {
  72. newPicture, err := oss.MultipleFileMv(requestParams.PicUrl)
  73. if err != nil {
  74. return err
  75. }
  76. info.PicUrl = newPicture
  77. }
  78. return nil
  79. },
  80. }),
  81. entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[banner.Info]{
  82. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[banner.Info]) (response.InfosData[banner.Info], error) {
  83. for i2 := range output.Infos {
  84. item := &output.Infos[i2]
  85. if strutils.IsStringNotEmpty(item.PicUrlStr) {
  86. fileObjects, err := oss.MultipleFileUrl(item.PicUrlStr)
  87. if err != nil {
  88. return output, err
  89. }
  90. item.PicUrl = fileObjects
  91. }
  92. }
  93. return output, nil
  94. },
  95. }),
  96. entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[banner.Info]{
  97. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output banner.Info) (banner.Info, error) {
  98. if strutils.IsStringNotEmpty(output.PicUrlStr) {
  99. fileObjects, err := oss.MultipleFileUrl(output.PicUrlStr)
  100. if err != nil {
  101. return output, err
  102. }
  103. output.PicUrl = fileObjects
  104. }
  105. return output, nil
  106. },
  107. }),
  108. entity_crud.WithQueryOrderBy[banner.Info](entity.ColumnCreatedTime+" desc"),
  109. entity_crud.WithQueryConditionFieldCallback[banner.Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
  110. switch fieldName {
  111. case banner.FieldTitle:
  112. conditions.Like(columnName, "%"+value.(string)+"%")
  113. return true, nil
  114. case banner.ColumnPageUrl:
  115. conditions.Like(columnName, "%"+value.(string)+"%")
  116. return true, nil
  117. default:
  118. return false, nil
  119. }
  120. }))
  121. }
  122. func (svc *BannerService) queryByKeyFields(e *banner.Entity, dbExecutor database.Executor) (*banner.Entity, error) {
  123. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  124. TableName: domain.TableName(dbSchema, e),
  125. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  126. })
  127. if err != nil {
  128. if database.IsErrorDBRecordNotExist(err) {
  129. return nil, errors.New(e.DomainCNName() + "不存在")
  130. }
  131. return nil, err
  132. }
  133. existEntity := new(banner.Entity)
  134. err = sql.ParseSqlResult(result, existEntity)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return existEntity, nil
  139. }
  140. func (svc *BannerService) checkExistByKeyFields(e *banner.Entity, dbExecutor database.Executor) error {
  141. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  142. TableName: domain.TableName(dbSchema, e),
  143. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  144. })
  145. if err != nil {
  146. return err
  147. }
  148. if !exist {
  149. return errors.New(e.DomainCNName() + "不存在")
  150. }
  151. return nil
  152. }