| | |
| | | package service |
| | | |
| | | import ( |
| | | "fmt" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "regexp" |
| | | "strconv" |
| | | "time" |
| | | "wms/constvar" |
| | | "wms/models" |
| | | "wms/pkg/blevex" |
| | |
| | | OperationTypeName string `json:"operationTypeName,omitempty"` //业务名称 |
| | | FromLocation string `json:"fromLocation,omitempty"` //源位置名称 |
| | | ToLocation string `json:"toLocation,omitempty"` //目标位置名称 |
| | | Date time.Time `json:"date,omitempty"` //日期 |
| | | Company string `json:"company,omitempty"` // 供应商/客户 |
| | | Carrier string `json:"carrier,omitempty"` //承运商名称 |
| | | WaybillNumber string `json:"waybillNumber"` //运单号 |
| | | ReceiverName string `json:"receiverName"` //收货人 |
| | | } |
| | | |
| | | const ( |
| | |
| | | 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 |
| | |
| | | } |
| | | return |
| | | } |
| | | func parseDateString(input string) (time.Time, error) { |
| | | // 定义正则表达式 |
| | | 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 |
| | | } |
| | |
| | | return |
| | | } |
| | | |
| | | func AddNewHistoryReportRecord(moveHistoryId int) { |
| | | func AddNewHistoryReportRecord(record *models.MoveHistory, operation *models.Operation) { |
| | | var report HistoryReport |
| | | record, err := models.NewMoveHistorySearch().SetID(uint(moveHistoryId)).First() |
| | | if err != nil { |
| | | logx.Errorf("AddNewHistoryReportRecord Find err:%v", err) |
| | | return |
| | | } |
| | | err = structx.AssignTo(record, &report) |
| | | err := structx.AssignTo(record, &report) |
| | | if err != nil { |
| | | logx.Errorf("AddNewHistoryReportRecord AssignTo err:%v", err) |
| | | return |
| | | } |
| | | err = blevex.Add(HistoryReportIndexName, strconv.Itoa(moveHistoryId), report) |
| | | |
| | | report.Date = record.UpdatedAt |
| | | report.Carrier = operation.LogisticCompany.Name |
| | | report.Company = operation.CompanyName |
| | | report.WaybillNumber = operation.WaybillNumber |
| | | report.ReceiverName = operation.ReceiverName |
| | | |
| | | err = blevex.Add(HistoryReportIndexName, strconv.Itoa(record.Id), report) |
| | | if err != nil { |
| | | logx.Errorf("AddNewHistoryReportRecord bleve add err:%v", err) |
| | | return |