excel_data_controller.go
4.6 KB
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
package web_client
import (
"path/filepath"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)
type ExcelDataController struct {
baseController
}
func (controller *ExcelDataController) responseExcelByFile(ctx *context.Context, excelExport *excel.ExcelExport, fileName string) error {
ctx.Output.Header("Content-Disposition", "attachment; filename="+fileName)
ctx.Output.Header("Content-Description", "File Transfer")
ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
ctx.Output.Header("Content-Transfer-Encoding", "binary")
ctx.Output.Header("Expires", "0")
ctx.Output.Header("Cache-Control", "must-revalidate")
ctx.Output.Header("Pragma", "public")
//跳过保存文件,直接写入ctx.ResponseWriter
excelExport.ExcelFile.Write(ctx.ResponseWriter)
return nil
}
//导出公司用户
func (controller ExcelDataController) ExportCompanyUser() {
excelService := service.NewExcelDataService(nil)
companyUserListQuery := &query.CompanyUserListQuery{}
err := controller.Unmarshal(companyUserListQuery)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
companyUserListQuery.Operator = controller.GetOperator()
data, err := excelService.ExportCompanyUser(companyUserListQuery)
if err != nil {
log.Logger.Debug("excelService.ExportCompanyUser err:" + err.Error())
controller.Response(nil, err)
return
}
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
if err != nil {
log.Logger.Debug("excelTool.ExportData err:" + err.Error())
controller.Response(nil, err)
return
}
controller.responseExcelByFile(controller.Ctx, excelTool, "导出公司用户")
}
func (controller ExcelDataController) ImportDividendsReturnedOrder() {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error("接收文件失败," + err.Error())
controller.Response(nil, err)
return
}
if filepath.Ext(fileHeader.Filename) == "" {
//TODO 判断文件类型
log.Logger.Error("获取到的文件:" + fileHeader.Filename)
}
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "OriginalOrderNum", CnName: "*来源源单号"},
{EnName: "DividendsReturnedCustomerName", CnName: "*客户名称"},
{EnName: "OrderGoodName", CnName: "*产品名称"},
{EnName: "DividendsReturnedDate", CnName: "*退货日期"},
{EnName: "OrderTime", CnName: "*订单日期"},
{EnName: "RegionName", CnName: "*退货区域"},
{EnName: "OrderGoodQuantity", CnName: "*退货数量"},
{EnName: "OrderGoodPrice", CnName: "*退货价格"},
{EnName: "CooperationContractNumber", CnName: "项目合约编号"},
}
excelData, err := excelImport.OpenExcelFromIoReader(excelFile)
if err != nil {
log.Logger.Error("解析excel文件失败," + err.Error())
controller.Response(nil, err)
return
}
//退货单数据
returnedOrderData := []allied_creation_cooperation.ImportDividendsReturnedOrderData{}
for _, v := range excelData {
//TODO 按需转化
item := allied_creation_cooperation.ImportDividendsReturnedOrderData{
OriginalOrderNum: v["OriginalOrderNum"],
DividendsReturnedCustomerName: v["DividendsReturnedCustomerName"],
OrderGoodName: v["OrderGoodName"],
DividendsReturnedDate: v["DividendsReturnedDate"],
OrderTime: v["OrderTime"],
RegionName: v["RegionName"],
OrderGoodQuantity: v["OrderGoodQuantity"],
OrderGoodPrice: v["OrderGoodPrice"],
CooperationContractNumber: v["CooperationContractNumber"],
}
returnedOrderData = append(returnedOrderData, item)
}
creationCooperationGate := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(controller.GetOperator())
result, err := creationCooperationGate.ImportDividendsReturnedOrder(allied_creation_cooperation.ReqImportDividendsReturnedOrder{
DividendsReturnedOrderData: returnedOrderData,
})
if err != nil {
controller.Response(nil, err)
return
}
controller.Response(result, nil)
}