main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "fmt"
  4. "my/src/common"
  5. clientApi "my/src/server/client"
  6. "my/src/server/db/repo"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/xuri/excelize/v2"
  11. _ "github.com/joho/godotenv/autoload"
  12. )
  13. var FILE_PATH string
  14. func ReadClientsFromFile(file_path string) ([]repo.ClientInfo, error) {
  15. clients := []repo.ClientInfo{}
  16. file, err := excelize.OpenFile(file_path)
  17. if err != nil {
  18. fmt.Println(err)
  19. return clients, err
  20. }
  21. // Get all the rows in the Sheet1.
  22. rows, err := file.GetRows("Клиенты")
  23. if err != nil {
  24. fmt.Println(err)
  25. return clients, err
  26. }
  27. // Пропускаем заголовок и обрабатываем каждую строку
  28. for i, row := range rows {
  29. if i == 0 {
  30. continue
  31. }
  32. client := repo.ClientInfo{}
  33. client.Id2 = row[0]
  34. if len(row) > 1 {
  35. client.Mark = row[1]
  36. }
  37. if len(row) > 2 {
  38. if row[2] == "Да" {
  39. client.Contractor = true
  40. } else {
  41. client.Contractor = false
  42. }
  43. }
  44. if len(row) > 3 {
  45. client.FullName = row[3]
  46. }
  47. if len(row) > 4 {
  48. client.Type = row[4]
  49. }
  50. if len(row) > 5 {
  51. if row[5] != "" {
  52. phones := strings.Split(row[5], ",")
  53. client.Phones = phones
  54. // fmt.Println("PHones:", client.Phones)
  55. }
  56. }
  57. if len(row) > 6 {
  58. client.Email = row[6]
  59. }
  60. if len(row) > 7 {
  61. client.LegalAddress = row[7]
  62. }
  63. if len(row) > 8 {
  64. client.PhysicalAddress = row[8]
  65. }
  66. if len(row) > 9 {
  67. if row[9] != "" {
  68. client.RegistrationDate = row[9]
  69. }
  70. }
  71. if len(row) > 10 {
  72. client.AdChannel = row[10]
  73. }
  74. if len(row) > 11 {
  75. client.RegData1 = row[11]
  76. }
  77. if len(row) > 12 {
  78. client.RegData2 = row[12]
  79. }
  80. if len(row) > 13 {
  81. client.Note = row[13]
  82. }
  83. if len(row) > 14 {
  84. if row[14] != "" {
  85. value, err := strconv.Atoi(row[14])
  86. if err != nil {
  87. fmt.Println("Error string to int:", err)
  88. return clients, err
  89. }
  90. client.RequestCount = value
  91. }
  92. }
  93. if len(row) > 15 {
  94. client.Birthday = row[15]
  95. }
  96. if len(row) > 16 {
  97. str := row[16]
  98. if str != "" {
  99. if strings.Contains(str, ".") {
  100. res := strings.ReplaceAll(row[16], ".", "")
  101. parse, _ := strconv.ParseInt(res, 10, 64)
  102. client.Income = common.Money(parse)
  103. } else {
  104. parse, _ := strconv.ParseInt(str, 10, 64)
  105. client.Income = common.Money(parse * 100)
  106. }
  107. }
  108. }
  109. // fmt.Println("Client:", client)
  110. clients = append(clients, client)
  111. }
  112. return clients, err
  113. }
  114. func main() {
  115. portEnv := os.Getenv("PORT")
  116. url := "http://localhost" + portEnv + "/api/v1/client"
  117. // fmt.Println(url)
  118. if len(os.Args) > 1 {
  119. FILE_PATH = os.Args[1]
  120. clients, err := ReadClientsFromFile(FILE_PATH)
  121. if err != nil {
  122. fmt.Println(err)
  123. }
  124. for _, client := range clients {
  125. // fmt.Println(client)
  126. err = clientApi.PostData[repo.ClientInfo](url, client)
  127. if err != nil {
  128. fmt.Println(err)
  129. }
  130. }
  131. }
  132. }