From 115bd9b51f5d8eade4658f844de37516486c60e7 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期六, 18 十一月 2023 17:25:25 +0800
Subject: [PATCH] crm获取aps项目模块信息

---
 api/v1/salesDetails.go |  346 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 291 insertions(+), 55 deletions(-)

diff --git a/api/v1/salesDetails.go b/api/v1/salesDetails.go
index d7f8bec..6b71e25 100644
--- a/api/v1/salesDetails.go
+++ b/api/v1/salesDetails.go
@@ -1,12 +1,23 @@
 package v1
 
 import (
+	"aps_crm/conf"
+	"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"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/credentials/insecure"
 	"strconv"
 )
 
@@ -18,7 +29,7 @@
 //	@Summary	娣诲姞閿�鍞槑缁�
 //	@Produce	application/json
 //	@Param		object	body		request.AddSalesDetails	true	"鏌ヨ鍙傛暟"
-//	@Success	200		{object}	contextx.Response{}
+//	@Success	200		{object}	contextx.Response{data=request.AddSalesDetails}
 //	@Router		/api/salesDetails/add [post]
 func (s *SalesDetailsApi) Add(c *gin.Context) {
 	var params request.AddSalesDetails
@@ -33,13 +44,30 @@
 		return
 	}
 
+	count, err := model.NewSalesDetailsSearch().SetNumber(params.Number).Count()
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "缂栫爜楠岃瘉澶辫触")
+		return
+	}
+	if count > 0 {
+		ctx.FailWithMsg(ecode.UnknownErr, "缂栫爜宸插瓨鍦�")
+		return
+	}
+
+	if salesDetails.MemberId == 0 {
+		userInfo := utils.GetUserInfo(c)
+		if userInfo.UserType == constvar.UserTypeSub {
+			salesDetails.MemberId = userInfo.CrmUserId
+		}
+	}
+
 	errCode = salesDetailsService.AddSalesDetails(&salesDetails)
 	if errCode != ecode.OK {
 		ctx.Fail(errCode)
 		return
 	}
 
-	ctx.Ok()
+	ctx.OkWithDetailed(salesDetails)
 }
 
 // Delete
@@ -58,6 +86,29 @@
 
 	id, _ := strconv.Atoi(c.Param("id"))
 	errCode := salesDetailsService.DeleteSalesDetails(id)
