新增逻辑 根据车辆数量及单次服务数量来决定时间点是否可预约
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"io"
|
||||
"pet-house.com/business/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CtxBodyJSONParse(ctx *context.Context) any {
|
||||
@@ -12,3 +14,86 @@ func CtxBodyJSONParse(ctx *context.Context) any {
|
||||
_ = json.Unmarshal(body, _i)
|
||||
return _i
|
||||
}
|
||||
|
||||
type TimeObject struct {
|
||||
Time string `json:"time"`
|
||||
Y bool `json:"y"`
|
||||
}
|
||||
|
||||
func GetStrDays(day int, hour int, reserveMap map[string]models.ReserveTimeFilter, orderTimeMap map[string]string, carNum int, orderNumMap map[string]int, carServiceNum int) map[string][]TimeObject {
|
||||
// 获取当前时间
|
||||
currentTime := time.Now()
|
||||
// 计算半小时后的时间
|
||||
halfHourLater := currentTime.Add(time.Duration(hour) * time.Minute)
|
||||
|
||||
// 计算下一个整点时间
|
||||
nextHour := time.Date(
|
||||
halfHourLater.Year(),
|
||||
halfHourLater.Month(),
|
||||
halfHourLater.Day(),
|
||||
halfHourLater.Hour()+1, // 下一个整点小时
|
||||
0, // 下一个整点分钟数为0
|
||||
0, // 下一个整点秒数为0
|
||||
0, // 下一个整点纳秒数为0
|
||||
time.Local, // 下一个整点时区
|
||||
)
|
||||
|
||||
// 工作时间范围
|
||||
workStart := 9
|
||||
workEnd := 19
|
||||
var dayHoursMap = make(map[string][]TimeObject)
|
||||
// 循环生成每半个小时的整点时间列表,直到一周后
|
||||
oneWeekLater := currentTime.Add(time.Duration(day) * 24 * time.Hour)
|
||||
for nextHour.Before(oneWeekLater) {
|
||||
//且不存在过滤时间中
|
||||
_, existsDay := reserveMap[nextHour.Format("2006-01-02")]
|
||||
_, existsHour := reserveMap[nextHour.Format("15")]
|
||||
_, existOrderTime := orderTimeMap[nextHour.Format("2006-01-02 15:04:00")]
|
||||
orderNum, existOrderNum := orderNumMap[nextHour.Format("2006-01-02 15:04:00")]
|
||||
if (nextHour.Hour() >= workStart && nextHour.Hour() < workEnd) && (!existsDay && !existsHour) && !existOrderTime {
|
||||
key := nextHour.Format("2006-01-02")
|
||||
if _, ok := dayHoursMap[key]; ok {
|
||||
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: true,
|
||||
})
|
||||
} else {
|
||||
dayHoursMap[key] = []TimeObject{{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: true,
|
||||
}}
|
||||
}
|
||||
} else {
|
||||
key := nextHour.Format("2006-01-02")
|
||||
//当前订单数小于车辆数 可接单
|
||||
if existOrderNum && orderNum < carNum*carServiceNum {
|
||||
if _, ok := dayHoursMap[key]; ok {
|
||||
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: true,
|
||||
})
|
||||
} else {
|
||||
dayHoursMap[key] = []TimeObject{{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: true,
|
||||
}}
|
||||
}
|
||||
} else if nextHour.Hour() >= workStart && nextHour.Hour() < workEnd {
|
||||
if _, ok := dayHoursMap[key]; ok {
|
||||
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: false,
|
||||
})
|
||||
} else {
|
||||
dayHoursMap[key] = []TimeObject{{
|
||||
Time: nextHour.Format("15:04"),
|
||||
Y: false,
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
nextHour = nextHour.Add(30 * time.Minute) // 下一个整点半时间
|
||||
}
|
||||
return dayHoursMap
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"pet-house.com/business/models"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetStrDays(t *testing.T) {
|
||||
var ReserveMap map[string]models.ReserveTimeFilter
|
||||
ReserveMap = make(map[string]models.ReserveTimeFilter)
|
||||
ReserveMap["2024-04-10"] = models.ReserveTimeFilter{
|
||||
Type: 1,
|
||||
Content: "2024-04-10",
|
||||
}
|
||||
ReserveMap["12"] = models.ReserveTimeFilter{
|
||||
Type: 2,
|
||||
Content: "12",
|
||||
}
|
||||
days := GetStrDays(7, 30, ReserveMap, nil, 1, nil, 2)
|
||||
fmt.Println(days)
|
||||
}
|
||||
Reference in New Issue
Block a user