github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/import/joke.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  	filePath := "/Users/aaron/work/zentao/product/zd/行业数据/笑话.json"
    15  
    16  	tableName := model.Joke{}.TableName()
    17  	db := comm.GetDB()
    18  	err := db.Exec(fmt.Sprintf(comm.TruncateTable, tableName)).Error
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	content := fileUtils.ReadFileBuf(filePath)
    24  
    25  	arr := make([]string, 0)
    26  
    27  	json.Unmarshal(content, &arr)
    28  
    29  	for _, item := range arr {
    30  		item = strings.TrimSpace(item)
    31  
    32  		cate := model.Joke{
    33  			Content: item,
    34  		}
    35  
    36  		if cate.Content != "" {
    37  			db.Save(&cate)
    38  		}
    39  	}
    40  }