github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/import/phone-model.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  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    10  )
    11  
    12  func main() {
    13  	filePath := "/Users/aaron/work/zentao/product/zd/行业数据/手机型号.txt"
    14  
    15  	db := comm.GetDB()
    16  	err := db.Exec(fmt.Sprintf(comm.TruncateTable, model.PhoneModel{}.TableName())).Error
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	content := fileUtils.ReadFile(filePath)
    22  
    23  	for _, line := range strings.Split(content, "\n") {
    24  		arr := strings.Split(strings.TrimSpace(line), ",")
    25  
    26  		brand := strings.TrimSpace(arr[0])
    27  		model1 := strings.TrimSpace(arr[1])
    28  		area := strings.TrimSpace(arr[2])
    29  		brandName := strings.TrimSpace(arr[3])
    30  		modelName := strings.TrimSpace(arr[4])
    31  
    32  		po := model.PhoneModel{
    33  			Brand:     brand,
    34  			Model:     model1,
    35  			Area:      area,
    36  			BrandName: brandName,
    37  			ModelName: modelName,
    38  		}
    39  
    40  		db.Save(&po)
    41  	}
    42  }