2023-06-20 03:52:26 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-06-20 06:59:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2023-06-20 03:52:26 +00:00
|
|
|
"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
|
2023-07-21 07:20:18 +00:00
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
bodyData []byte
|
2023-06-20 03:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSaveMapLibraryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveMapLibraryLogic {
|
|
|
|
return &SaveMapLibraryLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 07:20:18 +00:00
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
func (l *SaveMapLibraryLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bodyData, err := ioutil.ReadAll(r.Body)
|
|
|
|
defer r.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.bodyData = bodyData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SaveMapLibraryLogic) SaveMapLibrary(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-06-20 03:52:26 +00:00
|
|
|
if userinfo.GetIdType() != auth.IDTYPE_User {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
|
|
|
|
}
|
2023-07-21 07:20:18 +00:00
|
|
|
|
|
|
|
if len(l.bodyData) == 0 {
|
|
|
|
return resp.SetStatus(basic.CodeApiErr, http.ErrBodyReadAfterClose.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2023-06-20 06:59:13 +00:00
|
|
|
var postData []types.SaveMapLibraryData
|
2023-07-21 07:20:18 +00:00
|
|
|
if err = json.Unmarshal(l.bodyData, &postData); err != nil {
|
2023-06-20 06:59:13 +00:00
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeSaveErr, "param err")
|
|
|
|
}
|
2023-06-20 03:52:26 +00:00
|
|
|
sort := int64(0)
|
|
|
|
status := int64(1)
|
|
|
|
now := time.Now().Unix()
|
2023-06-21 10:06:39 +00:00
|
|
|
createList := make([]gmodel.FsMapLibrary, 0, len(postData))
|
|
|
|
updateList := make([]gmodel.FsMapLibrary, 0, len(postData))
|
2023-06-20 03:52:26 +00:00
|
|
|
//开启事务
|
2023-06-21 10:06:39 +00:00
|
|
|
for _, v := range postData {
|
|
|
|
postDataVal := v
|
|
|
|
infoByte, _ := json.Marshal(postDataVal.Info)
|
|
|
|
infoJsonStr := string(infoByte)
|
|
|
|
switch postDataVal.Mid {
|
|
|
|
case "": //新增
|
|
|
|
createList = append(createList, gmodel.FsMapLibrary{
|
|
|
|
Title: &postDataVal.Info.Title,
|
|
|
|
Info: &infoJsonStr,
|
|
|
|
Sort: &sort,
|
|
|
|
Status: &status,
|
|
|
|
Ctime: &now,
|
|
|
|
TagId: &postDataVal.Tag.Id,
|
|
|
|
})
|
|
|
|
default: //修改
|
|
|
|
midInt, err := strconv.ParseInt(postDataVal.Mid, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "mid is not a number")
|
2023-06-20 03:52:26 +00:00
|
|
|
}
|
2023-06-21 10:06:39 +00:00
|
|
|
updateList = append(updateList, gmodel.FsMapLibrary{
|
|
|
|
Id: midInt,
|
|
|
|
Title: &postDataVal.Info.Title,
|
|
|
|
Info: &infoJsonStr,
|
|
|
|
TagId: &postDataVal.Tag.Id,
|
|
|
|
})
|
2023-06-20 03:52:26 +00:00
|
|
|
}
|
2023-06-21 10:06:39 +00:00
|
|
|
}
|
|
|
|
err = l.svcCtx.AllModels.FsMapLibrary.SaveMapLibraryWithTransaction(l.ctx, createList, updateList)
|
2023-06-20 03:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
2023-06-21 10:06:39 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save map library")
|
2023-06-20 03:52:26 +00:00
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
|
|
|
}
|