126 lines
3.7 KiB
Go
126 lines
3.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"muyu-apiserver/pkg/tenantctx"
|
|
"muyu-apiserver/rpc/production/internal/svc"
|
|
"muyu-apiserver/rpc/production/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetProductionPlanLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetProductionPlanLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductionPlanLogic {
|
|
return &GetProductionPlanLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *GetProductionPlanLogic) GetProductionPlan(in *pb.GetProductionPlanReq) (*pb.ProductionPlanInfo, error) {
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
|
|
|
plan, err := l.svcCtx.PlanModel.FindOneByPlanId(l.ctx, in.PlanId)
|
|
if err != nil {
|
|
return nil, ErrPlanNotFound
|
|
}
|
|
|
|
if plan.TenantId != tenantId {
|
|
return nil, ErrPermissionDenied
|
|
}
|
|
|
|
info := &pb.ProductionPlanInfo{
|
|
PlanId: plan.PlanId,
|
|
TenantId: plan.TenantId,
|
|
PlanCode: plan.PlanCode,
|
|
ProductId: plan.ProductId,
|
|
ProductName: plan.ProductName,
|
|
Color: plan.Color,
|
|
FabricCode: plan.FabricCode,
|
|
ColorCode: plan.ColorCode,
|
|
TargetQuantity: fmt.Sprintf("%.2f", plan.TargetQuantity),
|
|
CompletedQuantity: fmt.Sprintf("%.2f", plan.CompletedQuantity),
|
|
YarnUsagePerMeter: fmt.Sprintf("%.4f", plan.YarnUsagePerMeter),
|
|
ProductionPrice: fmt.Sprintf("%.2f", plan.ProductionPrice),
|
|
SupplierId: plan.SupplierId,
|
|
Status: plan.Status,
|
|
Remark: plan.Remark.String,
|
|
Creator: plan.Creator,
|
|
Operator: plan.Operator,
|
|
CreatedAt: plan.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: plan.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
if plan.StartTime.Valid {
|
|
info.StartTime = plan.StartTime.Time.Format("2006-01-02")
|
|
}
|
|
|
|
// Load yarn ratios
|
|
ratios, err := l.svcCtx.YarnRatioModel.FindByPlanId(l.ctx, in.PlanId)
|
|
if err == nil {
|
|
for _, r := range ratios {
|
|
info.YarnRatios = append(info.YarnRatios, &pb.YarnRatioInfo{
|
|
RatioId: r.RatioId,
|
|
PlanId: r.PlanId,
|
|
YarnName: r.YarnName,
|
|
Ratio: fmt.Sprintf("%.2f", r.Ratio),
|
|
AmountPerMeter: fmt.Sprintf("%.4f", r.AmountPerMeter),
|
|
TotalAmount: fmt.Sprintf("%.2f", r.TotalAmount),
|
|
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
}
|
|
|
|
// Load process steps
|
|
steps, err := l.svcCtx.ProcessStepModel.FindByPlanId(l.ctx, in.PlanId)
|
|
if err == nil {
|
|
for _, s := range steps {
|
|
stepInfo := &pb.ProcessStepInfo{
|
|
StepId: s.StepId,
|
|
PlanId: s.PlanId,
|
|
StepType: s.StepType,
|
|
StepOrder: s.StepOrder,
|
|
Status: s.Status,
|
|
OperatorId: s.OperatorId,
|
|
CreatedAt: s.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: s.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
if s.CompletedAt.Valid {
|
|
stepInfo.CompletedAt = s.CompletedAt.Time.Format("2006-01-02 15:04:05")
|
|
}
|
|
if s.Remark.Valid {
|
|
stepInfo.Remark = s.Remark.String
|
|
}
|
|
info.ProcessSteps = append(info.ProcessSteps, stepInfo)
|
|
}
|
|
}
|
|
|
|
// Load inbound records
|
|
records, _, err := l.svcCtx.InboundModel.FindByPlanId(l.ctx, in.PlanId, 1, 100)
|
|
if err == nil {
|
|
for _, r := range records {
|
|
rec := &pb.InboundRecordInfo{
|
|
RecordId: r.RecordId,
|
|
TenantId: r.TenantId,
|
|
PlanId: r.PlanId,
|
|
ProductId: r.ProductId,
|
|
BatchNo: r.BatchNo,
|
|
Quantity: fmt.Sprintf("%.2f", r.Quantity),
|
|
Rolls: r.Rolls,
|
|
PricePerMeter: fmt.Sprintf("%.2f", r.PricePerMeter),
|
|
OperatorId: r.OperatorId,
|
|
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
if r.Remark.Valid {
|
|
rec.Remark = r.Remark.String
|
|
}
|
|
info.InboundRecords = append(info.InboundRecords, rec)
|
|
}
|
|
}
|
|
|
|
return info, nil
|
|
}
|