pet-house/business/api/order.go

127 lines
3.9 KiB
Go

package api
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/core/server/database"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
)
type PetGoodsInfo struct {
PetId int64
GoodsIds []int64
}
type OrderCreateRequest struct {
PetGoodsInfos []PetGoodsInfo //宠物商品信息列表
ServiceTime string //预约时间 yyyy-MM-dd HH:mm
ServiceAddrId int64 //预约服务地址
}
// 创建
func (p DefParty) orderCreate() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderCreate", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var orderCreateRequest OrderCreateRequest
json.Unmarshal(body, &orderCreateRequest)
var userServiceAddr models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("uid = ? and id = ?", headerBaseInfo.Uid, orderCreateRequest.ServiceAddrId).Find(&userServiceAddr)
if userServiceAddr.Id == 0 {
UserServiceAddrNotExistError.Fail(ctx, nil)
return
}
serviceAddr := ServiceAddrMap[userServiceAddr.AreaId]
if serviceAddr.Id == 0 {
ServiceAddrNotExistError.Fail(ctx, nil)
return
}
orderId := NextId.Generate().String()
orderMain := models.OrderMain{
OrderId: orderId,
Uid: headerBaseInfo.Uid,
Status: 1,
ServiceTime: orderCreateRequest.ServiceTime,
ServiceAddrId: orderCreateRequest.ServiceAddrId,
ServiceRemark: "",
}
var orderSubList []models.OrderSub
var orderSubDetailList []models.OrderDetail
for _, value := range orderCreateRequest.PetGoodsInfos {
subOrderId := NextId.Generate().String()
var totalAmount int32 = 0
for _, value := range value.GoodsIds {
goods := GoodsMap[value]
totalAmount = totalAmount + goods.Price
orderSubDetailList = append(orderSubDetailList, models.OrderDetail{SubOrderId: subOrderId, GoodsId: value, Amount: goods.Price})
}
orderSub := models.OrderSub{
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
Status: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: 0,
}
orderSubList = append(orderSubList, orderSub)
}
tx := database.Instance().Begin()
db := tx.Model(&models.OrderDetail{}).CreateInBatches(&orderSubDetailList, len(orderSubDetailList))
db1 := tx.Model(&models.OrderSub{}).CreateInBatches(&orderSubList, len(orderSubList))
db2 := tx.Model(&models.OrderMain{}).Create(&orderMain)
if db.Error != nil || db1.Error != nil || db2.Error != nil {
tx.Callback()
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()
Success(ctx, orderCreateRequest, nil)
})
}}
}
type OrderListRequest struct {
Status int //订单状态 0所有 1待服务 2服务中 3已完成 4已取消
PageNo int //页码
PageSize int //数据数量
}
type OrderListResponse struct {
}
// 订单列表
func (p DefParty) orderList() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderList", func(ctx *context.Context) {
})
}}
}
// 获取订单可预约时间
func (p DefParty) orderServiceTime() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderServiceTime", func(ctx *context.Context) {
})
}}
}
// 支付
func (p DefParty) orderPay() web_iris.Party {
return web_iris.Party{Perfix: p.Perfix, PartyFunc: func(index iris.Party) {
index.Post(OrderBase+"/orderPay", func(ctx *context.Context) {
})
}}
}