/
utils.go
106 lines (87 loc) · 2.72 KB
/
utils.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
package main
import (
"fmt"
"time"
)
func checkAvailableDate(date time.Time) bool {
jst := time.FixedZone("Asia/Tokyo", 9*60*60)
t := time.Date(2020, 1, 1, 0, 0, 0, 0, jst)
t = t.AddDate(0, 0, availableDays)
return date.Before(t)
}
func getUsableTrainClassList(fromStation Station, toStation Station) []string {
usable := map[string]string{}
for key, value := range TrainClassMap {
usable[key] = value
}
if !fromStation.IsStopExpress {
delete(usable, "express")
}
if !fromStation.IsStopSemiExpress {
delete(usable, "semi_express")
}
if !fromStation.IsStopLocal {
delete(usable, "local")
}
if !toStation.IsStopExpress {
delete(usable, "express")
}
if !toStation.IsStopSemiExpress {
delete(usable, "semi_express")
}
if !toStation.IsStopLocal {
delete(usable, "local")
}
ret := []string{}
for _, v := range usable {
ret = append(ret, v)
}
return ret
}
func (train Train) getAvailableSeats(fromStation Station, toStation Station, seatClass string, isSmokingSeat bool) ([]Seat, error) {
// 指定種別の空き座席を返す
var err error
// 全ての座席を取得する
query := "SELECT * FROM seat_master WHERE train_class=? AND seat_class=? AND is_smoking_seat=?"
seatList := []Seat{}
err = dbx.Select(&seatList, query, train.TrainClass, seatClass, isSmokingSeat)
if err != nil {
return nil, err
}
availableSeatMap := map[string]Seat{}
for _, seat := range seatList {
availableSeatMap[fmt.Sprintf("%d_%d_%s", seat.CarNumber, seat.SeatRow, seat.SeatColumn)] = seat
}
// すでに取られている予約を取得する
query = `
SELECT sr.reservation_id, sr.car_number, sr.seat_row, sr.seat_column
FROM seat_reservations sr, reservations r, seat_master s, station_master std, station_master sta
WHERE
r.reservation_id=sr.reservation_id AND
s.train_class=r.train_class AND
s.car_number=sr.car_number AND
s.seat_column=sr.seat_column AND
s.seat_row=sr.seat_row AND
std.name=r.departure AND
sta.name=r.arrival
`
if train.IsNobori {
query += "AND ((sta.id < ? AND ? <= std.id) OR (sta.id < ? AND ? <= std.id) OR (? < sta.id AND std.id < ?))"
} else {
query += "AND ((std.id <= ? AND ? < sta.id) OR (std.id <= ? AND ? < sta.id) OR (sta.id < ? AND ? < std.id))"
}
seatReservationList := []SeatReservation{}
err = dbx.Select(&seatReservationList, query, fromStation.ID, fromStation.ID, toStation.ID, toStation.ID, fromStation.ID, toStation.ID)
if err != nil {
return nil, err
}
for _, seatReservation := range seatReservationList {
key := fmt.Sprintf("%d_%d_%s", seatReservation.CarNumber, seatReservation.SeatRow, seatReservation.SeatColumn)
delete(availableSeatMap, key)
}
ret := []Seat{}
for _, seat := range availableSeatMap {
ret = append(ret, seat)
}
return ret, nil
}