2026-02-28 15:29:16 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"strconv"
|
2026-07-05 08:06:28 +09:00
|
|
|
"strings"
|
2026-02-28 15:29:16 +08:00
|
|
|
|
|
|
|
|
"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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
salesPrice, _ := strconv.ParseFloat(in.SalesPrice, 64)
|
|
|
|
|
|
2026-07-05 08:06:28 +09:00
|
|
|
productName := strings.TrimSpace(in.ProductName)
|
|
|
|
|
if productName == "" {
|
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "产品名称不能为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
product.ProductName = productName
|
2026-02-28 15:29:16 +08:00
|
|
|
product.ImageUrl = in.ImageUrl
|
|
|
|
|
product.Spec = in.Spec
|
2026-07-05 08:06:28 +09:00
|
|
|
product.Color = normalizeProductColor(in.Color)
|
|
|
|
|
if strings.TrimSpace(in.ColorNo) != "" {
|
|
|
|
|
product.ColorNo = normalizeColorNo(in.ColorNo)
|
|
|
|
|
}
|
2026-02-28 15:29:16 +08:00
|
|
|
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
|
|
|
|
|
}
|