muyu-apiserver/rpc/production/internal/logic/getsharelinkplanviewlogic.go

112 lines
3.1 KiB
Go

package logic
import (
"context"
"fmt"
"time"
"muyu-apiserver/rpc/production/internal/svc"
"muyu-apiserver/rpc/production/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetShareLinkPlanViewLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetShareLinkPlanViewLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetShareLinkPlanViewLogic {
return &GetShareLinkPlanViewLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}
func (l *GetShareLinkPlanViewLogic) GetShareLinkPlanView(in *pb.GetShareLinkReq) (*pb.ShareLinkPlanView, error) {
link, err := l.svcCtx.ShareLinkModel.FindByShortCode(l.ctx, in.ShortCode)
if err != nil {
return nil, ErrLinkNotFound
}
// Check if expired
if time.Now().After(link.ExpiresAt) {
return nil, ErrLinkExpired
}
plan, err := l.svcCtx.PlanModel.FindOneByPlanId(l.ctx, link.PlanId)
if err != nil {
return nil, ErrPlanNotFound
}
planInfo := &pb.ProductionPlanInfo{
PlanId: plan.PlanId,
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),
Status: plan.Status,
CreatedAt: plan.CreatedAt.Format("2006-01-02 15:04:05"),
}
if plan.StartTime.Valid {
planInfo.StartTime = plan.StartTime.Time.Format("2006-01-02")
}
// If hide_sensitive is set, mask price/supplier info
if link.HideSensitive == 0 {
planInfo.YarnUsagePerMeter = fmt.Sprintf("%.4f", plan.YarnUsagePerMeter)
planInfo.ProductionPrice = fmt.Sprintf("%.2f", plan.ProductionPrice)
planInfo.SupplierId = plan.SupplierId
}
// Load yarn ratios
ratios, err := l.svcCtx.YarnRatioModel.FindByPlanId(l.ctx, link.PlanId)
if err == nil {
for _, r := range ratios {
planInfo.YarnRatios = append(planInfo.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),
})
}
}
// Load process steps
steps, err := l.svcCtx.ProcessStepModel.FindByPlanId(l.ctx, link.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,
}
if s.CompletedAt.Valid {
stepInfo.CompletedAt = s.CompletedAt.Time.Format("2006-01-02 15:04:05")
}
planInfo.ProcessSteps = append(planInfo.ProcessSteps, stepInfo)
}
}
linkInfo := &pb.ShareLinkInfo{
LinkId: link.LinkId,
PlanId: link.PlanId,
ShortCode: link.ShortCode,
FactoryType: link.FactoryType,
HideSensitive: link.HideSensitive,
Status: link.Status,
ExpiresAt: link.ExpiresAt.Format("2006-01-02 15:04:05"),
}
return &pb.ShareLinkPlanView{
Plan: planInfo,
Link: linkInfo,
}, nil
}