jiangshuai
2023-11-17 bb5d729a62fa5e6875114018562458ea29ea1649
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"
@@ -25,6 +26,8 @@
   "wms/proto/product_inventory"
   "wms/proto/purchase_wms"
   "wms/request"
   "wms/utils/http"
   "wms/utils/upload"
)
type OperationController struct {
@@ -827,3 +830,106 @@
   }
   util.ResponseFormat(c, code.Success, "操作成功")
}
// inputSelfmade
//
//   @Tags      入库/出库
//   @Summary   打印-自制-入库
//   @Produce   application/json
//   @Param      id   path      int         true   "id"
//   @Success   200   {object}   util.Response   "成功"
//   @Router      /api-wms/v1/operation/inputSelfmade/{id} [put]
func (slf OperationController) InputSelfmade(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
   }
   readerCloser, err := http.HttpGetWithReadCloser(conf.FileTemplateConf.InputSelfmadeAddr)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "获取模版失败:"+err.Error())
      return
   }
   f, err := excelize.OpenReader(readerCloser)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "读取excel模版失败:"+err.Error())
      return
   }
   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 {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   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 {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), "xlsx", buf.Bytes())
   if err != nil {
      logx.Errorf("file upload err: %v", err)
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   util.ResponseFormat(c, code.Success, fileUrl)
}