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.
265 lines
6.8 KiB
265 lines
6.8 KiB
package vip
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/user"
|
|
"recook/internal/v2/lib/common"
|
|
"recook/internal/v2/model/recook/goods"
|
|
"recook/tools"
|
|
"time"
|
|
|
|
"git.oa00.com/go/mysql"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Proxy struct{}
|
|
|
|
type VipGoods struct {
|
|
SkuID int `json:"sku_id"`
|
|
GoodsID int `json:"goods_id"`
|
|
SkuName string `json:"sku_name"`
|
|
DiscountPrice decimal.Decimal `json:"discount_price"`
|
|
Coupon decimal.Decimal `json:"coupon"`
|
|
EffectTime int `json:"effect_time"`
|
|
EffectDayType int `json:"effect_day_type"`
|
|
}
|
|
|
|
func (o Proxy) VipGoods(c *gin.Context) {
|
|
var gs goods.RecookGoodsInfoModel
|
|
mysql.Db.Preload("SkuList").First(&gs, "is_virtual = 1")
|
|
data := make([]VipGoods, 0)
|
|
for _, v := range gs.SkuList {
|
|
data = append(data, VipGoods{
|
|
SkuID: int(v.Id),
|
|
GoodsID: int(v.GoodsId),
|
|
SkuName: v.Name,
|
|
DiscountPrice: v.DiscountPrice,
|
|
Coupon: v.Coupon,
|
|
EffectTime: v.EffectTime,
|
|
EffectDayType: v.EffectDayType,
|
|
})
|
|
}
|
|
back.Suc(c, "", data)
|
|
}
|
|
|
|
func (o Proxy) VipActive(c *gin.Context) {
|
|
id, _ := common.GetAppUserId(c)
|
|
if id == 0 {
|
|
back.Fail(c, "用户不存在")
|
|
return
|
|
}
|
|
key := fmt.Sprintf("user:vip:%d", id)
|
|
lock := dbc.Rds.SetNX(key, 1, time.Second*15)
|
|
defer dbc.Rds.Del(key)
|
|
if !lock.Val() {
|
|
back.Fail(c, "请勿重复操作")
|
|
return
|
|
}
|
|
|
|
key2 := "user:actived:set"
|
|
res := dbc.Rds.SIsMember(key2, id)
|
|
if res.Val() {
|
|
// 存在
|
|
back.Fail(c, "用户已激活")
|
|
return
|
|
}
|
|
|
|
var u1 user.Information
|
|
mysql.Db.First(&u1, "id = ?", id)
|
|
if u1.IsVip() {
|
|
dbc.Rds.SAdd(key2, id)
|
|
return
|
|
}
|
|
|
|
patch := make(map[string]interface{})
|
|
patch["level"] = 2
|
|
patch["parent_id"] = 0
|
|
patch["is_offline"] = true
|
|
now := time.Now()
|
|
end := now
|
|
level := u1.Level
|
|
if u1.VipUpgradeEnd.Valid && time.Now().Before(u1.VipUpgradeEnd.Time) {
|
|
// 续约
|
|
level = u1.OldLevel
|
|
now = u1.VipUpgradeStart.Time
|
|
end = u1.VipUpgradeEnd.Time
|
|
} else {
|
|
// 没有升级过 || 升级过已经过了上一次的时间重新计算
|
|
patch["old_level"] = u1.Level
|
|
patch["old_parent_id"] = u1.ParentID
|
|
patch["old_root_id"] = u1.RootID
|
|
}
|
|
end = end.AddDate(0, 0, 7)
|
|
patch["vip_upgrade_start"] = formatime.NewSecondFrom(now)
|
|
patch["vip_upgrade_end"] = formatime.NewSecondFrom(end)
|
|
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Table((&user.Information{}).TableName()).Where("id = ?", u1.ID).Updates(patch).Error; err != nil {
|
|
return err
|
|
}
|
|
res := dbc.Rds.SAdd(key2, id)
|
|
if r1, _ := res.Result(); r1 == 0 {
|
|
return errors.New("已使用")
|
|
}
|
|
kind := user.SevenDay
|
|
tx.Create(&user.VipHistory{
|
|
UserID: int(id),
|
|
Name: kind.Str(),
|
|
Start: &now,
|
|
End: &end,
|
|
Amount: decimal.Zero,
|
|
Level: level,
|
|
Nickname: u1.Nickname,
|
|
Mobile: u1.Mobile,
|
|
Kind: kind,
|
|
ShareID: int(id),
|
|
OrderID: 0,
|
|
})
|
|
return nil
|
|
}); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", "")
|
|
}
|
|
|
|
func (o Proxy) VipIsUsed(c *gin.Context) {
|
|
id, _ := common.GetAppUserId(c)
|
|
if id == 0 {
|
|
back.Fail(c, "用户不存在")
|
|
}
|
|
var u1 user.Information
|
|
mysql.Db.First(&u1, "id = ?", id)
|
|
if u1.IsVip() {
|
|
back.Suc(c, "操作成功", gin.H{"is_used": true})
|
|
return
|
|
}
|
|
key2 := "user:actived:set"
|
|
res := dbc.Rds.SIsMember(key2, id)
|
|
back.Suc(c, "操作成功", gin.H{"is_used": res.Val()})
|
|
}
|
|
|
|
// InviteCheck @Title 邀请码验证
|
|
func (o *Proxy) InviteCheck(c *gin.Context) {
|
|
args := argsGoodsInvite{}
|
|
err := tools.Params(&args, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.InviteNo == "" {
|
|
back.Fail(c, "二维码不存在或已失效")
|
|
return
|
|
}
|
|
recookGoodsInvite := goods.RecookGoodsInvite{}
|
|
if mysql.Db.Where("inviter_code = ? and status = ?", args.InviteNo, goods.RecookGoodsInviteStatusNone).First(&recookGoodsInvite).Error != nil {
|
|
back.Fail(c, "二维码已使用")
|
|
return
|
|
}
|
|
userId, _ := common.GetAppUserId(c)
|
|
var u1 user.Information
|
|
if err := mysql.Db.First(&u1, "id = ?", userId).Error; err != nil {
|
|
back.Fail(c, "二维码不存在或已失效")
|
|
return
|
|
}
|
|
if u1.Level == 10 {
|
|
// 合伙人或者真实vip店铺无需操作
|
|
back.Suc(c, "操作成功", gin.H{"isVip": true})
|
|
return
|
|
} else {
|
|
back.Suc(c, "操作成功", gin.H{"isVip": false})
|
|
return
|
|
}
|
|
}
|
|
|
|
type argsGoodsInvite struct {
|
|
InviteNo string
|
|
}
|
|
|
|
// Invite @Title 邀请码
|
|
func (o *Proxy) Invite(c *gin.Context) {
|
|
args := argsGoodsInvite{}
|
|
err := tools.Params(&args, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.InviteNo == "" {
|
|
back.Fail(c, "二维码不存在或已失效")
|
|
return
|
|
}
|
|
recookGoodsInvite := goods.RecookGoodsInvite{}
|
|
if mysql.Db.Where("inviter_code = ? and status = ?", args.InviteNo, goods.RecookGoodsInviteStatusNone).First(&recookGoodsInvite).Error != nil {
|
|
back.Fail(c, "二维码已使用")
|
|
return
|
|
}
|
|
userId, _ := common.GetAppUserId(c)
|
|
var u1 user.Information
|
|
if err := mysql.Db.First(&u1, "id = ?", userId).Error; err != nil {
|
|
back.Fail(c, "二维码不存在或已失效")
|
|
return
|
|
}
|
|
if u1.Level == 10 {
|
|
// 合伙人或者真实vip店铺无需操作
|
|
back.Fail(c, "合伙人无需领取权益卡")
|
|
return
|
|
}
|
|
patch := make(map[string]interface{})
|
|
patch["level"] = 2
|
|
patch["parent_id"] = 0
|
|
patch["is_offline"] = true
|
|
now := time.Now()
|
|
end := now
|
|
level := u1.Level
|
|
if u1.VipUpgradeEnd.Valid && time.Now().Before(u1.VipUpgradeEnd.Time) {
|
|
// 续约
|
|
level = u1.OldLevel
|
|
now = u1.VipUpgradeStart.Time
|
|
end = u1.VipUpgradeEnd.Time
|
|
} else {
|
|
// 没有升级过 || 升级过已经过了上一次的时间重新计算
|
|
patch["old_level"] = u1.Level
|
|
patch["old_parent_id"] = u1.ParentID
|
|
patch["old_root_id"] = u1.RootID
|
|
}
|
|
end = end.AddDate(1, 0, 0)
|
|
patch["vip_upgrade_start"] = formatime.NewSecondFrom(now)
|
|
patch["vip_upgrade_end"] = formatime.NewSecondFrom(end)
|
|
if err := mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Model(&recookGoodsInvite).Where(&recookGoodsInvite).Updates(map[string]interface{}{
|
|
"status": goods.RecookGoodsInviteStatusUsed,
|
|
"use_user_id": userId,
|
|
}).RowsAffected != 1 {
|
|
return errors.New("领取失败")
|
|
}
|
|
if err := tx.Table((&user.Information{}).TableName()).Where("id = ?", u1.ID).Updates(patch).Error; err != nil {
|
|
return errors.New("领取失败")
|
|
}
|
|
kind := user.Year
|
|
if tx.Create(&user.VipHistory{
|
|
UserID: int(userId),
|
|
Name: kind.Str(),
|
|
Start: &now,
|
|
End: &end,
|
|
Amount: decimal.Zero,
|
|
Level: level,
|
|
Nickname: u1.Nickname,
|
|
Mobile: u1.Mobile,
|
|
Kind: kind,
|
|
}).Error != nil {
|
|
return errors.New("领取失败")
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", gin.H{"endTime": end.Unix()})
|
|
return
|
|
}
|