112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/kataras/iris/v12/context"
|
|
"io"
|
|
"pet-house.com/business/models"
|
|
"time"
|
|
)
|
|
|
|
func CtxBodyJSONParse(ctx *context.Context) any {
|
|
body, _ := io.ReadAll(ctx.Request().Body)
|
|
var _i interface{}
|
|
_ = json.Unmarshal(body, _i)
|
|
return _i
|
|
}
|
|
|
|
type TimeObject struct {
|
|
Time string `json:"time"`
|
|
Y bool `json:"y"`
|
|
}
|
|
|
|
func GetStrDays(day int, minute int, reserveMap map[string]models.ReserveTimeFilter, orderTimeMap map[string]string, carNum int, orderNumMap map[string]int, carServiceNum int, times []string) map[string][]TimeObject {
|
|
// 获取当前时间
|
|
currentTime := time.Now().AddDate(0, 0, 1)
|
|
// 计算半小时后的时间
|
|
halfHourLater := currentTime.Add(time.Duration(minute) * time.Minute)
|
|
|
|
// 计算下一个整点时间
|
|
nextHour := time.Date(
|
|
halfHourLater.Year(),
|
|
halfHourLater.Month(),
|
|
halfHourLater.Day(),
|
|
halfHourLater.Hour(), // 下一个整点小时
|
|
0, // 下一个整点分钟数为0
|
|
0, // 下一个整点秒数为0
|
|
0, // 下一个整点纳秒数为0
|
|
time.Local, // 下一个整点时区
|
|
)
|
|
|
|
// 工作时间范围
|
|
workStart := 9
|
|
workEnd := 19
|
|
var dayHoursMap = make(map[string][]TimeObject)
|
|
var serviceDaysMap = make(map[string]string)
|
|
// 循环生成每半个小时的整点时间列表,直到一周后
|
|
oneWeekLater := currentTime.Add(time.Duration(day) * 24 * time.Hour)
|
|
for _, dayTime := range times {
|
|
serviceDaysMap[dayTime] = dayTime
|
|
}
|
|
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")]
|
|
orderNum, existOrderNum := orderNumMap[nextHour.Format("2006-01-02 15:04")]
|
|
if (nextHour.Hour() >= workStart && nextHour.Hour() < workEnd) && (!existsDay && !existsHour) && !existOrderTime {
|
|
key := nextHour.Format("2006-01-02")
|
|
if _, ok := serviceDaysMap[key]; len(serviceDaysMap) > 0 && !ok {
|
|
//不存在服务配置中返回false
|
|
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
|
|
Time: nextHour.Format("15:04"),
|
|
Y: false,
|
|
})
|
|
continue
|
|
}
|
|
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(time.Duration(minute) * time.Minute) // 下一个整点半时间
|
|
}
|
|
return dayHoursMap
|
|
}
|