Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson 2023-06-21 18:59:44 +08:00
commit 8bd6f019ba
4 changed files with 68 additions and 71 deletions

View File

@ -1,6 +1,9 @@
package gmodel package gmodel
import "context" import (
"context"
"gorm.io/gorm"
)
func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsMapLibrary, err error) { func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsMapLibrary, err error) {
db := ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`status` = ?", 1) db := ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`status` = ?", 1)
@ -25,3 +28,26 @@ func (ml *FsMapLibraryModel) ChangeStatusByIds(ctx context.Context, ids []int64,
} }
return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`id` in (?) ", ids).Update("status", 0).Error return ml.db.WithContext(ctx).Model(&FsMapLibrary{}).Where("`id` in (?) ", ids).Update("status", 0).Error
} }
func (ml *FsMapLibraryModel) SaveMapLibraryWithTransaction(ctx context.Context, createList []FsMapLibrary, updateList []FsMapLibrary) error {
return ml.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
//创建
for _, v := range createList {
if err := tx.Model(&FsMapLibrary{}).Create(&v).Error; err != nil {
return err
}
}
if len(updateList) == 0 {
return nil
}
//更新
notInIds := make([]int64, 0, len(updateList))
for _, v := range updateList {
notInIds = append(notInIds, v.Id)
if err := tx.Model(&FsMapLibrary{}).Where("`id` = ?", v.Id).Updates(&v).Error; err != nil {
return err
}
}
//删除
return tx.Model(&FsMapLibrary{}).Where("`id` not in (?) ", notInIds).Update("status", 0).Error
})
}

View File

@ -16,10 +16,12 @@ func (bm *FsProductTemplateBasemapModel) GetAllEnabledList(ctx context.Context,
return resp, err return resp, err
} }
func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap, notInIds []int64) error { func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap) error {
err := bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { return bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
notInIds := make([]int64, 0, len(dataList))
//更新 //更新
for _, v := range dataList { for _, v := range dataList {
notInIds = append(notInIds, v.Id)
err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error
if err != nil { if err != nil {
return err return err
@ -29,14 +31,7 @@ func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx contex
return nil return nil
} }
//删除不在ids里面的 //删除不在ids里面的
return bm.SoftDeleteByIdsNotIn(ctx, notInIds) return tx.Model(&FsProductTemplateBasemap{}).
Where("`status` = ? and `id` not in (?)", 1, notInIds).Update("status", 0).Error
}) })
return err
}
func (bm *FsProductTemplateBasemapModel) SoftDeleteByIdsNotIn(ctx context.Context, notInIds []int64) error {
if len(notInIds) == 0 {
return nil
}
return bm.db.WithContext(ctx).Model(&FsProductTemplateBasemap{}).
Where("`status` = ? and `id` not in (?)", 1, notInIds).Update("status", 0).Error
} }

View File

@ -6,7 +6,6 @@ import (
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"gorm.io/gorm"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strconv" "strconv"
@ -43,66 +42,44 @@ func (l *SaveMapLibraryLogic) SaveMapLibrary(userinfo *auth.UserInfo, req *http.
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeSaveErr, "param err") return resp.SetStatusWithMessage(basic.CodeSaveErr, "param err")
} }
//获取所有贴图数据[获取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) sort := int64(0)
status := int64(1) status := int64(1)
now := time.Now().Unix() now := time.Now().Unix()
needDeleteMid := make([]int64, 0, len(postData)) createList := make([]gmodel.FsMapLibrary, 0, len(postData))
mapPostMid := make(map[int64]struct{}) updateList := make([]gmodel.FsMapLibrary, 0, len(postData))
//开启事务 //开启事务
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error { for _, v := range postData {
for _, v := range postData { postDataVal := v
infoByte, _ := json.Marshal(v.Info) infoByte, _ := json.Marshal(postDataVal.Info)
infoJsonStr := string(infoByte) infoJsonStr := string(infoByte)
switch v.Mid { switch postDataVal.Mid {
case "": //新增 case "": //新增
err = mapLibraryModel.Create(l.ctx, &gmodel.FsMapLibrary{ createList = append(createList, gmodel.FsMapLibrary{
Title: &v.Info.Title, Title: &postDataVal.Info.Title,
Info: &infoJsonStr, Info: &infoJsonStr,
Sort: &sort, Sort: &sort,
Status: &status, Status: &status,
Ctime: &now, Ctime: &now,
TagId: &v.Tag.Id, TagId: &postDataVal.Tag.Id,
}) })
if err != nil { default: //修改
return err midInt, err := strconv.ParseInt(postDataVal.Mid, 10, 64)
} if err != nil {
default: //修改 logx.Error(err)
midInt, err := strconv.ParseInt(v.Mid, 10, 64) return resp.SetStatusWithMessage(basic.CodeServiceErr, "mid is not a number")
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,
Status: &status,
})
if err != nil {
return err
}
} }
updateList = append(updateList, gmodel.FsMapLibrary{
Id: midInt,
Title: &postDataVal.Info.Title,
Info: &infoJsonStr,
TagId: &postDataVal.Tag.Id,
})
} }
//删除需要删除的 }
for _, v := range maplibraryList { err = l.svcCtx.AllModels.FsMapLibrary.SaveMapLibraryWithTransaction(l.ctx, createList, updateList)
//旧的不在新的里面则删除
if _, ok := mapPostMid[v.Id]; !ok {
needDeleteMid = append(needDeleteMid, v.Id)
}
}
return mapLibraryModel.ChangeStatusByIds(l.ctx, needDeleteMid, 0)
})
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to save map library info") return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save map library")
} }
return resp.SetStatusWithMessage(basic.CodeOK, "success") return resp.SetStatusWithMessage(basic.CodeOK, "success")
} }

View File

@ -54,9 +54,8 @@ func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param can`t be empty array") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param can`t be empty array")
} }
updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData)) updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData))
baseMapIds := make([]int64, 0, len(postData))
for _, v := range postData { for _, v := range postData {
baseMapIds = append(baseMapIds, v.Id) val := v
ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local) ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
@ -64,14 +63,14 @@ func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
} }
ctime := ctimeT.Unix() ctime := ctimeT.Unix()
updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{ updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{
Id: v.Id, Id: val.Id,
Name: &v.Name, Name: &val.Name,
Url: &v.Url, Url: &val.Url,
Ctime: &ctime, Ctime: &ctime,
}) })
} }
//更新 //更新
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr, baseMapIds) err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map") return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")