123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package activity
- import (
- "app.yhyue.com/moapp/jybase/go-logger/logger"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/message/db"
- "app.yhyue.com/moapp/message/handler/award"
- "app.yhyue.com/moapp/message/rpc"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/v2/os/gcfg"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/os/gtime"
- "github.com/gogf/gf/v2/util/gconv"
- "time"
- )
- /*
- 投标人专属免费计划
- 待解决问题:连点处理
- */
- type BidderPlan struct {
- St, Ed time.Time //活动开始时间、结束时间
- Missions map[string]struct { //任务
- Points int `json:"points"`
- Schedule int `json:"schedule"`
- }
- ScheduleReward map[int]struct { //进度奖励
- SubVip int `json:"subvip"`
- Schedule int `json:"schedule"`
- }
- }
- const (
- BidderPlanActivityCode = "bidderPlan"
- BidderPlanRedis = "main"
- //活动累计进度
- BidderPlanScheduleCache = "bidderPlanScheduleValue_%s"
- //奖励领取状态
- BidderPlanScheduleAwardStatus = "bidderPlanScheduleAward_%s_%d"
- BidderPlanSubscribeMissionsStatus = "bidderPlanActivitySubscribeCacheKey_%s"
- BidderPlanInviteMissionsStatus = "bidderPlanActivityInviteCacheKey_%s"
- BidderPlanImproveInfoMissionsStatus = "bidderPlanActivityImproveInfoCacheKey_%s"
- )
- var JyBidderPlan *BidderPlan
- func init() {
- JyBidderPlan = &BidderPlan{
- St: gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "bidderPlan.dateRange.st").String(), "2006-01-02T15:04:05Z").Time,
- Ed: gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "bidderPlan.dateRange.ed").String(), "2006-01-02T15:04:05Z").Time,
- }
- //加载任务
- if loadMissionsErr := gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions").Scan(&JyBidderPlan.Missions); loadMissionsErr != nil {
- logger.Error(fmt.Sprintf("JyBidderPlan 加载任务异常 %v", loadMissionsErr))
- }
- //加载进度奖励
- if loadScheduleErr := gcfg.Instance().MustGet(gctx.New(), "bidderPlan.scheduleReward").Scan(&JyBidderPlan.ScheduleReward); loadScheduleErr != nil {
- logger.Error(fmt.Sprintf("JyBidderPlan 加载进度异常 %v", loadScheduleErr))
- }
- }
- // InActivity 是否在活动时间内
- func (BP *BidderPlan) InActivity() bool {
- now := time.Now()
- return now.After(JyBidderPlan.St) && now.Before(JyBidderPlan.Ed)
- }
- // cacheLong 缓存时间
- func (BP *BidderPlan) cacheLong() int {
- //return int(JyBidderPlan.Ed.Unix()-time.Now().Unix()))+ 60 * 60 * 24
- return 60 * 60 * 24 * 30
- }
- // GetSchedule 查询活动任务进度
- func (BP *BidderPlan) GetSchedule(userId string) int {
- return -redis.GetInt(BidderPlanRedis, fmt.Sprintf(BidderPlanScheduleCache, userId))
- }
- func (BP *BidderPlan) GetScheduleQuery(userId string, schedule int) (status int) {
- if BP.GetSchedule(userId) < schedule {
- return 0
- }
- key := fmt.Sprintf(BidderPlanScheduleAwardStatus, userId, schedule)
- if redis.Get(BidderPlanRedis, key) == nil {
- status = gconv.Int(redis.Incr(BidderPlanRedis, key))
- _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
- } else {
- status = redis.GetInt(BidderPlanRedis, key)
- }
- if status > 1 {
- status = 1
- }
- return
- }
- // ScheduleGiven 活动任务进度奖励
- func (BP *BidderPlan) ScheduleGiven(userId string, schedule int) error {
- if !BP.InActivity() {
- return fmt.Errorf("活动已结束")
- }
- if BP.GetScheduleQuery(userId, schedule) != 1 {
- return fmt.Errorf("非领取状态")
- }
- scheduleAward, ok := BP.ScheduleReward[schedule]
- if !ok {
- return fmt.Errorf("未知请求")
- }
- if err := award.GivenSubVip(userId, award.SubVip{
- Num: gconv.Int64(scheduleAward.SubVip),
- ActivityCode: BidderPlanActivityCode,
- Desc: fmt.Sprintf("达到%d剑鱼币", schedule),
- Date: time.Now().Unix(),
- }); err != nil {
- return fmt.Errorf("领取奖励异常")
- }
- key := fmt.Sprintf(BidderPlanScheduleAwardStatus, userId, schedule)
- redis.Put(BidderPlanRedis, key, -1, BP.cacheLong())
- return nil
- }
- // AddSchedule 增加活动进度
- func (BP *BidderPlan) AddSchedule(userId string, value int) (after int) {
- if value > 0 {
- key := fmt.Sprintf(BidderPlanScheduleCache, userId)
- after = -gconv.Int(redis.Decrby(BidderPlanRedis, key, value))
- if value == after { //首次设置过期值
- _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
- }
- }
- return
- }
- // FreeSubscribe 免费订阅
- type FreeSubscribe struct {
- OAreaP map[string]interface{} `json:"o_area_p"`
- AKey []interface{} `json:"a_key"`
- }
- // MissionsSubscribeQuery 订阅任务
- // status:0 未完成 1 待领取 -1已领取
- func (BP *BidderPlan) MissionsSubscribeQuery(userId string) (status int) {
- //非7天超级订阅或存在订阅内容则完成任务
- finish := func() bool {
- uData, ok := db.Mgo.FindById("user", userId, `{"o_jy":1,"i_vip_status":1,"i_member_status":1}`)
- if !ok || uData == nil || len(*uData) == 0 {
- return false
- }
- if gconv.Int((*uData)["i_vip_status"]) > 0 { //超级订阅
- return true
- } else if gconv.Int((*uData)["i_member_status"]) > 0 { //大会员
- return true
- }
- //查询免费订阅是否完成
- vStatus, err := rpc.GetUserVipBaseMsgByRpc(userId)
- if err != nil {
- return false
- }
- if vStatus.EntnicheStatus > 0 {
- return true
- } else {
- bytes := gconv.Bytes((*uData)["o_jy"])
- if bytes == nil || len(bytes) == 0 {
- return false
- }
- freeSub := FreeSubscribe{}
- if err = json.Unmarshal(bytes, &freeSub); err != nil {
- fmt.Println(err)
- return false
- }
- if len(freeSub.AKey) > 0 && len(freeSub.OAreaP) > 0 {
- return true
- }
- }
- return false
- }()
- if !finish {
- return 0
- }
- key := fmt.Sprintf(BidderPlanSubscribeMissionsStatus, userId)
- if redis.Get(BidderPlanRedis, key) == nil {
- status = gconv.Int(redis.Incr(BidderPlanRedis, key))
- _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
- } else {
- status = redis.GetInt(BidderPlanRedis, key)
- }
- if status > 1 {
- status = 1
- }
- return
- }
- // MissionsSubscribeGiven 订阅任务领取奖励
- func (BP *BidderPlan) MissionsSubscribeGiven(userId string) error {
- if !BP.InActivity() {
- return fmt.Errorf("活动已结束")
- }
- status := BP.MissionsSubscribeQuery(userId)
- if status != 1 {
- return fmt.Errorf("非领取状态")
- }
- if givenPointsErr := award.GivenPoints(userId, award.Points{
- Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.subscribe.points").Int64(),
- Type: 1006,
- ActivityCode: BidderPlanActivityCode,
- Desc: "免费订阅任务完成",
- Date: time.Now().Unix(),
- }); givenPointsErr != nil {
- return fmt.Errorf("领取奖励异常")
- }
- //设置已领取
- redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanSubscribeMissionsStatus, userId), -1, BP.cacheLong())
- //增加任务进度
- BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.subscribe.schedule").Int())
- return nil
- }
- // MissionsInviteQuery 邀请任务状态查询
- // status:0 未完成 1 待领取 -1已领取
- func (BP *BidderPlan) MissionsInviteQuery(userId string) (status int) {
- status = redis.GetInt(BidderPlanRedis, fmt.Sprintf(BidderPlanInviteMissionsStatus, userId))
- if status > 1 {
- status = 1
- }
- return status
- }
- // MissionsInviteGiven 邀请任务奖励领取
- func (BP *BidderPlan) MissionsInviteGiven(userId string) error {
- if !BP.InActivity() {
- return fmt.Errorf("活动已结束")
- }
- status := BP.MissionsInviteQuery(userId)
- if status != 1 {
- return fmt.Errorf("非领取状态")
- }
- //领取
- if givenPointsErr := award.GivenPoints(userId, award.Points{
- Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.invite.points").Int64(),
- Type: 1007,
- ActivityCode: BidderPlanActivityCode,
- Desc: "邀请领好礼任务完成",
- Date: time.Now().Unix(),
- }); givenPointsErr != nil {
- return fmt.Errorf("领取奖励异常")
- }
- //设置已领取
- redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanInviteMissionsStatus, userId), -1, BP.cacheLong())
- //增加任务进度
- BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.invite.schedule").Int())
- return nil
- }
- // MissionsImproveInfoQuery 完善信息任务
- // status:0 未完成 1 待领取 -1已领取
- func (BP *BidderPlan) MissionsImproveInfoQuery(userId string) (status int) {
- hasMsg := db.Mgo.Count("saleLeads", map[string]interface{}{
- "userid": userId,
- }) > 0
- if !hasMsg {
- return 0
- }
- key := fmt.Sprintf(BidderPlanImproveInfoMissionsStatus, userId)
- if redis.Get(BidderPlanRedis, key) == nil {
- status = gconv.Int(redis.Incr(BidderPlanRedis, key))
- _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
- } else {
- status = redis.GetInt(BidderPlanRedis, key)
- }
- if status > 1 {
- status = 1
- }
- return
- }
- // MissionsImproveInfoGiven 完善信息奖励领取
- func (BP *BidderPlan) MissionsImproveInfoGiven(userId string) error {
- if !BP.InActivity() {
- return fmt.Errorf("活动已结束")
- }
- status := BP.MissionsImproveInfoQuery(userId)
- if status != 1 {
- return fmt.Errorf("非领取状态")
- }
- if givenPointsErr := award.GivenPoints(userId, award.Points{
- Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.improveInfo.points").Int64(),
- Type: 1008,
- ActivityCode: BidderPlanActivityCode,
- Desc: "完善信息任务完成",
- Date: time.Now().Unix(),
- }); givenPointsErr != nil {
- return fmt.Errorf("领取奖励异常")
- }
- //设置已领取
- redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanImproveInfoMissionsStatus, userId), -1, BP.cacheLong())
- //增加任务进度
- BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.improveInfo.schedule").Int())
- return nil
- }
|