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

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/easysoft/zendata/cmd/test/others/func/comm"
     9  	"github.com/easysoft/zendata/cmd/test/others/func/model"
    10  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    11  )
    12  
    13  func main() {
    14  	tableName := "food"
    15  	filePath := "/Users/aaron/work/zentao/product/zd/行业数据/歌手歌名歌词 %d.json"
    16  
    17  	tableName = model.Place{}.TableName()
    18  	db := comm.GetDB()
    19  	err := db.Exec(fmt.Sprintf(comm.TruncateTable, tableName)).Error
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	for i := 0; i < 5; i++ {
    25  		content := fileUtils.ReadFileBuf(fmt.Sprintf(filePath, i+1))
    26  
    27  		data := make([]model.SongData, 0)
    28  		json.Unmarshal(content, &data)
    29  
    30  		for _, song := range data {
    31  			po := model.Song{
    32  				Name:   song.Name,
    33  				Singer: song.Singer,
    34  				Lyric:  strings.Join(song.Lyric, "\n"),
    35  			}
    36  
    37  			if po.Name != "" {
    38  				db.Save(&po)
    39  			}
    40  		}
    41  	}
    42  
    43  }