From 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 01 七月 2024 22:32:34 +0800
Subject: [PATCH] 月度统计出入库按类型汇总报表定时任务和手动跑任务接口

---
 models/operation_details.go |   90 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 82 insertions(+), 8 deletions(-)

diff --git a/models/operation_details.go b/models/operation_details.go
index 32e8f9a..8269f76 100644
--- a/models/operation_details.go
+++ b/models/operation_details.go
@@ -12,22 +12,39 @@
 	OperationDetails struct {
 		WmsModel
 		Id          int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		OperationID int    `json:"operationId" gorm:"type:int;not null;comment:鎿嶄綔璁板綍id"`      //鎿嶄綔id
-		ProductId   string `json:"productId" gorm:"type:varchar(191);not null;comment:浜у搧id"` //浜у搧id
+		OperationID int    `json:"operationId" gorm:"index;type:int;not null;comment:鎿嶄綔璁板綍id"` //鎿嶄綔id
+		ProductId   string `json:"productId" gorm:"type:varchar(191);not null;comment:浜у搧id"`  //浜у搧id
 		//ProductName string          `json:"productName" gorm:"type:varchar(255);not null;comment:浜у搧鍚嶇О"` //浜у搧鍚嶇О
 		Amount decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:鏁伴噺"` //鏁伴噺
 		//Unit        string          `json:"unit" gorm:"type:varchar(31);comment:鍗曚綅"`                    //鍗曚綅
 		Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"`
+
+		FromLocationID   int             `json:"fromLocationId"   gorm:"type:int;not null;comment:婧愪綅缃甶d"`         //婧愪綅缃甶d
+		FromLocation     Location        `json:"fromLocation"     gorm:"foreignKey:FromLocationID;references:Id"` //婧愪綅缃�
+		ToLocationID     int             `json:"toLocationId"    gorm:"type:int;not null;comment:鐩爣浣嶇疆id"`         //鐩爣浣嶇疆id
+		ToLocation       Location        `json:"toLocation"      gorm:"foreignKey:ToLocationID;references:Id"`    //鐩爣浣嶇疆
+		TotalGrossWeight decimal.Decimal `json:"totalGrossWeight" gorm:"type:decimal(20,3);comment:鎬绘瘺閲�"`
+		TotalNetWeight   decimal.Decimal `json:"totalNetWeight" gorm:"type:decimal(20,3);comment:鎬诲噣閲�"`
+		AuxiliaryAmount  decimal.Decimal `json:"auxiliaryAmount" gorm:"type:decimal(20,3);comment:杈呭姪鏁伴噺"`
+		AuxiliaryUnit    string          `json:"auxiliaryUnit" gorm:"type:varchar(191);comment:杈呭姪鍗曚綅"`
+		Remark           string          `gorm:"type:varchar(1024);comment:澶囨敞" json:"remark"`
+		IsInternalOutput bool            `json:"isInternalOutput"` //鏄惁璋冩嫧浜х敓鐨勫嚭搴�
+		DealerType       string          `json:"dealerType"`       //鍑哄叆搴撶被鍨�
+
+		Cost      decimal.Decimal `json:"cost" `      //鎴愭湰鍗曚环
+		SalePrice decimal.Decimal `json:"salePrice" ` //閿�鍞崟浠�
 	}
 
 	OperationDetailsSearch struct {
 		OperationDetails
-		Order    string
-		PageNum  int
-		PageSize int
-		Keyword  string
-		Orm      *gorm.DB
-		Preload  bool
+		Order        string
+		PageNum      int
+		PageSize     int
+		Keyword      string
+		Orm          *gorm.DB
+		Preload      bool
+		OperationIDs []int
+		Fields       string
 	}
 )
 
@@ -74,6 +91,21 @@
 	return slf
 }
 
+func (slf *OperationDetailsSearch) SetOperationIds(operationIds []int) *OperationDetailsSearch {
+	slf.OperationIDs = operationIds
+	return slf
+}
+
+func (slf *OperationDetailsSearch) SetFields(fields string) *OperationDetailsSearch {
+	slf.Fields = fields
+	return slf
+}
+
+func (slf *OperationDetailsSearch) SetProductId(productId string) *OperationDetailsSearch {
+	slf.ProductId = productId
+	return slf
+}
+
 func (slf *OperationDetailsSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&OperationDetails{})
 
@@ -91,6 +123,20 @@
 
 	if slf.OperationID != 0 {
 		db = db.Where("operation_id = ?", slf.OperationID)
+	}
+	if slf.ProductId != "" {
+		db = db.Where("product_id = ?", slf.ProductId)
+	}
+	if slf.Preload {
+		db = db.Preload("Product")
+	}
+
+	if len(slf.OperationIDs) > 0 {
+		db = db.Where("operation_id in ?", slf.OperationIDs)
+	}
+
+	if slf.Fields != "" {
+		db = db.Select(slf.Fields)
 	}
 
 	return db
@@ -256,3 +302,31 @@
 
 	return records, nil
 }
+
+func (slf *OperationDetailsSearch) GroupSum(groupField string, sumField string) ([]*GroupSum, error) {
+	var (
+		db     = slf.build()
+		result = make([]*GroupSum, 0)
+	)
+	if err := db.Select("sum(" + sumField + ") as sum, " + groupField + " as class").Group(groupField).Scan(&result).Error; err != nil {
+		return nil, fmt.Errorf("select group err: %v", err)
+	}
+	return result, nil
+}
+
+type GroupByDealerTypeWarehouse struct {
+	DealerType string
+	ProductID  string
+	Sum        decimal.Decimal
+}
+
+func (slf *OperationDetailsSearch) GroupMultiSumAmount() ([]*GroupByDealerTypeWarehouse, error) {
+	var (
+		db     = slf.build()
+		result = make([]*GroupByDealerTypeWarehouse, 0)
+	)
+	if err := db.Select("sum(amount) as sum, dealer_type, product_id").Group("product_id, dealer_type").Scan(&result).Error; err != nil {
+		return nil, fmt.Errorf("select group err: %v", err)
+	}
+	return result, nil
+}

--
Gitblit v1.8.0