package logic import ( "crypto/sha256" "errors" "fmt" "strconv" "strings" "time" "github.com/tealeg/xlsx" ) type SupplierLogic struct { OwningEntityName string `json:"owningEntityName"` LegalName string `json:"legalName"` LocalName string `json:"localName"` AbbreviationName string `json:"abbreviationName"` CountryOrRegionName string `json:"countryOrRegionName"` CompanyRegistrationNumber string `json:"companyRegistrationNumber"` SupplierType string `json:"supplierType"` ApprovalStatus string `json:"approvalStatus"` ApprovalDate string `json:"approvalDate"` LastReviewDate string `json:"lastReviewDate"` LegalEntityType string `json:"legalEntityType"` SanctionsCountryScreeningResult string `json:"sanctionsCountryScreeningResult"` KeyFinancial string `json:"keyFinancial"` CompanyAddress string `json:"companyAddress"` PrimaryContact string `json:"primaryContact"` DataOwnerDepartment string `json:"dataOwnerDepartment"` BasicCompanyInformation string `json:"basicCompanyInformation"` SupplementaryText string `json:"supplementaryText"` } type ErrSupplierRes struct { ID int `json:"id"` LegalName string `json:"legalName"` Remark string `json:"remark"` } func ImportSupplier(filePath string) ([]*SupplierLogic, error) { xlFile, err := xlsx.OpenFile(filePath) if err != nil { return nil, err } //开辟除表头外的行数的数组内存 //遍历sheet for sheetIndex, sheet := range xlFile.Sheets { var resourceArr []map[int]string //遍历每一行 //for rowIndex, row := range sheet.Rows { for _, row := range sheet.Rows { //开辟除表头外的行数的数组内存 objMap := make(map[int]string) //if len(row.Cells) <= 0 || row.Cells[0].String() == "" { // continue //} for cellIndex, cell := range row.Cells { text := cell.String() //如果是每一行的第一个单元格 objMap[cellIndex] = text } resourceArr = append(resourceArr, objMap) } if len(resourceArr) >= 2 { suppliers, err := getListFromRaw(resourceArr) if err != nil { return nil, errors.New(fmt.Sprintf("页码:%d,文件读取错误信息%s", sheetIndex+1, err.Error())) } if len(suppliers) > 0 { return suppliers, nil } } break } return nil, nil } func getListFromRaw(list []map[int]string) ([]*SupplierLogic, error) { var entrusts []*SupplierLogic kkMap := map[string]string{ "*使用组织/Owning Entity": "owningEntityName", "*供应商法定全称/Legal Name": "legalName", "供应商本地名称(如有)/Local Name (if applicable)": "localName", "供应商简称(如有)/Abbreviation/Trade Name (if applicable)": "abbreviationName", "*所在国家或地区/Country or Region": "countryOrRegionName", "*公司注册编号/Company Registration Number": "companyRegistrationNumber", "*供应商类型/Supplier Type": "supplierType", "*准入状态/Approval Status": "approvalStatus", "准入日期/Approval Date": "approvalDate", "法律实体形式/Legal Entity Type": "legalEntityType", "制裁与高风险国家筛查结果/Sanctions & High-Risk Country Screening Result": "sanctionsCountryScreeningResult", "关键财务与付款信息/Key Financial & Payment Information": "keyFinancial", "公司地址/Company Address": "companyAddress", "主要联系人、职位及联系方式/Primary Contact, Position&Details": "primaryContact", "数据维护部门&人员/Data Owner Department/Personnel": "dataOwnerDepartment", "公司信息概要/Summary of Basic Company Information": "basicCompanyInformation", "其他补充信息/Other Supplementary Information": "supplementaryText", } keyMap := list[0] for index, tt := range list { if index == 0 { continue } temp := &SupplierLogic{} for i, r := range tt { t := strings.TrimSpace(r) if _, ok := keyMap[i]; !ok { continue } keyString := strings.TrimSpace(keyMap[i]) if _, ok := kkMap[keyString]; !ok { fmt.Println(fmt.Sprintf("行数:%d字段信息(%s)没有匹配,请以模版为准", i, keyString)) continue } switch kkMap[keyString] { case "owningEntityName": temp.OwningEntityName = t case "legalName": temp.LegalName = t case "localName": temp.LocalName = t case "abbreviationName": temp.AbbreviationName = t case "countryOrRegionName": temp.CountryOrRegionName = t case "companyRegistrationNumber": temp.CompanyRegistrationNumber = t case "supplierType": temp.SupplierType = t case "approvalStatus": temp.ApprovalStatus = t case "approvalDate": temp.ApprovalDate = ParseExcelDate(t) //temp.ApprovalDate = t case "legalEntityType": temp.LegalEntityType = t case "sanctionsCountryScreeningResult": temp.SanctionsCountryScreeningResult = t case "keyFinancial": temp.KeyFinancial = t case "companyAddress": temp.CompanyAddress = t case "primaryContact": temp.PrimaryContact = t case "dataOwnerDepartment": temp.DataOwnerDepartment = t case "basicCompanyInformation": temp.BasicCompanyInformation = t case "supplementaryText": temp.SupplementaryText = t } } entrusts = append(entrusts, temp) } sha256.New() return entrusts, nil } func ParseExcelDate(v string) string { if v == "" { return "" } // 非数字,直接认为是 yyyy-mm-dd if _, err := strconv.ParseFloat(v, 64); err != nil { t, err := time.Parse("2006-01-02", v) if err != nil { return "" } return t.Format("2006-01-02") } // Excel 序列号 f, _ := strconv.ParseFloat(v, 64) base := time.Date(1899, 12, 30, 0, 0, 0, 0, time.Local) return base.AddDate(0, 0, int(f)).Format("2006-01-02") }