| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package main
- import (
- "fmt"
- "my/src/common"
- clientApi "my/src/server/client"
- "my/src/server/db/repo"
- "os"
- "strconv"
- "strings"
- "github.com/xuri/excelize/v2"
- _ "github.com/joho/godotenv/autoload"
- )
- var FILE_PATH string
- func ReadClientsFromFile(file_path string) ([]repo.ClientInfo, error) {
- clients := []repo.ClientInfo{}
- file, err := excelize.OpenFile(file_path)
- if err != nil {
- fmt.Println(err)
- return clients, err
- }
- // Get all the rows in the Sheet1.
- rows, err := file.GetRows("Клиенты")
- if err != nil {
- fmt.Println(err)
- return clients, err
- }
- // Пропускаем заголовок и обрабатываем каждую строку
- for i, row := range rows {
- if i == 0 {
- continue
- }
- client := repo.ClientInfo{}
- client.Id2 = row[0]
- if len(row) > 1 {
- client.Mark = row[1]
- }
- if len(row) > 2 {
- if row[2] == "Да" {
- client.Contractor = true
- } else {
- client.Contractor = false
- }
- }
- if len(row) > 3 {
- client.FullName = row[3]
- }
- if len(row) > 4 {
- client.Type = row[4]
- }
- if len(row) > 5 {
- if row[5] != "" {
- phones := strings.Split(row[5], ",")
- client.Phones = phones
- // fmt.Println("PHones:", client.Phones)
- }
- }
- if len(row) > 6 {
- client.Email = row[6]
- }
- if len(row) > 7 {
- client.LegalAddress = row[7]
- }
- if len(row) > 8 {
- client.PhysicalAddress = row[8]
- }
- if len(row) > 9 {
- if row[9] != "" {
- client.RegistrationDate = row[9]
- }
- }
- if len(row) > 10 {
- client.AdChannel = row[10]
- }
- if len(row) > 11 {
- client.RegData1 = row[11]
- }
- if len(row) > 12 {
- client.RegData2 = row[12]
- }
- if len(row) > 13 {
- client.Note = row[13]
- }
- if len(row) > 14 {
- if row[14] != "" {
- value, err := strconv.Atoi(row[14])
- if err != nil {
- fmt.Println("Error string to int:", err)
- return clients, err
- }
- client.RequestCount = value
- }
- }
- if len(row) > 15 {
- client.Birthday = row[15]
- }
- if len(row) > 16 {
- str := row[16]
- if str != "" {
- if strings.Contains(str, ".") {
- res := strings.ReplaceAll(row[16], ".", "")
- parse, _ := strconv.ParseInt(res, 10, 64)
- client.Income = common.Money(parse)
- } else {
- parse, _ := strconv.ParseInt(str, 10, 64)
- client.Income = common.Money(parse * 100)
- }
- }
- }
- // fmt.Println("Client:", client)
- clients = append(clients, client)
- }
- return clients, err
- }
- func main() {
- portEnv := os.Getenv("PORT")
- url := "http://localhost" + portEnv + "/api/v1/client"
- // fmt.Println(url)
- if len(os.Args) > 1 {
- FILE_PATH = os.Args[1]
- clients, err := ReadClientsFromFile(FILE_PATH)
- if err != nil {
- fmt.Println(err)
- }
- for _, client := range clients {
- // fmt.Println(client)
- err = clientApi.PostData[repo.ClientInfo](url, client)
- if err != nil {
- fmt.Println(err)
- }
- }
- }
- }
|