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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/easysoft/zendata/cmd/test/others/func/comm"
     8  	"github.com/easysoft/zendata/cmd/test/others/func/model"
     9  )
    10  
    11  func main() {
    12  	filePath := "/Users/aaron/work/zentao/product/zd/行业数据/城市电话区号.xlsx"
    13  
    14  	db := comm.GetDB()
    15  	db.Exec(fmt.Sprintf(comm.TruncateTable, model.AreaCodeCity{}.TableName()))
    16  
    17  	_, records := comm.GetExcelFirstSheet(filePath)
    18  
    19  	for colIndex := 0; colIndex < len(records[0]); colIndex++ {
    20  		state := ""
    21  
    22  		for rowIndex, row := range records {
    23  			if colIndex > len(row)-1 {
    24  				break
    25  			}
    26  
    27  			content := strings.TrimSpace(row[colIndex])
    28  
    29  			if rowIndex == 0 {
    30  				state = content
    31  				continue
    32  			}
    33  
    34  			if content == "" {
    35  				break
    36  			}
    37  
    38  			arr := strings.Split(content, " ")
    39  
    40  			if len(arr) < 2 {
    41  				if len(arr) < 2 {
    42  					continue
    43  				}
    44  			}
    45  
    46  			po := model.AreaCodeCity{
    47  				State: state,
    48  				City:  arr[0],
    49  				Code:  arr[1],
    50  			}
    51  
    52  			db.Save(&po)
    53  		}
    54  	}
    55  }