From 1cd1206c18ed14dd9ebf2baf477f46c847a271b0 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 15 三月 2024 16:39:19 +0800
Subject: [PATCH] merge

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

diff --git a/api/v1/salesDetails.go b/api/v1/salesDetails.go
index 874d2c4..b57b78d 100644
--- a/api/v1/salesDetails.go
+++ b/api/v1/salesDetails.go
@@ -15,6 +15,7 @@
 	"aps_crm/utils"
 	"github.com/gin-gonic/gin"
 	"github.com/shopspring/decimal"
+	"gorm.io/gorm"
 	"strconv"
 	"strings"
 )
@@ -240,7 +241,7 @@
 // GetProductInventoryInfo
 //
 // @Tags		SalesDetails
-// @Summary	鑾峰彇浜у搧搴撳瓨淇℃伅
+// @Summary	鑾峰彇浜у搧鍙戣揣淇℃伅
 // @Produce	application/json
 // @Param		number	path		string	true	"鏄庣粏缂栫爜"
 // @Success	200	{object}	response.ListResponse
@@ -272,6 +273,172 @@
 	ctx.OkWithDetailed(list)
 }
 
+type GetWarehouseProductInfoReq struct {
+	SaleDetailID     int    `json:"saleDetailID,omitempty"`
+	SaleDetailNumber string `json:"saleDetailNumber,omitempty"`
+}
+
+// GetDeliveryPrepareInfo
+// @Tags		SalesDetails
+// @Summary	鑾峰彇浜у搧鍏ュ簱淇℃伅
+// @Produce	application/json
+// @Param		object 	body GetWarehouseProductInfoReq	true	"鏄庣粏缂栫爜"
+// @Success	200	{object}	response.ListResponse
+// @Router		/api/salesDetails/getDeliveryPrepareInfo [post]
+func (s *SalesDetailsApi) GetDeliveryPrepareInfo(c *gin.Context) {
+	var params GetWarehouseProductInfoReq
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	if params.SaleDetailID == 0 {
+		ctx.FailWithMsg(ecode.ParamsErr, "鍙傛暟缂哄け")
+		return
+	}
+
+	salesDetails, err := salesDetailsService.GetSalesDetails(params.SaleDetailID)
+	if err == gorm.ErrRecordNotFound || salesDetails.Number != params.SaleDetailNumber {
+		ctx.FailWithMsg(ecode.ParamsErr, "閿�鍞鍗曚笉瀛樺湪")
+		return
+	}
+
+	productMap := model.ProductMap(salesDetails.Products)
+	client := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
+	grpcResp, err := client.GetOrderInputAndOutputInfo(ctx.GetCtx(), &product_inventory.GetOrderInputAndOutputInfoRequest{
+		Number: params.SaleDetailNumber,
+	})
+	if err != nil {
+		if strings.Contains(err.Error(), "record not found") {
+			ctx.Ok()
+			return
+		}
+		logx.Errorf("GetOrderInputAndOutputInfo err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒")
+		return
+	}
+
+	grpcOutputList := grpcResp.OutputList
+	grpcInputList := grpcResp.InputList
+	inputProductMap := make(map[string]*response.StoreInfo)
+	outputProductMap := make(map[string]*response.OutputSimpleInfo)
+	for _, v := range grpcOutputList {
+		if outputProductMap[v.Number] == nil {
+			simpleInfo := &response.OutputSimpleInfo{
+				Number: v.Number,
+			}
+			amount, _ := decimal.NewFromString(v.Amount)
+			simpleInfo.Amount = amount
+			outputProductMap[v.Number] = simpleInfo
+		} else {
+			amount, _ := decimal.NewFromString(v.Amount)
+			outputProductMap[v.Number].Amount = outputProductMap[v.Number].Amount.Add(amount)
+		}
+	}
+	for _, v := range grpcInputList {
+		if inputProductMap[v.Number] == nil {
+			storeInfo := &response.StoreInfo{
+				Number:      v.Number,
+				Name:        v.Name,
+				OrderAmount: productMap[v.Number].Amount,
+			}
+			finishAmount, _ := decimal.NewFromString(v.Amount)
+			storeInfo.FinishAmount = finishAmount
+			storeInfo.AvailableAmount = finishAmount
+			storeInfo.LeftAmount = storeInfo.OrderAmount
+			inputProductMap[v.Number] = storeInfo
+		} else {
+			finishAmount, _ := decimal.NewFromString(v.Amount)
+			inputProductMap[v.Number].FinishAmount = inputProductMap[v.Number].FinishAmount.Add(finishAmount)
+			inputProductMap[v.Number].AvailableAmount = inputProductMap[v.Number].FinishAmount
+		}
+	}
+	storeList := make([]*response.StoreInfo, 0, len(salesDetails.Products))
+	for _, product := range salesDetails.Products {
+		storeInfo := inputProductMap[product.Number]
+		if storeInfo == nil { //娌℃湁鍏ュ簱淇℃伅
+			storeInfo = &response.StoreInfo{
+				Name:            product.Number,
+				Number:          product.Number,
+				OrderAmount:     product.Amount,
+				FinishAmount:    decimal.Decimal{},
+				LeftAmount:      product.Amount,
+				AvailableAmount: decimal.Decimal{},
+			}
+		} else { //鏈夊叆搴撴暟閲忓啀鏌ュ嚭搴擄紝绠楀嚭鏈彂璐ф暟閲�
+			if outputProductMap[product.Number] != nil {
+				outputInfo := outputProductMap[product.Number]
+				storeInfo.LeftAmount = storeInfo.LeftAmount.Sub(outputInfo.Amount)           //鍓╀綑鍙戣揣鏁伴噺 = 璁㈠崟鏁伴噺 - 宸插彂璐ф暟閲�
+				storeInfo.AvailableAmount = storeInfo.AvailableAmount.Sub(outputInfo.Amount) //鍙敤鏁伴噺 = 鍏ュ簱瀹屾垚鏁伴噺 - 宸插彂璐ф暟閲�
+			}
+		}
+		storeList = append(storeList, storeInfo)
+	}
+	ctx.OkWithDetailed(storeList)
+}
+
+// GetDeliveryList
+// @Tags		SalesDetails
+// @Summary	鍙戣揣鏄庣粏
+// @Produce	application/json
+// @Param		object 	body GetWarehouseProductInfoReq	true	"鏄庣粏缂栫爜"
+// @Success	200	{object}	response.ListResponse
+// @Router		/api/salesDetails/getDeliveryList [post]
+func (s *SalesDetailsApi) GetDeliveryList(c *gin.Context) {
+	var params GetWarehouseProductInfoReq
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	if params.SaleDetailID == 0 {
+		ctx.FailWithMsg(ecode.ParamsErr, "鍙傛暟缂哄け")
+		return
+	}
+
+	salesDetails, err := salesDetailsService.GetSalesDetails(params.SaleDetailID)
+	if err == gorm.ErrRecordNotFound || salesDetails.Number != params.SaleDetailNumber {
+		ctx.FailWithMsg(ecode.ParamsErr, "閿�鍞鍗曚笉瀛樺湪")
+		return
+	}
+
+	productMap := model.ProductMap(salesDetails.Products)
+	client := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
+	grpcResp, err := client.GetOrderInputAndOutputInfo(ctx.GetCtx(), &product_inventory.GetOrderInputAndOutputInfoRequest{
+		Number: params.SaleDetailNumber,
+	})
+	if err != nil {
+		if strings.Contains(err.Error(), "record not found") {
+			ctx.Ok()
+			return
+		}
+		logx.Errorf("GetOrderInputAndOutputInfo err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒")
+		return
+	}
+
+	grpcOutputList := grpcResp.OutputList
+	outputList := make([]*response.OutputInfo, 0, len(grpcOutputList))
+	for _, v := range grpcOutputList {
+		outputInfo := &response.OutputInfo{
+			Number:      v.Number,
+			Name:        v.Name,
+			OrderAmount: productMap[v.Number].Amount.String(),
+			Unit:        v.Unit,
+			Invoice:     v.Invoice,
+			Carrier:     v.Carrier,
+			Waybill:     v.Waybill,
+			SalePrice:   v.SalePrice,
+			Valorem:     v.Valorem,
+			Warehouse:   v.Warehouse,
+			Amount:      v.Amount,
+			Status:      int(v.Status),
+			Specs:       productMap[v.Number].Specs,
+			CreateTime:  v.CreateTime,
+		}
+		outputList = append(outputList, outputInfo)
+	}
+	ctx.OkWithDetailed(outputList)
+}
+
 // GetApsProjectList
 //
 // @Tags		SalesDetails

--
Gitblit v1.8.0