excel_data_controller.go
5.2 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
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
package controllers
import (
"fmt"
"io"
"path/filepath"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/utils/excel"
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/ecelData/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/ecelData/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
)
type ExcelDataController struct {
beego.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
}
// FileImport 导入excel数据
func (controller *ExcelDataController) FileImport() {
var (
data interface{}
err error
r io.Reader
ext string
)
excelService := service.NewExcelDataService(nil)
r, ext, err = controller.GetFileWithExt()
if err != nil {
controller.Response(nil, err)
return
}
cmd := &command.ImportDataCommand{}
controller.ParseForm(cmd)
cmd.Operator = ParseOperateInfo(controller.BaseController)
cmd.Reader = r
cmd.FileExt = ext
switch cmd.Code {
//case domain.ImportCompanyUser:
// data, err = excelService.ImportCompanyUser(cmd)
//case domain.ImportCooperationUser:
// data, err = excelService.ImportCooperationUser(cmd)
//case domain.ImportOrganization:
// data, err = excelService.ImportOrganization(cmd)
case "ImportProductTrouble":
data, err = excelService.ImportProductTrouble(cmd)
case "ImportProductRecordLevel2":
data, err = excelService.ImportProductRecord(cmd)
case "ImportAttendance":
data, err = excelService.ImportDataAttendance(cmd)
default:
err = fmt.Errorf("导入不存在 Code:%v", cmd.Code)
}
controller.Response(data, err)
}
// FileExport 文件导出
func (controller *ExcelDataController) FileExport() {
// 1.读取command
exportDataCommand := &command.ExportDataCommand{}
err := controller.Unmarshal(exportDataCommand)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
exportDataCommand.Operator = ParseOperateInfo(controller.BaseController)
// 2.读取data
var data excel.ExcelMaker
var filename string
//var excelService = service.NewExcelDataService(nil)
switch exportDataCommand.Code {
//case domain.ExportCompanyUser:
// companyUserListQuery := &query.CompanyUserListQuery{}
// exportDataCommand.UnmarshalQuery(companyUserListQuery)
// companyUserListQuery.Operator = exportDataCommand.Operator
// data, err = excelService.ExportCompanyUser(companyUserListQuery)
// filename = "导出公司用户"
//case domain.ExportCooperationUser:
// companyUserListQuery := &query.CooperationUserListQuery{}
// exportDataCommand.UnmarshalQuery(companyUserListQuery)
// companyUserListQuery.Operator = exportDataCommand.Operator
// data, err = excelService.ExportCooperationUser(companyUserListQuery)
// filename = "导出共创用户"
default:
err = fmt.Errorf("export type :%v not exists", exportDataCommand.Code)
}
if err != nil {
log.Logger.Debug("excelService.Export err:" + err.Error())
controller.Response(nil, err)
return
}
// 3.返回文件
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, filename)
}
// GetExcelDataFields 获取导出excel数据的可选字段
func (controller *ExcelDataController) GetExcelDataFields() {
}
func (controller *ExcelDataController) GetFileWithExt() (io.Reader, string, error) {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error(err.Error())
return nil, "", fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == ".xlsx" || ext == ".xls" || ext == ".csv") {
return nil, "", fmt.Errorf("仅支持上传文件格式 xls/xlsx/csv")
}
return excelFile, ext, nil
}
func (controller *ExcelDataController) FileImportTemplate() {
// excelService := service.NewExcelDataService(nil)
code := controller.GetString(":code")
// cmd := &command.ImportDataCommand{}
// cmd.Code = code
// var data interface{}
// data, err := excelService.FileImportTemplate(cmd)
// controller.Response(data, err)
switch code {
case "ImportProductTrouble":
controller.Ctx.Output.Download("./download/excel_tpl/事故管理导入.xlsx")
case "ImportProductRecordLevel2":
controller.Ctx.Output.Download("./download/excel_tpl/二级品审核导入.xlsx")
case "ImportAttendance":
controller.Ctx.Output.Download("./download/excel_tpl/工时管理导入.xlsx")
}
}