You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
5.1 KiB
167 lines
5.1 KiB
package order_preview
|
|
|
|
import (
|
|
"github.com/astaxie/beego"
|
|
"github.com/shopspring/decimal"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/order_preview"
|
|
"recook/internal/model/user"
|
|
goods3 "recook/internal/v2/model/recook/goods"
|
|
)
|
|
|
|
// 分离读取请求 依据一个previewOrderId即可
|
|
|
|
type previewOrderResponse struct {
|
|
*order_preview.Information
|
|
TotalGoodsCount uint `json:"totalGoodsCount"`
|
|
Coupon *order_preview.CouponDetail `json:"coupon"`
|
|
Addr *order_preview.Addr `json:"addr"`
|
|
Brands []BrandManyGoods `json:"brands"`
|
|
CoinStatus CoinDeduction `json:"coinStatus"`
|
|
HasAuth bool `json:"hasAuth"`
|
|
HasCoin bool `json:"hasCoin"`
|
|
}
|
|
|
|
// 一对多
|
|
type BrandManyGoods struct {
|
|
BrandID uint `json:"brandId"`
|
|
BrandName string `json:"brandName"`
|
|
BrandLogoUrl string `json:"brandLogoUrl"`
|
|
BrandExpressTotalAmount decimal.Decimal `json:"brandExpressTotalAmount"`
|
|
BrandGoodsTotalAmount decimal.Decimal `json:"brandGoodsTotalAmount"`
|
|
BrandGoodsTotalCount uint `json:"brandGoodsTotalCount"`
|
|
Goods []order_preview.GoodsDetail `json:"goods"`
|
|
Coupon *order_preview.CouponDetail `json:"coupon"` // 品牌优惠券
|
|
}
|
|
|
|
type CoinDeduction struct {
|
|
Coin decimal.Decimal `json:"coin"`
|
|
IsUseCoin bool `json:"isUseCoin"`
|
|
IsEnable bool `json:"isEnable"`
|
|
}
|
|
|
|
func queryPreviewOrderGroup(previewOrderId uint) *previewOrderResponse {
|
|
var preOrderInfo order_preview.Information
|
|
dbc.DB.First(&preOrderInfo, "id = ?", previewOrderId)
|
|
|
|
var universeCoupon order_preview.CouponDetail
|
|
dbc.DB.First(&universeCoupon, "order_id = ? AND scope = 0", previewOrderId)
|
|
|
|
var addr order_preview.Addr
|
|
dbc.DB.First(&addr, "order_id = ?", previewOrderId)
|
|
|
|
var UserId = preOrderInfo.UserID
|
|
// 获取到所有商品, 内存中进行分组操作
|
|
var goodsList []order_preview.GoodsDetail
|
|
dbc.DB.Find(&goodsList, "order_id = ?", previewOrderId)
|
|
|
|
hasAuth := false
|
|
hasCoin := true
|
|
goodsIds := []uint{}
|
|
recookGoodsInfoModel := &goods3.RecookGoodsInfoModel{}
|
|
for _, detail := range goodsList {
|
|
goodsIds = append(goodsIds, detail.GoodsID)
|
|
if !recookGoodsInfoModel.HasCoin(detail.Storehouse) {
|
|
hasCoin = false
|
|
}
|
|
}
|
|
goodsInfos := goods.Information{}
|
|
dbc.DB.First(&goodsInfos, "id in (?) and has_auth = ?", goodsIds, 1)
|
|
if goodsInfos.ID > 0 {
|
|
hasAuth = true
|
|
}
|
|
|
|
totalGoodsCount := 0
|
|
brandMap := map[uint]uint{}
|
|
for _, v := range goodsList {
|
|
brandMap[v.BrandID] = 1
|
|
totalGoodsCount += int(v.Quantity)
|
|
}
|
|
|
|
brandList := make([]BrandManyGoods, 0, 0)
|
|
for k := range brandMap {
|
|
var brandInfo goods.Brand
|
|
dbc.DB.Select("id, name, logo_url").First(&brandInfo, "id = ?", k)
|
|
|
|
var brandCoupon order_preview.CouponDetail
|
|
dbc.DB.First(&brandCoupon, "order_id = ? AND brand_id = ?", previewOrderId, k)
|
|
|
|
internalGoodsList := make([]order_preview.GoodsDetail, 0, 0)
|
|
|
|
brandExpressTotalFee := decimal.NewFromFloat(0.0)
|
|
brandGoodsTotalAmount := decimal.NewFromFloat(0.0)
|
|
brandGoodsTotalCount := 0
|
|
for i, v := range goodsList {
|
|
if v.BrandID == k {
|
|
internalGoodsList = append(internalGoodsList, goodsList[i])
|
|
brandExpressTotalFee = brandExpressTotalFee.Add(v.ExpressFee)
|
|
brandGoodsTotalAmount = brandGoodsTotalAmount.Add(v.GoodsAmount)
|
|
brandGoodsTotalCount += int(v.Quantity)
|
|
}
|
|
}
|
|
|
|
brandCouponPtr := &order_preview.CouponDetail{}
|
|
if brandCoupon.ID == 0 {
|
|
brandCouponPtr = nil
|
|
} else {
|
|
brandCouponPtr = &brandCoupon
|
|
}
|
|
|
|
brandList = append(brandList, BrandManyGoods{
|
|
BrandID: brandInfo.ID,
|
|
BrandName: brandInfo.Name,
|
|
BrandLogoUrl: brandInfo.LogoURL,
|
|
BrandExpressTotalAmount: brandExpressTotalFee,
|
|
BrandGoodsTotalAmount: brandGoodsTotalAmount,
|
|
BrandGoodsTotalCount: uint(brandGoodsTotalCount),
|
|
Goods: internalGoodsList,
|
|
Coupon: brandCouponPtr,
|
|
})
|
|
}
|
|
|
|
addrPtr := &order_preview.Addr{}
|
|
if len(addr.Province) == 0 {
|
|
addrPtr = nil
|
|
} else {
|
|
addrPtr = &addr
|
|
}
|
|
|
|
universeCouponPtr := &order_preview.CouponDetail{}
|
|
if universeCoupon.ID == 0 {
|
|
universeCouponPtr = nil
|
|
} else {
|
|
universeCouponPtr = &universeCoupon
|
|
}
|
|
|
|
var wallet user.Wallet
|
|
err := dbc.DB.First(&wallet, "user_id = ?", UserId).Error
|
|
if err != nil {
|
|
beego.Warning(err)
|
|
}
|
|
|
|
coinDeduction := CoinDeduction{
|
|
Coin: wallet.Coin,
|
|
}
|
|
coinDeduction.IsUseCoin = true
|
|
if preOrderInfo.CoinTotalAmount.Equal(decimal.NewFromInt(0)) {
|
|
coinDeduction.IsUseCoin = false
|
|
}
|
|
if preOrderInfo.CoinTotalAmount.Add(wallet.Coin).GreaterThan(decimal.NewFromInt(0)) {
|
|
coinDeduction.IsEnable = true
|
|
} else {
|
|
coinDeduction.IsEnable = false
|
|
}
|
|
|
|
return &previewOrderResponse{
|
|
Information: &preOrderInfo,
|
|
TotalGoodsCount: uint(totalGoodsCount),
|
|
Coupon: universeCouponPtr,
|
|
Addr: addrPtr,
|
|
Brands: brandList,
|
|
CoinStatus: coinDeduction,
|
|
HasAuth: hasAuth,
|
|
HasCoin: hasCoin,
|
|
}
|
|
}
|