From 7a27dac6851c9eef5a640e591a0e1c6d550cdccb Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期六, 18 十一月 2023 21:10:08 +0800 Subject: [PATCH] 出入库报表支持全文搜索 --- pkg/blevex/bleve.go | 50 ++++ service/input_history_search.go | 96 +++++++++ models/move_history.go | 246 ++++++++++++++++++++++++ controllers/report_forms_controller.go | 155 ++++++++++---- models/db.go | 1 main.go | 1 service/search.go | 8 7 files changed, 502 insertions(+), 55 deletions(-) diff --git a/controllers/report_forms_controller.go b/controllers/report_forms_controller.go index 40488da..2489b45 100644 --- a/controllers/report_forms_controller.go +++ b/controllers/report_forms_controller.go @@ -157,64 +157,125 @@ // @Success 200 {object} util.ResponseList{data=[]response.InventoryHistory} "鎴愬姛" // @Router /api-wms/v1/forms/getHistory [post] func (slf ReportFormsController) GetHistory(c *gin.Context) { + slf.GetHistoryNew(c) + return + //var params request.GetInventoryHistory + //if err := c.BindJSON(¶ms); err != nil { + // util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�") + // return + //} + ////鑾峰彇鎿嶄綔璇︽儏 + //detailsSearch := models.NewOperationDetailsSearch() + //var ( + // result []*response.InventoryHistory + // total int64 + // ids []int + // err error + //) + //if params.KeyWord != "" { + // ids, err = service.SearchHistoryReport(params.KeyWord, params.BaseOperationType) + // + // if err != nil { + // util.ResponseFormat(c, code.InternalError, err.Error()) + // return + // } + // if len(ids) == 0 { + // util.ResponseFormatList(c, code.Success, result, 0) + // } + //} + // + //detailsSearch.Orm = detailsSearch.Orm.Model(&models.OperationDetails{}). + // Select("wms_operation.number, wms_operation.updated_at as date, material.name as product_name, wms_operation.from_location_id,"+ + // "wms_operation.to_location_id, wms_operation_details.amount, material.unit, wms_operation.contacter_name as contacted_name,"+ + // "wms_operation.base_operation_type, material.weight, wms_operation_details.product_id"). + // Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id"). + // Joins("left join material on wms_operation_details.product_id = material.id"). + // Where("wms_operation.status = ?", constvar.OperationStatus_Finish).Order("wms_operation.created_at desc") + //if len(ids) > 0 { + // detailsSearch.Orm = detailsSearch.Orm.Where("wms_operation_details.id in ?", ids) + //} + //if params.ProduceId != "" { + // detailsSearch.Orm = detailsSearch.Orm.Where("wms_operation_details.product_id = ?", params.ProduceId) + //} + //if params.BaseOperationType != 0 { + // detailsSearch.Orm = detailsSearch.Orm.Where("wms_operation.base_operation_type = ?", params.BaseOperationType) + //} + // + //err = detailsSearch.Orm.Count(&total).Error + //if err != nil { + // util.ResponseFormat(c, code.RequestParamError, "鏌ヨtotal澶辫触") + // return + //} + //if params.Page*params.PageSize > 0 { + // detailsSearch.Orm = detailsSearch.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize) + //} + // + //err = detailsSearch.Orm.Find(&result).Error + //if err != nil { + // util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ鎿嶄綔鏄庣粏澶辫触: %v", err)) + // return + //} + //var locationIds []int + //for _, history := range result { + // locationIds = append(locationIds, history.FromLocationId, history.ToLocationId) + //} + //locations, err := models.NewLocationSearch().SetIds(locationIds).FindNotTotal() + //if err != nil { + // util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ浣嶇疆澶辫触: %v", err)) + // return + //} + //for _, history := range result { + // history.Status = "瀹屾垚" + // for _, location := range locations { + // if history.ToLocationId == location.Id { + // history.ToLocation = location.Name + // } + // if history.FromLocationId == location.Id { + // history.FromLocation = location.Name + // } + // } + //} + //util.ResponseFormatList(c, code.Success, result, int(total)) +} + +func (slf ReportFormsController) GetHistoryNew(c *gin.Context) { var params request.GetInventoryHistory if err := c.BindJSON(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�") return } //鑾峰彇鎿嶄綔璇︽儏 - detailsSearch := models.NewOperationDetailsSearch() + detailsSearch := models.NewMoveHistorySearch() var ( result []*response.InventoryHistory - total int64 + total uint64 + ids []int + err error ) - detailsSearch.Orm = detailsSearch.Orm.Model(&models.OperationDetails{}). - Select("wms_operation.number, wms_operation.updated_at as date, material.name as product_name, wms_operation.from_location_id,"+ - "wms_operation.to_location_id, wms_operation_details.amount, material.unit, wms_operation.contacter_name as contacted_name,"+ - "wms_operation.base_operation_type, material.weight, wms_operation_details.product_id"). - Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id"). - Joins("left join material on wms_operation_details.product_id = material.id"). - Where("wms_operation.status = ?", constvar.OperationStatus_Finish).Order("wms_operation.created_at desc") - if params.ProduceId != "" { - detailsSearch.Orm = detailsSearch.Orm.Where("wms_operation_details.product_id = ?", params.ProduceId) + if params.KeyWord != "" { + ids, total, err = service.SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize) + if err != nil { + util.ResponseFormat(c, code.InternalError, err.Error()) + return + } + if len(ids) == 0 { + util.ResponseFormatList(c, code.Success, result, 0) + } + } + + detailsSearch.Orm = detailsSearch.Orm.Model(&models.MoveHistory{}). + Select("number, updated_at as date, product_name as product_name, from_location_id," + + "to_location_id, amount, unit, operator as contacted_name, base_operation_type, weight, product_id, from_location, to_location").Order("id desc") + if len(ids) > 0 { + detailsSearch.Orm = detailsSearch.Orm.Where("id in ?", ids) } if params.BaseOperationType != 0 { - detailsSearch.Orm = detailsSearch.Orm.Where("wms_operation.base_operation_type = ?", params.BaseOperationType) + detailsSearch.Orm = detailsSearch.Orm.Where("base_operation_type = ?", params.BaseOperationType) } - - err := detailsSearch.Orm.Count(&total).Error - if err != nil { - util.ResponseFormat(c, code.RequestParamError, "鏌ヨtotal澶辫触") - return - } - if params.Page*params.PageSize > 0 { - detailsSearch.Orm = detailsSearch.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize) - } - err = detailsSearch.Orm.Find(&result).Error if err != nil { util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ鎿嶄綔鏄庣粏澶辫触: %v", err)) return - } - var locationIds []int - for _, history := range result { - locationIds = append(locationIds, history.FromLocationId, history.ToLocationId) - } - locations, err := models.NewLocationSearch().SetIds(locationIds).FindNotTotal() - if err != nil { - util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("鏌ヨ浣嶇疆澶辫触: %v", err)) - return - } - for _, history := range result { - history.Status = "瀹屾垚" - for _, location := range locations { - if history.ToLocationId == location.Id { - history.ToLocation = location.Name - } - if history.FromLocationId == location.Id { - history.FromLocation = location.Name - } - } } util.ResponseFormatList(c, code.Success, result, int(total)) } @@ -247,18 +308,20 @@ } } var ( - amounts []*models.LocationProductAmount - total int64 - err error + amounts []*models.LocationProductAmount + total uint64 + totalInt64 int64 + err error ) if params.KeyWord != "" { amounts, total, err = service.SearchLocationReport(params.KeyWord, params.Page, params.PageSize) } else { - amounts, total, err = models.NewLocationProductAmountSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids).FindByPage() + amounts, totalInt64, err = models.NewLocationProductAmountSearch().SetPage(params.Page, params.PageSize).SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids).FindByPage() if err != nil { util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鏁伴噺澶辫触") return } + total = uint64(totalInt64) } var result []response.LocationForms diff --git a/main.go b/main.go index 11683c2..acfe495 100644 --- a/main.go +++ b/main.go @@ -69,6 +69,7 @@ }() go service.InitLocationReportData() + go service.InitHistoryReportData() logx.Error(server.ListenAndServe().Error()) } diff --git a/models/db.go b/models/db.go index 53e3be6..bd9de7c 100644 --- a/models/db.go +++ b/models/db.go @@ -116,6 +116,7 @@ ReorderRule{}, Attachment{}, LogisticCompany{}, + MoveHistory{}, ) return err } diff --git a/models/move_history.go b/models/move_history.go new file mode 100644 index 0000000..f47383f --- /dev/null +++ b/models/move_history.go @@ -0,0 +1,246 @@ +package models + +import ( + "fmt" + "google.golang.org/genproto/googleapis/type/decimal" + "gorm.io/gorm" + "wms/constvar" + "wms/pkg/mysqlx" +) + +type ( + // MoveHistory 绉诲姩鍘嗗彶 + MoveHistory struct { + WmsModel + Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + Number string `json:"number" gorm:"column:number;type:varchar(255)"` //鍗曞彿 + BaseOperationType constvar.BaseOperationType `json:"baseOperationType" gorm:"type:tinyint;not null;comment:鍩虹浣滀笟绫诲瀷"` //鍩虹浣滀笟绫诲瀷 + OperationTypeId int `json:"operationTypeId" gorm:"type:int;not null;comment:浣滀笟绫诲瀷id"` //浣滀笟绫诲瀷id + OperationTypeName string `json:"operationTypeName" gorm:"type:int;not null;comment:浣滀笟绫诲瀷鍚嶇О"` //浣滀笟绫诲瀷鍚嶇О + OperationId int `json:"operationRecordId" gorm:"type:int;not null;comment:鎿嶄綔id"` //鎿嶄綔id + ProductId int `json:"productId" gorm:"type:int;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:char(10);not null;comment:鍗曚綅"` //鍗曚綅 + Weight string `json:"weight" gorm:"type:decimal(20,2);not null;comment:閲嶉噺"` //閲嶉噺 + FromLocationId int `json:"fromLocationId" gorm:"type:int;not null;comment:婧愪綅缃甶d"` //婧愪綅缃甶d + FromLocation string `json:"fromLocation" gorm:"type:varchar(255);not null;comment:婧愪綅缃�"` //婧愪綅缃� + ToLocationId int `json:"toLocationId" gorm:"type:int;not null;comment:鐩爣浣嶇疆id"` //鐩爣浣嶇疆id + ToLocation string `json:"toLocation" gorm:"type:varchar(255);not null;comment:鐩爣浣嶇疆"` //鐩爣浣嶇疆 + Operator string `json:"operator" gorm:"type:varchar(255);not null;comment:鎿嶄綔鑰�"` //鎿嶄綔鑰� + } + + MoveHistorySearch struct { + MoveHistory + Order string + PageNum int + PageSize int + Keyword string + Orm *gorm.DB + Preload bool + } +) + +func (slf *MoveHistory) TableName() string { + return "wms_move_history" +} + +func NewMoveHistorySearch() *MoveHistorySearch { + return &MoveHistorySearch{Orm: mysqlx.GetDB()} +} + +func (slf *MoveHistorySearch) SetOrm(tx *gorm.DB) *MoveHistorySearch { + slf.Orm = tx + return slf +} + +func (slf *MoveHistorySearch) SetPage(page, size int) *MoveHistorySearch { + slf.PageNum, slf.PageSize = page, size + return slf +} + +func (slf *MoveHistorySearch) SetOrder(order string) *MoveHistorySearch { + slf.Order = order + return slf +} + +func (slf *MoveHistorySearch) SetID(id uint) *MoveHistorySearch { + slf.ID = id + return slf +} + +func (slf *MoveHistorySearch) SetKeyword(keyword string) *MoveHistorySearch { + slf.Keyword = keyword + return slf +} + +func (slf *MoveHistorySearch) SetPreload(preload bool) *MoveHistorySearch { + slf.Preload = preload + return slf +} + +func (slf *MoveHistorySearch) build() *gorm.DB { + var db = slf.Orm.Model(&MoveHistory{}) + + if slf.ID != 0 { + db = db.Where("id = ?", slf.ID) + } + + if slf.Order != "" { + db = db.Order(slf.Order) + } + + if slf.Keyword != "" { + db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) + } + + return db +} + +// Create 鍗曟潯鎻掑叆 +func (slf *MoveHistorySearch) Create(record *MoveHistory) error { + var db = slf.build() + + if err := db.Create(record).Error; err != nil { + return err + } + + return nil +} + +// CreateBatch 鎵归噺鎻掑叆 +func (slf *MoveHistorySearch) CreateBatch(records []*MoveHistory) error { + var db = slf.build() + + if err := db.Create(&records).Error; err != nil { + return fmt.Errorf("create batch err: %v, records: %+v", err, records) + } + + return nil +} + +func (slf *MoveHistorySearch) Update(record *MoveHistory) error { + var db = slf.build() + + if err := db.Omit("CreatedAt").Updates(record).Error; err != nil { + return fmt.Errorf("save err: %v, record: %+v", err, record) + } + + return nil +} + +func (slf *MoveHistorySearch) UpdateByMap(upMap map[string]interface{}) error { + var ( + db = slf.build() + ) + + if err := db.Updates(upMap).Error; err != nil { + return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap) + } + + return nil +} + +func (slf *MoveHistorySearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error { + var ( + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if err := db.Updates(upMap).Error; err != nil { + return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap) + } + + return nil +} + +func (slf *MoveHistorySearch) Delete() error { + var db = slf.build() + return db.Delete(&MoveHistory{}).Error +} + +func (slf *MoveHistorySearch) First() (*MoveHistory, error) { + var ( + record = new(MoveHistory) + db = slf.build() + ) + + if err := db.First(record).Error; err != nil { + return record, err + } + + return record, nil +} + +func (slf *MoveHistorySearch) Find() ([]*MoveHistory, int64, error) { + var ( + records = make([]*MoveHistory, 0) + total int64 + db = slf.build() + ) + + if err := db.Count(&total).Error; err != nil { + return records, total, fmt.Errorf("find count err: %v", err) + } + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, total, fmt.Errorf("find records err: %v", err) + } + + return records, total, nil +} + +func (slf *MoveHistorySearch) FindNotTotal() ([]*MoveHistory, error) { + var ( + records = make([]*MoveHistory, 0) + db = slf.build() + ) + + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find records err: %v", err) + } + + return records, nil +} + +// FindByQuery 鎸囧畾鏉′欢鏌ヨ. +func (slf *MoveHistorySearch) FindByQuery(query string, args []interface{}) ([]*MoveHistory, int64, error) { + var ( + records = make([]*MoveHistory, 0) + total int64 + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if err := db.Count(&total).Error; err != nil { + return records, total, fmt.Errorf("find by query count err: %v", err) + } + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) + } + + return records, total, nil +} + +// FindByQueryNotTotal 鎸囧畾鏉′欢鏌ヨ&涓嶆煡璇㈡�绘潯鏁�. +func (slf *MoveHistorySearch) FindByQueryNotTotal(query string, args []interface{}) ([]*MoveHistory, error) { + var ( + records = make([]*MoveHistory, 0) + db = slf.Orm.Table(slf.TableName()).Where(query, args...) + ) + + if slf.PageNum*slf.PageSize > 0 { + db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) + } + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) + } + + return records, nil +} diff --git a/pkg/blevex/bleve.go b/pkg/blevex/bleve.go index 2c06117..3afe948 100644 --- a/pkg/blevex/bleve.go +++ b/pkg/blevex/bleve.go @@ -1,6 +1,7 @@ package blevex import ( + "fmt" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/mapping" "github.com/yanyiwu/gojieba" @@ -72,23 +73,60 @@ } return index, err } -func Search(indexName string, keyword string) (ids []string, err error) { +func Search(indexName string, keyword string, from, size int) (ids []string, total uint64, err error) { index, err := LoadIndex(indexName) if err != nil { - return nil, err + return } req := bleve.NewSearchRequest(bleve.NewQueryStringQuery(keyword)) + req.From = from + req.Size = size res, err := index.Search(req) if err != nil { - panic(err) + return nil, 0, err } - if res == nil || res.Total == 0 { - return ids, nil + if res == nil { + return } for _, ret := range dealResult(res) { ids = append(ids, ret.Id) } - return ids, nil + return ids, res.Total, nil +} + +func ComplexSearch(indexName string, keyword string, conditions map[string]interface{}, from, size int) (ids []string, total uint64, err error) { + index, err := LoadIndex(indexName) + if err != nil { + return nil, 0, err + } + + // Create a boolean query with a should clause for fuzzy search + boolQuery := bleve.NewBooleanQuery() + + fuzzyQuery := bleve.NewFuzzyQuery(keyword) + fuzzyQuery.SetFuzziness(2) // Set the fuzziness level as needed + + boolQuery.AddShould(fuzzyQuery) + + // Add a must clause for category filtering + for key, val := range conditions { + query := bleve.NewQueryStringQuery(fmt.Sprintf("%s:%s", key, val)) + boolQuery.AddMust(query) + } + req := bleve.NewSearchRequest(boolQuery) + req.From = from + req.Size = size + res, err := index.Search(req) + if err != nil { + return nil, 0, err + } + if res == nil { + return + } + for _, ret := range dealResult(res) { + ids = append(ids, ret.Id) + } + return ids, res.Total, nil } type Result struct { diff --git a/service/input_history_search.go b/service/input_history_search.go new file mode 100644 index 0000000..319d763 --- /dev/null +++ b/service/input_history_search.go @@ -0,0 +1,96 @@ +package service + +import ( + "github.com/spf13/cast" + "gorm.io/gorm" + "strconv" + "wms/constvar" + "wms/models" + "wms/pkg/blevex" + "wms/pkg/logx" + "wms/pkg/structx" +) + +type HistoryReport struct { + ID int + BaseOperationType constvar.BaseOperationType + Number string //鎿嶄綔鍗曞彿 + ProductId string //浜у搧缂栧彿 + ProductName string //浜у搧鍚嶇О + OperationTypeName string //涓氬姟鍚嶇О + FromLocation string //婧愪綅缃悕绉� + ToLocation string //鐩爣浣嶇疆鍚嶇О +} + +const ( + HistoryReportIndexName = "HistoryReport.bleve" +) + +func InitHistoryReportData() { + docCount, err := blevex.DocCount(HistoryReportIndexName) + if err != nil { + logx.Errorf("InitHistoryReportData get doc count err:%v", err) + return + } + if docCount > 0 { + 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) + 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 + } + } + return nil + }).Error + + if err != nil { + logx.Errorf("InitHistoryReportData scan err:%v", err) + } + return +} + +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) + if err != nil { + return + } + if len(ids) == 0 { + return + } + recordIds = make([]int, 0, len(ids)) + for _, id := range ids { + recordIds = append(recordIds, cast.ToInt(id)) + } + return +} + +func AddNewHistoryReportRecord(moveHistoryId int) { + 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) + if err != nil { + logx.Errorf("AddNewHistoryReportRecord AssignTo err:%v", err) + return + } + err = blevex.Add(HistoryReportIndexName, strconv.Itoa(moveHistoryId), report) + if err != nil { + logx.Errorf("AddNewHistoryReportRecord bleve add err:%v", err) + return + } +} diff --git a/service/search.go b/service/search.go index 0d48905..d0733cb 100644 --- a/service/search.go +++ b/service/search.go @@ -49,8 +49,10 @@ return } -func SearchLocationReport(keyword string, page, pageSize int) (list []*models.LocationProductAmount, total int64, err error) { - ids, err := blevex.Search(LocationReportIndexName, keyword) +func SearchLocationReport(keyword string, page, pageSize int) (list []*models.LocationProductAmount, total uint64, err error) { + var ids []string + from := (page - 1) * pageSize + ids, total, err = blevex.Search(LocationReportIndexName, keyword, from, pageSize) if err != nil { return } @@ -61,7 +63,7 @@ for _, id := range ids { recordIds = append(recordIds, cast.ToInt(id)) } - list, total, err = models.NewLocationProductAmountSearch().SetPage(page, pageSize).SetPreload(true).SetIds(recordIds).FindByPage() + list, err = models.NewLocationProductAmountSearch().SetPreload(true).SetIds(recordIds).Find() return } -- Gitblit v1.8.0