zhangqian
2024-04-07 a8914a16b23e93f6bfd12bcfd5cbe8b24cf7eb84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package service
 
import (
    "github.com/spf13/cast"
    "strconv"
    "wms/models"
    "wms/pkg/blevex"
    "wms/pkg/logx"
)
 
type LocationReport struct {
    LocationProductAmountID int
    LocationName            string `json:"name" gorm:"index;type:varchar(255);not null;comment:位置名称"` //位置名称
    LocationJointName       string `json:"jointName" gorm:"type:varchar(255);comment:拼接名称"`           //拼接名称
    MaterialName            string `json:"materialName"`                                              //物料名称
    ProductCategoryName     string `json:"productCategoryName"`                                       //分类名称
}
 
const (
    LocationReportIndexName = "locationReport.bleve"
)
 
func InitLocationReportData() {
    docCount, err := blevex.DocCount(LocationReportIndexName)
    if err != nil {
        logx.Errorf("InitLocationReportData get doc count err:%v", err)
        return
    }
    if docCount > 0 {
        return
    }
 
    reports := make([]*LocationReport, 0)
    search := models.NewLocationProductAmountSearch()
    err = search.Orm.Model(&models.LocationProductAmount{}).
        Raw("select wms_location_product_amount.id as LocationProductAmountID, wms_location.name as LocationName, wms_location.joint_name as LocationJointName, material.name as MaterialName, wms_product_category.Name as ProductCategoryName from wms_location_product_amount " +
            "left join wms_location on wms_location.id=wms_location_product_amount.location_id " +
            "left join material on material.id = wms_location_product_amount.product_id " +
            "left join wms_product_category on wms_product_category.id=material.category_id").Scan(&reports).Error
    if err != nil {
        logx.Errorf("InitLocationReportData scan err:%v", err)
    }
    for _, result := range reports {
        err = blevex.Add(LocationReportIndexName, strconv.Itoa(result.LocationProductAmountID), result)
        if err != nil {
            logx.Errorf("InitLocationReportData add failed, err:%v, index:%v, data:%v", err, LocationReportIndexName, result)
        }
    }
    return
}
 
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
    }
    if len(ids) == 0 {
        return
    }
    recordIds := make([]int, 0, len(ids))
    for _, id := range ids {
        recordIds = append(recordIds, cast.ToInt(id))
    }
    list, err = models.NewLocationProductAmountSearch().SetPreload(true).SetIds(recordIds).Find()
    return
}
 
func AddNewLocationReportRecord(id int) {
    var report LocationReport
    err := models.NewLocationProductAmountSearch().Orm.Model(&models.LocationProductAmount{}).
        Raw("select wms_location_product_amount.id as LocationProductAmountID, wms_location.name as LocationName, wms_location.joint_name as LocationJointName, material.name as MaterialName, wms_product_category.Name as ProductCategoryName from wms_location_product_amount "+
            "left join wms_location on wms_location.id=wms_location_product_amount.location_id "+
            "left join material on material.id = wms_location_product_amount.product_id "+
            "left join wms_product_category on wms_product_category.id=material.category_id where LocationProductAmountID = ?", id).Scan(&report).Error
    if err != nil {
        logx.Errorf("AddNewLocationReportRecord scan err:%v", err)
        return
    }
    err = blevex.Add(LocationReportIndexName, strconv.Itoa(id), report)
    if err != nil {
        logx.Errorf("AddNewLocationReportRecord add err:%v", err)
        return
    }
}