fusenapi/server/map-library/internal/logic/getmaplibrarylistlogic.go

82 lines
2.4 KiB
Go
Raw Normal View History

2023-06-14 06:57:20 +00:00
package logic
import (
2023-06-20 03:52:26 +00:00
"encoding/json"
2023-06-14 07:45:56 +00:00
"fusenapi/model/gmodel"
2023-06-15 04:00:32 +00:00
"fusenapi/server/map-library/internal/svc"
"fusenapi/server/map-library/internal/types"
2023-06-14 06:57:20 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-06-14 07:45:56 +00:00
"time"
2023-06-14 06:57:20 +00:00
"context"
"github.com/zeromicro/go-zero/core/logx"
)
type GetMapLibraryListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetMapLibraryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMapLibraryListLogic {
return &GetMapLibraryListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (resp *basic.Response) {
2023-06-15 04:00:32 +00:00
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}
2023-06-14 07:45:56 +00:00
mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn)
2023-06-20 11:56:18 +00:00
mapLibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx)
2023-06-14 07:45:56 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get map library list")
}
if len(mapLibraryList) == 0 {
return resp.SetStatus(basic.CodeOK)
}
productTemplateTagIds := make([]int64, 0, len(mapLibraryList))
for _, v := range mapLibraryList {
productTemplateTagIds = append(productTemplateTagIds, *v.TagId)
}
//获取标签列表
productTemplateTagsModel := gmodel.NewFsProductTemplateTagsModel(l.svcCtx.MysqlConn)
templateTagList, err := productTemplateTagsModel.GetListByIds(l.ctx, productTemplateTagIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product template tags")
}
mapTag := make(map[int64]int)
for k, v := range templateTagList {
mapTag[v.Id] = k
}
list := make([]types.GetMapLibraryListRsp, 0, len(mapLibraryList))
for _, v := range mapLibraryList {
data := types.GetMapLibraryListRsp{
Mid: v.Id,
Ctime: time.Unix(*v.Ctime, 0).Format("2006-01-02 15:04:05"),
}
//tag拼装
if tagIndex, ok := mapTag[*v.TagId]; ok {
2023-06-20 10:37:56 +00:00
data.Tag = &types.MapLibraryListTag{
2023-06-14 07:45:56 +00:00
Id: templateTagList[tagIndex].Id,
Title: *templateTagList[tagIndex].Title,
}
}
2023-06-20 03:52:26 +00:00
//解析info
2023-06-20 08:43:38 +00:00
var info interface{}
2023-06-20 03:52:26 +00:00
if err = json.Unmarshal([]byte(*v.Info), &info); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "json parse info err")
}
data.Info = info
2023-06-14 07:45:56 +00:00
list = append(list, data)
}
2023-06-14 08:04:43 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
2023-06-14 06:57:20 +00:00
}