Selaa lähdekoodia

优化历史类单据查询SQL执行效率

zhou.xiaoning 1 vuosi sitten
vanhempi
commit
5a29c55e29
2 muutettua tiedostoa jossa 34 lisäystä ja 16 poistoa
  1. 22 11
      controllers/order/order.go
  2. 12 5
      controllers/taaccount/taaccount.go

+ 22 - 11
controllers/order/order.go

@@ -409,19 +409,24 @@ func QueryTradeOrderDetail(c *gin.Context) {
 		Select(`to_char(TRADE_ORDERDETAIL.ORDERID) as ORDERID, to_char(TRADE_ORDERDETAIL.PREORDERID) as PREORDERID, to_char(TRADE_ORDERDETAIL.CANCELORDERID) as CANCELORDERID, to_char(TRADE_ORDERDETAIL.RELATEDID) as RELATEDID,
 				TRADE_ORDERDETAIL.*, TRADE_ORDERDETAIL.ORDERQTY - TRADE_ORDERDETAIL.TRADEQTY - TRADE_ORDERDETAIL.CANCELQTY as ENABLEQTY, 
 				GOODS.GOODSCODE, GOODS.GOODSNAME,GOODS.DECIMALPLACE, GOODS.QTYDECIMALPLACE,GOODS.GOODUNITID, MARKET.MARKETNAME, MARKET.TRADEMODE`).
-		Where(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSRC != 10 and TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
+		// Where(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSRC != 10 and TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
+		Where("TRADE_ORDERDETAIL.ORDERSRC != 10").
+		In("TRADE_ORDERDETAIL.ACCOUNTID", strings.Split(req.AccountID, ",")).
 		Desc("TRADE_ORDERDETAIL.ORDERTIME")
 	if len(req.OrderStatus) > 0 {
-		s = s.And(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
+		// s = s.And(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
+		s = s.In("TRADE_ORDERDETAIL.ORDERSTATUS", strings.Split(req.OrderStatus, ","))
 	}
 	if len(req.TradeMode) > 0 {
-		s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
+		// s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
+		s = s.In("MARKET.TRADEMODE", strings.Split(req.TradeMode, ","))
 	}
 	if req.OrderID > 0 {
 		s = s.And("TRADE_ORDERDETAIL.ORDERID = ?", req.OrderID)
 	}
 	if req.IncOrderID != "" {
-		s = s.And(fmt.Sprintf("TRADE_ORDERDETAIL.ORDERID > %v", req.IncOrderID))
+		// s = s.And(fmt.Sprintf("TRADE_ORDERDETAIL.ORDERID > %v", req.IncOrderID))
+		s = s.And("TRADE_ORDERDETAIL.ORDERID > ?", req.IncOrderID)
 	}
 	if err := s.Find(&datas); err != nil {
 		// 查询失败
@@ -515,22 +520,28 @@ func QueryHisTradeOrderDetail(c *gin.Context) {
 		Select(`to_char(HIS_TRADE_ORDERDETAIL.ORDERID) as ORDERID, to_char(HIS_TRADE_ORDERDETAIL.PREORDERID) as PREORDERID, to_char(HIS_TRADE_ORDERDETAIL.CANCELORDERID) as CANCELORDERID, to_char(HIS_TRADE_ORDERDETAIL.RELATEDID) as RELATEDID,
 				HIS_TRADE_ORDERDETAIL.*, 
 				GOODS.GOODSCODE, GOODS.GOODSNAME,GOODS.DECIMALPLACE, GOODS.QTYDECIMALPLACE,GOODS.GOODUNITID, MARKET.MARKETNAME, MARKET.TRADEMODE`).
-		Where(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSRC != 10 and HIS_TRADE_ORDERDETAIL.ISVALIDDATA = 1 and HIS_TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
+		// Where(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSRC != 10 and HIS_TRADE_ORDERDETAIL.ISVALIDDATA = 1 and HIS_TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
+		Where("HIS_TRADE_ORDERDETAIL.ORDERSRC != 10 and HIS_TRADE_ORDERDETAIL.ISVALIDDATA = 1").
+		In("HIS_TRADE_ORDERDETAIL.ACCOUNTID", strings.Split(req.AccountID, ",")).
 		Desc("HIS_TRADE_ORDERDETAIL.ORDERTIME")
 	if len(req.OrderStatus) > 0 {
-		s = s.And(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
+		// s = s.And(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
+		s = s.And("HIS_TRADE_ORDERDETAIL.ORDERSTATUS", strings.Split(req.OrderStatus, ","))
 	}
 	if len(req.TradeMode) > 0 {
-		s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
+		// s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
+		s = s.And("MARKET.TRADEMODE", strings.Split(req.TradeMode, ","))
 	}
 	if req.OrderID > 0 {
 		s = s.And("HIS_TRADE_ORDERDETAIL.ORDERID = ?", req.OrderID)
 	}
 	if len(req.StartDate) > 0 {
-		s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') >= to_date('%s','yyyy-MM-dd')", req.StartDate))
+		// s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') >= to_date('%s','yyyy-MM-dd')", req.StartDate))
+		s = s.And("HIS_TRADE_ORDERDETAIL.HISTRADEDATE >= ?", strings.Replace(req.StartDate, "-", "", -1))
 	}
 	if len(req.EndDate) > 0 {
-		s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') <= to_date('%s','yyyy-MM-dd')", req.EndDate))
+		// s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') <= to_date('%s','yyyy-MM-dd')", req.EndDate))
+		s = s.And("HIS_TRADE_ORDERDETAIL.HISTRADEDATE <= ?", strings.Replace(req.EndDate, "-", "", -1))
 	}
 	if err := s.Find(&datas); err != nil {
 		// 查询失败
@@ -819,11 +830,11 @@ func QueryHisTradeDetail(c *gin.Context) {
 	}
 	if len(req.StartDate) > 0 {
 		// s = s.And(fmt.Sprintf("to_date(HIS_TRADE_TRADEDETAIL.HISTRADEDATE,'yyyyMMdd') >= to_date('%s','yyyy-MM-dd')", req.StartDate))
-		s = s.And("HIS_TRADE_TRADEDETAIL.HISTRADEDATE >= ?", req.StartDate)
+		s = s.And("HIS_TRADE_TRADEDETAIL.HISTRADEDATE >= ?", strings.Replace(req.StartDate, "-", "", -1))
 	}
 	if len(req.EndDate) > 0 {
 		// s = s.And(fmt.Sprintf("to_date(HIS_TRADE_TRADEDETAIL.HISTRADEDATE,'yyyyMMdd') <= to_date('%s','yyyy-MM-dd')", req.EndDate))
-		s = s.And("HIS_TRADE_TRADEDETAIL.HISTRADEDATE <= ?", req.EndDate)
+		s = s.And("HIS_TRADE_TRADEDETAIL.HISTRADEDATE <= ?", strings.Replace(req.EndDate, "-", "", -1))
 	}
 	if err := s.Find(&datas); err != nil {
 		// 查询失败

+ 12 - 5
controllers/taaccount/taaccount.go

@@ -1,7 +1,6 @@
 package taaccount
 
 import (
-	"fmt"
 	"mtp2_if/controllers/order"
 	"mtp2_if/db"
 	"mtp2_if/global/app"
@@ -154,6 +153,7 @@ func QueryAmountLog(c *gin.Context) {
 	// 查询数据
 	datas := make([]QueryAmountLogRsp, 0)
 	engine := db.GetEngine()
+	accountIDs := strings.Split(req.OperateType, ",")
 	// OPERATETYPENAME 显示时,旧的号段用operateType, 新的使用accountBusinessCode
 	s := engine.Table("TAACCOUNTLOG").
 		Join("INNER", "ENUMDICITEM", "ENUMDICITEM.ENUMITEMSTATUS = 1 and ENUMDICITEM.ENUMDICCODE = 'accountBusinessCode' and ENUMDICITEM.ENUMITEMNAME = TAACCOUNTLOG.BUSINESSCODE").
@@ -173,9 +173,14 @@ func QueryAmountLog(c *gin.Context) {
          			ELSE
           				DECODE(TAACCOUNTLOG.OPERATETYPE, NULL, '--', 0, '系统', E.ENUMDICNAME) 
 				END AS OPERATETYPENAME`).
-		Where(fmt.Sprintf("TAACCOUNTLOG.AMOUNT <> 0 and TAACCOUNTLOG.ACCOUNTID in (%s)", req.AccountID)).Desc("TAACCOUNTLOG.AUTOID")
+		// Where(fmt.Sprintf("TAACCOUNTLOG.AMOUNT <> 0 and TAACCOUNTLOG.ACCOUNTID in (%s)", req.AccountID)).
+		Where("TAACCOUNTLOG.AMOUNT <> 0").
+		In("TAACCOUNTLOG.ACCOUNTID", accountIDs).
+		Desc("TAACCOUNTLOG.AUTOID")
 	if len(req.OperateType) > 0 {
-		s = s.And(fmt.Sprintf("TAACCOUNTLOG.OPERATETYPE in (%s)", req.OperateType))
+		// s = s.And(fmt.Sprintf("TAACCOUNTLOG.OPERATETYPE in (%s)", req.OperateType))
+		operateTypes := strings.Split(req.OperateType, ",")
+		s = s.In("TAACCOUNTLOG.OPERATETYPE", operateTypes)
 	}
 	if err := s.Find(&datas); err != nil {
 		// 查询失败
@@ -300,10 +305,12 @@ func QueryHisAmountLog(c *gin.Context) {
 		s = s.In("HIS_TAACCOUNTLOG.OPERATETYPE", strings.Split(req.OperateType, ","))
 	}
 	if len(req.StartDate) > 0 {
-		s = s.And("to_date(HIS_TAACCOUNTLOG.HISTRADEDATE,'yyyyMMdd') >= to_date(?,'yyyy-MM-dd')", req.StartDate)
+		// s = s.And("to_date(HIS_TAACCOUNTLOG.HISTRADEDATE,'yyyyMMdd') >= to_date(?,'yyyy-MM-dd')", req.StartDate)
+		s = s.And("HIS_TAACCOUNTLOG.HISTRADEDATE >= ?", req.StartDate)
 	}
 	if len(req.EndDate) > 0 {
-		s = s.And("to_date(HIS_TAACCOUNTLOG.HISTRADEDATE,'yyyyMMdd') <= to_date(?,'yyyy-MM-dd')", req.EndDate)
+		// s = s.And("to_date(HIS_TAACCOUNTLOG.HISTRADEDATE,'yyyyMMdd') <= to_date(?,'yyyy-MM-dd')", req.EndDate)
+		s = s.And("HIS_TAACCOUNTLOG.HISTRADEDATE <= ?", req.EndDate)
 	}
 	if err := s.Find(&datas); err != nil {
 		// 查询失败