This commit is contained in:
laodaming 2023-06-21 16:35:43 +08:00
parent 89dfd58e1c
commit dfcceaea8e
6 changed files with 160 additions and 1 deletions

View File

@ -1,6 +1,9 @@
package gmodel package gmodel
import "context" import (
"context"
"gorm.io/gorm"
)
// TODO: 使用model的属性做你想做的 // TODO: 使用model的属性做你想做的
@ -12,3 +15,28 @@ func (bm *FsProductTemplateBasemapModel) GetAllEnabledList(ctx context.Context,
err = db.Find(&resp).Error err = db.Find(&resp).Error
return resp, err 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 {
//更新
for _, v := range dataList {
err := tx.Where("`id` = ? ", v.Id).Updates(&v).Error
if err != nil {
return err
}
}
if len(notInIds) == 0 {
return nil
}
//删除不在ids里面的
return bm.SoftDeleteByIdsNotIn(ctx, notInIds)
})
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

@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/product-template/base-map-list", Path: "/product-template/base-map-list",
Handler: GetBaseMapListHandler(serverCtx), Handler: GetBaseMapListHandler(serverCtx),
}, },
{
Method: http.MethodPost,
Path: "/product-template/base-map-update",
Handler: SaveBaseMapHandler(serverCtx),
},
}, },
) )
} }

View File

@ -0,0 +1,28 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/product-templatev2/internal/logic"
"fusenapi/server/product-templatev2/internal/svc"
)
func SaveBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 创建一个业务逻辑层实例
l := logic.NewSaveBaseMapLogic(r.Context(), svcCtx)
resp := l.SaveBaseMap(r)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -0,0 +1,80 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/server/product-templatev2/internal/types"
"fusenapi/utils/basic"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"time"
"context"
"fusenapi/server/product-templatev2/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SaveBaseMapLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSaveBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBaseMapLogic {
return &SaveBaseMapLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
authKey := r.Header.Get("Auth-Key")
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
_, err := genentModel.Find(l.ctx, authKey)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
bodyBytes, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var postData []types.SaveBaseMapReq
if err = json.Unmarshal(bodyBytes, &postData); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request param")
}
//空数组
if len(postData) == 0 {
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)
ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err create time format")
}
ctime := ctimeT.Unix()
updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{
Id: v.Id,
Name: &v.Name,
Url: &v.Url,
Ctime: &ctime,
})
}
//更新
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr, baseMapIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}

View File

@ -35,6 +35,13 @@ type GetBaseMapListRsp struct {
Ctime string `json:"ctime"` Ctime string `json:"ctime"`
} }
type SaveBaseMapReq struct {
Id int64 `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Ctime string `json:"ctime"`
}
type Response struct { type Response struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"msg"` Message string `json:"msg"`

View File

@ -14,6 +14,10 @@ service product-templatev2 {
//获取底图列表 //获取底图列表
@handler GetBaseMapListHandler @handler GetBaseMapListHandler
get /product-template/base-map-list( ) returns (response); get /product-template/base-map-list( ) returns (response);
//保存底图信息
@handler SaveBaseMapHandler
post /product-template/base-map-update ( ) returns (response);
} }
//获取产品模板详情 //获取产品模板详情
@ -44,4 +48,11 @@ type GetBaseMapListRsp {
Name string `json:"name"` Name string `json:"name"`
Url string `json:"url"` Url string `json:"url"`
Ctime string `json:"ctime"` Ctime string `json:"ctime"`
}
//保存底图信息
type SaveBaseMapReq {
Id int64 `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Ctime string `json:"ctime"`
} }