Compare commits

...

11 Commits

Author SHA1 Message Date
yan.y 4338653110 体重修改 2025-05-21 11:47:16 +08:00
yan.y ee2b52ebf1 体重修改 2025-03-14 14:27:00 +08:00
yan.y 1898623537 预约时间修正 2025-02-18 18:13:25 +08:00
yan.y 0080963f2b 预约时间修正 2025-02-14 14:11:30 +08:00
yan.y 385439997b update 2025-01-22 18:42:11 +08:00
yiyan 7e018a1e68 优惠券问题修复 2025-01-09 22:43:19 +08:00
yan.y 35251458c6 优惠券修正 2025-01-09 15:07:42 +08:00
yan.y 596645cbf1 update 2025-01-04 23:05:32 +08:00
yiyan 54a082f8f1 接口更新 2024-12-29 20:51:14 +08:00
yan.y da28f930a9 Merge remote-tracking branch 'origin/v1' into v1 2024-12-29 12:38:47 +08:00
yiyan 315b9d9050 接口更新 2024-12-21 21:59:20 +08:00
9 changed files with 465 additions and 162 deletions

View File

@ -130,8 +130,9 @@ var ServiceAddrMap map[int64]models.ServiceAddr
var PetGoodsMap map[string][]models.PetGoods
var GoodsMap map[int64]models.Goods
var CarMap map[int]models.ServiceCar
var ReserveMap map[string]models.ReserveTimeFilter
var AddrServiceMap map[int64]models.AddrServiceTime
// var ReserveMap map[string]models.ReserveTimeFilter
// var AddrServiceMap map[int64]models.AddrServiceTime
var CouponsMap map[int]models.Coupons
func DataInit() {
@ -143,7 +144,7 @@ func DataInit() {
PetBaseInfoMapC1[value.Id] = value
}
PetBaseInfoMap = PetBaseInfoMapC1
zap_server.ZAPLOG.Info("dataInit petBastInfoMap : ", zap.Any("petBastInfoMap", PetBaseInfoMap))
//zap_server.ZAPLOG.Info("dataInit petBastInfoMap : ", zap.Any("petBastInfoMap", PetBaseInfoMap))
//--------------------------------------------------服务地址数据---------------------------------------------------------
var serviceAddrList []models.ServiceAddr
database.Instance().Model(&models.ServiceAddr{}).Where("status = 1").Find(&serviceAddrList)
@ -151,8 +152,8 @@ func DataInit() {
for _, value := range serviceAddrList {
ServiceAddrMapC1[value.Id] = value
}
ServiceAddrMap := ServiceAddrMapC1
zap_server.ZAPLOG.Info("dataInit ServiceAddrMap : ", zap.Any("ServiceAddrMap", ServiceAddrMap))
ServiceAddrMap = ServiceAddrMapC1
//zap_server.ZAPLOG.Info("dataInit ServiceAddrMap : ", zap.Any("ServiceAddrMap", ServiceAddrMap))
//--------------------------------------------------宠物商品关联数据---------------------------------------------------------
var petGoodsList []models.PetGoods
database.Instance().Model(&models.PetGoods{}).Where("status = 1").Find(&petGoodsList)
@ -166,7 +167,7 @@ func DataInit() {
}
}
PetGoodsMap = PetGoodsMapC1
zap_server.ZAPLOG.Info("dataInit petGoodsMap : ", zap.Any("petGoodsMap", PetGoodsMap))
//zap_server.ZAPLOG.Info("dataInit petGoodsMap : ", zap.Any("petGoodsMap", PetGoodsMap))
//--------------------------------------------------商品数据---------------------------------------------------------
var goodsList []models.Goods
database.Instance().Model(&models.Goods{}).Where("status = 1").Find(&goodsList)
@ -175,31 +176,31 @@ func DataInit() {
GoodsMapC1[value.Id] = value
}
GoodsMap = GoodsMapC1
zap_server.ZAPLOG.Info("dataInit GoodsMap : ", zap.Any("GoodsMap", GoodsMap))
//zap_server.ZAPLOG.Info("dataInit GoodsMap : ", zap.Any("GoodsMap", GoodsMap))
var carList []models.ServiceCar
database.Instance().Model(&models.ServiceCar{}).Find(&carList)
database.Instance().Model(&models.ServiceCar{}).Where("car_status = 1").Find(&carList)
var CarMapC1 = make(map[int]models.ServiceCar)
for _, value := range carList {
CarMapC1[value.Id] = value
}
CarMap = CarMapC1
zap_server.ZAPLOG.Info("dataInit CarMap : ", zap.Any("CarMap", CarMap))
var timesList []models.ReserveTimeFilter
database.Instance().Model(&models.ReserveTimeFilter{}).Find(&timesList)
var ReserveMapC1 = make(map[string]models.ReserveTimeFilter)
for _, value := range timesList {
ReserveMapC1[value.Content] = value
}
ReserveMap = ReserveMapC1
zap_server.ZAPLOG.Info("dataInit ReserveMap : ", zap.Any("ReserveMap", ReserveMap))
var serviceTimesList []models.AddrServiceTime
database.Instance().Model(&models.AddrServiceTime{}).Find(&serviceTimesList)
var AddrServiceMapC1 = make(map[int64]models.AddrServiceTime)
for _, value := range serviceTimesList {
AddrServiceMapC1[value.ServiceAddrId] = value
}
AddrServiceMap = AddrServiceMapC1
zap_server.ZAPLOG.Info("dataInit AddrServiceMap : ", zap.Any("AddrServiceMap", AddrServiceMap))
//zap_server.ZAPLOG.Info("dataInit CarMap : ", zap.Any("CarMap", CarMap))
//var timesList []models.ReserveTimeFilter
//database.Instance().Model(&models.ReserveTimeFilter{}).Find(&timesList)
//var ReserveMapC1 = make(map[string]models.ReserveTimeFilter)
//for _, value := range timesList {
// ReserveMapC1[value.Content] = value
//}
//ReserveMap = ReserveMapC1
//zap_server.ZAPLOG.Info("dataInit ReserveMap : ", zap.Any("ReserveMap", ReserveMap))
//var serviceTimesList []models.AddrServiceTime
//database.Instance().Model(&models.AddrServiceTime{}).Find(&serviceTimesList)
//var AddrServiceMapC1 = make(map[int64]models.AddrServiceTime)
//for _, value := range serviceTimesList {
// AddrServiceMapC1[value.ServiceAddrId] = value
//}
//AddrServiceMap = AddrServiceMapC1
//zap_server.ZAPLOG.Info("dataInit AddrServiceMap : ", zap.Any("AddrServiceMap", AddrServiceMap))
var couponsList []models.Coupons
database.Instance().Model(&models.Coupons{}).Where("status = 1 and expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Find(&couponsList)
var CouponsMapC1 = make(map[int]models.Coupons)
@ -207,7 +208,7 @@ func DataInit() {
CouponsMapC1[value.Id] = value
}
CouponsMap = CouponsMapC1
zap_server.ZAPLOG.Info("dataInit CouponsMap : ", zap.Any("Coupons", CouponsMap))
//zap_server.ZAPLOG.Info("dataInit CouponsMap : ", zap.Any("Coupons", CouponsMap))
}
func DataCacheJob() {

View File

@ -55,15 +55,15 @@ func (p DefParty) goodsList() web_iris.Party {
}
//先取默认的商品
key := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + "0" + "0"
goodsList := getPetGoodsList(key)
goodsList := GetPetGoodsList(key)
key1 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0"
goodsList = append(goodsList, getPetGoodsList(key1)...)
goodsList = append(goodsList, GetPetGoodsList(key1)...)
key2 := strconv.Itoa(userPetInfo.PetBaseInfo.Id) + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair)
goodsList = append(goodsList, getPetGoodsList(key2)...)
goodsList = append(goodsList, GetPetGoodsList(key2)...)
key3 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + strconv.Itoa(userPetInfo.PetBaseInfo.Hair)
goodsList = append(goodsList, getPetGoodsList(key3)...)
goodsList = append(goodsList, GetPetGoodsList(key3)...)
key4 := "0" + strconv.Itoa(userPetInfo.PetBaseInfo.PetType) + strconv.Itoa(userPetInfo.PetInfo.Weight) + "0"
goodsList = append(goodsList, getPetGoodsList(key4)...)
goodsList = append(goodsList, GetPetGoodsList(key4)...)
var goodsT1 []GoodsDetail
var goodsT2 []GoodsDetail
var goodsT3 []GoodsDetail
@ -110,11 +110,14 @@ func (p DefParty) goodsList() web_iris.Party {
}}
}
func getPetGoodsList(key string) []GoodsDetail {
func GetPetGoodsList(key string) []GoodsDetail {
var goodsList []GoodsDetail
baseGoodsList := PetGoodsMap[key]
for _, value := range baseGoodsList {
goods := GoodsMap[value.GoodsId]
if goods.Id == 0 {
continue
}
goodsDetail := GoodsDetail{
Id: goods.Id,
Name: goods.Name,

View File

@ -15,7 +15,6 @@ import (
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
"strconv"
"strings"
"sync"
"time"
)
@ -58,9 +57,15 @@ func (p DefParty) orderCreatePreCheck() web_iris.Party {
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
var goodsDiscountAmount = 0
var goodsNotDiscountAmount = 0
var projectionServiceTime = 0
for _, value := range orderCreateRequest.PetGoodsInfos {
for _, gid := range value.GoodsIds {
goods := GoodsMap[gid]
if goods.Time != "/" && len(goods.Time) > 0 {
goodsTime, _ := strconv.Atoi(goods.Time)
projectionServiceTime += goodsTime
}
if value.Cid > 0 {
var userCouponsInfo models.UserCoupons
database.Instance().Model(&models.UserCoupons{}).Where("id = ?", value.Cid).Order(clause.OrderByColumn{Column: clause.Column{Name: "update_time"}, Desc: true}).Find(&userCouponsInfo)
@ -78,6 +83,11 @@ func (p DefParty) orderCreatePreCheck() web_iris.Party {
}
}
}
haveReserve := checkOrderServiceTime(orderCreateRequest.ServiceTime, projectionServiceTime)
if haveReserve {
OrderCreateError.DefFail(ctx, orderCreateRequest, "当前订单预估服务时长预计:"+strconv.Itoa(projectionServiceTime)+"分钟,服务时间过长,请更换时间段预约或联系客服协助预约")
return
}
orderCreatePreCheckResponse := OrderCreatePreCheckResponse{
HasAlert: false,
PayType: 0,
@ -87,9 +97,9 @@ func (p DefParty) orderCreatePreCheck() web_iris.Party {
//此处计算享受会员折扣及优惠券折扣的价格
var discountAmount = int(utils.RoundToOneDecimalPlace(float64(goodsNotDiscountAmount)*(discount/100.0))*10) + goodsDiscountAmount
if userInfo.Amount < discountAmount {
orderCreatePreCheckResponse.HasAlert = true
orderCreatePreCheckResponse.AlertMsg = "您的会员余额不够,需要进行线下支付,当前订单将不享受会员折扣优惠价格"
orderCreatePreCheckResponse.PayType = 0
//orderCreatePreCheckResponse.HasAlert = false
//orderCreatePreCheckResponse.AlertMsg = "您的会员余额不够,需要进行线下支付,当前订单将不享受会员折扣优惠价格"
//orderCreatePreCheckResponse.PayType = 0
} else {
orderCreatePreCheckResponse.PayType = 1
}
@ -127,18 +137,6 @@ func (p DefParty) orderCreate() web_iris.Party {
for _, value := range orderCreateRequest.PetGoodsInfos {
database.Instance().Model(&models.UserCoupons{}).Where("id = ? and coupons_status = 1", value.Cid).Find(&userCoupons)
userCouponsList = append(userCouponsList, userCoupons)
if value.Cid > 0 {
if userCoupons.Id == 0 {
CouponsNotExistError.Fail(ctx, nil)
return
}
coupons := CouponsMap[userCoupons.Cid]
if coupons.Id == 0 {
var couponsData *models.Coupons
database.Instance().Model(&models.Coupons{}).Where("id = ?", coupons.Id).Find(&couponsData)
coupons = models.Coupons{Id: couponsData.Id, Discount: couponsData.Discount, GoodsSubType: couponsData.GoodsSubType}
}
}
userPet := GetUserPet(headerBaseInfo.Uid, value.PetId)
var dog = userPet.PetBaseInfo.PetType == 2
@ -204,6 +202,16 @@ func (p DefParty) orderCreate() web_iris.Party {
var coupons models.Coupons
for _, value := range orderCreateRequest.PetGoodsInfos {
//2025-02-06 00:00:00
givenTimestamp := int64(1738771200000)
// 获取当前的系统时间戳(毫秒级)
currentTimestamp := time.Now().UnixMilli()
//时间小于2025-02-06 00:00:00 不使用优惠券
if currentTimestamp < givenTimestamp {
value.Cid = 0
}
//可以参与会员折扣的金额
var needDiscountAmount = 0
//不可参与会员折扣的金额
@ -212,6 +220,11 @@ func (p DefParty) orderCreate() web_iris.Party {
var userCouponsInfo models.UserCoupons
database.Instance().Model(&models.UserCoupons{}).Where("id = ?", value.Cid).Order(clause.OrderByColumn{Column: clause.Column{Name: "update_time"}, Desc: true}).Find(&userCouponsInfo)
coupons = CouponsMap[userCouponsInfo.Cid]
if coupons.Id == 0 {
var couponsData *models.Coupons
database.Instance().Model(&models.Coupons{}).Where("id = ?", userCouponsInfo.Cid).Find(&couponsData)
coupons = models.Coupons{Id: couponsData.Id, Discount: couponsData.Discount, GoodsSubType: couponsData.GoodsSubType}
}
subOrderId := NextId.Generate().String()
var totalAmount = 0
var projectionServiceTime = 0
@ -546,10 +559,7 @@ func GetOrderDetail(orderId string) OrderDetail {
}
func checkOrderServiceTime(serviceTime string, projectionServiceTime int) bool {
/*type orderMainTmp struct {
ServiceTime string
ProjectionServiceTime int
}
serviceTimeD, _ := time.Parse("2006-01-02 15:04", serviceTime)
hour := serviceTimeD.Hour()
if hour == 20 && projectionServiceTime > 2*60 {
@ -559,14 +569,9 @@ func checkOrderServiceTime(serviceTime string, projectionServiceTime int) bool {
return true
}
var orderMainTmpList []orderMainTmp
//服务时间>=预约时间 and 服务时间<=预计结束时间 时间范围内无订单才可预约
database.Instance().Model(&models.OrderMain{}).Where("service_time >= ? and service_time < DATE_FORMAT(DATE_ADD('"+serviceTime+"', INTERVAL "+strconv.Itoa(projectionServiceTime)+" MINUTE), '%Y-%m-%d %H:%i') and order_status != 3 and order_status != 4 and status = 1", serviceTime).Find(&orderMainTmpList)
//时间点订单数量<服务车辆*车辆单次服务数量 才可接受预约
if len(orderMainTmpList) > 0 {
return len(orderMainTmpList) >= len(CarMap)*CarServiceNum
}*/
return false
var orderTempList []utils.OrderTemp
database.Instance().Model(&models.OrderMain{}).Where("service_time >= DATE_SUB(NOW(), INTERVAL 7 DAY) and order_status != 3 and order_status != 4 and status = 1").Find(&orderTempList)
return utils.CheckAvailability(orderTempList, len(CarMap), serviceTime, projectionServiceTime)
}
type OrderListRequest struct {
@ -781,10 +786,6 @@ type OrderServiceTimeRequest struct {
ServiceAddrId int64
}
type OrderServiceTimeResponse struct {
Times map[string][]TimeObject `json:"times"`
}
// 获取订单可预约时间
func (p DefParty) orderServiceTime() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
@ -797,81 +798,9 @@ func (p DefParty) orderServiceTime() web_iris.Party {
ParamError.Fail(ctx, orderServiceTimeRequest)
return
}
type orderMainTmp struct {
ServiceTime string
ProjectionServiceTime int
}
var orderMainTmpList []orderMainTmp
database.Instance().Model(&models.OrderMain{}).Where("service_time >= DATE_SUB(NOW(), INTERVAL 7 DAY) and order_status != 3 and order_status != 4 and status = 1").Find(&orderMainTmpList)
// 执行查询
var orderMainTmp1 []struct {
ServiceTime string
Count int
ProjectionServiceTime int
}
err := database.Instance().Raw(`
SELECT service_time, COUNT(1) AS count, projection_service_time
FROM order_mains
WHERE service_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
AND order_status != 3
AND order_status != 4
AND status = 1
GROUP BY service_time
HAVING count > 0
`).Scan(&orderMainTmp1).Error
if err != nil {
ServerError.Fail(ctx, err)
return
}
var orderTimeMap = make(map[string]string)
var orderTimeNum = make(map[string]int)
var orderTimeCount = make(map[string]int)
var orderProjectionTime = make(map[string]int)
for _, val := range orderMainTmp1 {
orderTimeCount[val.ServiceTime] = val.Count
orderProjectionTime[val.ServiceTime] = val.ProjectionServiceTime
}
for _, value := range orderMainTmpList {
orderTimeMap[value.ServiceTime] = value.ServiceTime
orderTimeNum[value.ServiceTime] = orderTimeNum[value.ServiceTime] + 1
if value.ProjectionServiceTime > 0 {
//计算出服务时间点之后累计延长时间
date, _ := time.Parse("2006-01-02 15:04", value.ServiceTime)
lastDate := date.Add(time.Minute * time.Duration(value.ProjectionServiceTime))
for t := date; t.Before(lastDate); t = t.Add(time.Hour) {
timeStr := t.Format("2006-01-02 15:04")
if timeStr != value.ServiceTime {
orderTimeMap[timeStr] = timeStr
orderTimeNum[timeStr] = orderTimeNum[timeStr] + 1
orderTimeCount[timeStr] = orderTimeCount[timeStr] + 1
}
}
}
}
serviceTime := AddrServiceMap[orderServiceTimeRequest.ServiceAddrId]
times := strings.Split(serviceTime.Times, ",")
carNum := len(CarMap)
daysMap := utils.GetStrDays(7, 60, ReserveMap, orderTimeMap, carNum, orderTimeNum, CarServiceNum, times, orderTimeCount, orderProjectionTime)
var dayHoursMap = make(map[string][]TimeObject)
for day, values := range daysMap {
key := day
for _, value := range values {
if _, ok := dayHoursMap[key]; ok {
dayHoursMap[key] = append(dayHoursMap[key], TimeObject{
Time: value.Time,
Y: value.Y,
})
} else {
dayHoursMap[key] = []TimeObject{{
Time: value.Time,
Y: value.Y,
}}
}
}
}
Success(ctx, headerBaseInfo, OrderServiceTimeResponse{dayHoursMap})
var orderTempList []utils.OrderTemp
database.Instance().Model(&models.OrderMain{}).Where("service_time >= DATE_SUB(NOW(), INTERVAL 7 DAY) and order_status != 3 and order_status != 4 and status = 1").Find(&orderTempList)
Success(ctx, headerBaseInfo, utils.ProcessOrders(orderTempList, len(CarMap), 7))
})
}}
}

View File

@ -4,27 +4,32 @@ import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"io"
"pet-house.com/business/models"
"pet-house.com/business/utils"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
"strconv"
)
var CatWeightMap = map[int]string{
1: "0-1.5kg",
2: "1.5-3kg",
//1: "0-1.5kg",
2: "0-3kg",
3: "3-5kg",
4: "5-8kg",
//5: "8kg以上",
6: "8kg以上",
}
var DogWeightMap = map[int]string{
1: "0-3kg",
1: "0-5kg",
2: "3-6kg",
3: "6-9kg",
3: "5-9kg",
4: "9-13kg",
5: "13-18kg",
6: "18-22kg",
7: "22-30kg",
8: "0-3kg",
}
type UserPetInfo struct {
@ -145,7 +150,7 @@ func (p DefParty) petAddOrEdit() web_iris.Party {
} else {
pet.Id = petAddOrEditRequest.Id
var userPetInfo models.Pet
database.Instance().Model(&models.Pet{}).Where("id = ? and uid = ?", pet.Id, headerBaseInfo.Uid).Find(&userPetInfo)
database.Instance().Model(&models.Pet{}).Where("id = ?", pet.Id).Find(&userPetInfo)
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
if userPetInfo.Id == 0 && userInfo.Role == 0 {
@ -159,6 +164,83 @@ func (p DefParty) petAddOrEdit() web_iris.Party {
PetInfoNotUpdateError.Fail(ctx, petAddOrEditRequest)
return
}
} else {
if userPetInfo.Weight != pet.Weight {
//护理人员修改体重,需要更新对应的服务项目
var orderMainList []models.OrderMain
database.Instance().Model(&models.OrderMain{}).Where("uid = ? and order_status in (1,2)", userPetInfo.Uid).Find(&orderMainList)
for _, orderMain := range orderMainList {
if orderMain.PayStatus == 1 {
continue
}
var orderSubList []models.OrderSub
database.Instance().Model(&models.OrderSub{}).Where("main_order_id = ? ", orderMain.OrderId).Find(&orderSubList)
for orderSubIndex, orderSub := range orderSubList {
var orderDetailList []models.OrderDetail
database.Instance().Model(&models.OrderDetail{}).Where("sub_order_id = ? ", orderSub.OrderId).Find(&orderDetailList)
for orderDetailIndex, orderDetail := range orderDetailList {
var newGoodsDetail GoodsDetail
orderGoods := GoodsMap[orderDetail.GoodsId]
var goodsList []GoodsDetail
petBaseInfo := PetBaseInfoMap[userPetInfo.PetId]
key1 := strconv.Itoa(petBaseInfo.Id) + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + "0"
goodsList = append(goodsList, GetPetGoodsList(key1)...)
key2 := strconv.Itoa(petBaseInfo.Id) + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + strconv.Itoa(petBaseInfo.Hair)
goodsList = append(goodsList, GetPetGoodsList(key2)...)
key3 := "0" + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + strconv.Itoa(petBaseInfo.Hair)
goodsList = append(goodsList, GetPetGoodsList(key3)...)
key4 := "0" + strconv.Itoa(petBaseInfo.PetType) + strconv.Itoa(pet.Weight) + "0"
goodsList = append(goodsList, GetPetGoodsList(key4)...)
for _, goodsDetail := range goodsList {
//取同名称 同类型 同子类型的商品
if goodsDetail.Name == orderGoods.Name && goodsDetail.GoodsType == orderGoods.GoodsType && goodsDetail.GoodsSubType == orderGoods.GoodsSubType {
zap_server.ZAPLOG.Info("更新体重命中的商品", zap.Any("原价", orderGoods.Price), zap.Any("Name", goodsDetail.Name), zap.Any("Price", goodsDetail.Price), zap.Any("GoodsType", goodsDetail.GoodsType), zap.Any("GoodsSubType", goodsDetail.GoodsSubType))
newGoodsDetail = goodsDetail
break
}
}
if newGoodsDetail.Id > 0 {
zap_server.ZAPLOG.Info("订单更新前", zap.Any("orderDetailId", orderDetail.SubOrderId), zap.Any("GoodsId", orderDetail.GoodsId), zap.Any("Amount", orderDetail.Amount), zap.Any("DiscountAmount", orderDetail.DiscountAmount))
//更新订单商品
orderDetail.GoodsId = newGoodsDetail.Id
orderDetail.Amount = newGoodsDetail.Price
orderDetail.DiscountAmount = int(utils.RoundToOneDecimalPlace(float64(orderDetail.Amount)*(float64(orderDetail.Discount)/100.0)) * 10)
zap_server.ZAPLOG.Info("商品订单更新后", zap.Any("orderDetailId", orderDetail.SubOrderId), zap.Any("GoodsId", orderDetail.GoodsId), zap.Any("Amount", orderDetail.Amount), zap.Any("DiscountAmount", orderDetail.DiscountAmount))
database.Instance().Model(&orderDetail).Updates(&orderDetail)
orderDetailList[orderDetailIndex] = orderDetail
}
}
var totalAmount = 0
var goodsOriginAmount = 0
var goodsDiscountAmount = 0
for _, orderDetail := range orderDetailList {
if orderDetail.DiscountAmount > 0 {
goodsDiscountAmount = goodsDiscountAmount + orderDetail.DiscountAmount
} else {
goodsOriginAmount = goodsOriginAmount + orderDetail.Amount
}
totalAmount = totalAmount + orderDetail.Amount
}
zap_server.ZAPLOG.Info("子订单更新前", zap.Any("orderSubId", orderSub.OrderId), zap.Any("goodsOriginAmount", orderSub.GoodsOriginAmount), zap.Any("goodsDiscountAmount", orderSub.GoodsDiscountAmount), zap.Any("totalAmount", orderSub.TotalAmount), zap.Any("PayAmount", orderSub.PayAmount))
orderSub.GoodsOriginAmount = goodsOriginAmount
orderSub.GoodsDiscountAmount = goodsDiscountAmount
orderSub.TotalAmount = totalAmount
orderSub.PayAmount = goodsDiscountAmount + (goodsOriginAmount * 10)
zap_server.ZAPLOG.Info("子订单更新后", zap.Any("orderSubId", orderSub.OrderId), zap.Any("goodsOriginAmount", orderSub.GoodsOriginAmount), zap.Any("goodsDiscountAmount", orderSub.GoodsDiscountAmount), zap.Any("totalAmount", orderSub.TotalAmount), zap.Any("PayAmount", orderSub.PayAmount))
database.Instance().Model(&orderSub).Updates(&orderSub)
orderSubList[orderSubIndex] = orderSub
}
var totalAmount = 0
for _, orderSub := range orderSubList {
totalAmount = totalAmount + orderSub.TotalAmount
}
zap_server.ZAPLOG.Info("主订单更新前", zap.Any("orderMainId", orderMain.OrderId), zap.Any("totalAmount", orderMain.TotalAmount))
orderMain.TotalAmount = totalAmount
zap_server.ZAPLOG.Info("主订单更新后", zap.Any("orderMainId", orderMain.OrderId), zap.Any("totalAmount", orderMain.TotalAmount))
database.Instance().Model(&orderMain).Updates(&orderMain)
}
}
}
updateValues := map[string]interface{}{
"NickName": pet.NickName,

View File

@ -6,9 +6,10 @@ require (
github.com/bwmarrin/snowflake v0.3.0
github.com/dgb8901/go-wechat-miniapp-sdk v1.0.1-release
github.com/kataras/iris/v12 v12.2.10
github.com/kellydunn/golang-geo v0.7.0
github.com/spf13/viper v1.18.2
github.com/wechatpay-apiv3/wechatpay-go v0.2.20
go.uber.org/zap v1.27.0
golang.org/x/net v0.21.0
gorm.io/gorm v1.25.8
pet-house.com/core v0.0.0
)
@ -24,7 +25,6 @@ require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2/v4 v4.0.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
@ -51,11 +51,9 @@ require (
github.com/kataras/sitemap v0.0.6 // indirect
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kylelemons/go-gypsy v1.0.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/lib/pq v1.3.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailgun/raymond/v2 v2.0.48 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
@ -82,13 +80,10 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wechatpay-apiv3/wechatpay-go v0.2.20 // indirect
github.com/yosssi/ace v0.0.5 // indirect
github.com/ziutek/mymysql v1.5.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect

View File

@ -12,6 +12,7 @@ github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8L
github.com/OwnLocal/goes v1.0.0/go.mod h1:8rIFjBGTue3lCU0wplczcUgt9Gxgrkkrw7etMIcn8TM=
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 h1:KkH3I3sJuOLP3TjA/dfr4NAY8bghDwnXiU7cTKxQqo0=
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06/go.mod h1:7erjKLwalezA0k99cWs5L11HWOAPNjdUZ6RxH1BXbbM=
github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw=
github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
@ -48,8 +49,6 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
@ -126,8 +125,6 @@ github.com/kataras/sitemap v0.0.6 h1:w71CRMMKYMJh6LR2wTgnk5hSgjVNB9KL60n5e2KHvLY
github.com/kataras/sitemap v0.0.6/go.mod h1:dW4dOCNs896OR1HmG+dMLdT7JjDk7mYBzoIRwuj5jA4=
github.com/kataras/tunnel v0.0.4 h1:sCAqWuJV7nPzGrlb0os3j49lk2JhILT0rID38NHNLpA=
github.com/kataras/tunnel v0.0.4/go.mod h1:9FkU4LaeifdMWqZu7o20ojmW4B7hdhv2CMLwfnHGpYw=
github.com/kellydunn/golang-geo v0.7.0 h1:A5j0/BvNgGwY6Yb6inXQxzYwlPHc6WVZR+MrarZYNNg=
github.com/kellydunn/golang-geo v0.7.0/go.mod h1:YYlQPJ+DPEzrHx8kT3oPHC/NjyvCCXE+IuKGKdrjrcU=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@ -136,8 +133,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/go-gypsy v1.0.0 h1:7/wQ7A3UL1bnqRMnZ6T8cwCOArfZCxFmb1iTxaOOo1s=
github.com/kylelemons/go-gypsy v1.0.0/go.mod h1:chkXM0zjdpXOiqkCW1XcCHDfjfk14PH2KKkQWxfJUcU=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
@ -147,8 +142,6 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECae
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailgun/raymond/v2 v2.0.48 h1:5dmlB680ZkFG2RN/0lvTAghrSxIESeu9/2aeDqACtjw=
@ -257,8 +250,6 @@ github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FB
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=

View File

@ -219,6 +219,7 @@ type ServiceCar struct {
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"-"` //创建时间
UpdateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP" json:"-"` //更新时间
ServiceAddr ServiceAddr `gorm:"-" json:"serviceAddr"` //当前服务区域
CarStatus int `gorm:"not null"` //车辆状态 1在线 2下线
}
// CarOrder 车辆订单

125
business/utils/Test1.go Normal file
View File

@ -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))
}
*/

View File

@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/kataras/iris/v12/context"
"io"
"math"
@ -134,3 +135,178 @@ 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
}
// 获取当前时间
currentTime := time.Now()
// 指定时区,例如 "Asia/Shanghai"
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("Error loading location:", err)
continue
}
// 如果当前时间 >= 当前时间段的开始时间,则直接标记为占用
slotTime, err := time.ParseInLocation("2006-01-02 15:04:05", slot, location)
if err != nil {
fmt.Println("Error parsing time slot:", err)
continue
}
// 如果当前时间大于等于这个时间段的开始时间,则表示该时间段已经占用
if currentTime.UnixMilli() >= slotTime.UnixMilli() {
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
}