-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (136 loc) · 3.46 KB
/
main.go
File metadata and controls
162 lines (136 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"github.com/google/uuid"
"github.com/xuri/excelize/v2"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
log.Fatal("database connection string is not set")
}
f, err := excelize.OpenFile("filename.xlsx")
if err != nil {
panic(err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
// Get all the rows in the Sheet1.
rows, err := f.GetRows("Sheet1")
if err != nil {
log.Fatal(err)
}
persons := make([]*Person, 0)
for _, row := range rows {
if row[1] != "Name" && row[2] != "Telephone 1" && row[4] != "Amount" && row[2] != "" {
persons = append(persons, &Person{
FullNames: row[1],
Phone: row[2],
Amount: row[4],
})
}
}
for _, p := range persons {
fname := strings.Split(p.FullNames, " ")
if len(fname) > 1 {
p.FirstName = fname[0]
if fname[1] == "" && len(fname) > 2 {
p.SecondName = fname[2]
} else {
p.SecondName = strings.Trim(fmt.Sprint(fname[1:]), "[]")
}
} else {
fname = strings.Split(p.FullNames, ".")
if len(fname) > 1 {
p.FirstName = fname[0]
if fname[1] == "" && len(fname) > 2 {
p.SecondName = fname[2]
} else {
p.SecondName = strings.Trim(fmt.Sprint(fname[1:]), "[]")
}
} else {
p.FirstName = p.FullNames
p.SecondName = p.FullNames
}
}
p.Amount = strings.TrimSuffix(p.Amount, "RWF")
p.RecordedBy = "username" //eg:078xxx
p.NameSpace = "namespace" //eg:kigali.gasabo.gatsata
p.ForRent = true
p.Sector = "sector-name" //eg:Gatsata
p.Cell = "cell-name" //eg:Karuruma
p.Village = "village-name" //eg:Rugoro
if !strings.HasPrefix(p.Phone, "0") {
p.Phone = "0" + p.Phone
}
p.Phone = strings.ReplaceAll(p.Phone, ",", "")
}
log.Printf("connecting to database")
phoneMap := make(map[string]bool)
uniquePersons := make([]*Person, 0)
duplicatedPersons := make([]*Person, 0)
for _, p := range persons {
if !phoneMap[p.Phone] {
phoneMap[p.Phone] = true
p.Id = uuid.New().String()
uniquePersons = append(uniquePersons, p)
} else {
duplicatedPersons = append(duplicatedPersons, p)
}
}
db := New(ctx, dsn)
if err := db.Open(); err != nil {
log.Fatal(err)
}
defer db.Close()
log.Printf("connected to database")
for index, p := range uniquePersons {
log.Printf("%d. FirstName: %s, SecondName: %s, Phone: %s, Amount: %s\n", index+1, p.FirstName, p.SecondName, p.Phone, p.Amount)
}
log.Printf("inserting above people into database")
// insert persons into database
res, err := db.insertPersons(ctx, uniquePersons)
if err != nil {
log.Fatal(err)
}
log.Printf("inserted %d persons", len(res))
log.Println("Add ids to duplicated persons")
for _, p := range duplicatedPersons {
for _, r := range res {
if p.Phone == r.Phone {
p.Id = r.Id
}
}
}
log.Println("inserting above people into properties")
allPersons := removeDuplicates(append(res, duplicatedPersons...))
// insert persons into prop erties
out, err := db.insertPortperties(ctx, allPersons)
if err != nil {
log.Fatal(err)
}
log.Printf("inserted %d properties", len(out))
os.Exit(0)
db.Close()
}
func removeDuplicates(s []*Person) []*Person {
encountered := map[Person]bool{}
result := make([]*Person, 0)
for _, v := range s {
if !encountered[*v] {
encountered[*v] = true
result = append(result, v)
}
}
return result
}