This commit is contained in:
laodaming 2023-06-26 16:53:36 +08:00
parent 0afaed5089
commit 7f71afd8b0
7 changed files with 248 additions and 1 deletions

View File

@ -10,3 +10,10 @@ func (l *FsProductModel3dLightModel) GetAll(ctx context.Context) (resp []FsProdu
err = l.db.WithContext(ctx).Model(&FsProductModel3dLight{}).Where("`status` = ?", 1).Find(&resp).Error
return resp, err
}
func (l *FsProductModel3dLightModel) Create(ctx context.Context, data *FsProductModel3dLight) error {
return l.db.WithContext(ctx).Create(&data).Error
}
func (l *FsProductModel3dLightModel) Update(ctx context.Context, id int64, data *FsProductModel3dLight) error {
return l.db.WithContext(ctx).Where("`id` = ?", id).Updates(data).Error
}

View File

@ -5,7 +5,7 @@ import (
)
func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64) (resp *FsProductModel3d, err error) {
err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id).Find(&resp).Error
err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id).First(&resp).Error
return resp, err
}
func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, fields ...string) (resp []FsProductModel3d, err error) {
@ -46,3 +46,6 @@ func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3
err = db.Find(&resp).Error
return resp, err
}
func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error {
return d.db.WithContext(ctx).Where("`id` = ? ", id).Updates(&data).Error
}

View File

@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/product-model/other-info",
Handler: GetModelOtherInfoHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/product-model/update-model",
Handler: UpdateProductModelHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,41 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/basic"
"fusenapi/server/product-model/internal/logic"
"fusenapi/server/product-model/internal/svc"
"fusenapi/server/product-model/internal/types"
)
func UpdateProductModelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdateProductModelReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUpdateProductModelLogic(r.Context(), svcCtx)
resp := l.UpdateProductModel(&req, 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,113 @@
package logic
import (
"encoding/json"
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"gorm.io/gorm"
"net/http"
"strings"
"time"
"context"
"fusenapi/server/product-model/internal/svc"
"fusenapi/server/product-model/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateProductModelLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUpdateProductModelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductModelLogic {
return &UpdateProductModelLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateProductModelLogic) UpdateProductModel(req *types.UpdateProductModelReq, 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")
}
if req.ModelData.Id <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "modelData`s id is required")
}
//查询模型信息
productModel3dInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.ModelData.Id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "3d model info is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model info")
}
lightInfoBytes, _ := json.Marshal(req.LightData.Info)
lightInfoJson := string(lightInfoBytes)
modelLightSaveData := gmodel.FsProductModel3dLight{
Name: &req.LightData.Name,
Info: &lightInfoJson,
}
tx := l.svcCtx.MysqlConn.Begin()
lightModel := gmodel.NewFsProductModel3dLightModel(tx)
productModel3dModel := gmodel.NewFsProductModel3dModel(tx)
now := time.Now().Unix()
if req.LightData.Id > 0 { //更新
modelLightSaveData.Id = req.LightData.Id
err = lightModel.Update(l.ctx, req.LightData.Id, &modelLightSaveData)
} else { //添加
modelLightSaveData.Ctime = &now
err = lightModel.Create(l.ctx, &modelLightSaveData)
}
if err != nil {
logx.Error(err)
tx.Rollback()
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save light info")
}
partListStr := ""
for _, v := range req.ModelData.PartsList {
partListStr += fmt.Sprintf("%d,", v)
partListStr = strings.TrimRight(partListStr, ",")
}
//赋值
req.ModelData.LightData = modelLightSaveData.Id
//目前1个模型只有1组灯光
req.ModelData.LightList = []int64{modelLightSaveData.Id}
modelInfoBytes, _ := json.Marshal(req.ModelData)
modelInfoJson := string(modelInfoBytes)
lightIdsBytes, _ := json.Marshal([]int64{modelLightSaveData.Id})
lightIdsStr := string(lightIdsBytes)
modelSaveData := gmodel.FsProductModel3d{
Name: productModel3dInfo.Name,
ModelInfo: &modelInfoJson,
Light: &modelLightSaveData.Id,
LightList: &lightIdsStr,
PartId: &req.ModelData.Parts,
PartList: &partListStr,
}
if err = productModel3dModel.Update(l.ctx, req.ModelData.Id, &modelSaveData); err != nil {
logx.Error(err)
tx.Rollback()
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save model info")
}
tx.Commit()
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UpdateProductModelRsp{
ModelId: productModel3dInfo.Id,
LightId: modelLightSaveData.Id,
LightList: []int64{modelLightSaveData.Id},
})
}

View File

@ -36,6 +36,45 @@ type PartListItem struct {
ModelInfo interface{} `json:"model_info"`
}
type UpdateProductModelReq struct {
ModelData ModelData `json:"modelData"`
LightData LightData `json:"lightData"`
}
type LightData struct {
Id int64 `json:"id"`
Name string `json:"name"`
Info LightInfo `json:"info"`
}
type LightInfo struct {
Name string `json:"name"`
Hdr map[string]interface{} `json:"hdr"`
LightData []map[string]interface{} `json:"lightData"`
}
type ModelData struct {
Id int64 `json:"id"`
Name string `json:"name"`
KnifeTerritory string `json:"knifeTerritory"`
Cover string `json:"cover"`
CameraData map[string]interface{} `json:"cameraData"`
ControlsData map[string]interface{} `json:"controlsData"`
Material map[string]interface{} `json:"material"`
ModelData map[string]interface{} `json:"modelData"`
LightList []int64 `json:"lightList"`
Parts int64 `json:"parts"`
PartsList []int64 `json:"partsList"`
Tag int64 `json:"tag"`
LightData int64 `json:"lightData"`
}
type UpdateProductModelRsp struct {
ModelId int64 `json:"modelId"`
LightId int64 `json:"lightId"`
LightList []int64 `json:"lightList"`
}
type Request struct {
}

View File

@ -16,6 +16,9 @@ service product-model {
//获取产品模型其他信息
@handler GetModelOtherInfoHandler
get /product-model/other-info(GetModelOtherInfoReq) returns (response);
//更新产品模型
@handler UpdateProductModelHandler
post /product-model/update-model(UpdateProductModelReq) returns (response);
}
//获取产品模型详情
@ -45,4 +48,40 @@ type PartListItem {
Name string `json:"name"`
MaterialImg string `json:"material_img"`
ModelInfo interface{} `json:"model_info"`
}
//更新产品模型
type UpdateProductModelReq {
ModelData ModelData `json:"modelData"`
LightData LightData `json:"lightData"`
}
type LightData {
Id int64 `json:"id"`
Name string `json:"name"`
Info LightInfo `json:"info"`
}
type LightInfo {
Name string `json:"name"`
Hdr map[string]interface{} `json:"hdr"`
LightData []map[string]interface{} `json:"lightData"`
}
type ModelData {
Id int64 `json:"id"`
Name string `json:"name"`
KnifeTerritory string `json:"knifeTerritory"`
Cover string `json:"cover"`
CameraData map[string]interface{} `json:"cameraData"`
ControlsData map[string]interface{} `json:"controlsData"`
Material map[string]interface{} `json:"material"`
ModelData map[string]interface{} `json:"modelData"`
LightList []int64 `json:"lightList"`
Parts int64 `json:"parts"`
PartsList []int64 `json:"partsList"`
Tag int64 `json:"tag"`
LightData int64 `json:"lightData"`
}
type UpdateProductModelRsp {
ModelId int64 `json:"modelId"`
LightId int64 `json:"lightId"`
LightList []int64 `json:"lightList"`
}