fix
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/map-library/internal/svc"
|
||||
"fusenapi/server/map-library/internal/types"
|
||||
@@ -31,7 +32,7 @@ func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (res
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
|
||||
}
|
||||
mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn)
|
||||
mapLibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx)
|
||||
mapLibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx, "")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get map library list")
|
||||
@@ -59,7 +60,6 @@ func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (res
|
||||
data := types.GetMapLibraryListRsp{
|
||||
Mid: v.Id,
|
||||
Ctime: time.Unix(*v.Ctime, 0).Format("2006-01-02 15:04:05"),
|
||||
Info: *v.Info,
|
||||
}
|
||||
//tag拼装
|
||||
if tagIndex, ok := mapTag[*v.TagId]; ok {
|
||||
@@ -68,6 +68,13 @@ func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (res
|
||||
Title: *templateTagList[tagIndex].Title,
|
||||
}
|
||||
}
|
||||
//解析info
|
||||
var info types.Info
|
||||
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
|
||||
list = append(list, data)
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
||||
|
||||
98
server/map-library/internal/logic/savemaplibrarylogic.go
Normal file
98
server/map-library/internal/logic/savemaplibrarylogic.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fusenapi/server/map-library/internal/svc"
|
||||
"fusenapi/server/map-library/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SaveMapLibraryLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSaveMapLibraryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMapLibraryLogic {
|
||||
return &SaveMapLibraryLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SaveMapLibraryLogic) SaveMapLibrary(req *types.SaveMapLibraryReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if userinfo.GetIdType() != auth.IDTYPE_User {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
|
||||
}
|
||||
//获取所有贴图数据[获取id]
|
||||
mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn)
|
||||
maplibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx, "`id`")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get map library list")
|
||||
}
|
||||
|
||||
sort := int64(0)
|
||||
status := int64(1)
|
||||
now := time.Now().Unix()
|
||||
needDeleteMid := make([]int64, 0, len(req.Data))
|
||||
mapPostMid := make(map[int64]struct{})
|
||||
//开启事务
|
||||
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
|
||||
for _, v := range req.Data {
|
||||
infoByte, _ := json.Marshal(v.Info)
|
||||
infoJsonStr := string(infoByte)
|
||||
switch v.Mid {
|
||||
case "": //新增
|
||||
err = mapLibraryModel.Create(l.ctx, &gmodel.FsMapLibrary{
|
||||
Title: &v.Info.Title,
|
||||
Info: &infoJsonStr,
|
||||
Sort: &sort,
|
||||
Status: &status,
|
||||
Ctime: &now,
|
||||
TagId: &v.Tag.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default: //修改
|
||||
midInt, err := strconv.ParseInt(v.Mid, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapPostMid[midInt] = struct{}{}
|
||||
err = mapLibraryModel.Update(l.ctx, midInt, &gmodel.FsMapLibrary{
|
||||
Title: &v.Info.Title,
|
||||
Info: &infoJsonStr,
|
||||
TagId: &v.Tag.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
//删除需要删除的
|
||||
for _, v := range maplibraryList {
|
||||
//旧的不在新的里面则删除
|
||||
if _, ok := mapPostMid[v.Id]; !ok {
|
||||
needDeleteMid = append(needDeleteMid, v.Id)
|
||||
}
|
||||
}
|
||||
return mapLibraryModel.ChangeStatusByIds(l.ctx, needDeleteMid, 0)
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to save map library info")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
Reference in New Issue
Block a user