89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package banner
|
|
|
|
import (
|
|
"context"
|
|
|
|
"sundynix-micro-go/app/file/rpc/fileservice"
|
|
"sundynix-micro-go/app/plant/api/internal/svc"
|
|
plantModel "sundynix-micro-go/app/plant/model"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetActiveBannerListLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetActiveBannerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetActiveBannerListLogic {
|
|
return &GetActiveBannerListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
|
}
|
|
|
|
func (l *GetActiveBannerListLogic) GetActiveBannerList() (interface{}, error) {
|
|
var list []plantModel.Banner
|
|
l.svcCtx.DB.Model(&plantModel.Banner{}).
|
|
Where("is_active = 1").
|
|
Order("sort asc, created_at desc").
|
|
Find(&list)
|
|
|
|
// Collect image IDs for batch fetch
|
|
var imageIds []string
|
|
for _, item := range list {
|
|
if item.ImageID != "" {
|
|
imageIds = append(imageIds, item.ImageID)
|
|
}
|
|
}
|
|
|
|
// Fetch image URLs from file service
|
|
imageUrlMap := make(map[string]string)
|
|
if len(imageIds) > 0 {
|
|
resp, err := l.svcCtx.FileRpc.GetFilesByIds(l.ctx, &fileservice.GetFilesByIdsReq{Ids: imageIds})
|
|
if err != nil {
|
|
logx.Errorf("Fetch banner image files failed: %v", err)
|
|
} else if resp != nil {
|
|
for _, f := range resp.Files {
|
|
if f != nil && f.Url != "" {
|
|
imageUrlMap[f.Id] = f.Url
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type BannerItem struct {
|
|
Id string `json:"id"`
|
|
Title string `json:"title"`
|
|
ImageId string `json:"imageId"`
|
|
ImageUrl string `json:"imageUrl"`
|
|
Sort int `json:"sort"`
|
|
IsActive int `json:"isActive"`
|
|
TargetUrl string `json:"targetUrl"`
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|
|
var result []BannerItem
|
|
for _, item := range list {
|
|
imageUrl := ""
|
|
if u, ok := imageUrlMap[item.ImageID]; ok {
|
|
imageUrl = u
|
|
}
|
|
createdAt := ""
|
|
if !item.CreatedAt.IsZero() {
|
|
createdAt = item.CreatedAt.Format("2006-01-02 15:04:05")
|
|
}
|
|
result = append(result, BannerItem{
|
|
Id: item.ID,
|
|
Title: item.Title,
|
|
ImageId: item.ImageID,
|
|
ImageUrl: imageUrl,
|
|
Sort: item.Sort,
|
|
IsActive: item.IsActive,
|
|
TargetUrl: item.TargetURL,
|
|
CreatedAt: createdAt,
|
|
})
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"list": result,
|
|
}, nil
|
|
}
|