zhangqian
2023-11-20 426ca2366cc521d88b7483f0cefd811f996e8a22
controllers/operation.go
@@ -1,16 +1,21 @@
package controllers
import (
   "context"
   "encoding/json"
   "errors"
   "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"
   "os"
   "sort"
   "strconv"
   "time"
   "wms/conf"
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
@@ -18,7 +23,11 @@
   "wms/opa"
   "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 {
@@ -407,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
               }
@@ -456,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
                  }
@@ -472,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
                  }
@@ -482,6 +494,9 @@
            if err := tx.Create(&operationInputs).Error; err != nil {
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdatePurchaseStatus(operation.SourceNumber)
         }
      }
@@ -525,6 +540,9 @@
            if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdateSalesDetailStatus(operation.SourceNumber)
         }
      }
@@ -599,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
               }
@@ -647,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
                  }
@@ -679,7 +699,55 @@
      util.ResponseFormat(c, code.RequestError, err.Error())
      return
   }
   util.ResponseFormat(c, code.Success, "操作成功")
}
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
   }
}
func CloseProductInventoryServiceConn() {
   if ProductInventoryServiceConn != nil {
      ProductInventoryServiceConn.Close()
   }
   if PurchaseServiceConn != nil {
      PurchaseServiceConn.Close()
   }
}
func UpdateSalesDetailStatus(number string) {
   client := product_inventory.NewProductInventoryServiceClient(ProductInventoryServiceConn)
   _, err := client.UpdateSalesDetailStatus(context.Background(), &product_inventory.UpdateSalesDetailStatusRequest{
      Number:            number,
      SalesDetailStatus: "已出库",
   })
   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)
   }
}
// ListTransfer
@@ -727,3 +795,141 @@
   }
   util.ResponseFormat(c, code.Success, companies)
}
// Cancel
//
//   @Tags      入库/出库
//   @Summary   取消
//   @Produce   application/json
//   @Param      id   path      int         true   "id"
//   @Success   200   {object}   util.Response   "成功"
//   @Router      /api-wms/v1/operation/cancel/{id} [put]
func (slf OperationController) Cancel(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 operation.Status != constvar.OperationStatus_Ready {
      util.ResponseFormat(c, code.RequestError, "该出入库信息无法取消")
      return
   }
   operation.Status = constvar.OperationStatus_Cancel
   if err := models.NewOperationSearch().SetID(operation.Id).Save(operation); err != nil {
      util.ResponseFormat(c, code.SaveFail, err.Error())
      return
   }
   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)
}