优惠券相关逻辑

This commit is contained in:
yan.y
2024-08-12 13:37:43 +08:00
parent e2fb325452
commit 3cd4e4b335
11 changed files with 369 additions and 84 deletions
+4 -3
View File
@@ -15,9 +15,10 @@ import (
)
type LoginRequest struct {
Code string
NickName string
HeadImgUrl string
Code string
NickName string
HeadImgUrl string
InviteUserId int64
}
type LoginResponse struct {
+12
View File
@@ -172,6 +172,18 @@ func (p DefParty) carServiceProcess() web_iris.Party {
}
database.Instance().Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
}
} else {
userAmountRecord := models.UserAmountRecord{
UserId: orderUserInfo.Id,
Type: 3,
OriginAmount: userOriginAmount,
ProAmount: p1,
CurrAmount: orderUserInfo.Amount,
OrderId: mainOrder.OrderId,
ProScene: "修改订单金额",
Status: 1,
}
database.Instance().Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
}
subOrderInfo.PayTime = time.Now()
subOrderInfo.PayStatus = 1
+12
View File
@@ -42,6 +42,7 @@ var (
ParamError = Error{Code: 201, Msg: "参数错误"}
IllegalError = Error{Code: 202, Msg: "非法请求"}
UserError = Error{Code: 203, Msg: "用户错误"}
ServerError = Error{Code: 203, Msg: "服务器异常"}
TokenError = Error{Code: 204, Msg: "Token失效,请重新登录"}
UserNotExistError = Error{Code: 205, Msg: "用户不存在"}
PetNotExistError = Error{Code: 206, Msg: "用户宠物不存在"}
@@ -57,6 +58,8 @@ var (
PetInfoNotDelError = Error{Code: 215, Msg: "当前宠物存在待服务订单,无法删除"}
PetInfoNotUpdateError = Error{Code: 216, Msg: "当前宠物存在待服务订单,无法修改"}
OrderUpdateBalanceNotEnough = Error{Code: 217, Msg: "当前余额不足"}
CouponsNotExistError = Error{Code: 218, Msg: "优惠券不存在"}
UserCouponsNExistError = Error{Code: 219, Msg: "已领取同类型优惠券"}
)
func Success(ctx *context.Context, request any, data any) {
@@ -125,6 +128,7 @@ 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 CouponsMap map[int]models.Coupons
func DataInit() {
//--------------------------------------------------宠物基础信息数据---------------------------------------------------------
@@ -193,6 +197,14 @@ func DataInit() {
}
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)
for _, value := range couponsList {
CouponsMapC1[value.Id] = value
}
CouponsMap = CouponsMapC1
zap_server.ZAPLOG.Info("dataInit CouponsMap : ", zap.Any("Coupons", CouponsMap))
}
func DataCacheJob() {
+5
View File
@@ -10,6 +10,7 @@ var PetBase = "/pet"
var CarBase = "/car"
var Admin = "/admin"
var System = "/system"
var CouponsBase = "/coupons"
func (p DefParty) RegisterList() []web_iris.Party {
ps := []web_iris.Party{
@@ -46,6 +47,10 @@ func (p DefParty) RegisterList() []web_iris.Party {
p.carServiceOrderList(),
p.carServiceProcess(),
p.index(),
//优惠券
p.getCouponsList(),
p.getUserCouponsList(),
p.drawCoupons(),
//系统
p.systemConfigInfo(),
p.systemBanners(),
+120
View File
@@ -0,0 +1,120 @@
package api
import (
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"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"
"strings"
"time"
)
func (p DefParty) getCouponsList() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(CouponsBase+"/getCouponsList", func(ctx *context.Context) {
headerInfo := GetHeaderBaseInfo(ctx)
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
if userInfo == nil || userInfo.Id == 0 {
UserNotExistError.Fail(ctx, nil)
return
}
var coupons []models.Coupons
database.Instance().Model(&models.Coupons{}).Where("status = 1 and expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Find(&coupons)
for index, coupon := range coupons {
coupons[index].PeriodInfo = strings.Split(coupon.Period, ",")
}
Success(ctx, headerInfo, coupons)
})
}}
}
type GetUserCouponsListRequest struct {
Status int
}
func (p DefParty) getUserCouponsList() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(CouponsBase+"/getUserCouponsList", func(ctx *context.Context) {
headerInfo := GetHeaderBaseInfo(ctx)
var getUserCouponsListRequest GetUserCouponsListRequest
body, _ := io.ReadAll(ctx.Request().Body)
json.Unmarshal(body, &getUserCouponsListRequest)
if getUserCouponsListRequest.Status == 0 {
ParamError.Fail(ctx, nil)
return
}
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
if userInfo == nil || userInfo.Id == 0 {
UserNotExistError.Fail(ctx, nil)
return
}
var coupons []models.Coupons
database.Instance().Model(&models.Coupons{}).Joins("JOIN user_coupons on coupons.id = user_coupons.cid and coupons.expire_time > ?", time.Now().UnixNano()/int64(time.Millisecond)).Where(" user_coupons.coupons_status = ? and user_coupons.uid = ?", getUserCouponsListRequest.Status, headerInfo.Uid).Find(&coupons)
for index, coupon := range coupons {
coupons[index].PeriodInfo = strings.Split(coupon.Period, ",")
}
Success(ctx, headerInfo, coupons)
})
}}
}
type DrawCouponsRequest struct {
Cid int
}
func (p DefParty) drawCoupons() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(CouponsBase+"/drawCoupons", func(ctx *context.Context) {
headerInfo := GetHeaderBaseInfo(ctx)
var drawCouponsRequest DrawCouponsRequest
body, _ := io.ReadAll(ctx.Request().Body)
json.Unmarshal(body, &drawCouponsRequest)
if drawCouponsRequest.Cid == 0 {
ParamError.Fail(ctx, nil)
return
}
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerInfo.Uid).Find(&userInfo)
if userInfo == nil || userInfo.Id == 0 {
UserNotExistError.Fail(ctx, nil)
return
}
var coupons *models.Coupons
database.Instance().Model(&models.Coupons{}).Where("id = ?", drawCouponsRequest.Cid).Find(&coupons)
if coupons.Id == 0 {
CouponsNotExistError.Fail(ctx, nil)
return
}
lockMap := &utils.KeyedMutex{}
lockMap.Lock(headerInfo.Token)
var userCoupons *models.UserCoupons
database.Instance().Model(&models.UserCoupons{}).Where("uid = ? and cid = ?", headerInfo.Uid, drawCouponsRequest.Cid).Find(&userCoupons)
if userCoupons.Id > 0 {
UserCouponsNExistError.Fail(ctx, nil)
lockMap.Unlock(headerInfo.Token)
return
}
newUserCoupons := models.UserCoupons{
Uid: headerInfo.Uid,
Cid: drawCouponsRequest.Cid,
}
err := database.Instance().Create(&newUserCoupons).Error
if err != nil {
ServerError.Fail(ctx, nil)
lockMap.Unlock(headerInfo.Token)
return
}
lockMap.Unlock(headerInfo.Token)
Success(ctx, headerInfo, nil)
})
}}
}
+20 -17
View File
@@ -18,15 +18,16 @@ type GoodsListRequest struct {
}
type GoodsDetail struct {
Id int64 `json:"id"` //商品ID
Name string `json:"name"` //商品名称
GoodsType int `json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int `json:"price"` //价格
ParentId int64 `json:"parentId"` //商品父ID
BuyMany int `json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
Id int64 `json:"id"` //商品ID
Name string `json:"name"` //商品名称
GoodsType int `json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int `json:"price"` //价格
ParentId int64 `json:"parentId"` //商品父ID
BuyMany int `json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
GoodsSubType int `json:"goodsSubType"` //商品子类型
GoodsSubList []GoodsDetail `json:"goodsSubList,omitempty"`
}
@@ -123,19 +124,21 @@ func getPetGoodsList(key string) []GoodsDetail {
BuyMany: goods.BuyMany,
GoodsDetail: goods.GoodsDetail,
GoodsIcon: goods.GoodsIcon,
GoodsSubType: goods.GoodsSubType,
GoodsSubList: nil,
}
for _, goodsSub := range GoodsMap {
if goodsSub.ParentId == goodsDetail.Id {
goodsDetail.GoodsSubList = append(goodsDetail.GoodsSubList, GoodsDetail{
Id: goodsSub.Id,
Name: goodsSub.Name,
GoodsType: goodsSub.GoodsType,
Time: goodsSub.Time,
Price: goodsSub.Price,
BuyMany: goodsSub.BuyMany,
GoodsDetail: goodsSub.GoodsDetail,
GoodsIcon: goodsSub.GoodsIcon,
Id: goodsSub.Id,
Name: goodsSub.Name,
GoodsType: goodsSub.GoodsType,
Time: goodsSub.Time,
Price: goodsSub.Price,
BuyMany: goodsSub.BuyMany,
GoodsDetail: goodsSub.GoodsDetail,
GoodsIcon: goodsSub.GoodsIcon,
GoodsSubType: goodsSub.GoodsSubType,
})
}
}
+24 -22
View File
@@ -4,7 +4,6 @@ import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"go.uber.org/zap"
"pet-house.com/business/models"
"pet-house.com/business/utils"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web/web_iris"
@@ -25,28 +24,31 @@ var frontExcludes = [...]string{
func ModuleInit() {
utils.WechatInit()
_ = database.Instance().AutoMigrate(
//&models.User{},
//&models.Pet{},
//&models.PetBaseInfo{},
//&models.ServiceAddr{},
//&models.UserServiceAddr{},
//&models.Goods{},
//&models.PetGoods{},
//&models.SystemConfig{},
//&models.OrderMain{},
//&models.OrderSub{},
//&models.OrderDetail{},
//&models.ServiceCar{},
//&models.CarOrder{},
//&models.ServiceCarUser{},
//&models.ServiceUserMark{},
//&models.ServiceUserMarkRecord{},
//&models.ReserveTimeFilter{},
//&models.OrderServiceRecord{},
//&models.AddrServiceTime{},
&models.UserAmountRecord{},
_err := database.Instance().AutoMigrate(
//&models.User{},
//&models.Pet{},
//&models.PetBaseInfo{},
//&models.ServiceAddr{},
//&models.UserServiceAddr{},
//&models.Goods{},
//&models.PetGoods{},
//&models.SystemConfig{},
//&models.OrderMain{},
//&models.OrderSub{},
//&models.OrderDetail{},
//&models.ServiceCar{},
//&models.CarOrder{},
//&models.ServiceCarUser{},
//&models.ServiceUserMark{},
//&models.ServiceUserMarkRecord{},
//&models.ReserveTimeFilter{},
//&models.OrderServiceRecord{},
//&models.AddrServiceTime{},
//&models.UserAmountRecord{},
//&models.Coupons{},
//&models.UserCoupons{},
)
zap_server.ZAPLOG.Info("data init ", zap.Any("err", _err))
DataInit()
DataCacheJob()
}
+77 -19
View File
@@ -20,8 +20,9 @@ import (
)
type PetGoodsInfo struct {
PetId int64
GoodsIds []int64
Cid int //优惠券ID
PetId int64 //宠物ID
GoodsIds []int64 //商品列表
}
type OrderCreateRequest struct {
@@ -56,7 +57,7 @@ func (p DefParty) orderCreatePreCheck() web_iris.Party {
for _, value := range orderCreateRequest.PetGoodsInfos {
for _, value := range value.GoodsIds {
goods := GoodsMap[value]
orderTotalAmount = orderTotalAmount + int(goods.Price)
orderTotalAmount = orderTotalAmount + goods.Price
}
}
orderCreatePreCheckResponse := OrderCreatePreCheckResponse{
@@ -95,6 +96,20 @@ func (p DefParty) orderCreate() web_iris.Party {
}
var projectionServiceTime = 0
for _, value := range orderCreateRequest.PetGoodsInfos {
var userCoupons *models.UserCoupons
database.Instance().Model(&models.UserCoupons{}).Where("uid = ? and cid = ? and coupons_status = 1", headerBaseInfo.Uid, value.Cid).Find(&userCoupons)
if value.Cid > 0 {
if userCoupons.Id == 0 {
CouponsNotExistError.Fail(ctx, nil)
return
}
coupons := CouponsMap[value.Cid]
if coupons.Id == 0 {
CouponsNotExistError.Fail(ctx, nil)
return
}
}
userPet := GetUserPet(headerBaseInfo.Uid, value.PetId)
var dog = userPet.PetBaseInfo.PetType == 2
var goodsType1 = false
@@ -148,14 +163,23 @@ func (p DefParty) orderCreate() web_iris.Party {
var orderSubDetailList []models.OrderDetail
var mainGoods = 0
for _, value := range orderCreateRequest.PetGoodsInfos {
coupons := CouponsMap[value.Cid]
subOrderId := NextId.Generate().String()
var totalAmount = 0
var discountTotalAmount = 0
var projectionServiceTime = 0
var goodsName string
for _, value := range value.GoodsIds {
goods := GoodsMap[value]
totalAmount = totalAmount + goods.Price
orderSubDetailList = append(orderSubDetailList, models.OrderDetail{SubOrderId: subOrderId, GoodsId: value, Amount: goods.Price})
if coupons.Id > 0 && coupons.GoodsSubType == goods.GoodsSubType {
//单个订单折扣
discountTotalAmount = discountTotalAmount + int(utils.RoundToOneDecimalPlace(float64(goods.Price)*(float64(coupons.Discount)/100.0))*10)
} else {
discountTotalAmount = discountTotalAmount + goods.Price
}
orderSubDetailList = append(orderSubDetailList, models.OrderDetail{SubOrderId: subOrderId, Cid: coupons.Id, Discount: coupons.Discount, GoodsId: value, Amount: goods.Price})
if goods.Time != "/" && len(goods.Time) > 0 {
goodsTime, _ := strconv.Atoi(goods.Time)
projectionServiceTime += goodsTime
@@ -169,17 +193,16 @@ func (p DefParty) orderCreate() web_iris.Party {
var petInfo = "宠物名称:" + userPetInfo.PetInfo.NickName + "</br>品种:" + userPetInfo.PetBaseInfo.Assortment + "</br>服务项目:" + goodsName
projectionServiceTimeAll += projectionServiceTime
orderSub := models.OrderSub{
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
OrderStatus: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: totalAmount,
PetInfo: petInfo,
Status: 1,
//PayTime: time.Now(),
OrderId: subOrderId,
MainOrderId: orderMain.OrderId,
OrderStatus: 1,
PetId: value.PetId,
PayType: 1,
Discount: 100,
TotalAmount: totalAmount,
PayAmount: totalAmount,
PetInfo: petInfo,
Status: 1,
ProjectionServiceTime: projectionServiceTime,
}
orderTotalAmount = orderTotalAmount + totalAmount
@@ -190,6 +213,7 @@ func (p DefParty) orderCreate() web_iris.Party {
orderLock.Unlock()
return
}
orderMain.ProjectionServiceTime = projectionServiceTimeAll
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
@@ -236,6 +260,18 @@ func (p DefParty) orderCreate() web_iris.Party {
//orderSubList[index].PayAmount = int32(math.Round(float64(orderSubList[index].TotalAmount) * (discount / 100.0)))
orderSubList[index].PayAmount = int(utils.RoundToOneDecimalPlace(float64(orderSubList[index].TotalAmount)*(discount/100.0)) * 10)
}
} else {
userAmountRecord := models.UserAmountRecord{
UserId: userInfo.Id,
Type: 3,
OriginAmount: userInfo.Amount,
ProAmount: discountAmount,
CurrAmount: userInfo.Amount,
OrderId: orderId,
ProScene: "下单",
Status: 1,
}
tx.Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
}
var findUserServiceAddr models.UserServiceAddr
database.Instance().Model(&models.UserServiceAddr{}).Where("id = ? and uid = ?", orderCreateRequest.ServiceAddrId, headerBaseInfo.Uid).Find(&findUserServiceAddr)
@@ -284,7 +320,7 @@ func GetOrderDetail(orderId string) OrderDetail {
Uid: orderMain.Uid,
UserAmount: float64(userInfo.Amount) / 10.0,
}
if orderMain.PayDiscount > 0 {
if orderMain.PayStatus == 1 {
orderDetail.DiscountAmount = strconv.FormatFloat(float64(orderMain.PayTotalAmount)/10.0, 'f', 1, 64)
}
var subOrderList []SubOrder
@@ -307,7 +343,7 @@ func GetOrderDetail(orderId string) OrderDetail {
Discount: float64(orderSub.Discount),
Goods: goods,
}
if orderSub.OrderStatus == 1 {
if orderSub.PayType == 3 {
x := float64(orderSub.PayAmount) / 10
orderSubR1.PayAmount = strconv.FormatFloat(x, 'f', 1, 64)
}
@@ -318,6 +354,9 @@ func GetOrderDetail(orderId string) OrderDetail {
} else {
orderSubR1.DiscountAmount = strconv.Itoa(orderSubR1.TotalAmount)
}
if orderSub.PayType == 1 {
orderSubR1.PayAmount = strconv.Itoa(orderSubR1.TotalAmount)
}
subOrderList = append(subOrderList, orderSubR1)
}
var carOrder models.CarOrder
@@ -723,11 +762,11 @@ func (p DefParty) orderGoodsUpdate() web_iris.Party {
allProjectionServiceTime = allProjectionServiceTime + value.ProjectionServiceTime
}
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", orderMain.Uid).Find(&userInfo)
//已支付
if orderMain.PayStatus == 1 {
if orderMain.PayType == 3 {
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", orderMain.Uid).Find(&userInfo)
var originAmount = userInfo.Amount
//先退还 当前订单
userInfo.Amount = userInfo.Amount + orderSub.PayAmount
@@ -780,6 +819,25 @@ func (p DefParty) orderGoodsUpdate() web_iris.Party {
database.Instance().Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
zap_server.ZAPLOG.Info("会员金额扣除", zap.Any("用户ID", userInfo.Id), zap.Any("订单ID", orderMain.OrderId), zap.Any("用户修改之前余额", originAmount), zap.Any("当前余额", userInfo.Amount), zap.Any("商品修改之前金额", preAmount), zap.Any("商品修改之后金额", orderMain.PayTotalAmount), zap.Any("折扣", userInfo.Discount))
}
} else {
var x = 0
if originOrderTotalAmount > nowOrderTotalAmount {
x = originOrderTotalAmount - nowOrderTotalAmount
} else if nowOrderTotalAmount > originOrderTotalAmount {
x = nowOrderTotalAmount - originOrderTotalAmount
}
userAmountRecord := models.UserAmountRecord{
UserId: userInfo.Id,
Type: 3,
OriginAmount: userInfo.Amount,
ProAmount: x * 10,
CurrAmount: userInfo.Amount,
OrderId: orderMain.OrderId,
ProScene: "修改订单服务项目",
Status: 1,
}
database.Instance().Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
}
database.Instance().Where("sub_order_id = ?", orderGoodsUpdateRequest.OrderId).Delete(&models.OrderDetail{})