| | |
| | | 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/opa" |
| | | "wms/pkg/logx" |
| | | "wms/pkg/structx" |
| | | "wms/proto/product_inventory" |
| | | "wms/proto/purchase_wms" |
| | | "wms/request" |
| | | ) |
| | | |
| | |
| | | } |
| | | |
| | | func CheckDetailsRepeat(details []*models.OperationDetails) bool { |
| | | detailIDs := []string{} |
| | | var detailIDs []string |
| | | var tempID string |
| | | for _, v := range details { |
| | | detailIDs = append(detailIDs, v.ProductId) |
| | |
| | | util.ResponseFormat(c, code.Success, "修改成功") |
| | | } |
| | | |
| | | // DeleteDevice |
| | | // Delete |
| | | // |
| | | // @Tags 入库/出库 |
| | | // @Summary 删除入库/出库信息 |
| | |
| | | util.ResponseFormat(c, code.Success, "删除成功") |
| | | } |
| | | |
| | | // DeleteDevice |
| | | // Finish |
| | | // |
| | | // @Tags 入库/出库 |
| | | // @Summary 更改记录状态 |
| | |
| | | } |
| | | |
| | | if operation.BaseOperationType == constvar.BaseOperationTypeIncoming { |
| | | operationInputs := []*models.Operation{} |
| | | var operationInputs []*models.Operation |
| | | for k, v := range listProdt { |
| | | value, ok := mapProdt[v.ID] |
| | | if !ok { |
| | |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | |
| | | if err == nil { |
| | | operationTransfer := &models.Operation{ |
| | | Number: operation.Number, |
| | |
| | | Comment: operation.Comment, |
| | | BaseOperationType: constvar.BaseOperationTypeInternal, |
| | | Details: []*models.OperationDetails{ |
| | | &models.OperationDetails{ |
| | | { |
| | | ProductId: v.ID, |
| | | Amount: value, |
| | | }, |
| | | }, |
| | | } |
| | | operationTransfer.Id = 0 |
| | | operationTransfer.FromLocationID = locationRule.AreaId |
| | | operationTransfer.ToLocationID = locationRule.LocationId |
| | | operationTransfer.Status = constvar.OperationStatus_Finish |
| | | operationTransfer.Details = []*models.OperationDetails{ |
| | | &models.OperationDetails{ |
| | | ProductId: v.ID, |
| | | Amount: value, |
| | | }, |
| | | } |
| | | operationInputs = append(operationInputs, operationTransfer) |
| | |
| | | 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 |
| | | } |
| | | } else { |
| | | //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段 |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(operation.ToLocationID). |
| | | First() |
| | | locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = operation.ToLocationID |
| | | locAmount.ProductId = v.ID |
| | | locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05") |
| | | locAmount.Amount = locAmount.Amount.Add(value) |
| | | if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil { |
| | | return res.Error |
| | | if err == nil { |
| | | operationTransfer := &models.Operation{ |
| | | Number: operation.Number, |
| | | SourceNumber: operation.SourceNumber, |
| | | OperationTypeId: 0, |
| | | OperationTypeName: operation.OperationTypeName, |
| | | Status: constvar.OperationStatus_Finish, |
| | | FromLocationID: locationRule.AreaId, |
| | | ToLocationID: locationRule.LocationId, |
| | | OperationDate: operation.OperationDate, |
| | | ContacterID: operation.ContacterID, |
| | | ContacterName: operation.ContacterName, |
| | | CompanyID: operation.CompanyID, |
| | | CompanyName: operation.CompanyName, |
| | | Comment: operation.Comment, |
| | | BaseOperationType: constvar.BaseOperationTypeInternal, |
| | | Details: []*models.OperationDetails{ |
| | | { |
| | | ProductId: v.ID, |
| | | Amount: value, |
| | | }, |
| | | }, |
| | | } |
| | | operationInputs = append(operationInputs, operationTransfer) |
| | | |
| | | //if err := tx.Create(&operationTransfer).Error; err != nil { |
| | | // return err |
| | | //} |
| | | |
| | | //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段 |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(locationRule.LocationId). |
| | | First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = locationRule.LocationId |
| | | 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 |
| | | } |
| | | } else { |
| | | //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段 |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(operation.ToLocationID). |
| | | First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = operation.ToLocationID |
| | | 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 |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | if err := tx.Create(&operationInputs).Error; err != nil { |
| | | return err |
| | | } |
| | | } |
| | | if operation.SourceNumber != "" { |
| | | go UpdatePurchaseStatus(operation.SourceNumber) |
| | | } |
| | | |
| | | } |
| | |
| | | return err |
| | | } |
| | | } |
| | | if operation.SourceNumber != "" { |
| | | go UpdateSalesDetailStatus(operation.SourceNumber) |
| | | } |
| | | } |
| | | |
| | | if operation.BaseOperationType == constvar.BaseOperationTypeInternal { |
| | | var operationInputs []*models.Operation |
| | | for _, v := range listProdt { |
| | | value, ok := mapProdt[v.ID] |
| | | if !ok { |
| | |
| | | return err |
| | | } |
| | | |
| | | toLocAmount, res := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(operation.ToLocationID). |
| | | FirstRes() |
| | | if res.Error != nil { |
| | | locationRule, err := models.NewLocationProductSearch().SetProductId(v.ID).SetAreaId(operation.ToLocationID).First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | toLocAmount.Amount = toLocAmount.Amount.Add(value) |
| | | if err := models.NewLocationProductAmountSearch().SetID(toLocAmount.Id).Update(toLocAmount); err != nil { |
| | | |
| | | if err == nil { |
| | | operationTransfer := &models.Operation{ |
| | | Number: operation.Number, |
| | | SourceNumber: operation.SourceNumber, |
| | | OperationTypeId: 0, |
| | | OperationTypeName: operation.OperationTypeName, |
| | | Status: constvar.OperationStatus_Finish, |
| | | FromLocationID: locationRule.AreaId, |
| | | ToLocationID: locationRule.LocationId, |
| | | OperationDate: operation.OperationDate, |
| | | ContacterID: operation.ContacterID, |
| | | ContacterName: operation.ContacterName, |
| | | CompanyID: operation.CompanyID, |
| | | CompanyName: operation.CompanyName, |
| | | Comment: operation.Comment, |
| | | BaseOperationType: constvar.BaseOperationTypeInternal, |
| | | Details: []*models.OperationDetails{ |
| | | { |
| | | ProductId: v.ID, |
| | | Amount: value, |
| | | }, |
| | | }, |
| | | } |
| | | operationInputs = append(operationInputs, operationTransfer) |
| | | |
| | | //if err := tx.Create(&operationTransfer).Error; err != nil { |
| | | // return err |
| | | //} |
| | | |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(locationRule.LocationId). |
| | | First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = locationRule.LocationId |
| | | 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 |
| | | } |
| | | } else { |
| | | locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | if err == nil { |
| | | operationTransfer := &models.Operation{ |
| | | Number: operation.Number, |
| | | SourceNumber: operation.SourceNumber, |
| | | OperationTypeId: 0, |
| | | OperationTypeName: operation.OperationTypeName, |
| | | Status: constvar.OperationStatus_Finish, |
| | | FromLocationID: locationRule.AreaId, |
| | | ToLocationID: locationRule.LocationId, |
| | | OperationDate: operation.OperationDate, |
| | | ContacterID: operation.ContacterID, |
| | | ContacterName: operation.ContacterName, |
| | | CompanyID: operation.CompanyID, |
| | | CompanyName: operation.CompanyName, |
| | | Comment: operation.Comment, |
| | | BaseOperationType: constvar.BaseOperationTypeInternal, |
| | | Details: []*models.OperationDetails{ |
| | | { |
| | | ProductId: v.ID, |
| | | Amount: value, |
| | | }, |
| | | }, |
| | | } |
| | | operationInputs = append(operationInputs, operationTransfer) |
| | | |
| | | //if err := tx.Create(&operationTransfer).Error; err != nil { |
| | | // return err |
| | | //} |
| | | |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(locationRule.LocationId). |
| | | First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = locationRule.LocationId |
| | | 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 |
| | | } |
| | | } else { |
| | | locAmount, err := models.NewLocationProductAmountSearch(). |
| | | SetProductId(v.ID). |
| | | SetLocationId(operation.ToLocationID). |
| | | First() |
| | | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| | | return err |
| | | } |
| | | locAmount.LocationId = operation.ToLocationID |
| | | locAmount.ProductId = v.ID |
| | | locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05") |
| | | locAmount.Amount = locAmount.Amount.Add(value) |
| | | if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil { |
| | | return res.Error |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if len(operationInputs) > 0 { |
| | | if err := tx.Create(&operationInputs).Error; err != nil { |
| | | return err |
| | | } |
| | | } |
| | |
| | | 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 |
| | |
| | | } |
| | | 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, "操作成功") |
| | | } |