69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"muyu-apiserver/model"
|
||
|
|
"muyu-apiserver/pkg/tenantctx"
|
||
|
|
"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 CreateYarnLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCreateYarnLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateYarnLogic {
|
||
|
|
return &CreateYarnLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *CreateYarnLogic) CreateYarn(in *pb.CreateYarnReq) (*pb.IdResp, error) {
|
||
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
||
|
|
yarnName := strings.TrimSpace(in.YarnName)
|
||
|
|
if yarnName == "" {
|
||
|
|
return nil, status.Error(codes.InvalidArgument, "纱线名称不能为空")
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(in.SupplierId) == "" {
|
||
|
|
return nil, status.Error(codes.InvalidArgument, "供应商不能为空")
|
||
|
|
}
|
||
|
|
supplier, err := l.svcCtx.SupplierModel.FindOneBySupplierId(l.ctx, in.SupplierId)
|
||
|
|
if err != nil || supplier.TenantId != tenantId {
|
||
|
|
return nil, status.Error(codes.InvalidArgument, "供应商不存在")
|
||
|
|
}
|
||
|
|
|
||
|
|
color := normalizeYarnColor(in.Color)
|
||
|
|
if _, err := l.svcCtx.YarnModel.FindOneByUnique(l.ctx, tenantId, yarnName, color, in.SupplierId); err == nil {
|
||
|
|
return nil, status.Error(codes.AlreadyExists, "纱线已存在")
|
||
|
|
} else if !errors.Is(err, model.ErrNotFound) {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
yarnId := uid.Generate()
|
||
|
|
yarn := &model.InvYarn{
|
||
|
|
YarnId: yarnId,
|
||
|
|
TenantId: tenantId,
|
||
|
|
YarnName: yarnName,
|
||
|
|
Color: color,
|
||
|
|
WeightGM: parseDecimalString(in.WeightGM),
|
||
|
|
SupplierId: in.SupplierId,
|
||
|
|
DyeFactory: normalizeDyeFactory(in.DyeFactory),
|
||
|
|
ImageUrl: in.ImageUrl,
|
||
|
|
Remark: sql.NullString{String: in.Remark, Valid: in.Remark != ""},
|
||
|
|
Status: 1,
|
||
|
|
}
|
||
|
|
if _, err := l.svcCtx.YarnModel.Insert(l.ctx, yarn); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
return &pb.IdResp{Id: yarnId}, nil
|
||
|
|
}
|