This commit is contained in:
laodaming 2023-06-21 18:06:39 +08:00
parent 28cc8a19ba
commit cac0161585
4 changed files with 68 additions and 71 deletions

View File

@ -1,6 +1,9 @@
package gmodel
import "context"
import (
"context"
"gorm.io/gorm"
)
func (ml *FsMapLibraryModel) GetAllEnabledList(ctx context.Context, fields ...string) (resp []FsMapLibrary, err error) {
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
}
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
}
func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap, notInIds []int64) error {
err := bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx context.Context, dataList []FsProductTemplateBasemap) error {
return bm.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
notInIds := make([]int64, 0, len(dataList))
//更新
for _, v := range dataList {
notInIds = append(notInIds, v.Id)
err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error
if err != nil {
return err
@ -29,14 +31,7 @@ func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx contex
return nil
}
//删除不在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/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"strconv"
@ -43,66 +42,44 @@ func (l *SaveMapLibraryLogic) SaveMapLibrary(userinfo *auth.UserInfo, req *http.
logx.Error(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)
status := int64(1)
now := time.Now().Unix()
needDeleteMid := make([]int64, 0, len(postData))
mapPostMid := make(map[int64]struct{})
createList := make([]gmodel.FsMapLibrary, 0, len(postData))
updateList := make([]gmodel.FsMapLibrary, 0, len(postData))
//开启事务
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
for _, v := range postData {
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,
Status: &status,
})
if err != nil {
return err
}
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")
}
updateList = append(updateList, gmodel.FsMapLibrary{
Id: midInt,
Title: &postDataVal.Info.Title,
Info: &infoJsonStr,
TagId: &postDataVal.Tag.Id,
})
}
//删除需要删除的
for _, v := range maplibraryList {
//旧的不在新的里面则删除
if _, ok := mapPostMid[v.Id]; !ok {
needDeleteMid = append(needDeleteMid, v.Id)
}
}
return mapLibraryModel.ChangeStatusByIds(l.ctx, needDeleteMid, 0)
})
}
err = l.svcCtx.AllModels.FsMapLibrary.SaveMapLibraryWithTransaction(l.ctx, createList, updateList)
if err != nil {
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")
}

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")
}
updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData))
baseMapIds := make([]int64, 0, len(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)
if err != nil {
logx.Error(err)
@ -64,14 +63,14 @@ func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
}
ctime := ctimeT.Unix()
updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{
Id: v.Id,
Name: &v.Name,
Url: &v.Url,
Id: val.Id,
Name: &val.Name,
Url: &val.Url,
Ctime: &ctime,
})
}
//更新
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr, baseMapIds)
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")