excel_data_controller.go 2.0 KB
package web_client

import (
	"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/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, "导出公司用户")
}