jiangshuai
2023-11-16 a0125ef3d6ce35be7b6dc4919c4192dba4a7329a
controllers/operation.go
@@ -1,20 +1,29 @@
package controllers
import (
   "context"
   "encoding/json"
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "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"
   "wms/models"
   "wms/opa"
   "wms/pkg/logx"
   "wms/pkg/structx"
   "wms/proto/product_inventory"
   "wms/proto/purchase_wms"
   "wms/request"
)
@@ -254,7 +263,7 @@
   util.ResponseFormat(c, code.Success, "修改成功")
}
// DeleteDevice
// Delete
//
//   @Tags      入库/出库
//   @Summary   删除入库/出库信息
@@ -296,7 +305,7 @@
   util.ResponseFormat(c, code.Success, "删除成功")
}
// DeleteDevice
// Finish
//
//   @Tags      入库/出库
//   @Summary   更改记录状态
@@ -404,6 +413,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
               }
@@ -453,6 +463,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
                  }
@@ -469,6 +480,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
                  }
@@ -480,6 +492,9 @@
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdatePurchaseStatus(operation.SourceNumber)
         }
      }
@@ -489,8 +504,20 @@
            if !ok {
               return errors.New("产品种类异常")
            }
            if v.Amount.LessThan(value) {
               return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
            //todo 演示测试数据
            data, err := os.ReadFile("conf/input.json")
            if err != nil {
               return errors.New("文件读取失败")
            }
            m := make(map[string]interface{})
            err = json.Unmarshal(data, &m)
            if err != nil {
               return errors.New("格式转换失败")
            }
            if opa.OpaCheck(c, m, "operation") {
               if v.Amount.LessThan(value) {
                  return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
               }
            }
            listProdt[k].Amount = listProdt[k].Amount.Sub(value)
            if err := tx.Save(listProdt[k]).Error; err != nil {
@@ -510,6 +537,9 @@
            if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
               return err
            }
         }
         if operation.SourceNumber != "" {
            go UpdateSalesDetailStatus(operation.SourceNumber)
         }
      }
@@ -584,6 +614,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
               }
@@ -632,6 +663,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
                  }
@@ -664,10 +696,58 @@
      util.ResponseFormat(c, code.RequestError, err.Error())
      return
   }
   util.ResponseFormat(c, code.Success, "操作成功")
}
// listTransfer
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
// @Tags      入库/出库
// @Summary   库存调拨列表
// @Produce   application/json
@@ -697,3 +777,53 @@
   }
   util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}
// GetLogisticCompanyList
// @Tags      入库/出库
// @Summary   获取物流公司列表
// @Produce   application/json
// @Success   200 {object} util.ResponseList{data=[]models.LogisticCompany} "成功"
// @Router    /api-wms/v1/operation/getLogisticCompanyList [get]
func (slf OperationController) GetLogisticCompanyList(c *gin.Context) {
   companies, err := models.NewLogisticCompanySearch().FindNotTotal()
   if err != nil {
      util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
      return
   }
   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, "操作成功")
}