8
0

mini_live_config.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package service
  2. import (
  3. "ecos/application/domain/mini_live_config"
  4. "ecos/osm"
  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 miniLiveConfigService = &MiniLiveConfigService{}
  20. type MiniLiveConfigService struct{}
  21. func (svc *MiniLiveConfigService) Init(appInstance *application.App) error {
  22. svc.v1(appInstance)
  23. return nil
  24. }
  25. func (svc *MiniLiveConfigService) Destroy() error {
  26. return nil
  27. }
  28. func (svc *MiniLiveConfigService) v1(appInstance *application.App) {
  29. v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
  30. appInstance.Infrastructure())
  31. entity_crud.BindSimple[mini_live_config.Info](v1Binder, &entity_crud.Simple[mini_live_config.Info]{
  32. Entity: &mini_live_config.Entity{},
  33. Schema: dbSchema,
  34. CreateJsonBody: &mini_live_config.CreateMiniLiveConfigJsonBody{},
  35. DeleteQueryParams: &mini_live_config.DeleteMiniLiveConfigQueryParams{},
  36. UpdateJsonBody: &mini_live_config.UpdateMiniLiveConfigJsonBody{},
  37. QueryQueryParams: &mini_live_config.GetMiniLiveConfigsQueryParams{},
  38. GetByIDQueryParams: &mini_live_config.GetMiniLiveConfigQueryParams{},
  39. },
  40. entity_crud.WithCreateTx(),
  41. entity_crud.WithUpdateTx(),
  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[*mini_live_config.CreateMiniLiveConfigJsonBody](params)
  45. if err != nil {
  46. return err
  47. }
  48. record, err := domain.ToConcrete[*mini_live_config.Entity](e)
  49. if err != nil {
  50. return err
  51. }
  52. if strutils.IsStringNotEmpty(requestParams.PlaceholderImage) {
  53. newFilePath, err := osm.MultipleFileMv(requestParams.PlaceholderImage)
  54. if err != nil {
  55. return err
  56. }
  57. record.PlaceholderImage = newFilePath
  58. }
  59. if strutils.IsStringNotEmpty(requestParams.LiveCoverUrl) {
  60. newFilePath, err := osm.MultipleFileMv(requestParams.LiveCoverUrl)
  61. if err != nil {
  62. return err
  63. }
  64. record.LiveCoverUrl = newFilePath
  65. }
  66. return nil
  67. },
  68. }),
  69. entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  70. Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  71. requestParams, err := request.ToConcrete[*mini_live_config.UpdateMiniLiveConfigJsonBody](params)
  72. if err != nil {
  73. return err
  74. }
  75. record, err := domain.ToConcrete[*mini_live_config.Entity](e)
  76. if err != nil {
  77. return err
  78. }
  79. if strutils.IsStringNotEmpty(requestParams.PlaceholderImage) {
  80. newFilePath, err := osm.MultipleFileMv(requestParams.PlaceholderImage)
  81. if err != nil {
  82. return err
  83. }
  84. record.PlaceholderImage = newFilePath
  85. }
  86. if strutils.IsStringNotEmpty(requestParams.LiveCoverUrl) {
  87. newFilePath, err := osm.MultipleFileMv(requestParams.LiveCoverUrl)
  88. if err != nil {
  89. return err
  90. }
  91. record.LiveCoverUrl = newFilePath
  92. }
  93. return nil
  94. },
  95. }),
  96. entity_crud.WithQueryFormCustomConditionFunc[mini_live_config.Info](func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (*entity_crud.CustomCondition, error) {
  97. requestParams, err := request.ToConcrete[*mini_live_config.GetMiniLiveConfigsQueryParams](params)
  98. if err != nil {
  99. return nil, err
  100. }
  101. conditions := sql.NewConditions()
  102. if strutils.IsStringNotEmpty(requestParams.ComponentID) {
  103. conditions.Equal(mini_live_config.ColumnComponentID, requestParams.ComponentID)
  104. }
  105. return &entity_crud.CustomCondition{
  106. Conditions: conditions,
  107. OrderBy: entity.ColumnCreatedTime + " DESC",
  108. }, nil
  109. }),
  110. entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[mini_live_config.Info]{
  111. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output mini_live_config.Info) (mini_live_config.Info, error) {
  112. if strutils.IsStringNotEmpty(output.PlaceholderImageStr) {
  113. fileObjects, err := osm.MultipleFileUrl(output.PlaceholderImageStr)
  114. if err != nil {
  115. return output, err
  116. }
  117. output.PlaceholderImage = fileObjects
  118. }
  119. if strutils.IsStringNotEmpty(output.LiveCoverUrlStr) {
  120. fileObjects, err := osm.MultipleFileUrl(output.LiveCoverUrlStr)
  121. if err != nil {
  122. return output, err
  123. }
  124. output.LiveCoverUrl = fileObjects
  125. }
  126. return output, nil
  127. },
  128. }),
  129. entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[mini_live_config.Info]{
  130. OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[mini_live_config.Info]) (response.InfosData[mini_live_config.Info], error) {
  131. infos := output.Infos
  132. for i1 := range infos {
  133. info := &infos[i1]
  134. if strutils.IsStringNotEmpty(info.PlaceholderImageStr) {
  135. fileObjects, err := osm.MultipleFileUrl(info.PlaceholderImageStr)
  136. if err != nil {
  137. return output, err
  138. }
  139. info.PlaceholderImage = fileObjects
  140. }
  141. if strutils.IsStringNotEmpty(info.LiveCoverUrlStr) {
  142. fileObjects, err := osm.MultipleFileUrl(info.LiveCoverUrlStr)
  143. if err != nil {
  144. return output, err
  145. }
  146. info.LiveCoverUrl = fileObjects
  147. }
  148. }
  149. return output, nil
  150. },
  151. }),
  152. )
  153. binding.PutBind(v1Binder, &binding.SimpleBindItem[any]{
  154. Path: "/miniLiveConfig/enabledOrDisable",
  155. SendResponseFunc: response.SendMsgResponse,
  156. RequestParams: &mini_live_config.EnabledOrDisableJsonBody{},
  157. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  158. requestParams, err := request.ToConcrete[*mini_live_config.EnabledOrDisableJsonBody](params)
  159. if err != nil {
  160. return nil, err
  161. }
  162. err = database.Transaction(i.DBExecutor(), func(tx database.Executor) error {
  163. err = database.Update(tx, &sql.UpdateExecuteParams{
  164. TableName: domain.TableName(dbSchema, &mini_live_config.Entity{}),
  165. Conditions: sql.NewConditions().Equal(entity.ColumnID, requestParams.ID),
  166. TableRow: sql.NewTableRow().Add(mini_live_config.ColumnIsEnabled, requestParams.IsEnabled),
  167. })
  168. if requestParams.IsEnabled == Enabled {
  169. err = database.Update(tx, &sql.UpdateExecuteParams{
  170. TableName: domain.TableName(dbSchema, &mini_live_config.Entity{}),
  171. Conditions: sql.NewConditions().Not(entity.ColumnID, requestParams.ID),
  172. TableRow: sql.NewTableRow().Add(mini_live_config.ColumnIsEnabled, Disable),
  173. })
  174. if err != nil {
  175. return err
  176. }
  177. }
  178. return nil
  179. })
  180. return nil, nil
  181. },
  182. })
  183. }
  184. func (svc *MiniLiveConfigService) queryByKeyFields(e *mini_live_config.Entity, dbExecutor database.Executor) (*mini_live_config.Entity, error) {
  185. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  186. TableName: domain.TableName(dbSchema, e),
  187. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  188. })
  189. if err != nil {
  190. if database.IsErrorDBRecordNotExist(err) {
  191. return nil, errors.New(e.DomainCNName() + "不存在")
  192. }
  193. return nil, err
  194. }
  195. existEntity := new(mini_live_config.Entity)
  196. err = sql.ParseSqlResult(result, existEntity)
  197. if err != nil {
  198. return nil, err
  199. }
  200. return existEntity, nil
  201. }
  202. func (svc *MiniLiveConfigService) checkExistByKeyFields(e *mini_live_config.Entity, dbExecutor database.Executor) error {
  203. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  204. TableName: domain.TableName(dbSchema, e),
  205. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  206. })
  207. if err != nil {
  208. return err
  209. }
  210. if !exist {
  211. return errors.New(e.DomainCNName() + "不存在")
  212. }
  213. return nil
  214. }