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

---
 service/input_history_search.go |   65 ++++++++++++++++++++++++++------
 1 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/service/input_history_search.go b/service/input_history_search.go
index ac6f575..c2d96bb 100644
--- a/service/input_history_search.go
+++ b/service/input_history_search.go
@@ -1,9 +1,12 @@
 package service
 
 import (
+	"fmt"
 	"github.com/spf13/cast"
 	"gorm.io/gorm"
+	"regexp"
 	"strconv"
+	"time"
 	"wms/constvar"
 	"wms/models"
 	"wms/pkg/blevex"
@@ -20,7 +23,7 @@
 	OperationTypeName string                     `json:"operationTypeName,omitempty"` //涓氬姟鍚嶇О
 	FromLocation      string                     `json:"fromLocation,omitempty"`      //婧愪綅缃悕绉�
 	ToLocation        string                     `json:"toLocation,omitempty"`        //鐩爣浣嶇疆鍚嶇О
-	Date              string                     `json:"date,omitempty"`              //鏃ユ湡
+	Date              time.Time                  `json:"date,omitempty"`              //鏃ユ湡
 	Company           string                     `json:"company,omitempty"`           // 渚涘簲鍟�/瀹㈡埛
 	Carrier           string                     `json:"carrier,omitempty"`           //鎵胯繍鍟嗗悕绉�
 	WaybillNumber     string                     `json:"waybillNumber"`               //杩愬崟鍙�
@@ -41,19 +44,24 @@
 		return
 	}
 	records := make([]*models.MoveHistory, 0, 100)
-	reports := make([]*HistoryReport, 0, 100)
 	err = models.NewMoveHistorySearch().Orm.FindInBatches(&records, 100, func(tx *gorm.DB, batch int) error {
-		err = structx.AssignTo(records, &reports)
+		operationIds := make([]int, 0, len(records))
+		for _, record := range records {
+			operationIds = append(operationIds, record.OperationId)
+		}
+
+		operations, err := models.NewOperationSearch().SetPreload(true).SetIds(operationIds).FindNotTotal()
 		if err != nil {
-			logx.Errorf("AddNewHistoryReportRecord AssignTo err:%v", err)
 			return err
 		}
-		for _, report := range reports {
-			err = blevex.Add(HistoryReportIndexName, strconv.Itoa(report.ID), report)
-			if err != nil {
-				logx.Errorf("InitHistoryReportData add failed, err:%v, index:%v, data:%v", err, HistoryReportIndexName, report)
-				return err
-			}
+		operationMap := make(map[int]*models.Operation, len(operations))
+
+		for _, operation := range operations {
+			operationMap[operation.Id] = operation
+		}
+
+		for _, record := range records {
+			AddNewHistoryReportRecord(record, operationMap[record.OperationId])
 		}
 		return nil
 	}).Error
@@ -63,11 +71,44 @@
 	}
 	return
 }
+func parseDateString(input string) (time.Time, error) {
+	// 瀹氫箟姝e垯琛ㄨ揪寮�
+	regex := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})|(\d{4})/(\d{2})/(\d{2})|(\d{8})`)
+
+	// 鍖归厤瀛楃涓�
+	matches := regex.FindStringSubmatch(input)
+	if len(matches) == 0 {
+		return time.Time{}, fmt.Errorf("Invalid date format")
+	}
+	// 鎻愬彇骞存湀鏃�
+	year := matches[1] + matches[4] + matches[7]
+	month := matches[2] + matches[5]
+	day := matches[3] + matches[6]
+
+	// 鏋勫缓鏃堕棿瀵硅薄
+	parsedTime, err := time.ParseInLocation("20060102", fmt.Sprintf("%s%s%s", year, month, day), time.Local)
+	if err != nil {
+		return time.Time{}, err
+	}
+
+	return parsedTime, nil
+}
 
 func SearchHistoryReport(keyword string, operationType constvar.BaseOperationType, page, pageSize int) (recordIds []int, total uint64, err error) {
 	var ids []string
 	from := (page - 1) * pageSize
-	ids, total, err = blevex.ComplexSearch(HistoryReportIndexName, keyword, map[string]interface{}{"baseOperationType": operationType}, from, pageSize)
+	t, err := time.ParseInLocation("20060102", keyword, time.Local)
+	//t, err := parseDateString(keyword)
+	m := make(map[string]interface{})
+	if operationType > 0 {
+		m["baseOperationType"] = operationType
+	}
+	if err != nil || t.IsZero() {
+		ids, total, err = blevex.ComplexSearch(HistoryReportIndexName, keyword, m, from, pageSize)
+	} else {
+		ids, total, err = blevex.TimeSearch(HistoryReportIndexName, t, m, from, pageSize)
+	}
+
 	if err != nil {
 		return
 	}
@@ -89,7 +130,7 @@
 		return
 	}
 
-	report.Date = record.UpdatedAt.Format("2006-01-02")
+	report.Date = record.UpdatedAt
 	report.Carrier = operation.LogisticCompany.Name
 	report.Company = operation.CompanyName
 	report.WaybillNumber = operation.WaybillNumber

--
Gitblit v1.8.0