63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"muyu-apiserver/pkg/tenantctx"
|
||
|
|
"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 UpdateYarnLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewUpdateYarnLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateYarnLogic {
|
||
|
|
return &UpdateYarnLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *UpdateYarnLogic) UpdateYarn(in *pb.UpdateYarnReq) (*pb.Empty, error) {
|
||
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
||
|
|
yarn, err := l.svcCtx.YarnModel.FindOneByYarnId(l.ctx, in.YarnId)
|
||
|
|
if err != nil || yarn.TenantId != tenantId {
|
||
|
|
return nil, status.Error(codes.NotFound, "纱线不存在")
|
||
|
|
}
|
||
|
|
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, "供应商不存在")
|
||
|
|
}
|
||
|
|
|
||
|
|
yarn.YarnName = yarnName
|
||
|
|
yarn.Color = normalizeYarnColor(in.Color)
|
||
|
|
yarn.WeightGM = parseDecimalString(in.WeightGM)
|
||
|
|
yarn.SupplierId = in.SupplierId
|
||
|
|
yarn.DyeFactory = normalizeDyeFactory(in.DyeFactory)
|
||
|
|
yarn.ImageUrl = in.ImageUrl
|
||
|
|
yarn.Remark = sql.NullString{String: in.Remark, Valid: in.Remark != ""}
|
||
|
|
if in.Status == 0 {
|
||
|
|
yarn.Status = 0
|
||
|
|
} else {
|
||
|
|
yarn.Status = 1
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := l.svcCtx.YarnModel.Update(l.ctx, yarn); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
return &pb.Empty{}, nil
|
||
|
|
}
|