liujiandao
2023-11-18 115bd9b51f5d8eade4658f844de37516486c60e7
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"
)
@@ -33,18 +44,27 @@
      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
   }
   if params.CodeRule.Method == 1 {
      autoCode := model.GetAutoCode(salesDetails.Id, &params.CodeRule)
      m := map[string]interface{}{
         "number": autoCode,
      }
      _ = model.NewSalesDetailsSearch().SetId(salesDetails.Id).UpdateByMap(m)
   }
   ctx.OkWithDetailed(salesDetails)
@@ -147,6 +167,10 @@
   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
}
@@ -166,7 +190,13 @@
      return
   }
   salesDetailss, total, errCode := salesDetailsService.GetSalesDetailsList(params)
   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
@@ -177,3 +207,200 @@
      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()
}