From dfcceaea8ee356a54a251fdaf587fad8a0a516e9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 21 Jun 2023 16:35:43 +0800 Subject: [PATCH] fix --- .../fs_product_template_basemap_logic.go | 30 ++++++- .../internal/handler/routes.go | 5 ++ .../internal/handler/savebasemaphandler.go | 28 +++++++ .../internal/logic/savebasemaplogic.go | 80 +++++++++++++++++++ .../internal/types/types.go | 7 ++ server_api/product-templatev2.api | 11 +++ 6 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 server/product-templatev2/internal/handler/savebasemaphandler.go create mode 100644 server/product-templatev2/internal/logic/savebasemaplogic.go diff --git a/model/gmodel/fs_product_template_basemap_logic.go b/model/gmodel/fs_product_template_basemap_logic.go index ae70c090..382c8afd 100644 --- a/model/gmodel/fs_product_template_basemap_logic.go +++ b/model/gmodel/fs_product_template_basemap_logic.go @@ -1,6 +1,9 @@ package gmodel -import "context" +import ( + "context" + "gorm.io/gorm" +) // TODO: 使用model的属性做你想做的 @@ -12,3 +15,28 @@ func (bm *FsProductTemplateBasemapModel) GetAllEnabledList(ctx context.Context, err = db.Find(&resp).Error 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 +} diff --git a/server/product-templatev2/internal/handler/routes.go b/server/product-templatev2/internal/handler/routes.go index d2bae9c4..0c838655 100644 --- a/server/product-templatev2/internal/handler/routes.go +++ b/server/product-templatev2/internal/handler/routes.go @@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/product-template/base-map-list", Handler: GetBaseMapListHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/product-template/base-map-update", + Handler: SaveBaseMapHandler(serverCtx), + }, }, ) } diff --git a/server/product-templatev2/internal/handler/savebasemaphandler.go b/server/product-templatev2/internal/handler/savebasemaphandler.go new file mode 100644 index 00000000..69f85393 --- /dev/null +++ b/server/product-templatev2/internal/handler/savebasemaphandler.go @@ -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) + } + } +} diff --git a/server/product-templatev2/internal/logic/savebasemaplogic.go b/server/product-templatev2/internal/logic/savebasemaplogic.go new file mode 100644 index 00000000..72d1a197 --- /dev/null +++ b/server/product-templatev2/internal/logic/savebasemaplogic.go @@ -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") +} diff --git a/server/product-templatev2/internal/types/types.go b/server/product-templatev2/internal/types/types.go index 21a68f20..91f77e33 100644 --- a/server/product-templatev2/internal/types/types.go +++ b/server/product-templatev2/internal/types/types.go @@ -35,6 +35,13 @@ type GetBaseMapListRsp struct { 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 { Code int `json:"code"` Message string `json:"msg"` diff --git a/server_api/product-templatev2.api b/server_api/product-templatev2.api index 64c72fdd..f570984b 100644 --- a/server_api/product-templatev2.api +++ b/server_api/product-templatev2.api @@ -14,6 +14,10 @@ service product-templatev2 { //获取底图列表 @handler GetBaseMapListHandler 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"` Url string `json:"url"` Ctime string `json:"ctime"` +} +//保存底图信息 +type SaveBaseMapReq { + Id int64 `json:"id"` + Name string `json:"name"` + Url string `json:"url"` + Ctime string `json:"ctime"` } \ No newline at end of file