85 lines
2.0 KiB
Go
85 lines
2.0 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 ListInboundLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewListInboundLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListInboundLogic {
|
|
return &ListInboundLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *ListInboundLogic) ListInbound(in *pb.ListInboundReq) (*pb.ListInboundResp, error) {
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
|
|
|
// Verify requester has access to this plan (owner or associated factory)
|
|
plan, err := l.svcCtx.PlanModel.FindOneByPlanId(l.ctx, in.PlanId)
|
|
if err != nil {
|
|
return nil, ErrPlanNotFound
|
|
}
|
|
if plan.TenantId != tenantId {
|
|
factories, fErr := l.svcCtx.PlanFactoryModel.FindByPlanId(l.ctx, in.PlanId)
|
|
if fErr != nil {
|
|
return nil, ErrPermissionDenied
|
|
}
|
|
authorized := false
|
|
for _, f := range factories {
|
|
if f.FactoryTenantId == tenantId {
|
|
authorized = true
|
|
break
|
|
}
|
|
}
|
|
if !authorized {
|
|
return nil, ErrPermissionDenied
|
|
}
|
|
}
|
|
|
|
page := in.Page
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
pageSize := in.PageSize
|
|
if pageSize <= 0 {
|
|
pageSize = 20
|
|
}
|
|
|
|
records, total, err := l.svcCtx.InboundModel.FindByPlanId(l.ctx, in.PlanId, page, pageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
list := make([]*pb.InboundRecordInfo, 0, len(records))
|
|
for _, r := range records {
|
|
list = append(list, &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,
|
|
Remark: r.Remark.String,
|
|
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
return &pb.ListInboundResp{
|
|
Total: total,
|
|
List: list,
|
|
}, nil
|
|
}
|