62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strconv"
|
|
|
|
"muyu-apiserver/model"
|
|
"muyu-apiserver/pkg/uid"
|
|
"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 CreateProductLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateProductLogic {
|
|
return &CreateProductLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *CreateProductLogic) CreateProduct(in *pb.CreateProductReq) (*pb.IdResp, error) {
|
|
productId := uid.Generate()
|
|
|
|
stockQuantity, _ := strconv.ParseFloat(in.StockQuantity, 64)
|
|
costPrice, _ := strconv.ParseFloat(in.CostPrice, 64)
|
|
salesPrice, _ := strconv.ParseFloat(in.SalesPrice, 64)
|
|
|
|
product := &model.InvProduct{
|
|
ProductId: productId,
|
|
ProductName: in.ProductName,
|
|
ImageUrl: in.ImageUrl,
|
|
Spec: in.Spec,
|
|
Color: in.Color,
|
|
UnitPieces: in.UnitPieces,
|
|
UnitRolls: in.UnitRolls,
|
|
StockQuantity: stockQuantity,
|
|
Location: in.Location,
|
|
CostPrice: costPrice,
|
|
SalesPrice: salesPrice,
|
|
Remark: sql.NullString{String: in.Remark, Valid: in.Remark != ""},
|
|
Status: 1,
|
|
}
|
|
|
|
_, err := l.svcCtx.ProductModel.Insert(l.ctx, product)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
return &pb.IdResp{Id: productId}, nil
|
|
}
|