预约时间修正
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
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))
|
||||
}
|
||||
*/
|
||||
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"io"
|
||||
"math"
|
||||
@@ -134,3 +135,156 @@ func GetStrDays(day int, minute int, reserveMap map[string]models.ReserveTimeFil
|
||||
func RoundToOneDecimalPlace(value float64) float64 {
|
||||
return math.Round(value*10) / 10
|
||||
}
|
||||
|
||||
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 := 10; h <= 19; h++ {
|
||||
timeSlots = append(timeSlots, fmt.Sprintf("%04d-%02d-%02d %02d:00:00", day.Year(), day.Month(), day.Day(), h))
|
||||
}
|
||||
}
|
||||
return timeSlots
|
||||
}
|
||||
|
||||
type OrderServiceTimeResponse struct {
|
||||
Times map[string][]TimeObject `json:"times"`
|
||||
}
|
||||
|
||||
type OrderTemp struct {
|
||||
OrderId string
|
||||
Uid int64
|
||||
ServiceTime string // 格式: "2025-02-11 09:00"
|
||||
ProjectionServiceTime int64 // 服务时长,单位:分钟
|
||||
}
|
||||
|
||||
func ProcessOrders(orders []OrderTemp, maxCars int, maxDays int) OrderServiceTimeResponse {
|
||||
// 存储每个时间段的车辆占用情况
|
||||
response := OrderServiceTimeResponse{
|
||||
Times: make(map[string][]TimeObject),
|
||||
}
|
||||
|
||||
// 用来记录每个用户的占用时间
|
||||
userOccupiedTime := make(map[int64]map[string]bool)
|
||||
|
||||
// 遍历每个订单
|
||||
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)
|
||||
|
||||
// 记录用户的占用时间
|
||||
if userOccupiedTime[order.Uid] == nil {
|
||||
userOccupiedTime[order.Uid] = make(map[string]bool)
|
||||
}
|
||||
|
||||
// 处理服务时间占用
|
||||
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())
|
||||
// 用户在该时间段占用时间,标记为已占用
|
||||
userOccupiedTime[order.Uid][timeSlot] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 计算时间段的范围,生成当前时间到未来 maxDays 天内的每小时时间段
|
||||
startTime := time.Now()
|
||||
allTimeSlots := generateTimeSlots(startTime, maxDays)
|
||||
|
||||
// 填充每个时间段的占用情况
|
||||
for _, slot := range allTimeSlots {
|
||||
isOccupied := false
|
||||
// 格式化时间为 "HH:MM"
|
||||
timeOnly := slot[11:16]
|
||||
|
||||
// 计算该时间段的总占用情况
|
||||
occupiedCars := 0
|
||||
for _, userTimes := range userOccupiedTime {
|
||||
if userTimes[slot] {
|
||||
occupiedCars++
|
||||
}
|
||||
}
|
||||
|
||||
// 如果当前时间段的车占用数大于或等于最大车数,则标记为占用
|
||||
if occupiedCars >= maxCars {
|
||||
isOccupied = true
|
||||
}
|
||||
|
||||
// 提取日期作为 key
|
||||
date := slot[:10]
|
||||
|
||||
// 根据占用情况设置 Y 的值,true 表示可预约,false 表示不可预约
|
||||
response.Times[date] = append(response.Times[date], TimeObject{
|
||||
Time: timeOnly,
|
||||
Y: !isOccupied, // true 是可预约,false 是不可预约
|
||||
})
|
||||
}
|
||||
return response
|
||||
}
|
||||
func CheckAvailability(orders []OrderTemp, maxCars int, checkTime string, durationMinutes int) bool {
|
||||
// 解析传入的时间字符串
|
||||
startTime, err := time.Parse("2006-01-02 15:04", checkTime)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing check time:", err)
|
||||
return true // 出现解析错误时直接返回不可预约
|
||||
}
|
||||
|
||||
// 计算结束时间
|
||||
endTime := startTime.Add(time.Duration(durationMinutes) * time.Minute)
|
||||
|
||||
// 用来记录每个用户的占用时间
|
||||
userOccupiedTime := make(map[int64]map[string]bool)
|
||||
|
||||
// 遍历每个订单
|
||||
for _, order := range orders {
|
||||
orderStartTime, err := time.Parse("2006-01-02 15:04", order.ServiceTime)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing order time:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 计算每个订单的结束时间
|
||||
orderEndTime := orderStartTime.Add(time.Duration(order.ProjectionServiceTime) * time.Minute)
|
||||
|
||||
// 记录用户的占用时间
|
||||
if userOccupiedTime[order.Uid] == nil {
|
||||
userOccupiedTime[order.Uid] = make(map[string]bool)
|
||||
}
|
||||
|
||||
// 处理服务时间占用
|
||||
for t := orderStartTime; t.Before(orderEndTime); t = t.Add(time.Hour) {
|
||||
// 格式化每个小时的整点
|
||||
timeSlot := fmt.Sprintf("%04d-%02d-%02d %02d:00:00", t.Year(), t.Month(), t.Day(), t.Hour())
|
||||
// 用户在该时间段占用时间,标记为已占用
|
||||
userOccupiedTime[order.Uid][timeSlot] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 检查指定时间段是否有足够的空闲时间
|
||||
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())
|
||||
|
||||
// 计算该时间段的总占用情况
|
||||
occupiedCars := 0
|
||||
for _, userTimes := range userOccupiedTime {
|
||||
if userTimes[timeSlot] {
|
||||
occupiedCars++
|
||||
}
|
||||
}
|
||||
|
||||
// 如果当前时间段的车占用数大于或等于最大车数,则表示不可预约
|
||||
if occupiedCars >= maxCars {
|
||||
return true // 如果不可预约,返回true
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有占用冲突,则可以预约
|
||||
return false // 如果可以预约,返回false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user