From e1c0fe2768d32f79920ceda92383981d4ff12058 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期五, 15 三月 2024 10:07:50 +0800
Subject: [PATCH] 获取销售明细产品信息

---
 api/v1/salesDetails.go |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/api/v1/salesDetails.go b/api/v1/salesDetails.go
index 874d2c4..9f94f9e 100644
--- a/api/v1/salesDetails.go
+++ b/api/v1/salesDetails.go
@@ -11,6 +11,7 @@
 	"aps_crm/pkg/logx"
 	"aps_crm/pkg/structx"
 	"aps_crm/proto/crm_aps"
+	"aps_crm/proto/crm_srm"
 	"aps_crm/proto/product_inventory"
 	"aps_crm/utils"
 	"github.com/gin-gonic/gin"
@@ -385,3 +386,112 @@
 	}
 	ctx.Ok()
 }
+
+// GetSalesDetailsProductInfo
+//
+// @Tags		SalesDetails
+// @Summary	  鑾峰彇閿�鍞槑缁嗕骇鍝佷俊鎭�
+// @Produce	application/json
+// @Param		number	path		string	true	"鏄庣粏缂栫爜"
+// @Success	200	{object}	response.ListResponse
+//
+//	@Router		/api/salesDetails/getSalesDetailsProductInfo/{number} [get]
+func (s *SalesDetailsApi) GetSalesDetailsProductInfo(c *gin.Context) {
+	ctx, ok := contextx.NewContext(c, nil)
+	if !ok {
+		return
+	}
+	number := c.Param("number")
+	first, err := model.NewSalesDetailsSearch().SetPreload(true).SetNumber(number).First()
+	if err != nil {
+		ctx.FailWithMsg(ecode.DBErr, "鏌ヨ閿�鍞槑缁嗗嚭閿�")
+		return
+	}
+	resp := make([]response.SalesDetailsProductInfo, 0)
+	amountMap := make(map[string]int64)
+	overMap := make(map[string]int64)
+	for _, product := range first.Products {
+		amountMap[product.Number] = 0
+		overMap[product.Number] = 0
+		var sdpi response.SalesDetailsProductInfo
+		sdpi.ProductId = product.Number
+		sdpi.ProductName = product.Name
+		sdpi.Specs = product.Specs
+		sdpi.Unit = product.Unit
+		sdpi.Amount = product.Amount
+		sdpi.Cost = product.Cost
+		sdpi.Price = product.Price
+		sdpi.Total = product.Total
+		sdpi.Profit = product.Profit
+		sdpi.Margin = product.Margin
+		resp = append(resp, sdpi)
+	}
+	srmClient := crm_srm.NewCrmAndSrmServiceClient(grpc_init.CrmSrmGrpcServiceConn)
+	info, err := srmClient.CrmGetPurchaseInfo(c, &crm_srm.CrmGetPurchaseInfoRequest{SalesDetailsNumber: number})
+	if err != nil {
+		logx.Errorf("grpc CrmGetPurchaseInfo err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "鍐呴儴閿欒")
+		return
+	}
+	//閲囪喘鏁伴噺
+	for _, purchaseInfo := range info.Info {
+		amount := amountMap[purchaseInfo.ProductId]
+		amount += purchaseInfo.Amount
+		amountMap[purchaseInfo.ProductId] = amount
+		over := overMap[purchaseInfo.ProductId]
+		over += purchaseInfo.FinishAmount
+		overMap[purchaseInfo.ProductId] = over
+	}
+	for i := 0; i < len(resp); i++ {
+		resp[i].PurchaseAmount = amountMap[resp[i].ProductId]
+		amountMap[resp[i].ProductId] = 0
+		resp[i].PurchaseFinishAmount = overMap[resp[i].ProductId]
+		resp[i].FinishAmount = resp[i].FinishAmount + overMap[resp[i].ProductId]
+		overMap[resp[i].ProductId] = 0
+	}
+
+	apsClient := crm_aps.NewCrmAndApsGrpcServiceClient(grpc_init.CrmApsGrpcServiceConn)
+	productInfo, err := apsClient.CrmGetMakeAndOutsourcingProductInfo(c, &crm_aps.CrmGetMakeAndOutsourcingProductInfoRequest{SalesDetailsNumber: number})
+	if err != nil {
+		logx.Errorf("grpc CrmGetMakeAndOutsourcingProductInfo err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "鍐呴儴閿欒")
+		return
+	}
+	//鍒堕�犳暟閲�
+	for _, makeInfo := range productInfo.Info {
+		if makeInfo.Type == 2 {
+			amount := amountMap[makeInfo.ProductId]
+			amount += makeInfo.Amount
+			amountMap[makeInfo.ProductId] = amount
+			over := overMap[makeInfo.ProductId]
+			over += makeInfo.FinishAmount
+			overMap[makeInfo.ProductId] = over
+		}
+	}
+	for i := 0; i < len(resp); i++ {
+		resp[i].MakeAmount = amountMap[resp[i].ProductId]
+		amountMap[resp[i].ProductId] = 0
+		resp[i].MakeFinishAmount = overMap[resp[i].ProductId]
+		resp[i].FinishAmount = resp[i].FinishAmount + overMap[resp[i].ProductId]
+		overMap[resp[i].ProductId] = 0
+	}
+	//濮斿鏁伴噺
+	for _, outsourcingInfo := range productInfo.Info {
+		if outsourcingInfo.Type == 3 {
+			amount := amountMap[outsourcingInfo.ProductId]
+			amount += outsourcingInfo.Amount
+			amountMap[outsourcingInfo.ProductId] = amount
+			over := overMap[outsourcingInfo.ProductId]
+			over += outsourcingInfo.FinishAmount
+			overMap[outsourcingInfo.ProductId] = over
+		}
+	}
+	for i := 0; i < len(resp); i++ {
+		resp[i].OutsourcingAmount = amountMap[resp[i].ProductId]
+		amountMap[resp[i].ProductId] = 0
+		resp[i].OutsourcingFinishAmount = overMap[resp[i].ProductId]
+		resp[i].FinishAmount = resp[i].FinishAmount + overMap[resp[i].ProductId]
+		overMap[resp[i].ProductId] = 0
+	}
+	ctx.OkWithDetailed(resp)
+}

--
Gitblit v1.8.0