liujiandao
2023-12-02 002539c8dc7eae6e64da1608245be01a889c061c
api/v1/salesDetails.go
@@ -4,14 +4,18 @@
   "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"
@@ -166,7 +170,12 @@
   salesDetailsModel.DeliverType = salesDetails.DeliverType
   salesDetailsModel.QuotationId = salesDetails.QuotationId
   salesDetailsModel.Status = salesDetails.Status
   salesDetailsModel.Source = salesDetails.Source
   if salesDetails.Source == "" {
      salesDetailsModel.Source = "CRM自建"
   } else {
      salesDetailsModel.Source = salesDetails.Source
   }
   salesDetailsModel.ProjectId = salesDetails.ProjectId
   return ecode.OK, salesDetailsModel
}
@@ -189,7 +198,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)
@@ -209,18 +218,18 @@
//   @Tags      SalesDetails
//   @Summary   更新销售明细状态
//   @Produce   application/json
//   @Param      object   body      request.UpdateSalesDetails   true   "查询参数"
//   @Param      object   body      request.UpdateSalesDetailsStatus   true   "查询参数"
//   @Success   200      {object}   contextx.Response{}
//   @Router      /api/salesDetails/update [post]
//   @Router      /api/salesDetails/updateStatus [post]
func (s *SalesDetailsApi) UpdateStatus(c *gin.Context) {
   var params request.UpdateSalesDetails
   var params request.UpdateSalesDetailsStatus
   ctx, ok := contextx.NewContext(c, &params)
   if !ok {
      return
   }
   m := make(map[string]interface{})
   m["status"] = params.SalesDetails.Status
   m["status"] = params.Status
   err := model.NewSalesDetailsSearch().SetId(params.Id).UpdateByMap(m)
   if err != nil {
      ctx.FailWithMsg(ecode.UnknownErr, "更新失败")
@@ -271,44 +280,111 @@
      ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误")
      return
   }
   ctx.OkWithDetailed(info.ProductList)
   var list []response.ProductInfo
   err = structx.AssignTo(info.ProductList, &list)
   if err != nil {
      ctx.FailWithMsg(ecode.UnknownErr, "转换错误")
      return
   }
   ctx.OkWithDetailed(list)
}
// CreateOperation
// GetApsProjectList
//
// @Tags      SalesDetails
// @Summary   创建产品出库信息
// @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)
}
// SendSalesDetailsToOtherSystem
//
// @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) {
//   @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
   }
   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
   }
   client := product_inventory.NewProductInventoryServiceClient(ProductInventoryServiceConn)
   products := make([]*product_inventory.InventoryProduct, 0)
   //推送到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()
      products = append(products, &p)
      wmsProducts = append(wmsProducts, &p)
   }
   _, err := client.CreateOperation(ctx.GetCtx(), &product_inventory.CreateOperationRequest{
   clientWms := product_inventory.NewProductInventoryServiceClient(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),
      ProductList: products,
      Source:      "CRM",
      ProductList: wmsProducts,
   })
   if err != nil {
      logx.Errorf("CreateOperation err: %v", err.Error())
      ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误")
   }
   //推送到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)
      ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误: "+err.Error())
      return
   }
   ctx.Ok()