fusenapi/server/product-template/internal/logic/savebasemaplogic.go

81 lines
2.1 KiB
Go

package logic
import (
"encoding/json"
"fusenapi/model/gmodel"
"fusenapi/server/product-template/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"io/ioutil"
"net/http"
"time"
"context"
"fusenapi/server/product-template/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SaveBaseMapLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
bodyBytes []byte
}
func NewSaveBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBaseMapLogic {
return &SaveBaseMapLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
func (l *SaveBaseMapLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
logx.Error(err)
}
l.bodyBytes = bodyBytes
}
func (l *SaveBaseMapLogic) SaveBaseMap(req *types.Request, userInfo *auth.BackendUserInfo) (resp *basic.Response) {
var err error
var postData []types.SaveBaseMapReq
if err = json.Unmarshal(l.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))
for _, v := range postData {
val := v
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: val.Id,
Name: &val.Name,
Url: &val.Url,
Ctime: &ctime,
})
}
//更新
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")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}