59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"strconv"
|
||
|
|
|
||
|
|
"muyu-apiserver/rpc/inventory/internal/svc"
|
||
|
|
"muyu-apiserver/rpc/inventory/pb"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
|
"google.golang.org/grpc/codes"
|
||
|
|
"google.golang.org/grpc/status"
|
||
|
|
)
|
||
|
|
|
||
|
|
type UpdateProductLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewUpdateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductLogic {
|
||
|
|
return &UpdateProductLogic{
|
||
|
|
ctx: ctx,
|
||
|
|
svcCtx: svcCtx,
|
||
|
|
Logger: logx.WithContext(ctx),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *UpdateProductLogic) UpdateProduct(in *pb.UpdateProductReq) (*pb.Empty, error) {
|
||
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, in.ProductId)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Error(codes.NotFound, "product not found")
|
||
|
|
}
|
||
|
|
|
||
|
|
stockQuantity, _ := strconv.ParseFloat(in.StockQuantity, 64)
|
||
|
|
costPrice, _ := strconv.ParseFloat(in.CostPrice, 64)
|
||
|
|
salesPrice, _ := strconv.ParseFloat(in.SalesPrice, 64)
|
||
|
|
|
||
|
|
product.ProductName = in.ProductName
|
||
|
|
product.ImageUrl = in.ImageUrl
|
||
|
|
product.Spec = in.Spec
|
||
|
|
product.Color = in.Color
|
||
|
|
product.UnitPieces = in.UnitPieces
|
||
|
|
product.UnitRolls = in.UnitRolls
|
||
|
|
product.StockQuantity = stockQuantity
|
||
|
|
product.Location = in.Location
|
||
|
|
product.CostPrice = costPrice
|
||
|
|
product.SalesPrice = salesPrice
|
||
|
|
product.Remark = sql.NullString{String: in.Remark, Valid: in.Remark != ""}
|
||
|
|
|
||
|
|
err = l.svcCtx.ProductModel.Update(l.ctx, product)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.Empty{}, nil
|
||
|
|
}
|