import.go 3.4 KB
package service

import (
	"fmt"
	"strings"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/ecelData/command"

	"github.com/linmadan/egglib-go/utils/excel"
)

type ExcelDataService struct {
}

func NewExcelDataService(param map[string]interface{}) *ExcelDataService {
	return &ExcelDataService{}
}

// 导入结果
func (srv ExcelDataService) importResultWithHeader(headers []excel.DataField, failRows []interface{}, totalRow int) interface{} {
	var result = map[string]interface{}{
		"successCount": 0,
		"fail":         struct{}{},
	}
	if len(failRows) == 0 {
		result["successCount"] = totalRow
	}
	if len(failRows) > 0 {
		fail := map[string]interface{}{
			"body": failRows,
		}
		var header = make(map[string]string)
		for i := 0; i < len(headers); i++ {
			item := headers[i]
			header[item.EnName] = item.CnName
		}
		fail["header"] = header
		result["fail"] = fail
	}
	return result
}

func (srv ExcelDataService) fieldValueAllEmpty(param map[string]string) bool {
	isAllEmpty := true
	for _, v := range param {
		value := strings.TrimSpace(v)
		if len(value) > 0 {
			isAllEmpty = false
		}
	}
	return isAllEmpty
}

// // FileImportTemplate 导入模板
func (srv ExcelDataService) FileImportTemplate(importDataCommand *command.ImportDataCommand) (interface{}, error) {
	var mapTemplate = map[string]string{
		//domain.ImportCompanyUser:           "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210916/object/1631807485_EaxECeRz23WpzrMZmbwdEPRJ3Pdxpx5X.xlsx",
		//domain.ImportOrganization:          "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210916/object/1631807490_r2XWhGmbWWmpbeePBkZ3EJQFKcZEMpEm.xlsx",
		//domain.ImportDividendsOrders:       "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210927/object/1632743206_NPYTKw6RGhXn4TpYNEQhGGdCkXKXTnxM.xlsx",
		//domain.ImportCooperationUser:       "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210926/object/1632635345_SbfzmkXjQHhCwPw4MB7zb5EBBtdp2MSE.xlsx",
		//domain.ImportDividendsReturnOrders: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20210927/object/1632743214_rNHm6ZBXZaC5xKTrsE7M4h45MY6n6Ff3.xlsx",
		//二级品审核导入
		//"ImportProductRecordLevel2": "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20221030/object/1667144411_R3h4nQxxcMJ7ympktMsYBBQ6bAcFC6xj.xlsx",
		//工时管理导入
		//"ImportAttendance": "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20221030/object/1667144523_w66QpzZTfxsmhbM5mmBrHjpytcydMNs2.xlsx",
		//事故管理导入
		//	"ImportProductTrouble": "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20221030/object/1667144570_xYKYMrMnXWTyasDPnX2JNXEZHd3rJsWG.xlsx",
	}
	var url string
	var ok bool
	if url, ok = mapTemplate[importDataCommand.Code]; !ok {
		return nil, application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("模板:%v 不存在", importDataCommand.Code))
	}
	return map[string]string{
		"url": url,
	}, nil
}

func (srv ExcelDataService) GetExcelDataFields(code string) (interface{}, error) {
	dataFields := []DataFieldOptions{}
	//switch code {
	//case domain.ExportCompanyUser:
	//	dataFileds = (ExportCompanyUserData{}).AllFields()
	//case domain.ExportCooperationUser:
	//	dataFileds = (ExportCooperationUserData{}).AllFields()
	//}
	return dataFields, nil
}