123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package service
- import (
- "erpims/application/domain/about_us"
- "erpims/oss"
- "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 aboutUsService = &AboutUsService{}
- type AboutUsService struct{}
- func (svc *AboutUsService) Init(appInstance *application.App) error {
- svc.v1(appInstance)
- return nil
- }
- func (svc *AboutUsService) Destroy() error {
- return nil
- }
- func (svc *AboutUsService) v1(appInstance *application.App) {
- v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
- appInstance.Infrastructure())
- entity_crud.BindSimple[about_us.Info](v1Binder,
- &entity_crud.Simple[about_us.Info]{
- Entity: &about_us.Entity{},
- Schema: dbSchema,
- CreateJsonBody: &about_us.CreateAboutUsJsonBody{},
- DeleteQueryParams: &about_us.DeleteAboutUsQueryParams{},
- UpdateJsonBody: &about_us.UpdateAboutUsJsonBody{},
- QueryQueryParams: &about_us.GetAboutUsesQueryParams{},
- GetByIDQueryParams: &about_us.GetAboutUsQueryParams{},
- },
- entity_crud.WithCreateTx(),
- 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[*about_us.CreateAboutUsJsonBody](params)
- if err != nil {
- return err
- }
- info, err := domain.ToConcrete[*about_us.Entity](e)
- if err != nil {
- return err
- }
- //图片处理
- if strutils.IsStringNotEmpty(requestParams.WxMpQrCode) {
- newPicture, err := oss.MultipleFileMv(requestParams.WxMpQrCode)
- if err != nil {
- return err
- }
- info.WxMpQrCode = newPicture
- }
- if strutils.IsStringNotEmpty(requestParams.ContactQrCode) {
- newPicture, err := oss.MultipleFileMv(requestParams.ContactQrCode)
- if err != nil {
- return err
- }
- info.ContactQrCode = newPicture
- }
- return nil
- },
- }),
- entity_crud.WithUpdateTx(),
- 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[*about_us.UpdateAboutUsJsonBody](params)
- info, err := domain.ToConcrete[*about_us.Entity](e)
- if err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(requestParams.WxMpQrCode) {
- newPicture, err := oss.MultipleFileMv(requestParams.WxMpQrCode)
- if err != nil {
- return err
- }
- info.WxMpQrCode = newPicture
- }
- //封面处理
- if strutils.IsStringNotEmpty(requestParams.ContactQrCode) {
- newPicture, err := oss.MultipleFileMv(requestParams.ContactQrCode)
- if err != nil {
- return err
- }
- info.ContactQrCode = newPicture
- }
- return nil
- },
- }),
- entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[about_us.Info]{
- 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) {
- for i2 := range output.Infos {
- item := &output.Infos[i2]
- if strutils.IsStringNotEmpty(item.WxMpQrCodeStr) {
- fileObjects, err := oss.MultipleFileUrl(item.WxMpQrCodeStr)
- if err != nil {
- return output, err
- }
- item.WxMpQrCode = fileObjects
- }
- if strutils.IsStringNotEmpty(item.ContactQrCodeStr) {
- fileObjects, err := oss.MultipleFileUrl(item.ContactQrCodeStr)
- if err != nil {
- return output, err
- }
- item.ContactQrCode = fileObjects
- }
- }
- return output, nil
- },
- }),
- entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[about_us.Info]{
- OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output about_us.Info) (about_us.Info, error) {
- if strutils.IsStringNotEmpty(output.WxMpQrCodeStr) {
- fileObjects, err := oss.MultipleFileUrl(output.WxMpQrCodeStr)
- if err != nil {
- return output, err
- }
- output.WxMpQrCode = fileObjects
- }
- if strutils.IsStringNotEmpty(output.ContactQrCodeStr) {
- fileObjects, err := oss.MultipleFileUrl(output.ContactQrCodeStr)
- if err != nil {
- return output, err
- }
- output.ContactQrCode = fileObjects
- }
- return output, nil
- },
- }),
- entity_crud.WithQueryOrderBy[about_us.Info](entity.ColumnCreatedTime+" desc"))
- }
- func (svc *AboutUsService) queryByKeyFields(e *about_us.Entity, dbExecutor database.Executor) (*about_us.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(about_us.Entity)
- err = sql.ParseSqlResult(result, existEntity)
- if err != nil {
- return nil, err
- }
- return existEntity, nil
- }
- func (svc *AboutUsService) checkExistByKeyFields(e *about_us.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
- }
|