pet-house/business/utils/commonUtil.go

132 lines
4.2 KiB
Go

package utils
import (
"encoding/json"
"github.com/kataras/iris/v12/context"
"io"
"math"
"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, orderTimeCount map[string]int) map[string][]TimeObject {
// 获取当前时间
currentTime := time.Now()
// 计算半小时后的时间
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 := 22
var dayHoursMap = make(map[string][]TimeObject)
var serviceDaysMap = make(map[string]string)
// 循环生成每半个小时的整点时间列表,直到一周后
oneWeekLater := currentTime.Add(time.Duration(day) * 24 * time.Hour)
oneWeekLater = time.Date(oneWeekLater.Year(), oneWeekLater.Month(), oneWeekLater.Day(), 23, 0, 0, 0, oneWeekLater.Location())
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")
key1 := nextHour.Format("2006-01-02 15:04")
if _, ok := serviceDaysMap[key1]; len(serviceDaysMap) > 0 && !ok {
//不存在服务配置中返回false
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
Time: nextHour.Format("15:04"),
Y: false,
})
nextHour = nextHour.Add(time.Duration(minute) * time.Minute)
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")
key1 := nextHour.Format("2006-01-02 15:04")
//当前订单数小于车辆数 可接单 时间范围可能会超出到11点
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 {
//查看当前时间对应的订单数量
orderCount := orderTimeCount[key1]
//预约时间对应的订单数<车辆数量 当前时间可以预约
if orderCount < carNum*carServiceNum {
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
Time: nextHour.Format("15:04"),
Y: true,
})
} else {
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
}
func RoundToOneDecimalPlace(value float64) float64 {
return math.Round(value*10) / 10
}