jiangshuai
2023-11-22 ed81fc8d860adb66676d4990e4ce0ff56cff79a6
controllers/operation.go
@@ -7,6 +7,7 @@
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "github.com/xuri/excelize/v2"
   "google.golang.org/grpc"
   "google.golang.org/grpc/credentials/insecure"
   "gorm.io/gorm"
@@ -23,7 +24,10 @@
   "wms/pkg/logx"
   "wms/pkg/structx"
   "wms/proto/product_inventory"
   "wms/proto/purchase_wms"
   "wms/request"
   "wms/utils/http"
   "wms/utils/upload"
)
type OperationController struct {
@@ -412,6 +416,7 @@
               locAmount.ProductId = v.ID
               locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
               locAmount.Amount = locAmount.Amount.Add(value)
               locAmount.ProductCategoryID = v.CategoryId
               if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                  return res.Error
               }
@@ -461,6 +466,7 @@
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  locAmount.ProductCategoryID = v.CategoryId
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
@@ -477,6 +483,7 @@
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  locAmount.ProductCategoryID = v.CategoryId
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
@@ -487,6 +494,9 @@
            if err := tx.Create(&operationInputs).Error; err != nil {
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdatePurchaseStatus(operation.SourceNumber)
         }
      }
@@ -530,6 +540,9 @@
            if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdateSalesDetailStatus(operation.SourceNumber)
         }
      }
@@ -604,6 +617,7 @@
               locAmount.ProductId = v.ID
               locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
               locAmount.Amount = locAmount.Amount.Add(value)
               locAmount.ProductCategoryID = v.CategoryId
               if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                  return res.Error
               }
