| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- package service
- import (
- "ecos/application/domain/mini_carousel_config"
- "ecos/application/domain/mini_live_config"
- "ecos/application/domain/mini_page_component"
- "ecos/application/domain/mini_quality_product_config"
- "ecos/application/domain/mini_video_config"
- "ecos/osm"
- "encoding/json"
- "git.sxidc.com/go-framework/baize/convenient/entity_crud"
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/application"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/pkg/errors"
- )
- var miniPageComponentService = &MiniPageComponentService{}
- type MiniPageComponentService struct{}
- func (svc *MiniPageComponentService) Init(appInstance *application.App) error {
- svc.v1(appInstance)
- return nil
- }
- func (svc *MiniPageComponentService) Destroy() error {
- return nil
- }
- func (svc *MiniPageComponentService) v1(appInstance *application.App) {
- v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
- appInstance.Infrastructure())
- entity_crud.BindSimple[mini_page_component.Info](v1Binder, &entity_crud.Simple[mini_page_component.Info]{
- Entity: &mini_page_component.Entity{},
- Schema: dbSchema,
- CreateJsonBody: &mini_page_component.CreateMiniPageComponentJsonBody{},
- DeleteQueryParams: &mini_page_component.DeleteMiniPageComponentQueryParams{},
- UpdateJsonBody: &mini_page_component.UpdateMiniPageComponentJsonBody{},
- QueryQueryParams: &mini_page_component.GetMiniPageComponentsQueryParams{},
- GetByIDQueryParams: &mini_page_component.GetMiniPageComponentQueryParams{},
- },
- entity_crud.WithCreateTx(),
- entity_crud.WithUpdateTx(),
- entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
- Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- requestParams, err := request.ToConcrete[*mini_page_component.CreateMiniPageComponentJsonBody](params)
- if err != nil {
- return err
- }
- record, err := domain.ToConcrete[*mini_page_component.Entity](e)
- if err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(requestParams.ComponentConfig) {
- if requestParams.ComponentTypeID == "1" { //轮播图
- var config mini_page_component.CarouselConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if len(config.Items) > 0 {
- for idx := range config.Items {
- if strutils.IsStringNotEmpty(config.Items[idx].ImageUrl) {
- newFilePath, err := osm.MultipleFileMv(config.Items[idx].ImageUrl)
- if err != nil {
- return err
- }
- config.Items[idx].ImageUrl = newFilePath
- }
- }
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- } else if requestParams.ComponentTypeID == "2" { //2是直播
- var config mini_page_component.LiveWindowConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(config.LiveCoverUrl) {
- newFilePath, err := osm.MultipleFileMv(config.LiveCoverUrl)
- if err != nil {
- return err
- }
- config.LiveCoverUrl = newFilePath
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- } else if requestParams.ComponentTypeID == "3" { //3是宣传视频
- var config mini_page_component.VideoComponentConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(config.VideoUrlStr) {
- newFilePath, err := osm.MultipleFileMv(config.VideoUrlStr)
- if err != nil {
- return err
- }
- config.VideoUrlStr = newFilePath
- }
- if strutils.IsStringNotEmpty(config.VideoCoverStr) {
- newFilePath, err := osm.MultipleFileMv(config.VideoCoverStr)
- if err != nil {
- return err
- }
- config.VideoCoverStr = newFilePath
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- }
- }
- return nil
- },
- After: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- record, err := domain.ToConcrete[*mini_page_component.Entity](e)
- if err != nil {
- return err
- }
- if record.IsEnabled == Enabled {
- //当前启用 把别的关掉
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_page_component.Entity{}),
- Conditions: sql.NewConditions().Not(entity.ColumnID, e.GetID()).Equal(mini_page_component.ColumnComponentTypeID, record.ComponentTypeID),
- TableRow: sql.NewTableRow().Add(mini_page_component.ColumnIsEnabled, Disable),
- })
- if err != nil {
- return err
- }
- }
- return nil
- },
- }),
- entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
- Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- requestParams, err := request.ToConcrete[*mini_page_component.UpdateMiniPageComponentJsonBody](params)
- if err != nil {
- return err
- }
- record, err := domain.ToConcrete[*mini_page_component.Entity](e)
- if err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(requestParams.ComponentConfig) {
- if requestParams.ComponentTypeID == "1" { //轮播图
- var config mini_page_component.CarouselConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if len(config.Items) > 0 {
- for idx := range config.Items {
- if strutils.IsStringNotEmpty(config.Items[idx].ImageUrl) {
- newFilePath, err := osm.MultipleFileMv(config.Items[idx].ImageUrl)
- if err != nil {
- return err
- }
- config.Items[idx].ImageUrl = newFilePath
- }
- }
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- } else if requestParams.ComponentTypeID == "2" { //2是直播
- var config mini_page_component.LiveWindowConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(config.LiveCoverUrl) {
- newFilePath, err := osm.MultipleFileMv(config.LiveCoverUrl)
- if err != nil {
- return err
- }
- config.LiveCoverUrl = newFilePath
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- } else if requestParams.ComponentTypeID == "3" { //3是宣传视频
- var config mini_page_component.VideoComponentConfig
- if err := json.Unmarshal([]byte(requestParams.ComponentConfig), &config); err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(config.VideoUrlStr) {
- newFilePath, err := osm.MultipleFileMv(config.VideoUrlStr)
- if err != nil {
- return err
- }
- config.VideoUrlStr = newFilePath
- }
- if strutils.IsStringNotEmpty(config.VideoCoverStr) {
- newFilePath, err := osm.MultipleFileMv(config.VideoCoverStr)
- if err != nil {
- return err
- }
- config.VideoCoverStr = newFilePath
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return err
- }
- record.ComponentConfig = string(updatedConfig)
- }
- }
- return nil
- },
- After: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- record, err := domain.ToConcrete[*mini_page_component.Entity](e)
- if err != nil {
- return err
- }
- if record.IsEnabled == Enabled {
- //当前启用 把别的同code的关掉
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_page_component.Entity{}),
- Conditions: sql.NewConditions().Not(entity.ColumnID, e.GetID()).Equal(mini_page_component.ColumnComponentTypeID, record.ComponentTypeID),
- TableRow: sql.NewTableRow().Add(mini_page_component.ColumnIsEnabled, Disable),
- })
- if err != nil {
- return err
- }
- }
- return nil
- },
- }),
- entity_crud.WithQueryFormCustomConditionFunc[mini_page_component.Info](func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (*entity_crud.CustomCondition, error) {
- requestParams, err := request.ToConcrete[*mini_page_component.GetMiniPageComponentsQueryParams](params)
- if err != nil {
- return nil, err
- }
- conditions := sql.NewConditions()
- if strutils.IsStringNotEmpty(requestParams.PageID) {
- conditions = conditions.Equal(mini_page_component.ColumnPageID, requestParams.PageID)
- }
- if strutils.IsStringNotEmpty(requestParams.ComponentTypeID) {
- conditions = conditions.Equal(mini_page_component.ColumnComponentTypeID, requestParams.ComponentTypeID)
- }
- if strutils.IsStringNotEmpty(requestParams.TypeName) {
- conditions = conditions.Like(mini_page_component.ColumnTypeName, "%"+requestParams.TypeName+"%")
- }
- if strutils.IsStringNotEmpty(requestParams.IsEnabled) {
- conditions = conditions.Equal(mini_page_component.ColumnPageID, requestParams.IsEnabled)
- }
- return &entity_crud.CustomCondition{
- Conditions: conditions,
- OrderBy: mini_page_component.ColumnSortOrder,
- }, nil
- }),
- entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[mini_page_component.Info]{
- OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output mini_page_component.Info) (mini_page_component.Info, error) {
- if strutils.IsStringNotEmpty(output.ComponentConfig) {
- if output.ComponentTypeID == "1" { //轮播图
- var config mini_page_component.CarouselConfig
- if err := json.Unmarshal([]byte(output.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if len(config.Items) > 0 {
- for idx := range config.Items {
- if strutils.IsStringNotEmpty(config.Items[idx].ImageUrl) {
- fileObjects, err := osm.MultipleFileUrl(config.Items[idx].ImageUrl)
- if err != nil {
- return output, err
- }
- config.Items[idx].ImageUrls = fileObjects
- }
- }
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- output.ComponentConfig = string(updatedConfig)
- } else if output.ComponentTypeID == "2" { //2是直播
- var config mini_page_component.LiveWindowConfig
- if err := json.Unmarshal([]byte(output.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if strutils.IsStringNotEmpty(config.LiveCoverUrl) {
- fileObjects, err := osm.MultipleFileUrl(config.LiveCoverUrl)
- if err != nil {
- return output, err
- }
- config.LiveCoverUrlFile = fileObjects
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- output.ComponentConfig = string(updatedConfig)
- } else if output.ComponentTypeID == "3" { //3是宣传视频
- var config mini_page_component.VideoComponentConfig
- if err := json.Unmarshal([]byte(output.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if strutils.IsStringNotEmpty(config.VideoUrlStr) {
- fileObjects, err := osm.MultipleFileUrl(config.VideoUrlStr)
- if err != nil {
- return output, err
- }
- config.VideoUrlFile = fileObjects
- }
- if strutils.IsStringNotEmpty(config.VideoCoverStr) {
- fileObjects, err := osm.MultipleFileUrl(config.VideoCoverStr)
- if err != nil {
- return output, err
- }
- config.VideoCoverFile = fileObjects
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- output.ComponentConfig = string(updatedConfig)
- }
- }
- return output, nil
- },
- }),
- entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[mini_page_component.Info]{
- OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[mini_page_component.Info]) (response.InfosData[mini_page_component.Info], error) {
- infos := output.Infos
- for i1 := range infos {
- info := &infos[i1]
- if strutils.IsStringNotEmpty(info.ComponentConfig) {
- if info.ComponentTypeID == "1" { //轮播图
- var config mini_page_component.CarouselConfig
- if err := json.Unmarshal([]byte(info.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if len(config.Items) > 0 {
- for idx := range config.Items {
- if strutils.IsStringNotEmpty(config.Items[idx].ImageUrl) {
- fileObjects, err := osm.MultipleFileUrl(config.Items[idx].ImageUrl)
- if err != nil {
- return output, err
- }
- config.Items[idx].ImageUrls = fileObjects
- }
- }
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- info.ComponentConfig = string(updatedConfig)
- } else if info.ComponentTypeID == "2" { //2是直播
- var config mini_page_component.LiveWindowConfig
- if err := json.Unmarshal([]byte(info.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if strutils.IsStringNotEmpty(config.LiveCoverUrl) {
- fileObjects, err := osm.MultipleFileUrl(config.LiveCoverUrl)
- if err != nil {
- return output, err
- }
- config.LiveCoverUrlFile = fileObjects
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- info.ComponentConfig = string(updatedConfig)
- } else if info.ComponentTypeID == "3" { //3是宣传视频
- var config mini_page_component.VideoComponentConfig
- if err := json.Unmarshal([]byte(info.ComponentConfig), &config); err != nil {
- return output, nil
- }
- if strutils.IsStringNotEmpty(config.VideoUrlStr) {
- fileObjects, err := osm.MultipleFileUrl(config.VideoUrlStr)
- if err != nil {
- return output, err
- }
- config.VideoUrlFile = fileObjects
- }
- if strutils.IsStringNotEmpty(config.VideoCoverStr) {
- fileObjects, err := osm.MultipleFileUrl(config.VideoCoverStr)
- if err != nil {
- return output, err
- }
- config.VideoCoverFile = fileObjects
- }
- updatedConfig, err := json.Marshal(config)
- if err != nil {
- return output, nil
- }
- info.ComponentConfig = string(updatedConfig)
- }
- }
- }
- return output, nil
- },
- }),
- entity_crud.WithDeleteTx(),
- entity_crud.WithDeleteCallbacks(&entity_crud.DeleteCallbacks{
- After: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
- requestParams, err := request.ToConcrete[*mini_page_component.DeleteMiniPageComponentQueryParams](params)
- if err != nil {
- return err
- }
- err = database.Delete(tx, &sql.DeleteExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_carousel_config.Entity{}),
- Conditions: sql.NewConditions().Equal(mini_carousel_config.ColumnComponentID, requestParams.ID),
- })
- if err != nil {
- return err
- }
- err = database.Delete(tx, &sql.DeleteExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_live_config.Entity{}),
- Conditions: sql.NewConditions().Equal(mini_live_config.ColumnComponentID, requestParams.ID),
- })
- if err != nil {
- return err
- }
- err = database.Delete(tx, &sql.DeleteExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_quality_product_config.Entity{}),
- Conditions: sql.NewConditions().Equal(mini_quality_product_config.ColumnComponentID, requestParams.ID),
- })
- if err != nil {
- return err
- }
- err = database.Delete(tx, &sql.DeleteExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_video_config.Entity{}),
- Conditions: sql.NewConditions().Equal(mini_video_config.ColumnComponentID, requestParams.ID),
- })
- if err != nil {
- return err
- }
- return nil
- },
- }),
- )
- binding.PutBind(v1Binder, &binding.SimpleBindItem[any]{
- Path: "/miniPageComponent/enabledOrDisable",
- SendResponseFunc: response.SendMsgResponse,
- RequestParams: &mini_page_component.EnabledOrDisableJsonBody{},
- ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
- requestParams, err := request.ToConcrete[*mini_page_component.EnabledOrDisableJsonBody](params)
- if err != nil {
- return nil, err
- }
- err = database.Transaction(i.DBExecutor(), func(tx database.Executor) error {
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_page_component.Entity{}),
- Conditions: sql.NewConditions().Equal(entity.ColumnID, requestParams.ID),
- TableRow: sql.NewTableRow().Add(mini_page_component.ColumnIsEnabled, requestParams.IsEnabled),
- })
- //这个启用 别的都关掉
- if requestParams.IsEnabled == Enabled {
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_page_component.Entity{}),
- Conditions: sql.NewConditions().Not(entity.ColumnID, requestParams.ID).Equal(mini_page_component.ColumnComponentTypeID, requestParams.ComponentTypeID),
- TableRow: sql.NewTableRow().Add(mini_page_component.ColumnIsEnabled, Disable),
- })
- if err != nil {
- return err
- }
- }
- return nil
- })
- return nil, nil
- },
- })
- binding.PutBind(v1Binder, &binding.SimpleBindItem[any]{
- Path: "/miniPageComponent/sortOrder",
- SendResponseFunc: response.SendMsgResponse,
- RequestParams: &mini_page_component.SortOrderJsonBody{},
- ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
- requestParams, err := request.ToConcrete[*mini_page_component.SortOrderJsonBody](params)
- if err != nil {
- return nil, err
- }
- err = database.Transaction(i.DBExecutor(), func(tx database.Executor) error {
- for _, sortOrder := range requestParams.SortOrders {
- err = database.Update(tx, &sql.UpdateExecuteParams{
- TableName: domain.TableName(dbSchema, &mini_page_component.Entity{}),
- Conditions: sql.NewConditions().Equal(entity.ColumnID, sortOrder.ID),
- TableRow: sql.NewTableRow().Add(mini_page_component.ColumnSortOrder, sortOrder.SortOrder),
- })
- if err != nil {
- return err
- }
- }
- return nil
- })
- if err != nil {
- return nil, err
- }
- return nil, nil
- },
- })
- }
- func (svc *MiniPageComponentService) queryByKeyFields(e *mini_page_component.Entity, dbExecutor database.Executor) (*mini_page_component.Entity, error) {
- result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
- TableName: domain.TableName(dbSchema, e),
- Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
- })
- if err != nil {
- if database.IsErrorDBRecordNotExist(err) {
- return nil, errors.New(e.DomainCNName() + "不存在")
- }
- return nil, err
- }
- existEntity := new(mini_page_component.Entity)
- err = sql.ParseSqlResult(result, existEntity)
- if err != nil {
- return nil, err
- }
- return existEntity, nil
- }
- func (svc *MiniPageComponentService) checkExistByKeyFields(e *mini_page_component.Entity, dbExecutor database.Executor) error {
- exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
- TableName: domain.TableName(dbSchema, e),
- Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
- })
- if err != nil {
- return err
- }
- if !exist {
- return errors.New(e.DomainCNName() + "不存在")
- }
- return nil
- }
|