2023-06-21 08:35:43 +00:00
|
|
|
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))
|
|
|
|
for _, v := range postData {
|
2023-06-21 10:06:39 +00:00
|
|
|
val := v
|
2023-06-21 08:35:43 +00:00
|
|
|
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{
|
2023-06-21 10:06:39 +00:00
|
|
|
Id: val.Id,
|
|
|
|
Name: &val.Name,
|
|
|
|
Url: &val.Url,
|
2023-06-21 08:35:43 +00:00
|
|
|
Ctime: &ctime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
//更新
|
2023-06-21 10:06:39 +00:00
|
|
|
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr)
|
2023-06-21 08:35:43 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
|
|
|
}
|