-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (71 loc) · 2.02 KB
/
main.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
package main
import (
"ShopOrders/db"
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
"os"
"sort"
"strings"
)
type Product struct {
ShelfName string
ProductName string
ProductID int
OrderID int
ProductCount int
AdditionalShelves string
}
func main() {
args := os.Args[1:]
orderNumbers := strings.Join(args, ",")
fmt.Println("Страница сборки заказов", orderNumbers)
fmt.Println("=+=+=+=")
db, err := db.ConnectDB()
if err != nil {
log.Fatal(err)
}
defer db.Close()
query := fmt.Sprintf(`SELECT shelf_name, product_name, product_id, order_id, product_count, additional_shelves FROM GetSummaryWithShelves(ARRAY[%s]);`, orderNumbers)
rows, err := db.Query(query)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var products []Product
for rows.Next() {
var product Product
var additionalShelves sql.NullString
err := rows.Scan(&product.ShelfName, &product.ProductName, &product.ProductID, &product.OrderID, &product.ProductCount, &additionalShelves)
if err != nil {
log.Fatal(err)
}
product.AdditionalShelves = additionalShelves.String
products = append(products, product)
}
sort.SliceStable(products, func(i, j int) bool {
if products[i].ShelfName != products[j].ShelfName {
return products[i].ShelfName < products[j].ShelfName
}
return products[i].ProductID < products[j].ProductID
})
var prevShelfName string
for _, product := range products {
shelfName := product.ShelfName
productName := product.ProductName
productID := product.ProductID
orderID := product.OrderID
productCount := product.ProductCount
additionalShelves := product.AdditionalShelves
if shelfName != prevShelfName {
fmt.Printf("\n%s\n", shelfName)
prevShelfName = shelfName
}
fmt.Printf("%s (id=%d)\norder %d, %d шт\n", productName, productID, orderID, productCount)
if productID != 1 && productID != 2 && productID != 4 && productID != 6 {
fmt.Printf("доп стеллаж: %s", additionalShelves)
}
fmt.Println("\n")
}
}