Pull latest changes from upstream/main (GitHub) including: - Purchase RPC service (proto, server, logic, models) - Gateway route updates for purchase endpoints - SQL migration and config updates Excludes deploy/bin/ pre-compiled arm64 binaries (builds use multi-stage Dockerfiles targeting amd64).
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"muyu-apiserver/pkg/tenantctx"
|
|
"muyu-apiserver/rpc/purchase/internal/svc"
|
|
"muyu-apiserver/rpc/purchase/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetPurchaseStatsBySupplierLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetPurchaseStatsBySupplierLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPurchaseStatsBySupplierLogic {
|
|
return &GetPurchaseStatsBySupplierLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *GetPurchaseStatsBySupplierLogic) GetPurchaseStatsBySupplier(in *pb.PurchaseStatsSummaryReq) (*pb.PurchaseStatsBySupplierResp, error) {
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
|
|
|
orders, _, err := l.svcCtx.OrderModel.FindList(l.ctx, tenantId, 1, 10000, "", -1, in.StartDate, in.EndDate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
type supplierStats struct {
|
|
purchaseAmount float64
|
|
paidAmount float64
|
|
orderCount int64
|
|
}
|
|
statsMap := map[string]*supplierStats{}
|
|
for _, o := range orders {
|
|
if o.Status == 2 {
|
|
continue
|
|
}
|
|
s := statsMap[o.SupplierId]
|
|
if s == nil {
|
|
s = &supplierStats{}
|
|
statsMap[o.SupplierId] = s
|
|
}
|
|
s.purchaseAmount += o.ContractAmount
|
|
s.paidAmount += o.PaidAmount
|
|
s.orderCount++
|
|
}
|
|
|
|
items := make([]*pb.PurchaseStatsBySupplierItem, 0, len(statsMap))
|
|
for supplierId, s := range statsMap {
|
|
name := ""
|
|
if sup, err := l.svcCtx.SupplierModel.FindOneBySupplierId(l.ctx, supplierId); err == nil {
|
|
name = sup.SupplierName
|
|
}
|
|
items = append(items, &pb.PurchaseStatsBySupplierItem{
|
|
SupplierId: supplierId,
|
|
SupplierName: name,
|
|
PurchaseAmount: fmt.Sprintf("%.2f", s.purchaseAmount),
|
|
PaidAmount: fmt.Sprintf("%.2f", s.paidAmount),
|
|
OrderCount: s.orderCount,
|
|
})
|
|
}
|
|
return &pb.PurchaseStatsBySupplierResp{List: items}, nil
|
|
}
|