excel_data_controller.go 11.0 KB
package web_client

import (
	"strings"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"

	"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) fieldValueAllEmpty(param map[string]string) bool {
	isAllEmpty := true
	for _, v := range param {
		value := strings.TrimSpace(v)
		if len(value) > 0 {
			isAllEmpty = false
		}
	}
	return isAllEmpty
}

//导出公司用户
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) ExportCooperationUser() {
	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.ExportCooperationUser(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, "导出共创用户")
}

// ImportDividendsOrder TODO 导入分红订单
func (controller *ExcelDataController) ImportDividendsOrder() {
	importDividendsOrder(controller)
}

func importDividendsOrder(controller *ExcelDataController) {

	fileReader, err := controller.GetExcelFile()
	if err != nil {
		controller.Response(nil, err)
		return
	}
	excelImport := excel.NewExcelImport()
	excelImport.RowBegin = 2 //第2行开始读取
	excelImport.DataFields = []excel.DataField{
		{EnName: "OriginalOrderNum", CnName: "*来源单号"},
		{EnName: "DividendsCustomerName", CnName: "*客户名称"},
		{EnName: "OrderGoodName", CnName: "*产品名称"},
		{EnName: "OrderTime", CnName: "*订单日期"},
		{EnName: "RegionName", CnName: "订单区域"},
		{EnName: "OrderGoodQuantity", CnName: "*产品数量"},
		{EnName: "OrderGoodPrice", CnName: "*产品价格"},
		{EnName: "Expense", CnName: "费用"},
		{EnName: "CooperationContractNumber", CnName: "项目合约编号"},
	}
	excelData, err := excelImport.OpenExcelFromIoReader(fileReader)
	if err != nil {
		log.Logger.Error("解析excel文件失败," + err.Error())
		controller.Response(nil, err)
		return
	}
	//订单数据
	var returnedOrderData []allied_creation_cooperation.ImportDividendsOrderData
	for _, v := range excelData {
		if allEmpty := controller.fieldValueAllEmpty(v); allEmpty {
			//以全字段空跳过
			break
		}
		//TODO 按需转化
		item := allied_creation_cooperation.ImportDividendsOrderData{
			OriginalOrderNum:          strings.TrimSpace(v["OriginalOrderNum"]),
			DividendsCustomerName:     strings.TrimSpace(v["DividendsCustomerName"]),
			OrderGoodName:             strings.TrimSpace(v["OrderGoodName"]),
			OrderTime:                 strings.TrimSpace(v["OrderTime"]),
			RegionName:                strings.TrimSpace(v["RegionName"]),
			OrderGoodQuantity:         strings.TrimSpace(v["OrderGoodQuantity"]),
			OrderGoodPrice:            strings.TrimSpace(v["OrderGoodPrice"]),
			Expense:                   strings.TrimSpace(v["Expense"]),
			CooperationContractNumber: strings.TrimSpace(v["CooperationContractNumber"]),
		}
		returnedOrderData = append(returnedOrderData, item)
	}
	creationCooperationGate := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(controller.GetOperator())
	result, err := creationCooperationGate.ImportDividendsOrder(allied_creation_cooperation.ReqImportDividendsOrder{
		DividendsOrderData: returnedOrderData,
	})
	if err != nil {
		controller.Response(nil, err)
		return
	}
	controller.Response(result, nil)
}

// ImportDividendsReturnedOrder 导入分红退货单
func (controller *ExcelDataController) ImportDividendsReturnedOrder() {
	importDividendsReturnedOrder(controller)
}

