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.

115 lines
2.9 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"
"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)
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, "用户已激活")
return
}
var u1 user.Information
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 {
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("已使用")
}
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, "用户不存在")
}
key2 := "user:actived:set"
res := dbc.Rds.SIsMember(key2, id)
back.Suc(c, "操作成功", gin.H{"is_used": res.Val()})
}