|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/astaxie/beego/orm" |
| 7 | +) |
| 8 | + |
| 9 | +type Credit struct { |
| 10 | +} |
| 11 | + |
| 12 | +const ( |
| 13 | + cycleTypeOnce int8 = 0 // 一次 |
| 14 | + cycleTypeDay int8 = 1 // 一天 |
| 15 | + cycleTypeWeek int8 = 2 // 一周 |
| 16 | + cycleTypeMonth int8 = 3 // 一月 |
| 17 | + cycleTypeYear int8 = 4 // 一年 |
| 18 | +) |
| 19 | + |
| 20 | +// CreditRule 规则 |
| 21 | +type CreditRule struct { |
| 22 | + Id int // 规则ID |
| 23 | + Identify string `orm:"unique;size(32)"` // 积分标识,唯一 |
| 24 | + Title string // 规则名称 |
| 25 | + Intro string // 规则简介 |
| 26 | + Score int // 周期内每次奖励的积分 |
| 27 | + CycleType int8 // 奖励周期 |
| 28 | + RewardTimes int // 周次内总共奖励的次数 |
| 29 | + CreatedAt time.Time |
| 30 | + UpdatedAt time.Time |
| 31 | +} |
| 32 | + |
| 33 | +// CreditLog 积分变更日志记录 |
| 34 | +type CreditLog struct { |
| 35 | + Id int |
| 36 | + Uid int `orm:"index"` // 用户ID |
| 37 | + Identify string `orm:"index;size(32)"` // 规则标识 |
| 38 | + Score int // 积分,有正有负数,负数表示被扣分,正数表示加分 |
| 39 | + RewardBy int // 被谁奖励的,0 表示为系统奖励的,如果 Uid == RewardBy,表示用户自己充值的(如果有这个功能的话),否则表示是管理员(需要是管理员身份)打赏的 |
| 40 | + Log string `orm:"size(512)"` // 日志内容 |
| 41 | + CreatedAt time.Time `orm:"auto_now"` |
| 42 | +} |
| 43 | + |
| 44 | +// InitCreditRule 如果还没存在积分规则,则初始化积分规则 |
| 45 | +func InitCreditRule() { |
| 46 | + // 积分榜单增加 壕榜(积分排行) |
| 47 | + // 一个勤奋的用户,每个月大概能获取到的奖励金额:0.005x30(签到, 0.15) + 0.005(注册,0.005) + 0.008x30(邀请注册,0.24) + 0.032x30(日榜上榜, 0.96) + 0.064x4(周榜上榜0.26) + 1.0(月榜前十) = 3 |
| 48 | + rules := []CreditRule{ |
| 49 | + {Identify: "sign", Title: "签到奖励", Intro: "用户每天签到的奖励", CycleType: cycleTypeDay, RewardTimes: 1, Score: 5}, // 每天签到奖励, 0.005 元 |
| 50 | + {Identify: "reg", Title: "注册奖励", Intro: "新用户注册奖励", CycleType: cycleTypeOnce, RewardTimes: 1, Score: 8}, // 注册奖励 |
| 51 | + {Identify: "read", Title: "阅读规则", Intro: "用户每天阅读时长超过5分钟的奖励", CycleType: cycleTypeDay, RewardTimes: 1, Score: 5}, // 重在参与,阅读有奖。 0.005 元 |
| 52 | + {Identify: "invite", Title: "邀请注册", Intro: "邀请别人注册,获得奖励", CycleType: cycleTypeDay, RewardTimes: 1, Score: 8}, // 0.008 元 |
| 53 | + {Identify: "rank_day", Title: "阅读日榜前五十奖励", Intro: "用户阅读日榜上榜(前50)奖励", CycleType: cycleTypeDay, RewardTimes: 1, Score: 32}, // 0.032 x 50 x 30 = 48 / 月 |
| 54 | + {Identify: "rank_week", Title: "阅读周榜前五十奖励", Intro: "用户阅读周榜上榜(前50)奖励", CycleType: cycleTypeWeek, RewardTimes: 1, Score: 64}, // 0.128 x 50 x 4 = 12.8 / 月 |
| 55 | + {Identify: "rank_mon_top50", Title: "阅读月榜前五十奖励", Intro: "用户阅读月榜前五十奖励", CycleType: cycleTypeMonth, RewardTimes: 1, Score: 256}, // 0.256 x 30 = 7.68 / 月 |
| 56 | + {Identify: "rank_mon_top20", Title: "阅读月榜前二十奖励", Intro: "用户阅读月榜前二十奖励", CycleType: cycleTypeMonth, RewardTimes: 1, Score: 512}, // 0.5 x 10 = 5 / 月 |
| 57 | + {Identify: "rank_mon_top10", Title: "阅读月榜前十奖励", Intro: "用户阅读月榜前十奖励", CycleType: cycleTypeMonth, RewardTimes: 1, Score: 1024}, // 1.0 x 10 = 10 / 月 |
| 58 | + |
| 59 | + {Identify: "donate", Title: "打赏书籍", Intro: "用户打赏书籍消费", CycleType: cycleTypeDay, RewardTimes: 10, Score: 0}, // score 为0,表示隐藏这条积分记录 |
| 60 | + {Identify: "exchange", Title: "积分兑换", Intro: "用户积分兑换奖品", CycleType: cycleTypeMonth, RewardTimes: 20, Score: -10240}, // score 为0,表示隐藏这条积分记录 |
| 61 | + |
| 62 | + {Identify: "submit", Title: "收录奖励", Intro: "提交未被收录的书籍获得的奖励", CycleType: cycleTypeDay, RewardTimes: 10, Score: 5}, // 收录奖励, 0.005 元 |
| 63 | + {Identify: "charge", Title: "充值奖励", Intro: "用户充值", CycleType: cycleTypeDay, RewardTimes: 0, Score: 0}, // 收录奖励, 0.005 元 |
| 64 | + |
| 65 | + {Identify: "market_value", Title: "网站积分市值", Intro: "整站所产生的积分", CycleType: cycleTypeOnce, RewardTimes: 0, Score: 0}, // 收录奖励, 0.005 元 |
| 66 | + } |
| 67 | + o := orm.NewOrm() |
| 68 | + for _, rule := range rules { |
| 69 | + o.Insert(&rule) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func NewCreditRule() *CreditRule { |
| 74 | + return &CreditRule{} |
| 75 | +} |
| 76 | + |
| 77 | +func NewCreditLog() *CreditLog { |
| 78 | + return &CreditLog{} |
| 79 | +} |
| 80 | + |
| 81 | +func NewCredit() *Credit { |
| 82 | + return &Credit{} |
| 83 | +} |
| 84 | + |
| 85 | +// Insert 给用户新增积分 |
| 86 | +func (m *Credit) Insert(uid int, ruleIdentify string) { |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +// CreditCostPerMonth 每月成本估算,price 表示一元钱等于多少个积分。 |
| 91 | +// 计算结果计算网站一个月大概支出多少,以及一个勤奋的用户大概能拿到多少等价金额 |
| 92 | +// 注意:为避免积分规则设置错误,或者是积分规则设置者“滥发货币”引起不必要的“破产问题”, |
| 93 | +// 建议积分每个月限定总额兑换机制,兑换完就自行等待下次兑换。具体可以参考微信支付出行奖励积分兑换机制玩法, |
| 94 | +// 避免积分在跟实体金额兑换的时候玩死自己.... |
| 95 | +func (m *Credit) CreditCostPerMonth(price int) { |
| 96 | + // TODO |
| 97 | + _ = price |
| 98 | +} |
0 commit comments