-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
149 lines (129 loc) · 3.27 KB
/
converter.go
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
package manager
import (
"context"
"strconv"
"strings"
"time"
"github.com/galexrt/fivenet/gen/go/proto/resources/centrum"
"github.com/galexrt/fivenet/gen/go/proto/resources/timestamp"
"github.com/galexrt/fivenet/pkg/utils"
"github.com/galexrt/fivenet/query/fivenet/model"
"github.com/galexrt/fivenet/query/fivenet/table"
jet "github.com/go-jet/jet/v2/mysql"
"go.uber.org/zap"
)
var (
// Converter
tGksPhoneJMsg = table.GksphoneJobMessage
tGksPhoneSettings = table.GksphoneSettings
)
func (s *Housekeeper) ConvertPhoneJobMsgToDispatch() error {
if len(s.convertJobs) == 0 {
return nil
}
for {
select {
case <-s.ctx.Done():
return nil
case <-time.After(2 * time.Second):
}
if err := s.convertPhoneJobMsgToDispatch(); err != nil {
s.logger.Error("failed to convert gksphone job messages to dispatches", zap.Error(err))
}
}
}
func (s *Housekeeper) convertPhoneJobMsgToDispatch() error {
stmt := tGksPhoneJMsg.
SELECT(
tGksPhoneJMsg.ID,
tGksPhoneJMsg.Jobm,
tGksPhoneJMsg.Anon,
tGksPhoneJMsg.Gps,
tGksPhoneJMsg.Message,
tUsers.ID.AS("userid"),
).
FROM(
tGksPhoneJMsg.
INNER_JOIN(tGksPhoneSettings,
tGksPhoneSettings.PhoneNumber.EQ(tGksPhoneJMsg.Number),
).
INNER_JOIN(tUsers,
tUsers.Identifier.EQ(tGksPhoneSettings.Identifier),
),
).
WHERE(jet.AND(
tGksPhoneJMsg.Jobm.REGEXP_LIKE(jet.String("\\[\"("+strings.Join(s.convertJobs, "|")+")\"\\]")),
tGksPhoneJMsg.Owner.EQ(jet.Int32(0)),
)).
LIMIT(15)
var dest []struct {
*model.GksphoneJobMessage
UserId int32
}
if err := stmt.QueryContext(s.ctx, s.db, &dest); err != nil {
return err
}
s.logger.Debug("converting phone dispatch to fivenet", zap.Int("dispatch_count", len(dest)))
for _, msg := range dest {
job := strings.TrimSuffix(strings.TrimPrefix(*msg.Jobm, "[\""), "\"]")
gps, _ := strings.CutPrefix(*msg.Gps, "GPS: ")
gpsSplit := strings.Split(gps, ", ")
x, err := strconv.ParseFloat(gpsSplit[0], 32)
if err != nil {
continue
}
y, err := strconv.ParseFloat(gpsSplit[1], 32)
if err != nil {
continue
}
anon := false
if msg.Anon != nil && *msg.Anon == "1" {
anon = true
}
message := "N/A"
if msg.Message != nil {
if len(*msg.Message) > 250 {
message = utils.StringFirstN(*msg.Message, 250) + "..."
} else {
message = *msg.Message
}
}
dsp := ¢rum.Dispatch{
CreatedAt: timestamp.Now(),
Attributes: ¢rum.Attributes{},
Job: job,
Message: message,
X: x,
Y: y,
Anon: anon,
CreatorId: &msg.UserId,
}
if postal := s.postals.Closest(x, y); postal != nil {
dsp.Postal = postal.Code
}
s.logger.Debug("converted phone dispatch to fivenet", zap.String("job", job), zap.Int32("creator_id", msg.UserId), zap.Int32("phone_dsp_id", msg.ID))
if _, err := s.CreateDispatch(s.ctx, dsp); err != nil {
return err
}
if err := s.closePhoneJobMsg(s.ctx, msg.ID); err != nil {
return err
}
}
return nil
}
func (s *Housekeeper) closePhoneJobMsg(ctx context.Context, id int32) error {
stmt := tGksPhoneJMsg.
UPDATE(
tGksPhoneJMsg.Owner,
).
SET(
tGksPhoneJMsg.Owner.SET(jet.Int32(1)),
).
WHERE(
tGksPhoneJMsg.ID.EQ(jet.Int32(id)),
)
if _, err := stmt.ExecContext(ctx, s.db); err != nil {
return err
}
return nil
}