zhangqian
2024-06-05 b327b91db1c7015845ad293e8ccc48c4611819c6
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
package service
 
import (
    "fmt"
    "github.com/xuri/excelize/v2"
    "strconv"
    "time"
    "wms/models"
    "wms/request"
    "wms/response"
)
 
type LocationFormsService struct{}
 
func NewLocationFormsService() *LocationFormsService {
    return &LocationFormsService{}
}
 
func (slf *LocationFormsService) Query(params request.GetLocationForms) (result []*response.LocationForms, err error) {
    search, err := slf.BuildSearch(params)
    search = search.SetPage(params.Page, params.PageSize)
    amounts, err := search.Find()
    if err != nil {
        return
    }
    for _, amount := range amounts {
        resp := new(response.LocationForms)
        resp.Amount = amount.Amount
        resp.LocationId = amount.LocationId
        resp.LocationName = amount.Location.Name
        resp.ProduceId = amount.Product.ID
        resp.ProductName = amount.Product.Name
        resp.ProductTypeName = amount.ProductCategory.Name
        resp.Unit = amount.Product.Unit
        resp.Value = resp.Amount.Mul(amount.Product.Cost)
        result = append(result, resp)
    }
 
    return
}
 
func (slf *LocationFormsService) BuildSearch(params request.GetLocationForms) (search *models.LocationProductAmountSearch, err error) {
    ids := make([]int, 0)
    if params.LocationId != 0 {
        ids = append(ids, params.LocationId)
    } else {
        //查询位置
        locations, err := models.NewLocationSearch().SetJointName(params.WareHouseCode).SetType(3).FindAll()
        if err != nil {
            return nil, err
        }
        for _, location := range locations {
            ids = append(ids, location.Id)
        }
    }
 
    search = models.NewLocationProductAmountSearch().SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids)
 
    return search, nil
}
 
func (slf *LocationFormsService) Count(params request.GetLocationForms) (total int64, err error) {
    search, err := slf.BuildSearch(params)
    if err != nil {
        return 0, err
    }
    return search.Count()
}
 
func (slf *LocationFormsService) FetchAll(params request.GetLocationForms) (list []*response.LocationForms, err error) {
    total, err := slf.Count(params)
    if err != nil {
        return nil, err
    }
    list = make([]*response.LocationForms, 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 *LocationFormsService) Export(dataList []*response.LocationForms) (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.LocationName)
        f.SetCellValue("Sheet1", "B"+column, v.ProductName)
        f.SetCellValue("Sheet1", "C"+column, v.ProductTypeName)
        f.SetCellValue("Sheet1", "D"+column, v.Amount)
        f.SetCellValue("Sheet1", "E"+column, v.Value)
    }
 
    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
}