From a0125ef3d6ce35be7b6dc4919c4192dba4a7329a Mon Sep 17 00:00:00 2001
From: jiangshuai <291802688@qq.com>
Date: 星期四, 16 十一月 2023 14:33:51 +0800
Subject: [PATCH] 报废数量bug

---
 controllers/operation.go |  121 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 5bd16ef..1e56fd1 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -1,16 +1,20 @@
 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"
@@ -18,6 +22,8 @@
 	"wms/opa"
 	"wms/pkg/logx"
 	"wms/pkg/structx"
+	"wms/proto/product_inventory"
+	"wms/proto/purchase_wms"
 	"wms/request"
 )
 
@@ -257,7 +263,7 @@
 	util.ResponseFormat(c, code.Success, "淇敼鎴愬姛")
 }
 
-// DeleteDevice
+// Delete
 //
 //	@Tags		鍏ュ簱/鍑哄簱
 //	@Summary	鍒犻櫎鍏ュ簱/鍑哄簱淇℃伅
@@ -299,7 +305,7 @@
 	util.ResponseFormat(c, code.Success, "鍒犻櫎鎴愬姛")
 }
 
-// DeleteDevice
+// Finish
 //
 //	@Tags		鍏ュ簱/鍑哄簱
 //	@Summary	鏇存敼璁板綍鐘舵��
@@ -407,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
 					}
@@ -456,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
 						}
@@ -472,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
 						}
@@ -482,6 +491,9 @@
 				if err := tx.Create(&operationInputs).Error; err != nil {
 					return err
 				}
+			}
+			if operation.SourceNumber != "" {
+				go UpdatePurchaseStatus(operation.SourceNumber)
 			}
 
 		}
@@ -525,6 +537,9 @@
 				if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
 					return err
 				}
+			}
+			if operation.SourceNumber != "" {
+				go UpdateSalesDetailStatus(operation.SourceNumber)
 			}
 		}
 
@@ -599,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
 					}
@@ -647,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
 						}
@@ -679,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
@@ -712,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, "閿欒鐨刬d鍊�")
+		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, "鎿嶄綔鎴愬姛")
+}

--
Gitblit v1.8.0