yinbentan
2024-07-08 f4508a84236a4aff1c7b5bfa17a14a8ff95728ba
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package service
 
import (
    "fmt"
    "github.com/xuri/excelize/v2"
    "strconv"
    "time"
    "wms/constvar"
    "wms/models"
    "wms/request"
    "wms/response"
)
 
type HistoryFormsService struct{}
 
func NewHistoryFormsService() *HistoryFormsService {
    return &HistoryFormsService{}
}
 
func (slf *HistoryFormsService) Query(params request.GetInventoryHistory) (result []*response.InventoryHistory, err error) {
    search, err := slf.BuildSearch(params)
    search = search.SetPreload(true)
    if err != nil {
        return nil, err
    }
 
    if params.Page > 0 && params.PageSize > 0 {
        search = search.SetPage(params.Page, params.PageSize)
    }
    result = make([]*response.InventoryHistory, 0, params.PageSize)
    list, err := search.FindNotTotal()
 
    for _, v := range list {
        data := &response.InventoryHistory{
            Number:            v.Number,
            Date:              v.UpdatedAt.Format("2006-01-02"),
            ProductName:       v.ProductName,
            FromLocation:      v.FromLocation,
            ToLocation:        v.ToLocation,
            Amount:            v.Amount,
            AmountMoreUnits:   nil,
            Unit:              v.Unit,
            ContactedName:     v.Operator,
            BaseOperationType: v.BaseOperationType,
            Weight:            v.Weight,
            ProductId:         v.ProductId,
            FromLocationId:    v.FromLocationId,
            ToLocationId:      v.ToLocationId,
            OperationId:       v.OperationId,
            OperationTypeName: v.OperationTypeName,
        }
        moreUnit := v.Product.MoreUnit
        if moreUnit != nil && *moreUnit {
            data.AmountMoreUnits = CreateMoreUnit(v.Amount, v.Product.MoreUnitList)
        }
        result = append(result, data)
    }
 
    return result, nil
}
 
func (slf *HistoryFormsService) BuildSearch(params request.GetInventoryHistory) (search *models.MoveHistorySearch, err error) {
    search = models.NewMoveHistorySearch()
    var (
        ids         []int
        locationIds []int
    )
    if params.KeyWord != "" {
        ids, _, err = SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize)
        if err != nil {
            return
        }
        if len(ids) == 0 {
            return nil, nil
        }
    }
 
    search.Orm = search.Orm.Model(&models.MoveHistory{}).
        Select("number, updated_at, product_name, from_location_id, operation_id,to_location_id, amount, " +
            "unit, operator, base_operation_type, weight, product_id, from_location, to_location, operation_type_name, weight").Order("id desc")
    if len(ids) > 0 {
        search.Orm = search.Orm.Where("id in ?", ids)
    }
    if params.BaseOperationType != 0 {
        search.Orm = search.Orm.Where("base_operation_type = ?", params.BaseOperationType)
    }
 
    locationSearch := models.NewLocationSearch()
    if params.WarehouseCode != "" {
        locationSearch.SetJointName(params.WarehouseCode)
    }
    locations, err := locationSearch.FindNotTotal()
    if err != nil {
        return nil, err
    }
    for _, location := range locations {
        locationIds = append(locationIds, location.Id)
    }
    search.Orm = search.Orm.Where("from_location_id in ? or to_location_id in ?", locationIds, locationIds)
 
    return search, err
}
 
func (slf *HistoryFormsService) Count(params request.GetInventoryHistory) (total int64, err error) {
    search, err := slf.BuildSearch(params)
    if err != nil {
        return 0, err
    }
    total, err = search.Count()
    return
}
 
func (slf *HistoryFormsService) FetchAll(params request.GetInventoryHistory) (list []*response.InventoryHistory, err error) {
    total, err := slf.Count(params)
    if err != nil {
        return nil, err
    }
    list = make([]*response.InventoryHistory, 0, total)
    params.PageSize = 500
    page := 1
    for {
        params.Page = page
        data, err := slf.Query(params)
        if err != nil {
            return nil, err
        }
        if len(data) == 0 {
            break
        }
        list = append(list, data...)
        page++
    }
    return
}
 
func (slf *HistoryFormsService) Export(dataList []*response.InventoryHistory, params request.GetInventoryHistory) (filename string, err error) {
    var fileName string
    f := excelize.NewFile()
 
    // 自定义表头
    headers := []interface{}{"日期", "单号", "产品", "产品编码", "业务类型", "从", "至", map[string][]string{"数量": {"件", "匹", "米", "重量"}}, "单位", "重量"}
 
    // 设置表头
    if err := SetExcelHeader(headers, f); err != nil {
        return "", err
    }
 
    style, err := SetHeaderStyle(f)
    if err != nil {
        return "", err
    }
 
    lastColumn := getColumnAlphabet(13)
    f.SetCellStyle("Sheet1", "A1", lastColumn+"2", style)
    // 设置列宽
    f.SetColWidth("Sheet1", "A", "G", 30)
    f.SetColWidth("Sheet1", "H", "K", 15)
 
    for i, v := range dataList {
        column := strconv.Itoa(i + 3)
        f.SetCellValue("Sheet1", "A"+column, v.Date)
        f.SetCellValue("Sheet1", "B"+column, v.Number)
        f.SetCellValue("Sheet1", "C"+column, v.ProductName)
        f.SetCellValue("Sheet1", "D"+column, v.ProductId)
        f.SetCellValue("Sheet1", "E"+column, v.OperationTypeName)
        f.SetCellValue("Sheet1", "F"+column, v.FromLocation)
        f.SetCellValue("Sheet1", "G"+column, v.ToLocation)
        f.SetCellValue("Sheet1", "H"+column, v.Amount)
        FillMoreUnitToExcel(v.Amount, v.AmountMoreUnits, 7, i+3, f)
        f.SetCellValue("Sheet1", "L"+column, v.Unit)
        f.SetCellValue("Sheet1", "M"+column, v.Weight)
    }
 
    if params.BaseOperationType == constvar.BaseOperationTypeIncoming {
        fileName = fmt.Sprintf("入库明细报表%s.xlsx", time.Now().Format("2006-01-02-1504"))
    } else if params.BaseOperationType == constvar.BaseOperationTypeOutgoing {
        fileName = fmt.Sprintf("出库明细报表%s.xlsx", time.Now().Format("2006-01-02-1504"))
    }
 
    if err := f.SaveAs(fileName); err != nil {
        return fileName, err
    }
 
    return fileName, nil
}