about_us.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package service
  2. import (
  3. "erpims/application/domain/about_us"
  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 aboutUsService = &AboutUsService{}
  20. type AboutUsService struct{}
  21. func (svc *AboutUsService) Init(appInstance *application.App) error {
  22. svc.v1(appInstance)
  23. return nil
  24. }
  25. func (svc *AboutUsService) Destroy() error {
  26. return nil
  27. }
  28. func (svc *AboutUsService) v1(appInstance *application.App) {
  29. v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
  30. appInstance.Infrastructure())
  31. entity_crud.BindSimple[about_us.Info](v1Binder,
  32. &entity_crud.Simple[about_us.Info]{
  33. Entity: &about_us.Entity{},
  34. Schema: dbSchema,
  35. CreateJsonBody: &about_us.CreateAboutUsJsonBody{},
  36. DeleteQueryParams: &about_us.DeleteAboutUsQueryParams{},
  37. UpdateJsonBody: &about_us.UpdateAboutUsJsonBody{},
  38. QueryQueryParams: &about_us.GetAboutUsesQueryParams{},
  39. GetByIDQueryParams: &about_us.GetAboutUsQueryParams{},
  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[*about_us.CreateAboutUsJsonBody](params)
  45. if err != nil {
  46. return err
  47. }
  48. info, err := domain.ToConcrete[*about_us.Entity](e)
  49. if err != nil {
  50. return err
  51. }
  52. //图片处理
  53. if strutils.IsStringNotEmpty(requestParams.WxMpQrCode) {
  54. newPicture, err := oss.MultipleFileMv(requestParams.WxMpQrCode)
  55. if err != nil {
  56. return err
  57. }
  58. info.WxMpQrCode = newPicture
  59. }
  60. if strutils.IsStringNotEmpty(requestParams.ContactQrCode) {
  61. newPicture, err := oss.MultipleFileMv(requestParams.ContactQrCode)
  62. if err != nil {
  63. return err
  64. }
  65. info.ContactQrCode = newPicture
  66. }
  67. return nil
  68. },
  69. }),
  70. entity_crud.WithUpdateTx(),
  71. entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  72. Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  73. requestParams, err := request.ToConcrete[*about_us.UpdateAboutUsJsonBody](params)
  74. info, err := domain.ToConcrete[*about_us.Entity](e)
  75. if err != nil {
  76. return err
  77. }
  78. if strutils.IsStringNotEmpty(requestParams.WxMpQrCode) {
  79. newPicture, err := oss.MultipleFileMv(requestParams.WxMpQrCode)
  80. if err != nil {
  81. return err
  82. }
  83. info.WxMpQrCode = newPicture
  84. }
  85. //封面处理
  86. if strutils.IsStringNotEmpty(requestParams.ContactQrCode) {
  87. newPicture, err := oss.MultipleFileMv(requestParams.ContactQrCode)
  88. if err != nil {
  89. return err
  90. }
  91. info.ContactQrCode = newPicture
  92. }
  93. return nil
  94. },
  95. }),
  96. entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[about_us.Info]{
  97. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[about_us.Info]) (response.InfosData[about_us.Info], error) {
  98. for i2 := range output.Infos {
  99. item := &output.Infos[i2]
  100. if strutils.IsStringNotEmpty(item.WxMpQrCodeStr) {
  101. fileObjects, err := oss.MultipleFileUrl(item.WxMpQrCodeStr)
  102. if err != nil {
  103. return output, err
  104. }
  105. item.WxMpQrCode = fileObjects
  106. }
  107. if strutils.IsStringNotEmpty(item.ContactQrCodeStr) {
  108. fileObjects, err := oss.MultipleFileUrl(item.ContactQrCodeStr)
  109. if err != nil {
  110. return output, err
  111. }
  112. item.ContactQrCode = fileObjects
  113. }
  114. }
  115. return output, nil
  116. },
  117. }),
  118. entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[about_us.Info]{
  119. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output about_us.Info) (about_us.Info, error) {
  120. if strutils.IsStringNotEmpty(output.WxMpQrCodeStr) {
  121. fileObjects, err := oss.MultipleFileUrl(output.WxMpQrCodeStr)
  122. if err != nil {
  123. return output, err
  124. }
  125. output.WxMpQrCode = fileObjects
  126. }
  127. if strutils.IsStringNotEmpty(output.ContactQrCodeStr) {
  128. fileObjects, err := oss.MultipleFileUrl(output.ContactQrCodeStr)
  129. if err != nil {
  130. return output, err
  131. }
  132. output.ContactQrCode = fileObjects
  133. }
  134. return output, nil
  135. },
  136. }),
  137. entity_crud.WithQueryOrderBy[about_us.Info](entity.ColumnCreatedTime+" desc"))
  138. }
  139. func (svc *AboutUsService) queryByKeyFields(e *about_us.Entity, dbExecutor database.Executor) (*about_us.Entity, error) {
  140. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  141. TableName: domain.TableName(dbSchema, e),
  142. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  143. })
  144. if err != nil {
  145. if database.IsErrorDBRecordNotExist(err) {
  146. return nil, errors.New(e.DomainCNName() + "不存在")
  147. }
  148. return nil, err
  149. }
  150. existEntity := new(about_us.Entity)
  151. err = sql.ParseSqlResult(result, existEntity)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return existEntity, nil
  156. }
  157. func (svc *AboutUsService) checkExistByKeyFields(e *about_us.Entity, dbExecutor database.Executor) error {
  158. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  159. TableName: domain.TableName(dbSchema, e),
  160. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  161. })
  162. if err != nil {
  163. return err
  164. }
  165. if !exist {
  166. return errors.New(e.DomainCNName() + "不存在")
  167. }
  168. return nil
  169. }