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.

116 lines
2.9 KiB

3 years ago
package vip
import (
3 years ago
"errors"
"fmt"
3 years ago
"recook/internal/back"
3 years ago
"recook/internal/dbc"
"recook/internal/model/user"
"recook/internal/v2/lib/common"
3 years ago
"recook/internal/v2/model/recook/goods"
3 years ago
"time"
3 years ago
"git.oa00.com/go/mysql"
"github.com/gin-gonic/gin"
3 years ago
"github.com/golangkit/formatime"
3 years ago
"github.com/shopspring/decimal"
3 years ago
"gorm.io/gorm"
3 years ago
)
type Proxy struct{}
3 years ago
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"`
}
3 years ago
func (o Proxy) VipGoods(c *gin.Context) {
var gs goods.RecookGoodsInfoModel
3 years ago
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)
3 years ago
}
3 years ago
3 years ago
func (o Proxy) VipActive(c *gin.Context) {
3 years ago
id, _ := common.GetAppUserId(c)
if id == 0 {
back.Fail(c, "用户不存在")
3 years ago
return
3 years ago
}
key := fmt.Sprintf("user:vip:%d", id)
dbc.Rds.SetNX(key, 1, 15)
defer dbc.Rds.Del(key)
key2 := "user:actived:set"
res := dbc.Rds.SIsMember(key2, id)
if res.Val() {
// 存在
back.Fail(c, "用户已激活")
3 years ago
return
3 years ago
}
var u1 user.Information
3 years ago
mysql.Db.First(&u1, "id = ?", id)
3 years ago
patch := make(map[string]interface{})
patch["level"] = 2
patch["parent_id"] = 0
patch["is_offline"] = true
now1 := time.Now()
now := time.Date(now1.Year(), now1.Month(), now1.Day(), 0, 0, 0, 00, time.Local)
end := now
if u1.VipUpgradeEnd.Valid && time.Now().Before(u1.VipUpgradeEnd.Time) {
// 续约
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 {
3 years ago
if err := tx.Table((&user.Information{}).TableName()).Where("id = ?", u1.ID).Updates(patch).Error; err != nil {
3 years ago
return err
}
res := dbc.Rds.SAdd(key2, id)
if r1, _ := res.Result(); r1 == 0 {
return errors.New("已使用")
}
return nil
}); err != nil {
back.Fail(c, err.Error())
3 years ago
return
3 years ago
}
3 years ago
back.Suc(c, "操作成功", "")
3 years ago
}
func (o Proxy) VipIsUsed(c *gin.Context) {
id, _ := common.GetAppUserId(c)
if id == 0 {
back.Fail(c, "用户不存在")
}
key2 := "user:actived:set"
res := dbc.Rds.SIsMember(key2, id)
3 years ago
back.Suc(c, "操作成功", gin.H{"is_used": res.Val()})
3 years ago
}