126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package utils
|
||
|
||
/*import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
type TimeObject1 struct {
|
||
Time string `json:"time"` // 时间格式 "HH:MM"
|
||
Y bool `json:"y"` // Y=true: 可预约 (空闲),Y=false: 不可预约 (已占用)
|
||
}
|
||
|
||
type OrderServiceTimeResponse struct {
|
||
Times map[string][]TimeObject1 `json:"times"` // 按日期分组
|
||
}
|
||
|
||
func generateTimeSlots(startTime time.Time, maxDays int) []string {
|
||
var timeSlots []string
|
||
// 生成当前时间起未来 maxDays 天,9点到22点每小时整点的时间段
|
||
for d := 0; d < maxDays; d++ {
|
||
// 计算当前天的9点到22点时间段
|
||
day := startTime.Add(time.Duration(d) * 24 * time.Hour) // 当前日期
|
||
for h := 9; h <= 22; h++ {
|
||
timeSlots = append(timeSlots, fmt.Sprintf("%04d-%02d-%02d %02d:00:00", day.Year(), day.Month(), day.Day(), h))
|
||
}
|
||
}
|
||
return timeSlots
|
||
}
|
||
|
||
func processOrders(orders []Order, maxCars int, maxDays int) OrderServiceTimeResponse {
|
||
// 存储每个时间段的车辆占用情况
|
||
response := OrderServiceTimeResponse{
|
||
Times: make(map[string][]TimeObject1),
|
||
}
|
||
|
||
// 用来记录每个时间段的车占用情况
|
||
occupiedSlots := make(map[string]int) // key: 时间段, value: 占用的车数
|
||
|
||
// 遍历每个订单
|
||
for _, order := range orders {
|
||
startTime, err := time.Parse("2006-01-02 15:04", order.ServiceTime)
|
||
if err != nil {
|
||
fmt.Println("Error parsing time:", err)
|
||
continue
|
||
}
|
||
|
||
// 计算每个时间段的结束时间
|
||
endTime := startTime.Add(time.Duration(order.ProjectionServiceTime) * time.Minute)
|
||
|
||
// 处理服务时间占用
|
||
for t := startTime; t.Before(endTime); t = t.Add(time.Hour) {
|
||
// 格式化每个小时的整点
|
||
timeSlot := fmt.Sprintf("%04d-%02d-%02d %02d:00:00", t.Year(), t.Month(), t.Day(), t.Hour())
|
||
|
||
// 如果这个时间段已经被占用了,则增加占用车数
|
||
occupiedSlots[timeSlot]++
|
||
}
|
||
}
|
||
|
||
// 计算时间段的范围,生成当前时间到未来7天内的每小时时间段
|
||
startTime := time.Now()
|
||
allTimeSlots := generateTimeSlots(startTime, maxDays)
|
||
|
||
// 填充每个时间段的占用情况
|
||
for _, slot := range allTimeSlots {
|
||
isOccupied := false
|
||
// 格式化时间为 "HH:MM"
|
||
timeOnly := slot[11:16]
|
||
|
||
// 当前时间段的占用车数
|
||
occupiedCars := occupiedSlots[slot]
|
||
|
||
// 如果当前时间段的车占用数大于或等于最大车数,则标记为占用
|
||
if occupiedCars >= maxCars {
|
||
isOccupied = true
|
||
}
|
||
|
||
// 提取日期作为 key
|
||
date := slot[:10]
|
||
|
||
// 根据占用情况设置 Y 的值,true 表示可预约,false 表示不可预约
|
||
response.Times[date] = append(response.Times[date], TimeObject1{
|
||
Time: timeOnly,
|
||
Y: !isOccupied, // true 是可预约,false 是不可预约
|
||
})
|
||
}
|
||
|
||
return response
|
||
}
|
||
|
||
// 模拟的订单结构
|
||
type Order struct {
|
||
OrderID string
|
||
UID int64
|
||
ServiceTime string // 格式: "2025-02-11 09:00"
|
||
ProjectionServiceTime int64 // 服务时长,单位:分钟
|
||
}
|
||
|
||
func main() {
|
||
// 假设我们从数据库获取的订单数据
|
||
orders := []Order{
|
||
{"1886746333474197504", 10012, "2025-02-11 10:00", 600},
|
||
{"1887103577298571264", 11089, "2025-02-12 19:00", 125},
|
||
{"1887680412080148480", 10575, "2025-02-11 18:00", 120},
|
||
{"1888482012872839168", 10273, "2025-02-14 19:00", 65},
|
||
{"1888834477728206848", 10012, "2025-02-13 17:00", 180},
|
||
{"1888932349274492928", 10012, "2025-02-15 10:00", 60},
|
||
{"1889211058120298496", 10462, "2025-02-12 10:00", 120},
|
||
{"1889211312915877888", 10462, "2025-02-12 11:00", 120},
|
||
{"1889211441857171456", 10462, "2025-02-12 12:00", 120},
|
||
{"1889211684388605952", 10462, "2025-02-12 13:00", 120},
|
||
{"1889214115952463872", 10012, "2025-02-12 14:00", 120},
|
||
}
|
||
|
||
response := processOrders(orders, 2, 7) // 假设有2台车,查询近7天的预约情况
|
||
|
||
jsonData, err := json.MarshalIndent(response, "", " ")
|
||
if err != nil {
|
||
fmt.Println("Error marshaling to JSON:", err)
|
||
return
|
||
}
|
||
fmt.Println(string(jsonData))
|
||
}
|
||
*/
|