新增逻辑
This commit is contained in:
parent
74ae25bcd0
commit
d002f47478
|
|
@ -1,9 +1,59 @@
|
|||
package api
|
||||
|
||||
import "pet-house.com/business/models"
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"io"
|
||||
"pet-house.com/business/models"
|
||||
"pet-house.com/core/server/database"
|
||||
"pet-house.com/core/server/web/web_iris"
|
||||
)
|
||||
|
||||
var CarServiceNum = 2
|
||||
var CarServiceNum = 1
|
||||
|
||||
func GetCarInfo(carId int) models.ServiceCar {
|
||||
return CarMap[carId]
|
||||
}
|
||||
|
||||
type CarServiceOrderListRequest struct {
|
||||
Status int //订单状态 0所有 1待服务 2服务中 3已完成
|
||||
PageNo int //页码
|
||||
PageSize int //数据数量
|
||||
|
||||
}
|
||||
|
||||
type CarServiceOrderListResponse struct {
|
||||
OrderDetails []OrderDetail `json:"orderDetails"`
|
||||
PageNo int `json:"pageNo"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
// carServiceOrderList 车辆订单列表
|
||||
func (p DefParty) carServiceOrderList() web_iris.Party {
|
||||
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(CarBase+"/carServiceOrderList", func(ctx *context.Context) {
|
||||
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
||||
body, _ := io.ReadAll(ctx.Request().Body)
|
||||
var carServiceOrderListRequest CarServiceOrderListRequest
|
||||
json.Unmarshal(body, &carServiceOrderListRequest)
|
||||
var serviceCarUser models.ServiceCarUser
|
||||
database.Instance().Model(&models.ServiceCarUser{}).Where("uid = ?", headerBaseInfo.Uid).Find(&serviceCarUser)
|
||||
if serviceCarUser.Id == 0 {
|
||||
CarNotExistError.Fail(ctx, headerBaseInfo)
|
||||
return
|
||||
}
|
||||
var orderMains []models.OrderMain
|
||||
if carServiceOrderListRequest.Status == 0 {
|
||||
database.Instance().Model(&models.OrderMain{}).Joins("JOIN car_orders ON order_mains.order_id = car_orders.order_id").Where("car_orders.car_id = ? AND order_mains.status > 0", serviceCarUser.CarId).Offset((carServiceOrderListRequest.PageNo - 1) * carServiceOrderListRequest.PageSize).Limit(carServiceOrderListRequest.PageSize).Find(&orderMains)
|
||||
} else {
|
||||
database.Instance().Model(&models.OrderMain{}).Joins("JOIN car_orders ON order_mains.order_id = car_orders.order_id").Where("car_orders.car_id = ? AND order_mains.status = ?", serviceCarUser.CarId, carServiceOrderListRequest.Status).Offset((carServiceOrderListRequest.PageNo - 1) * carServiceOrderListRequest.PageSize).Limit(carServiceOrderListRequest.PageSize).Find(&orderMains)
|
||||
}
|
||||
var orderDetails []OrderDetail
|
||||
for _, value := range orderMains {
|
||||
orderDetails = append(orderDetails, GetOrderDetail(value.OrderId))
|
||||
}
|
||||
Success(ctx, carServiceOrderListRequest, CarServiceOrderListResponse{orderDetails, carServiceOrderListRequest.PageNo + 1, carServiceOrderListRequest.PageSize})
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ var (
|
|||
NotInServiceExistError = Error{Code: 209, Msg: "不在服务范围"}
|
||||
OrderCreateError = Error{Code: 210, Msg: "订单创建失败"}
|
||||
LoginError = Error{Code: 211, Msg: "授权登录失败"}
|
||||
OrderError = Error{Code: 212, Msg: "当前订单无法修改"}
|
||||
CarNotExistError = Error{Code: 213, Msg: "当前用户未关联车辆"}
|
||||
OrderExistError = Error{Code: 214, Msg: "订单不存在"}
|
||||
)
|
||||
|
||||
func Success(ctx *context.Context, request any, data any) {
|
||||
|
|
|
|||
|
|
@ -7,25 +7,35 @@ var GoodsBase = "/goods"
|
|||
var OrderBase = "/order"
|
||||
var ServiceBase = "/service"
|
||||
var PetBase = "/pet"
|
||||
var CarBase = "/car"
|
||||
|
||||
func (p DefParty) RegisterList() []web_iris.Party {
|
||||
ps := []web_iris.Party{
|
||||
//用户
|
||||
p.login(),
|
||||
p.getUserInfo(),
|
||||
//商品
|
||||
p.goodsList(),
|
||||
p.goodsDetail(),
|
||||
//订单
|
||||
p.orderCreate(),
|
||||
p.orderServiceTime(),
|
||||
p.orderList(),
|
||||
p.orderEdit(),
|
||||
p.orderDetail(),
|
||||
p.orderPay(),
|
||||
//宠物
|
||||
p.petList(),
|
||||
p.petInfo(),
|
||||
p.petTypeList(),
|
||||
p.petAddOrEdit(),
|
||||
p.delPet(),
|
||||
//服务地址
|
||||
p.serviceAddrList(),
|
||||
p.serviceAddOrEdit(),
|
||||
p.serviceAreaAddrList(),
|
||||
//车辆
|
||||
p.carServiceOrderList(),
|
||||
p.index(),
|
||||
}
|
||||
return ps
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"pet-house.com/core/server/database"
|
||||
"pet-house.com/core/server/web/web_iris"
|
||||
"pet-house.com/core/server/zap_server"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -29,6 +30,8 @@ type OrderCreateResponse struct {
|
|||
OrderDetail OrderDetail `json:"orderDetail"`
|
||||
}
|
||||
|
||||
var orderLock sync.Mutex
|
||||
|
||||
// 创建
|
||||
func (p DefParty) orderCreate() web_iris.Party {
|
||||
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
||||
|
|
@ -48,11 +51,17 @@ func (p DefParty) orderCreate() web_iris.Party {
|
|||
ServiceAddrNotExistError.Fail(ctx, nil)
|
||||
return
|
||||
}
|
||||
haveReserve := checkOrderServiceTime(orderCreateRequest.ServiceTime)
|
||||
if haveReserve {
|
||||
OrderCreateError.DefFail(ctx, orderCreateRequest, "当前时间点不可预约")
|
||||
return
|
||||
}
|
||||
orderLock.Lock()
|
||||
orderId := NextId.Generate().String()
|
||||
orderMain := models.OrderMain{
|
||||
OrderId: orderId,
|
||||
Uid: headerBaseInfo.Uid,
|
||||
Status: 1,
|
||||
Status: 0,
|
||||
ServiceTime: orderCreateRequest.ServiceTime,
|
||||
ServiceAddrId: orderCreateRequest.ServiceAddrId,
|
||||
ServiceRemark: "",
|
||||
|
|
@ -85,48 +94,78 @@ func (p DefParty) orderCreate() web_iris.Party {
|
|||
db2 := tx.Model(&models.OrderMain{}).Create(&orderMain)
|
||||
if db.Error != nil || db1.Error != nil || db2.Error != nil {
|
||||
tx.Callback()
|
||||
orderLock.Unlock()
|
||||
zap_server.ZAPLOG.Error("订单插入失败", zap.Any("db", db.Error), zap.Any("db1", db1.Error), zap.Any("db2", db2.Error))
|
||||
OrderCreateError.Fail(ctx, orderCreateRequest)
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
var findUserServiceAddr models.UserServiceAddr
|
||||
database.Instance().Model(&models.UserServiceAddr{}).Where("id = ? and uid = ?", orderMain.ServiceAddrId, headerBaseInfo.Uid).Find(&findUserServiceAddr)
|
||||
orderDetail := OrderDetail{
|
||||
OrderId: orderMain.OrderId,
|
||||
Status: orderMain.Status,
|
||||
ServiceTime: orderMain.ServiceTime,
|
||||
ServiceAddr: findUserServiceAddr,
|
||||
ServiceRemark: orderMain.ServiceRemark,
|
||||
CreateTime: orderMain.CreateTime.String(),
|
||||
}
|
||||
var subOrderList []SubOrder
|
||||
for _, orderSub := range orderSubList {
|
||||
var orderDetailList []models.OrderDetail
|
||||
database.Instance().Model(&models.OrderDetail{}).Where("sub_order_id = ? ", orderSub.OrderId).Find(&orderDetailList)
|
||||
var goods []models.Goods
|
||||
for _, orderDetail := range orderDetailList {
|
||||
good := GoodsMap[orderDetail.GoodsId]
|
||||
goods = append(goods, good)
|
||||
}
|
||||
orderSub := SubOrder{
|
||||
OrderId: orderSub.OrderId,
|
||||
Status: orderSub.Status,
|
||||
UserPetInfo: GetUserPet(headerBaseInfo.Uid, orderSub.PetId),
|
||||
TotalAmount: orderSub.TotalAmount,
|
||||
PayAmount: orderSub.PayAmount,
|
||||
Goods: goods,
|
||||
}
|
||||
subOrderList = append(subOrderList, orderSub)
|
||||
}
|
||||
orderDetail.CreateTime = time.DateTime
|
||||
orderDetail.SubOrderList = subOrderList
|
||||
Success(ctx, orderCreateRequest, OrderCreateResponse{orderDetail})
|
||||
orderLock.Unlock()
|
||||
Success(ctx, orderCreateRequest, OrderCreateResponse{GetOrderDetail(orderMain.OrderId)})
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
func GetOrderDetail(orderId string) OrderDetail {
|
||||
var orderMain models.OrderMain
|
||||
database.Instance().Model(&models.OrderMain{}).Where("order_id = ?", orderId).Find(&orderMain)
|
||||
var findUserServiceAddr models.UserServiceAddr
|
||||
database.Instance().Model(&models.UserServiceAddr{}).Where("id = ?", orderMain.ServiceAddrId).Find(&findUserServiceAddr)
|
||||
orderDetail := OrderDetail{
|
||||
OrderId: orderMain.OrderId,
|
||||
Status: orderMain.Status,
|
||||
ServiceTime: orderMain.ServiceTime,
|
||||
ServiceAddr: findUserServiceAddr,
|
||||
ServiceRemark: orderMain.ServiceRemark,
|
||||
CreateTime: orderMain.CreateTime.String(),
|
||||
}
|
||||
var subOrderList []SubOrder
|
||||
var orderSubList []models.OrderSub
|
||||
database.Instance().Model(&models.OrderSub{}).Where("main_order_id = ? ", orderMain.OrderId).Find(&orderSubList)
|
||||
for _, orderSub := range orderSubList {
|
||||
var orderDetailList []models.OrderDetail
|
||||
database.Instance().Model(&models.OrderDetail{}).Where("sub_order_id = ? ", orderSub.OrderId).Find(&orderDetailList)
|
||||
var goods []models.Goods
|
||||
for _, orderDetail := range orderDetailList {
|
||||
good := GoodsMap[orderDetail.GoodsId]
|
||||
goods = append(goods, good)
|
||||
}
|
||||
orderSub := SubOrder{
|
||||
OrderId: orderSub.OrderId,
|
||||
Status: orderSub.Status,
|
||||
UserPetInfo: GetUserPet(orderMain.Uid, orderSub.PetId),
|
||||
TotalAmount: orderSub.TotalAmount,
|
||||
PayAmount: orderSub.PayAmount,
|
||||
Goods: goods,
|
||||
}
|
||||
subOrderList = append(subOrderList, orderSub)
|
||||
}
|
||||
var carOrder models.CarOrder
|
||||
database.Instance().Model(&models.CarOrder{}).Where("order_id = ?", orderMain.OrderId).Find(&carOrder)
|
||||
if carOrder.Id > 0 {
|
||||
car := CarMap[carOrder.CarId]
|
||||
orderDetail.ServiceCar = &car
|
||||
} else {
|
||||
orderDetail.ServiceCar = &models.ServiceCar{}
|
||||
}
|
||||
orderDetail.CreateTime = time.DateTime
|
||||
orderDetail.SubOrderList = subOrderList
|
||||
return orderDetail
|
||||
}
|
||||
|
||||
func checkOrderServiceTime(serviceTime string) bool {
|
||||
type orderMainTmp struct {
|
||||
ServiceTime string
|
||||
}
|
||||
var orderMainTmpList []orderMainTmp
|
||||
database.Instance().Model(&models.OrderMain{}).Where("service_time = ?", serviceTime).Find(&orderMainTmpList)
|
||||
//时间点订单数量<服务车辆*车辆单次服务数量 才可接受预约
|
||||
if len(orderMainTmpList) > 0 {
|
||||
return len(orderMainTmpList) >= len(CarMap)*CarServiceNum
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type OrderListRequest struct {
|
||||
Status int //订单状态 0所有 1待服务 2服务中 3已完成 4已取消
|
||||
PageNo int //页码
|
||||
|
|
@ -135,7 +174,7 @@ type OrderListRequest struct {
|
|||
|
||||
type SubOrder struct {
|
||||
OrderId string `json:"orderId"` //子订单ID
|
||||
Status int `json:"status"` //子订单状态 1待服务 2服务中 3已完成 4已取消
|
||||
Status int `json:"status"` //子订单状态 0待派单 1待服务 2服务中 3已完成 4已取消
|
||||
UserPetInfo UserPetInfo `json:"userPetInfo"` //用户宠物信息
|
||||
TotalAmount int32 `json:"totalAmount"` //总金额 单位:分
|
||||
PayAmount int32 `json:"payAmount"` //实际支付金额 单位:分
|
||||
|
|
@ -144,18 +183,19 @@ type SubOrder struct {
|
|||
|
||||
type OrderDetail struct {
|
||||
OrderId string `json:"orderId"` //主订单号
|
||||
Status int `json:"status"` //主订单状态 1待服务 2服务中 3已完成
|
||||
Status int `json:"status"` //主订单状态 0待派单 1待服务 2服务中 3已完成 4已取消
|
||||
ServiceTime string `json:"serviceTime"` //服务时间
|
||||
ServiceAddr models.UserServiceAddr `json:"serviceAddr"` //服务地址信息
|
||||
ServiceRemark string `json:"serviceRemark"` //服务备注
|
||||
CreateTime string `json:"createTime"` //创建时间
|
||||
SubOrderList []SubOrder `json:"subOrderList"` //子订单列表
|
||||
ServiceCar *models.ServiceCar `json:"serviceCar"` //服务车辆 只有主订单状态为 1 2 3的时候才会存在
|
||||
}
|
||||
|
||||
type OrderListResponse struct {
|
||||
OrderDetail []OrderDetail `json:"orderDetail"` //订单列表
|
||||
PageNo int `json:"pageNo"` //页码
|
||||
PageSize int `json:"pageSize"` //数据数量
|
||||
OrderDetails []OrderDetail `json:"orderDetails"` //订单列表
|
||||
PageNo int `json:"pageNo"` //页码
|
||||
PageSize int `json:"pageSize"` //数据数量
|
||||
}
|
||||
|
||||
// 订单列表
|
||||
|
|
@ -213,9 +253,9 @@ func (p DefParty) orderList() web_iris.Party {
|
|||
orderDetails = append(orderDetails, orderListResponse)
|
||||
}
|
||||
var orderListResponse = OrderListResponse{
|
||||
OrderDetail: orderDetails,
|
||||
PageNo: orderListRequest.PageNo,
|
||||
PageSize: orderListRequest.PageSize,
|
||||
OrderDetails: orderDetails,
|
||||
PageNo: orderListRequest.PageNo + 1,
|
||||
PageSize: orderListRequest.PageSize,
|
||||
}
|
||||
Success(ctx, orderListRequest, orderListResponse)
|
||||
})
|
||||
|
|
@ -248,7 +288,7 @@ func (p DefParty) orderServiceTime() web_iris.Party {
|
|||
orderTimeNum[value.ServiceTime] = orderTimeNum[value.ServiceTime] + 1
|
||||
}
|
||||
carNum := len(CarMap)
|
||||
daysMap := utils.GetStrDays(7, 30, ReserveMap, orderTimeMap, carNum, orderTimeNum, 2)
|
||||
daysMap := utils.GetStrDays(7, 60, ReserveMap, orderTimeMap, carNum, orderTimeNum, CarServiceNum)
|
||||
var dayHoursMap = make(map[string][]TimeObject)
|
||||
for day, values := range daysMap {
|
||||
key := day
|
||||
|
|
@ -272,6 +312,62 @@ func (p DefParty) orderServiceTime() web_iris.Party {
|
|||
}}
|
||||
}
|
||||
|
||||
type OrderEditRequest struct {
|
||||
OrderId string //订单ID
|
||||
Status int //订单状态 4取消
|
||||
}
|
||||
|
||||
// 修改订单
|
||||
func (p DefParty) orderEdit() web_iris.Party {
|
||||
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(OrderBase+"/orderEdit", func(ctx *context.Context) {
|
||||
headerBaseInfo := GetHeaderBaseInfo(ctx)
|
||||
body, _ := io.ReadAll(ctx.Request().Body)
|
||||
var orderEditRequest OrderEditRequest
|
||||
json.Unmarshal(body, &orderEditRequest)
|
||||
var orderMain models.OrderMain
|
||||
var orderSub models.OrderSub
|
||||
database.Instance().Model(&models.OrderSub{}).Where("order_id = ?", orderEditRequest.OrderId).Find(&orderSub)
|
||||
database.Instance().Model(&models.OrderMain{}).Where("order_id = ?", orderSub.MainOrderId).Find(&orderMain)
|
||||
if orderMain.Uid != headerBaseInfo.Uid {
|
||||
OrderError.Fail(ctx, orderEditRequest)
|
||||
return
|
||||
}
|
||||
if orderSub.Status > 0 || orderEditRequest.Status == 0 {
|
||||
OrderError.Fail(ctx, orderEditRequest)
|
||||
return
|
||||
}
|
||||
orderSub.Status = orderEditRequest.Status
|
||||
updateValues := map[string]interface{}{
|
||||
"Status": orderSub.Status,
|
||||
}
|
||||
database.Instance().Model(&orderSub).Updates(&updateValues)
|
||||
Success(ctx, orderEditRequest, GetOrderDetail(orderMain.OrderId))
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
type OrderDetailRequest struct {
|
||||
OrderId string
|
||||
}
|
||||
|
||||
// 订单详情
|
||||
func (p DefParty) orderDetail() web_iris.Party {
|
||||
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
||||
index.Post(OrderBase+"/orderDetail", func(ctx *context.Context) {
|
||||
body, _ := io.ReadAll(ctx.Request().Body)
|
||||
var orderDetailRequest OrderDetailRequest
|
||||
json.Unmarshal(body, &orderDetailRequest)
|
||||
orderDetail := GetOrderDetail(orderDetailRequest.OrderId)
|
||||
if len(orderDetail.OrderId) == 0 {
|
||||
OrderExistError.Fail(ctx, orderDetailRequest)
|
||||
return
|
||||
}
|
||||
Success(ctx, orderDetailRequest, orderDetail)
|
||||
})
|
||||
}}
|
||||
}
|
||||
|
||||
// 支付
|
||||
func (p DefParty) orderPay() web_iris.Party {
|
||||
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ type CarOrder struct {
|
|||
type ServiceCarUser struct {
|
||||
Id int `gorm:"primaryKey;autoIncrement" json:"id"` //id
|
||||
CarId int `gorm:"index;not null" json:"carId"` //车ID
|
||||
Uid int64 `gorm:"index;not null" json:"uid"` //用户ID
|
||||
Uid int64 `gorm:"index;unique;not null" json:"uid"` //用户ID
|
||||
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"-"` //创建时间
|
||||
UpdateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP" json:"-"` //更新时间
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@ type TimeObject struct {
|
|||
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 {
|
||||
func GetStrDays(day int, minute 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)
|
||||
halfHourLater := currentTime.Add(time.Duration(minute) * time.Minute)
|
||||
|
||||
// 计算下一个整点时间
|
||||
nextHour := time.Date(
|
||||
halfHourLater.Year(),
|
||||
halfHourLater.Month(),
|
||||
halfHourLater.Day(),
|
||||
halfHourLater.Hour()+1, // 下一个整点小时
|
||||
0, // 下一个整点分钟数为0
|
||||
0, // 下一个整点秒数为0
|
||||
0, // 下一个整点纳秒数为0
|
||||
time.Local, // 下一个整点时区
|
||||
halfHourLater.Hour(), // 下一个整点小时
|
||||
0, // 下一个整点分钟数为0
|
||||
0, // 下一个整点秒数为0
|
||||
0, // 下一个整点纳秒数为0
|
||||
time.Local, // 下一个整点时区
|
||||
)
|
||||
|
||||
// 工作时间范围
|
||||
|
|
@ -93,7 +93,7 @@ func GetStrDays(day int, hour int, reserveMap map[string]models.ReserveTimeFilte
|
|||
}
|
||||
|
||||
}
|
||||
nextHour = nextHour.Add(30 * time.Minute) // 下一个整点半时间
|
||||
nextHour = nextHour.Add(time.Duration(minute) * time.Minute) // 下一个整点半时间
|
||||
}
|
||||
return dayHoursMap
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue