From a540cf74963886f5cb28a67b274b803fb605ec2e Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期日, 04 二月 2024 09:42:16 +0800
Subject: [PATCH] 更改来源为crm

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

diff --git a/api/v1/salesDetails.go b/api/v1/salesDetails.go
index 1a1e37d..bae4856 100644
--- a/api/v1/salesDetails.go
+++ b/api/v1/salesDetails.go
@@ -3,13 +3,20 @@
 import (
 	"aps_crm/constvar"
 	"aps_crm/model"
+	"aps_crm/model/grpc_init"
 	"aps_crm/model/request"
 	"aps_crm/model/response"
 	"aps_crm/pkg/contextx"
 	"aps_crm/pkg/ecode"
+	"aps_crm/pkg/logx"
+	"aps_crm/pkg/structx"
+	"aps_crm/proto/crm_aps"
+	"aps_crm/proto/product_inventory"
 	"aps_crm/utils"
 	"github.com/gin-gonic/gin"
+	"github.com/shopspring/decimal"
 	"strconv"
+	"strings"
 )
 
 type SalesDetailsApi struct{}
@@ -160,6 +167,13 @@
 	salesDetailsModel.CodeStandID = salesDetails.CodeStandID
 	salesDetailsModel.DeliverType = salesDetails.DeliverType
 	salesDetailsModel.QuotationId = salesDetails.QuotationId
+	salesDetailsModel.Status = salesDetails.Status
+	if salesDetails.Source == "" {
+		salesDetailsModel.Source = "CRM鑷缓"
+	} else {
+		salesDetailsModel.Source = salesDetails.Source
+	}
+	salesDetailsModel.ProjectId = salesDetails.ProjectId
 
 	return ecode.OK, salesDetailsModel
 }
@@ -182,7 +196,7 @@
 	var memberIds []int
 	userInfo := utils.GetUserInfo(c)
 	if userInfo.UserType == constvar.UserTypeSub {
-		memberIds = []int{userInfo.CrmUserId}
+		memberIds = userInfo.SubUserIds
 	}
 
 	salesDetailss, total, errCode := salesDetailsService.GetSalesDetailsList(params, memberIds)
@@ -196,3 +210,178 @@
 		Count: int(total),
 	})
 }
+
+// UpdateStatus
+//
+//	@Tags		SalesDetails
+//	@Summary	鏇存柊閿�鍞槑缁嗙姸鎬�
+//	@Produce	application/json
+//	@Param		object	body		request.UpdateSalesDetailsStatus	true	"鏌ヨ鍙傛暟"
+//	@Success	200		{object}	contextx.Response{}
+//	@Router		/api/salesDetails/updateStatus [post]
+func (s *SalesDetailsApi) UpdateStatus(c *gin.Context) {
+	var params request.UpdateSalesDetailsStatus
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+
+	m := make(map[string]interface{})
+	m["status"] = params.Status
+	err := model.NewSalesDetailsSearch().SetId(params.Id).UpdateByMap(m)
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "鏇存柊澶辫触")
+		return
+	}
+
+	ctx.Ok()
+}
+
+// GetProductInventoryInfo
+//
+// @Tags		SalesDetails
+// @Summary	鑾峰彇浜у搧搴撳瓨淇℃伅
+// @Produce	application/json
+// @Param		number	path		string	true	"鏄庣粏缂栫爜"
+// @Success	200	{object}	response.ListResponse
+//
+//	@Router		/api/salesDetails/getProductInventoryInfo/{number} [get]
+func (s *SalesDetailsApi) GetProductInventoryInfo(c *gin.Context) {
+	ctx, ok := contextx.NewContext(c, nil)
+	if !ok {
+		return
+	}
+	number := c.Param("number")
+	client := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
+	info, err := client.GetInventoryProductInfo(ctx.GetCtx(), &product_inventory.GetInventoryProductInfoRequest{Number: number})
+	if err != nil {
+		if strings.Contains(err.Error(), "record not found") {
+			ctx.Ok()
+			return
+		}
+		logx.Errorf("GetProductInfo err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒")
+		return
+	}
+	var list []response.ProductInfo
+	err = structx.AssignTo(info.ProductList, &list)
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "杞崲閿欒")
+		return
+	}
+	ctx.OkWithDetailed(list)
+}
+
+// GetApsProjectList
+//
+// @Tags		SalesDetails
+// @Summary	    鑾峰彇aps椤圭洰鍒楄〃
+// @Produce	application/json
+// @Success	200	{object}	response.Response
+//
+//	@Router		/api/salesDetails/getApsProjectList [get]
+func (s *SalesDetailsApi) GetApsProjectList(c *gin.Context) {
+	ctx, ok := contextx.NewContext(c, nil)
+	if !ok {
+		return
+	}
+	client := crm_aps.NewCrmAndApsGrpcServiceClient(grpc_init.CrmApsGrpcServiceConn)
+	projectList, err := client.GetApsProjectList(c, &crm_aps.GetApsProjectListRequest{})
+	if err != nil {
+		logx.Errorf("grpc GetApsProjectList err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇aps椤圭洰鍒楄〃澶辫触")
+		return
+	}
+	ctx.OkWithDetailed(projectList.List)
+}
+
+// SendSalesDetailsToOtherSystem
+//
+// @Tags		SalesDetails
+// @Summary	  鎺ㄩ�侀攢鍞槑缁嗕俊鎭埌鍏朵粬绯荤粺
+// @Produce	application/json
+// @Param		object	body		request.SalesDetails	true	"鏌ヨ鍙傛暟"
+// @Success	200	{object}	response.ListResponse
+//
+//	@Router		/api/salesDetails/sendSalesDetailsToOtherSystem [post]
+func (s *SalesDetailsApi) SendSalesDetailsToOtherSystem(c *gin.Context) {
+	var params request.SalesDetails
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	clientName := ""
+	if params.ClientId > 0 {
+		first, err := model.NewClientSearch(nil).SetId(params.ClientId).First()
+		if err != nil {
+			ctx.FailWithMsg(ecode.UnknownErr, "瀹㈡埛淇℃伅鏌ヨ澶辫触")
+			return
+		}
+		clientName = first.Name
+	}
+	m := make(map[string]interface{})
+	m["status"] = params.Status
+	m["project_id"] = params.ProjectId
+	err := model.NewSalesDetailsSearch().SetNumber(params.Number).UpdateByMap(m)
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "鐘舵�佹洿鏂板け璐�")
+		return
+	}
+
+	//鎺ㄩ�佸埌wms
+	wmsProducts := make([]*product_inventory.InventoryProduct, 0)
+	for _, product := range params.Products {
+		var p product_inventory.InventoryProduct
+		p.Id = product.Number
+		p.Amount = product.Amount.String()
+		wmsProducts = append(wmsProducts, &p)
+	}
+	clientWms := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
+	_, err = clientWms.CreateOperation(ctx.GetCtx(), &product_inventory.CreateOperationRequest{
+		Number:      params.Number,
+		Addressee:   params.Addressee,
+		Address:     params.Address,
+		Phone:       params.Phone,
+		DeliverType: int32(params.DeliverType),
+		Source:      "CRM",
+		ClientId:    int64(params.ClientId),
+		ClientName:  clientName,
+		ProductList: wmsProducts,
+	})
+	if err != nil {
+		logx.Errorf("grpc CreateOperation err: %v", err.Error())
+	}
+
+	//鎺ㄩ�佸埌aps
+	ApsProducts := make([]*crm_aps.SalesDetailsProduct, 0)
+	var total decimal.Decimal
+	for _, product := range params.Products {
+		var sp crm_aps.SalesDetailsProduct
+		sp.ProductId = product.Number
+		sp.Amount = product.Amount.IntPart()
+		ApsProducts = append(ApsProducts, &sp)
+		total = total.Add(product.Amount)
+	}
+
+	clientAps := crm_aps.NewCrmAndApsGrpcServiceClient(grpc_init.CrmApsGrpcServiceConn)
+	_, err = clientAps.SendSalesDetailsToApsProject(c, &crm_aps.SendSalesDetailsToApsProjectRequest{
+		Number:       params.Number,
+		ClientName:   params.Client.Name,
+		MemberName:   params.Member.Username,
+		SignTime:     params.SignTime,
+		DeliveryDate: params.DeliveryDate,
+		Source:       params.Source,
+		ProductTotal: total.IntPart(),
+		ProjectId:    params.ProjectId,
+		Products:     ApsProducts,
+	})
+	if err != nil {
+		//鐘舵�佽繕鍘�
+		m["status"] = constvar.WaitConfirmed
+		_ = model.NewSalesDetailsSearch().SetNumber(params.Number).UpdateByMap(m)
+		logx.Errorf("grpc SendSalesDetailsToApsProject err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "鎺ㄩ�佸け璐�,璇锋鏌ュ弬鏁版槸鍚︽纭�")
+		return
+	}
+	ctx.Ok()
+}

--
Gitblit v1.8.0