feat: 迁移plant
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBannerLogic {
|
||||
return &CreateBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CreateBannerLogic) CreateBanner(req *types.CreateBannerReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_ = userId
|
||||
banner := plantModel.Banner{
|
||||
Title: req.Title,
|
||||
ImageID: req.ImageId,
|
||||
Sort: req.Sort,
|
||||
IsActive: req.IsActive,
|
||||
TargetURL: req.TargetUrl,
|
||||
}
|
||||
if banner.IsActive == 0 {
|
||||
banner.IsActive = 1
|
||||
}
|
||||
return l.svcCtx.DB.Create(&banner).Error
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteBannerLogic {
|
||||
return &DeleteBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteBannerLogic) DeleteBanner(req *types.IdReq) error {
|
||||
return l.svcCtx.DB.Where("id = ?", req.Id).Delete(&plantModel.Banner{}).Error
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetBannerListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetBannerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBannerListLogic {
|
||||
return &GetBannerListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetBannerListLogic) GetBannerList(req *types.BannerListReq) (interface{}, error) {
|
||||
db := l.svcCtx.DB.Model(&plantModel.Banner{})
|
||||
if req.Title != "" {
|
||||
db = db.Where("title LIKE ?", "%"+req.Title+"%")
|
||||
}
|
||||
if req.IsActive > 0 {
|
||||
db = db.Where("is_active = ?", req.IsActive)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
current := req.Current
|
||||
if current <= 0 {
|
||||
current = 1
|
||||
}
|
||||
offset := (current - 1) * pageSize
|
||||
|
||||
var list []plantModel.Banner
|
||||
db.Order("sort asc, created_at desc").Limit(pageSize).Offset(offset).Find(&list)
|
||||
|
||||
type BannerItem struct {
|
||||
Id string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
ImageId string `json:"imageId"`
|
||||
Sort int `json:"sort"`
|
||||
IsActive int `json:"isActive"`
|
||||
TargetUrl string `json:"targetUrl"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
var result []BannerItem
|
||||
for _, item := range list {
|
||||
result = append(result, BannerItem{
|
||||
Id: item.ID,
|
||||
Title: item.Title,
|
||||
ImageId: item.ImageID,
|
||||
Sort: item.Sort,
|
||||
IsActive: item.IsActive,
|
||||
TargetUrl: item.TargetURL,
|
||||
CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"list": result,
|
||||
"total": total,
|
||||
"page": current,
|
||||
"pageSize": pageSize,
|
||||
"totalPage": math.Ceil(float64(total) / float64(pageSize)),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateBannerLogic {
|
||||
return &UpdateBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateBannerLogic) UpdateBanner(req *types.UpdateBannerReq) error {
|
||||
updates := map[string]interface{}{}
|
||||
if req.Title != "" {
|
||||
updates["title"] = req.Title
|
||||
}
|
||||
if req.ImageId != "" {
|
||||
updates["image_id"] = req.ImageId
|
||||
}
|
||||
if req.Sort != 0 {
|
||||
updates["sort"] = req.Sort
|
||||
}
|
||||
if req.IsActive != 0 {
|
||||
updates["is_active"] = req.IsActive
|
||||
}
|
||||
if req.TargetUrl != "" {
|
||||
updates["target_url"] = req.TargetUrl
|
||||
}
|
||||
return l.svcCtx.DB.Model(&plantModel.Banner{}).Where("id = ?", req.Id).Updates(updates).Error
|
||||
}
|
||||
Reference in New Issue
Block a user