123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package service
- import (
- "erpims/application/domain/announcement_policy_info"
- "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 announcementPolicyInfoService = &AnnouncementPolicyInfoService{}
- type AnnouncementPolicyInfoService struct{}
- func (svc *AnnouncementPolicyInfoService) Init(appInstance *application.App) error {
- svc.v1(appInstance)
- return nil
- }
- func (svc *AnnouncementPolicyInfoService) Destroy() error {
- return nil
- }
- func (svc *AnnouncementPolicyInfoService) v1(appInstance *application.App) {
- v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"),
- appInstance.Infrastructure())
- entity_crud.BindSimple[announcement_policy_info.Info](v1Binder,
- &entity_crud.Simple[announcement_policy_info.Info]{
- Entity: &announcement_policy_info.Entity{},
- Schema: dbSchema,
- CreateJsonBody: &announcement_policy_info.CreateAnnouncementPolicyInfoJsonBody{},
- DeleteQueryParams: &announcement_policy_info.DeleteAnnouncementPolicyInfoQueryParams{},
- UpdateJsonBody: &announcement_policy_info.UpdateAnnouncementPolicyInfoJsonBody{},
- QueryQueryParams: &announcement_policy_info.GetAnnouncementPolicyInfosQueryParams{},
- GetByIDQueryParams: &announcement_policy_info.GetAnnouncementPolicyInfoQueryParams{},
- },
- 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[*announcement_policy_info.CreateAnnouncementPolicyInfoJsonBody](params)
- if err != nil {
- return err
- }
- info, err := domain.ToConcrete[*announcement_policy_info.Entity](e)
- if err != nil {
- return err
- }
- //图片处理
- if strutils.IsStringNotEmpty(requestParams.TitleImgUrl) {
- newPicture, err := oss.MultipleFileMv(requestParams.TitleImgUrl)
- if err != nil {
- return err
- }
- info.TitleImgUrl = 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[*announcement_policy_info.UpdateAnnouncementPolicyInfoJsonBody](params)
- info, err := domain.ToConcrete[*announcement_policy_info.Entity](e)
- if err != nil {
- return err
- }
- if strutils.IsStringNotEmpty(requestParams.TitleImgUrl) {
- newPicture, err := oss.MultipleFileMv(requestParams.TitleImgUrl)
- if err != nil {
- return err
- }
- info.TitleImgUrl = newPicture
- }
- return nil
- },
- }),
- entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[announcement_policy_info.Info]{
- OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[announcement_policy_info.Info]) (response.InfosData[announcement_policy_info.Info], error) {
- for i2 := range output.Infos {
- item := &output.Infos[i2]
- if strutils.IsStringNotEmpty(item.TitleImgUrlStr) {
- fileObjects, err := oss.MultipleFileUrl(item.TitleImgUrlStr)
- if err != nil {
- return output, err
- }
- item.TitleImgUrl = fileObjects
- }
- }
- return output, nil
- },
- }),
- entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[announcement_policy_info.Info]{
- OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output announcement_policy_info.Info) (announcement_policy_info.Info, error) {
- if strutils.IsStringNotEmpty(output.TitleImgUrlStr) {
- fileObjects, err := oss.MultipleFileUrl(output.TitleImgUrlStr)
- if err != nil {
- return output, err
- }
- output.TitleImgUrl = fileObjects
- }
- return output, nil
- },
- }),
- entity_crud.WithQueryOrderBy[announcement_policy_info.Info](announcement_policy_info.ColumnReleaseDate+" desc"),
- entity_crud.WithQueryConditionFieldCallback[announcement_policy_info.Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
- switch fieldName {
- case announcement_policy_info.FieldTitle:
- conditions.Like(columnName, "%"+value.(string)+"%")
- return true, nil
- default:
- return false, nil
- }
- }))
- }
- func (svc *AnnouncementPolicyInfoService) queryByKeyFields(e *announcement_policy_info.Entity, dbExecutor database.Executor) (*announcement_policy_info.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(announcement_policy_info.Entity)
- err = sql.ParseSqlResult(result, existEntity)
- if err != nil {
- return nil, err
- }
- return existEntity, nil
- }
- func (svc *AnnouncementPolicyInfoService) checkExistByKeyFields(e *announcement_policy_info.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
- }
|