zhangqian
2024-06-12 e5df488268e29b272932e6cc1d2b1e7034590ba0
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
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)
    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)
    err = search.FindAs(&result)
 
    return result, nil
}
 
func (slf *HistoryFormsService) BuildSearch(params request.GetInventoryHistory) (search *models.MoveHistorySearch, err error) {
    search = models.NewMoveHistorySearch()
    var (
        ids []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 as date, product_name as product_name, from_location_id, operation_id,to_location_id, amount, " +
            "unit, operator as contacted_name, 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)
    }
    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 := []string{"日期", "单号", "产品", "产品编码", "业务类型", "从", "至", "数量", "单位", "重量"}
 
    // 设置表头
    for i, header := range headers {
        cell := getColumnAlphabet(i+1) + "1"
        f.SetCellValue("Sheet1", cell, header)
    }
 
    for i, v := range dataList {
        column := strconv.Itoa(i + 2)
        f.SetCellValue("Sheet1", "A"+column, v.Date.Format("2006-01-02"))
        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)
        f.SetCellValue("Sheet1", "I"+column, v.Unit)
        f.SetCellValue("Sheet1", "J"+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
}