func importDividendsReturnedOrder(controller *ExcelDataController) {

	fileReader, err := controller.GetExcelFile()
	if err != nil {
		controller.Response(nil, err)
		return
	}
	excelImport := excel.NewExcelImport()
	excelImport.RowBegin = 2 //第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(fileReader)
	if err != nil {
		log.Logger.Error("解析excel文件失败," + err.Error())
		controller.Response(nil, err)
		return
	}
	//退货单数据
	returnedOrderData := []allied_creation_cooperation.ImportDividendsReturnedOrderData{}
	for _, v := range excelData {
		if allEmpty := controller.fieldValueAllEmpty(v); allEmpty {
			//以全字段空跳过
			break
		}
		//TODO 按需转化
		item := allied_creation_cooperation.ImportDividendsReturnedOrderData{
			OriginalOrderNum:              strings.TrimSpace(v["OriginalOrderNum"]),
			DividendsReturnedCustomerName: strings.TrimSpace(v["DividendsReturnedCustomerName"]),
			OrderGoodName:                 strings.TrimSpace(v["OrderGoodName"]),
			DividendsReturnedDate:         strings.TrimSpace(v["DividendsReturnedDate"]),
			OrderTime:                     strings.TrimSpace(v["OrderTime"]),
			RegionName:                    strings.TrimSpace(v["RegionName"]),
			OrderGoodQuantity:             strings.TrimSpace(v["OrderGoodQuantity"]),
			OrderGoodPrice:                strings.TrimSpace(v["OrderGoodPrice"]),
			CooperationContractNumber:     strings.TrimSpace(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)
}

func (controller *ExcelDataController) ImportCompanyUser() {
	importCompanyUser(controller)
}
func importCompanyUser(controller *ExcelDataController) {
	excelService := service.NewExcelDataService(nil)
	r, err := controller.GetExcelFile()
	if err != nil {
		controller.Response(nil, err)
		return
	}
	cmd := &command.ImportDataCommand{}
	cmd.Operator = controller.GetOperator()
	cmd.Reader = r
	data, err := excelService.ImportCompanyUser(cmd)
	controller.Response(data, err)
}

func (controller *ExcelDataController) ImportCooperationUser() {
	importCooperationUser(controller)
}
func importCooperationUser(controller *ExcelDataController) {
	excelService := service.NewExcelDataService(nil)
	r, err := controller.GetExcelFile()
	if err != nil {
		controller.Response(nil, err)
		return
	}
	cmd := &command.ImportDataCommand{}
	cmd.Operator = controller.GetOperator()
	cmd.Reader = r
	data, err := excelService.ImportCooperationUser(cmd)
	controller.Response(data, err)
}

func (controller *ExcelDataController) ImportOrganization() {
	excelService := service.NewExcelDataService(nil)
	r, err := controller.GetExcelFile()
	if err != nil {
		controller.Response(nil, err)
		return
	}
	cmd := &command.ImportDataCommand{}
	cmd.Operator = controller.GetOperator()
	cmd.Reader = r
	data, err := excelService.ImportOrganization(cmd)
	controller.Response(data, err)
}

func (controller *ExcelDataController) FileImport() {
	// excelService := service.NewExcelDataService(nil)
	// r, err := controller.GetExcelFile()
	// if err != nil {
	// 	controller.Response(nil, err)
	// 	return
	// }
	// cmd := &command.ImportDataCommand{}
	//controller.Unmarshal(cmd)
	// controller.ParseForm(cmd)
	// cmd.Operator = controller.GetOperator()
	// cmd.Reader = r
	// var (
	// 	data interface{}
	// 	err  error
	// )
	code := controller.GetString("code")
	switch code {
	case domain.ImportCompanyUser:
		importCompanyUser(controller)
	case domain.ImportCooperationUser:
		importCooperationUser(controller)
	case domain.ImportDividendsOrders:
		importDividendsOrder(controller)
	case domain.ImportOrganization:
		importDividendsReturnedOrder(controller)
	}
	// controller.Response(data, err)
}

func (controller *ExcelDataController) FileImportTemplate() {

	excelService := service.NewExcelDataService(nil)
	cmd := &command.ImportDataCommand{}
	//controller.Unmarshal(cmd)
	code := controller.GetString(":code")
	cmd.Code = code
	var data interface{}
	data, err := excelService.FileImportTemplate(cmd)
	controller.Response(data, err)
}