|
|
@@ -0,0 +1,201 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "ecos/application/domain/traceability_video"
|
|
|
+ "ecos/osm"
|
|
|
+ "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 traceabilityVideoService = &TraceabilityVideoService{}
|
|
|
+
|
|
|
+type TraceabilityVideoService struct{}
|
|
|
+
|
|
|
+func (svc *TraceabilityVideoService) Init(appInstance *application.App) error {
|
|
|
+ svc.v1(appInstance)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (svc *TraceabilityVideoService) Destroy() error {
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (svc *TraceabilityVideoService) v1(appInstance *application.App) {
|
|
|
+ v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
|
|
|
+ appInstance.Infrastructure())
|
|
|
+
|
|
|
+ entity_crud.BindSimple[traceability_video.Info](v1Binder, &entity_crud.Simple[traceability_video.Info]{
|
|
|
+ Entity: &traceability_video.Entity{},
|
|
|
+ Schema: dbSchema,
|
|
|
+ CreateJsonBody: &traceability_video.CreateTraceabilityVideoJsonBody{},
|
|
|
+ DeleteQueryParams: &traceability_video.DeleteTraceabilityVideoQueryParams{},
|
|
|
+ UpdateJsonBody: &traceability_video.UpdateTraceabilityVideoJsonBody{},
|
|
|
+ QueryQueryParams: &traceability_video.GetTraceabilityVideosQueryParams{},
|
|
|
+ GetByIDQueryParams: &traceability_video.GetTraceabilityVideoQueryParams{},
|
|
|
+ },
|
|
|
+ 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[*traceability_video.CreateTraceabilityVideoJsonBody](params)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ record, err := domain.ToConcrete[*traceability_video.Entity](e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(requestParams.VideoCoverUrl) {
|
|
|
+ newFilePath, err := osm.MultipleFileMv(requestParams.VideoCoverUrl)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ record.VideoCoverUrl = newFilePath
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(requestParams.VideoUrl) {
|
|
|
+ newFilePath, err := osm.MultipleFileMv(requestParams.VideoUrl)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ record.VideoUrl = newFilePath
|
|
|
+ }
|
|
|
+ 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[*traceability_video.UpdateTraceabilityVideoJsonBody](params)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ record, err := domain.ToConcrete[*traceability_video.Entity](e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(requestParams.VideoCoverUrl) {
|
|
|
+ newFilePath, err := osm.MultipleFileMv(requestParams.VideoCoverUrl)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ record.VideoCoverUrl = newFilePath
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(requestParams.VideoUrl) {
|
|
|
+ newFilePath, err := osm.MultipleFileMv(requestParams.VideoUrl)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ record.VideoUrl = newFilePath
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[traceability_video.Info]{
|
|
|
+ OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output traceability_video.Info) (traceability_video.Info, error) {
|
|
|
+ if strutils.IsStringNotEmpty(output.VideoCoverUrlStr) {
|
|
|
+ fileObjects, err := osm.MultipleFileUrl(output.VideoCoverUrlStr)
|
|
|
+ if err != nil {
|
|
|
+ return output, err
|
|
|
+ }
|
|
|
+ output.VideoCoverUrl = fileObjects
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(output.VideoUrlStr) {
|
|
|
+ fileObjects, err := osm.MultipleFileUrl(output.VideoUrlStr)
|
|
|
+ if err != nil {
|
|
|
+ return output, err
|
|
|
+ }
|
|
|
+ output.VideoUrl = fileObjects
|
|
|
+ }
|
|
|
+ return output, nil
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[traceability_video.Info]{
|
|
|
+ OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[traceability_video.Info]) (response.InfosData[traceability_video.Info], error) {
|
|
|
+ infos := output.Infos
|
|
|
+ for i1 := range infos {
|
|
|
+ info := &infos[i1]
|
|
|
+ if strutils.IsStringNotEmpty(info.VideoCoverUrlStr) {
|
|
|
+ fileObjects, err := osm.MultipleFileUrl(info.VideoCoverUrlStr)
|
|
|
+ if err != nil {
|
|
|
+ if err != nil {
|
|
|
+ return output, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ info.VideoCoverUrl = fileObjects
|
|
|
+ }
|
|
|
+ if strutils.IsStringNotEmpty(info.VideoUrlStr) {
|
|
|
+ fileObjects, err := osm.MultipleFileUrl(info.VideoUrlStr)
|
|
|
+ if err != nil {
|
|
|
+ return output, err
|
|
|
+ }
|
|
|
+ info.VideoUrl = fileObjects
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return output, nil
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ entity_crud.WithQueryFormCustomConditionFunc[traceability_video.Info](func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (*entity_crud.CustomCondition, error) {
|
|
|
+ requestParams, err := request.ToConcrete[*traceability_video.GetTraceabilityVideosQueryParams](params)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ conditions := sql.NewConditions()
|
|
|
+ if strutils.IsStringNotEmpty(requestParams.VideoTitle) {
|
|
|
+ conditions.Equal(traceability_video.ColumnVideoTitle, "%"+requestParams.VideoTitle+"%")
|
|
|
+ }
|
|
|
+ return &entity_crud.CustomCondition{
|
|
|
+ Conditions: conditions,
|
|
|
+ OrderBy: entity.ColumnCreatedTime + " DESC",
|
|
|
+ }, nil
|
|
|
+ }),
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+func (svc *TraceabilityVideoService) queryByKeyFields(e *traceability_video.Entity, dbExecutor database.Executor) (*traceability_video.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(traceability_video.Entity)
|
|
|
+ err = sql.ParseSqlResult(result, existEntity)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return existEntity, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (svc *TraceabilityVideoService) checkExistByKeyFields(e *traceability_video.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
|
|
|
+}
|