优惠券相关逻辑

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

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 {

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

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() {

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
business/api/coupons.go Normal file
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)
})
}}
}

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,
})
}
}

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()
}

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{})

View File

@ -8,18 +8,19 @@ import "time"
// User 用户
type User struct {
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //用户ID
NickName string `json:"nickName"` //昵称
HeadImgUrl string `json:"headImgUrl"` //用户头像URL
Amount int `json:"amount"` //用户余额
OpenId string `gorm:"index;unique;not null" json:"openId"` //三方openid
UnionId string `gorm:"index;unique;not null" json:"unionId"` //三方unionid
UserType int `json:"userType"` //用户类型 0微信 1手机号
Mobile string `json:"mobile"` //手机号
Role int `json:"role"` //用户角色 0普通用户 1护理人员
Discount int `json:"discount"` //折扣
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;not null" json:"createTime"` //创建时间
Status int `json:"status"` //信息状态
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //用户ID
NickName string `json:"nickName"` //昵称
HeadImgUrl string `json:"headImgUrl"` //用户头像URL
Amount int `json:"amount"` //用户余额
OpenId string `gorm:"index;unique;not null" json:"openId"` //三方openid
UnionId string `gorm:"index;unique;not null" json:"unionId"` //三方unionid
UserType int `json:"userType"` //用户类型 0微信 1手机号
Mobile string `json:"mobile"` //手机号
Role int `json:"role"` //用户角色 0普通用户 1护理人员
Discount int `json:"discount"` //折扣
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;not null" json:"createTime"` //创建时间
Status int `json:"status"` //信息状态
InviteUserId int64 `json:"inviteUserId"` //邀请用户
}
// Pet 宠物信息
@ -83,17 +84,18 @@ type UserServiceAddr struct {
// Goods 商品信息
type Goods struct {
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //商品ID
Name string `gorm:"not null" json:"name"` //商品名称
GoodsType int `gorm:"not null" json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int `gorm:"not null" json:"price"` //价格
ParentId int64 `gorm:"not null" json:"parentId"` //商品父ID
BuyMany int `gorm:"not null" json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `gorm:"not null" json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"-"` //创建时间
Status int `json:"status"` //信息状态
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //商品ID
Name string `gorm:"not null" json:"name"` //商品名称
GoodsType int `gorm:"not null" json:"goodsType"` //商品类型 1基础服务 2附加产品 3附加服务
Time string `json:"time"` //时间
Price int `gorm:"not null" json:"price"` //价格
ParentId int64 `gorm:"not null" json:"parentId"` //商品父ID
BuyMany int `gorm:"not null" json:"buyMany"` //同类型是否可购买多个 0否 1是
GoodsDetail string `gorm:"not null" json:"goodsDetail"` //商品详情 图片URL
GoodsIcon string `json:"goodsIcon"` //iconUrl
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"-"` //创建时间
Status int `json:"status"` //信息状态
GoodsSubType int `gorm:"not null" json:"goodsSubType"` //商品子类型
}
// PetGoods 宠物商品
@ -167,6 +169,8 @@ type OrderSub struct {
// OrderDetail 订单明细
type OrderDetail struct {
Id int64 `gorm:"primaryKey;autoIncrement" json:"id"` //id
Cid int `json:"cid"` //子订单ID
Discount int `json:"discount"` //折扣
SubOrderId string `gorm:"index;not null" json:"subOrderId"` //子订单ID
GoodsId int64 `gorm:"index;not null" json:"goodsId"` //商品ID
Amount int `gorm:"not null" json:"amount"` //价格
@ -263,3 +267,29 @@ type AddrServiceTime struct {
ServiceAddrId int64 `gorm:"not null" json:"ServiceAddrId"` //服务地址ID
Times string `gorm:"not null" json:"times"` //时间列表
}
// Coupons 优惠券
type Coupons struct {
Id int `gorm:"primaryKey;autoIncrement" json:"id"` //id
Name string `gorm:"not null" json:"name"`
GoodsSubType int `gorm:"not null" json:"goodsSubType"` //商品子类型
Discount int `gorm:"not null" json:"discount"` //折扣
Source string `gorm:"not null" json:"source"` //来源
PeriodType int `gorm:"not null" json:"periodType"` //使用类型 0、无 1、星期 2、截至时间 3、指定日期可用
Period string `json:"-"` //使用信息 具体时间 星期类型1,2,3截止日期2024-09-01 23:59:59指定日期2024-08-10
PeriodInfo []string `gorm:"-" json:"periodInfo"`
ExpireTime int64 `json:"expireTime"` //优惠券过期时间
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"` //创建时间
UpdateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP" json:"-"` //更新时间
Status int `gorm:"default:1" json:"-"` //数据状态
}
type UserCoupons struct {
Id int `gorm:"primaryKey;autoIncrement" json:"id"` //id
Uid int64 `gorm:"not null" json:"uid"` //用户id
Cid int `gorm:"not null" json:"cid"` //优惠券ID
CreateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"` //创建时间
UpdateTime time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP" json:"-"` //更新时间
Status int `gorm:"default:1" json:"-"`
CouponsStatus int `gorm:"default:1" json:"status"` //状态 1未使用 2已使用 3已过期
}

View File

@ -6,6 +6,7 @@ import (
"pet-house.com/business/models"
"strconv"
"testing"
"time"
)
func TestGetStrDays(t *testing.T) {
@ -54,3 +55,21 @@ func TestMath(t *testing.T) {
var a = int(RoundToOneDecimalPlace(float64(150)*(95/100.0)) * 10)
fmt.Println(a)
}
func TestMap(t *testing.T) {
var c1 = make(map[int]models.Coupons)
c1[1] = models.Coupons{
Id: 1,
Name: "111",
GoodsSubType: 0,
Discount: 0,
Source: "",
PeriodType: 0,
Period: "",
PeriodInfo: nil,
ExpireTime: 0,
CreateTime: time.Time{},
UpdateTime: time.Time{},
Status: 0,
}
print("12345", c1[2].Id)
}

23
business/utils/lock.go Normal file
View File

@ -0,0 +1,23 @@
package utils
import "sync"
// KeyedMutex 提供基于键的锁定
type KeyedMutex struct {
mutexes sync.Map
}
// Lock 锁定指定键
func (km *KeyedMutex) Lock(key string) {
mu, _ := km.mutexes.LoadOrStore(key, &sync.Mutex{})
mu.(*sync.Mutex).Lock()
}
// Unlock 解锁指定键
func (km *KeyedMutex) Unlock(key string) {
mu, ok := km.mutexes.Load(key)
if ok {
mu.(*sync.Mutex).Unlock()
km.mutexes.Delete(key)
}
}