@@ -652,6 +666,7 @@
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  locAmount.ProductCategoryID = v.CategoryId
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
@@ -684,17 +699,23 @@
      util.ResponseFormat(c, code.RequestError, err.Error())
      return
   }
   if operation.SourceNumber != "" {
      go UpdateSalesDetailStatus(operation.SourceNumber)
   }
   util.ResponseFormat(c, code.Success, "操作成功")
}
var ProductInventoryServiceConn *grpc.ClientConn
var (
   ProductInventoryServiceConn *grpc.ClientConn
   PurchaseServiceConn         *grpc.ClientConn
)
func InitProductInventoryServiceConn() {
   var err error
   ProductInventoryServiceConn, err = grpc.Dial(conf.GrpcServerConf.CrmAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
   if err != nil {
      logx.Errorf("grpc dial product service error: %v", err.Error())
      return
   }
   PurchaseServiceConn, err = grpc.Dial(conf.GrpcServerConf.SrmAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
   if err != nil {
      logx.Errorf("grpc dial product service error: %v", err.Error())
      return
@@ -704,6 +725,9 @@
func CloseProductInventoryServiceConn() {
   if ProductInventoryServiceConn != nil {
      ProductInventoryServiceConn.Close()
   }
   if PurchaseServiceConn != nil {
      PurchaseServiceConn.Close()
   }
}
@@ -715,6 +739,14 @@
   })
   if err != nil {
      logx.Errorf("grpc dial UpdateSalesDetailStatus service error: %v", err)
   }
}
func UpdatePurchaseStatus(number string) {
   client := purchase_wms.NewPurchaseServiceClient(PurchaseServiceConn)
   _, err := client.UpdatePurchaseStatus(context.Background(), &purchase_wms.UpdatePurchaseStatusRequest{Number: number})
   if err != nil {
      logx.Errorf("grpc dial UpdatePurchaseStatus service error: %v", err)
   }
}
@@ -798,3 +830,203 @@
   }
   util.ResponseFormat(c, code.Success, "操作成功")
}
// outputOperation
//
//   @Tags      入库/出库
//   @Summary   打印
//   @Produce   application/json
//   @Param      id   path      int         true   "id"
//   @Success   200   {object}   util.Response   "成功"
//   @Router      /api-wms/v1/operation/outputOperation/{id} [put]
func (slf OperationController) OutputOperation(c *gin.Context) {
   id, err := strconv.Atoi(c.Param("id"))
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "错误的id值")
      return
   }
   if id == 0 {
      util.ResponseFormat(c, code.RequestParamError, "id为0")
      return
   }
   operation, err := models.NewOperationSearch().SetPreload(true).SetID(id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
      return
   }
   if len(operation.Details) == 0 {
      util.ResponseFormat(c, code.RequestParamError, "没有相关产品明细的信息")
      return
   }
   var fileUrl string
   if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
      fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Selfmade, operation)
      if err != nil {
         util.ResponseFormat(c, code.RequestParamError, err.Error())
         return
      }
   } else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
      fileUrl, err = ExportOutputOperation(constvar.FileTemplateCategory_Output, operation)
      if err != nil {
         util.ResponseFormat(c, code.RequestParamError, err.Error())
         return
      }
   } else {
      util.ResponseFormat(c, code.RequestParamError, "该记录不支持打印")
      return
   }
   util.ResponseFormat(c, code.Success, fileUrl)
}
func ExportInputSelfmade(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
   template, err := models.NewFileTemplateAttachmentSearch().SetPreload(true).SetCategory(category).First()
   if err != nil {
      return "", errors.New("获取模版记录失败:" + err.Error())
   }
   readerCloser, err := http.HttpGetWithReadCloser(template.Attachment.FileUrl)
   if err != nil {
      return "", errors.New("获取模版失败:" + err.Error())
   }
   f, err := excelize.OpenReader(readerCloser)
   if err != nil {
      return "", errors.New("读取excel模版失败:" + err.Error())
   }
   readerCloser.Close()
   style, _ := f.NewStyle(&excelize.Style{
      Border: []excelize.Border{
         {Type: "left", Color: "#000000", Style: 2},
         {Type: "top", Color: "#000000", Style: 2},
         {Type: "bottom", Color: "#000000", Style: 2},
         {Type: "right", Color: "#000000", Style: 2},
      },
      Alignment: &excelize.Alignment{
         Horizontal: "center",
         Vertical:   "center",
      },
   })
   styleLeft, _ := f.NewStyle(&excelize.Style{
      Alignment: &excelize.Alignment{
         Horizontal: "left",
         Vertical:   "center",
      },
   })
   f.SetCellValue("Sheet1", "B3", operation.CompanyName)
   f.SetCellValue("Sheet1", "E3", operation.OperationDate)
   f.SetCellValue("Sheet1", "H3", operation.Number)
   rowIndex := 5
   totalAmount := decimal.NewFromInt(0)
   for _, v := range operation.Details {
      f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), v.ProductId)
      f.SetCellValue("Sheet1", "B"+strconv.Itoa(rowIndex), v.Product.Name)
      f.SetCellValue("Sheet1", "C"+strconv.Itoa(rowIndex), v.Product.Specs)
      f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), v.Product.Unit)
      f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), v.Amount.String())
      f.SetCellValue("Sheet1", "H"+strconv.Itoa(rowIndex), operation.ToLocation.Name)
      f.SetCellValue("Sheet1", "I"+strconv.Itoa(rowIndex), operation.Comment)
      rowIndex++
      totalAmount = totalAmount.Add(v.Amount)
   }
   if err := f.MergeCell("Sheet1", "A"+strconv.Itoa(rowIndex), "C"+strconv.Itoa(rowIndex)); err != nil {
      return "", err
   }
   f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), "合计")
   f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), totalAmount.String())
   f.SetCellStyle("Sheet1", "A4", "I"+strconv.Itoa(rowIndex), style)
   rowIndex++
   f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), "审核:")
   f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), "检验:")
   f.SetCellValue("Sheet1", "G"+strconv.Itoa(rowIndex), "保管:")
   f.SetCellStyle("Sheet1", "A"+strconv.Itoa(rowIndex), "G"+strconv.Itoa(rowIndex), styleLeft)
   buf, err := f.WriteToBuffer()
   if err != nil {
      return "", err
   }
   fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), "xlsx", buf.Bytes())
   if err != nil {
      logx.Errorf("file upload err: %v", err)
      return "", err
   }
   return fileUrl, nil
}
func ExportOutputOperation(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
   template, err := models.NewFileTemplateAttachmentSearch().SetPreload(true).SetCategory(category).First()
   if err != nil {
      return "", errors.New("获取模版记录失败:" + err.Error())
   }
   readerCloser, err := http.HttpGetWithReadCloser(template.Attachment.FileUrl)
   if err != nil {
      return "", errors.New("获取模版失败:" + err.Error())
   }
   f, err := excelize.OpenReader(readerCloser)
   if err != nil {
      return "", errors.New("读取excel模版失败:" + err.Error())
   }
   readerCloser.Close()
   style, _ := f.NewStyle(&excelize.Style{
      Border: []excelize.Border{
         {Type: "left", Color: "#000000", Style: 1},
         {Type: "top", Color: "#000000", Style: 1},
         {Type: "bottom", Color: "#000000", Style: 1},
         {Type: "right", Color: "#000000", Style: 1},
      },
      Alignment: &excelize.Alignment{
         Horizontal: "center",
         Vertical:   "center",
      },
   })
   f.SetCellValue("Sheet1", "C2", operation.FromLocation.Name)
   f.SetCellValue("Sheet1", "H2", operation.WaybillNumber)
   f.SetCellValue("Sheet1", "H3", operation.SourceNumber)
   f.SetCellValue("Sheet1", "K3", operation.Number)
   f.SetCellValue("Sheet1", "C4", operation.CompanyName)
   rowIndex := 8
   totalAmount := decimal.NewFromInt(0)
   totalWeight := decimal.NewFromInt(0)
   addRows := len(operation.Details) - 1
   if addRows > 0 {
      f.InsertRows("Sheet1", 9, addRows)
      f.SetCellStyle("Sheet1", "A9", "K"+strconv.Itoa(rowIndex+addRows), style)
   }
   for k, v := range operation.Details {
      productTotalWeight := v.Amount.Mul(v.Product.Weight).Div(decimal.NewFromInt(1000))
      f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), k+1)
      f.SetCellValue("Sheet1", "B"+strconv.Itoa(rowIndex), v.Product.Name)
      f.SetCellValue("Sheet1", "H"+strconv.Itoa(rowIndex), v.Amount)
      f.SetCellValue("Sheet1", "I"+strconv.Itoa(rowIndex), v.Product.Weight)
      f.SetCellValue("Sheet1", "J"+strconv.Itoa(rowIndex), productTotalWeight)
      rowIndex++
      totalAmount = totalAmount.Add(v.Amount)
      totalWeight = totalWeight.Add(productTotalWeight)
   }
   buf, err := f.WriteToBuffer()
   if err != nil {
      return "", err
   }
   fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), "xlsx", buf.Bytes())
   if err != nil {
      logx.Errorf("file upload err: %v", err)
      return "", err
   }
   return fileUrl, nil
}