+	if errCode != ecode.OK {
+		ctx.Fail(errCode)
+		return
+	}
+
+	ctx.Ok()
+}
+
+// BatchDelete
+// @Tags	SalesDetails 閿�鍞槑缁�
+// @Summary	鎵归噺鍒犻櫎閿�鍞槑缁�
+// @Produce	application/json
+// @Param	object	body request.CommonIds	true "鍙傛暟"
+// @Success	200	{object}	contextx.Response{}
+// @Router		/api/salesDetails/delete [delete]
+func (s *SalesDetailsApi) BatchDelete(c *gin.Context) {
+	var params request.CommonIds
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+
+	errCode := salesDetailsService.BatchDeleteSalesDetails(params.Ids)
 	if errCode != ecode.OK {
 		ctx.Fail(errCode)
 		return
@@ -97,65 +148,15 @@
 	ctx.Ok()
 }
 
-// List
-//
-//	@Tags		SalesDetails
-//	@Summary	鑾峰彇閿�鍞槑缁嗗垪琛�
-//	@Produce	application/json
-//	@Success	200	{object}	contextx.Response{data=response.SalesDetailsResponse}
-//	@Router		/api/salesDetails/list [get]
-func (s *SalesDetailsApi) List(c *gin.Context) {
-	ctx, ok := contextx.NewContext(c, nil)
-	if !ok {
-		return
-	}
-
-	list, errCode := salesDetailsService.GetSalesDetailsList()
-	if errCode != ecode.OK {
-		ctx.Fail(errCode)
-		return
-	}
-
-	ctx.OkWithDetailed(response.SalesDetailsResponse{
-		List: list,
-	})
-}
-
 func checkSalesDetailsParams(salesDetails request.SalesDetails) (errCode int, salesDetailsModel model.SalesDetails) {
-	//if salesDetails.ClientId == 0 {
-	//	return ecode.InvalidParams, salesDetailsModel
-	//}
-	//
-	//if salesDetails.Number == "" {
-	//	return ecode.InvalidParams, salesDetailsModel
-	//}
-	//
-	//if salesDetails.MemberId == 0 {
-	//	return ecode.InvalidParams, salesDetailsModel
-	//}
-	//
-	//if salesDetails.SignTime == "" {
-	//	return ecode.InvalidParams, salesDetailsModel
-	//}
-
-	t, err := checkTimeFormat(salesDetails.SignTime)
-	if err != nil {
-		return ecode.InvalidParams, salesDetailsModel
-	}
-
-	t, err = checkTimeFormat(salesDetails.DeliveryDate)
-	if err != nil {
-		return ecode.InvalidParams, salesDetailsModel
-	}
-
 	salesDetailsModel.ClientId = salesDetails.ClientId
 	salesDetailsModel.Number = salesDetails.Number
 	salesDetailsModel.SaleChanceId = salesDetails.SaleChanceId
 	salesDetailsModel.SaleType = salesDetails.SaleType
-	salesDetailsModel.SignTime = t
+	salesDetailsModel.SignTime = salesDetails.SignTime
 	salesDetailsModel.MemberId = salesDetails.MemberId
-	salesDetailsModel.DeliveryDate = t
-	salesDetailsModel.WechatOrderStatus = salesDetails.WechatOrderStatus
+	salesDetailsModel.DeliveryDate = salesDetails.DeliveryDate
+	salesDetailsModel.WechatOrderStatusId = salesDetails.WechatOrderStatusId
 	salesDetailsModel.Address = salesDetails.Address
 	salesDetailsModel.Phone = salesDetails.Phone
 	salesDetailsModel.Remark = salesDetails.Remark
@@ -165,6 +166,241 @@
 	salesDetailsModel.LogisticCompany = salesDetails.LogisticCompany
 	salesDetailsModel.LogisticNumber = salesDetails.LogisticNumber
 	salesDetailsModel.LogisticCost = salesDetails.LogisticCost
+	salesDetailsModel.CodeStandID = salesDetails.CodeStandID
+	salesDetailsModel.DeliverType = salesDetails.DeliverType
+	salesDetailsModel.QuotationId = salesDetails.QuotationId
+	salesDetailsModel.Status = salesDetails.Status
+	salesDetailsModel.Source = salesDetails.Source
 
 	return ecode.OK, salesDetailsModel
 }
+
+// List
+//
+//	@Tags		SalesDetails
+//	@Summary	閿�鍞槑缁嗗崟鍒楄〃
+//	@Produce	application/json
+//	@Param		object	body		request.GetSalesDetailsList	true	"鍙傛暟"
+//	@Success	200		{object}	contextx.Response{data=response.SalesDetailsResponse}
+//	@Router		/api/salesDetails/list [post]
+func (con *SalesDetailsApi) List(c *gin.Context) {
+	var params request.GetSalesDetailsList
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+
+	var memberIds []int
+	userInfo := utils.GetUserInfo(c)
+	if userInfo.UserType == constvar.UserTypeSub {
+		memberIds = userInfo.SubUserIds
+	}
+
+	salesDetailss, total, errCode := salesDetailsService.GetSalesDetailsList(params, memberIds)
+	if errCode != ecode.OK {
+		ctx.Fail(errCode)
+		return
+	}
+
+	ctx.OkWithDetailed(response.SalesDetailsResponse{
+		List:  salesDetailss,
+		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()
+}
+
+var (
+	ProductInventoryServiceConn *grpc.ClientConn
+)
+
+func InitProductInventoryServiceConn() {
+	var err error
+	ProductInventoryServiceConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.WMS, grpc.WithTransportCredentials(insecure.NewCredentials()))
+	if err != nil {
+		logx.Errorf("grpc dial product service error: %v", err.Error())
+		return
+	}
+}
+
+func CloseProductInventoryServiceConn() {
+	if ProductInventoryServiceConn != nil {
+		ProductInventoryServiceConn.Close()
+	}
+}
+
+// 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(ProductInventoryServiceConn)
+	info, err := client.GetInventoryProductInfo(ctx.GetCtx(), &product_inventory.GetInventoryProductInfoRequest{Number: number})
+	if err != nil {
+		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)
+}
+
+// CreateOperation
+//
+// @Tags		SalesDetails
+// @Summary	鍒涘缓浜у搧鍑哄簱淇℃伅
+// @Produce	application/json
+// @Param		object	body		request.SalesDetails	true	"鏌ヨ鍙傛暟"
+// @Success	200	{object}	response.ListResponse
+//
+//	@Router		/api/salesDetails/createOperation [post]
+func (s *SalesDetailsApi) CreateOperation(c *gin.Context) {
+	var params request.SalesDetails
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	m := make(map[string]interface{})
+	m["status"] = params.Status
+	err := model.NewSalesDetailsSearch().SetNumber(params.Number).UpdateByMap(m)
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "鐘舵�佹洿鏂板け璐�")
+		return
+	}
+
+	client := product_inventory.NewProductInventoryServiceClient(ProductInventoryServiceConn)
+	products := make([]*product_inventory.InventoryProduct, 0)
+	for _, product := range params.Products {
+		var p product_inventory.InventoryProduct
+		p.Id = product.Number
+		p.Amount = product.Amount.String()
+		products = append(products, &p)
+	}
+	_, err = client.CreateOperation(ctx.GetCtx(), &product_inventory.CreateOperationRequest{
+		Number:      params.Number,
+		Addressee:   params.Addressee,
+		Address:     params.Address,
+		Phone:       params.Phone,
+		DeliverType: int32(params.DeliverType),
+		ProductList: products,
+	})
+	if err != nil {
+		logx.Errorf("CreateOperation err: %v", err.Error())
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒")
+		return
+	}
+	ctx.Ok()
+}
+
+// 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 {
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒: "+err.Error())
+		return
+	}
+	ctx.OkWithDetailed(projectList.List)
+}
+
+// SendSalesDetailsToApsProject
+//
+// @Tags		SalesDetails
+// @Summary	  鎺ㄩ�侀攢鍞槑缁嗕俊鎭埌aps椤圭洰妯″潡
+// @Produce	application/json
+// @Param		object	body		request.SalesDetails	true	"鏌ヨ鍙傛暟"
+// @Success	200	{object}	response.ListResponse
+//
+//	@Router		/api/salesDetails/sendSalesDetailsToApsProject [post]
+func (s *SalesDetailsApi) SendSalesDetailsToApsProject(c *gin.Context) {
+	var params request.SalesDetails
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	m := make(map[string]interface{})
+	m["status"] = params.Status
+	err := model.NewSalesDetailsSearch().SetNumber(params.Number).UpdateByMap(m)
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "鐘舵�佹洿鏂板け璐�")
+		return
+	}
+
+	products := 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()
+		products = append(products, &sp)
+		total = total.Add(product.Amount)
+	}
+
+	client := crm_aps.NewCrmAndApsGrpcServiceClient(grpc_init.CrmApsGrpcServiceConn)
+	_, err = client.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:     products,
+	})
+	if err != nil {
+		ctx.FailWithMsg(ecode.UnknownErr, "grpc璋冪敤閿欒: "+err.Error())
+		return
+	}
+	ctx.Ok()
+}

--
Gitblit v1.8.0