github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/import/country.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/easysoft/zendata/cmd/test/others/func/comm"
     7  	"github.com/easysoft/zendata/cmd/test/others/func/model"
     8  	stringUtils "github.com/easysoft/zendata/pkg/utils/string"
     9  )
    10  
    11  func main() {
    12  	filePath := "data/country/v1.xlsx"
    13  	sheetName := "country"
    14  
    15  	db := comm.GetDB()
    16  	db.Exec(fmt.Sprintf(comm.TruncateTable, model.DataCountry{}.TableName()))
    17  	db.AutoMigrate(
    18  		&model.DataCountry{},
    19  	)
    20  
    21  	records := comm.GetExcelTable(filePath, sheetName)
    22  
    23  	for _, record := range records {
    24  		po := model.DataCountry{
    25  			ContinentId:  stringUtils.ParseInt(record["continent_id"].(string)),
    26  			Continent:    record["continent"].(string),
    27  			AreaCode:     record["areacode"].(string),
    28  			EnglishShort: record["enshort"].(string),
    29  			EnglishFull:  record["enfull"].(string),
    30  			ChineseShort: record["cnshort"].(string),
    31  			ChineseFull:  record["cnfull"].(string),
    32  		}
    33  
    34  		db.Save(&po)
    35  	}
    36  }