|
|
@@ -0,0 +1,229 @@
|
|
|
+package cent_model
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/astaxie/beego"
|
|
|
+ "github.com/astaxie/beego/orm"
|
|
|
+
|
|
|
+ "fohow.com/cache"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ cent_balances_tablename = "cent_balances"
|
|
|
+
|
|
|
+ CENT_ONE_DAY = "one_day" //签到第一天
|
|
|
+ CENT_ONE_DAY_NAME = "签到第一天"
|
|
|
+ CENT_TWO_DAY = "two_day"
|
|
|
+ CENT_TWO_DAY_NAME = "签到第二天"
|
|
|
+ CENT_THREE_DAY = "three_day"
|
|
|
+ CENT_THREE_DAY_NAME = "签到第三天"
|
|
|
+ CENT_FOUR_DAY = "four_day"
|
|
|
+ CENT_FOUR_DAY_NAME = "签到第四天"
|
|
|
+ CENT_FIVE_DAY = "five_day"
|
|
|
+ CENT_FIVE_DAY_NAME = "签到第五天"
|
|
|
+ CENT_SIX_DAY = "six_day"
|
|
|
+ CENT_SIX_DAY_NAME = "签到第六天"
|
|
|
+ CENT_SEVEN_DAY = "seven_day"
|
|
|
+ CENT_SEVEN_DAY_NAME = "签到第七天"
|
|
|
+ CENT_FIRST_LOGIN = "first_login"
|
|
|
+ CENT_FIRST_LOGIN_NAME = "首次登录"
|
|
|
+ CENT_NEW_MAN = "new_man"
|
|
|
+ CENT_NEW_MAN_NAME = "新人注册"
|
|
|
+)
|
|
|
+
|
|
|
+//积分余额表
|
|
|
+type CentBalance struct {
|
|
|
+ Id int64 `orm:"column(id);pk" json:"id"` // int(11)
|
|
|
+ WxUId int64 `orm:"column(wx_uid)" json:"wx_uid"` // int(11)
|
|
|
+ Count int64 `orm:"column(count)" json:"count"` // int(11)
|
|
|
+ Source string `orm:"column(source);null" json:"source"` // varchar(64)
|
|
|
+ SourceName string `orm:"-" json:"source_name"` // varchar(64)
|
|
|
+ RelateId string `orm:"column(relate_id);null" json:"relate_id"` // varchar(255)
|
|
|
+ Remark string `orm:"column(remark);null" json:"remark"` // varchar(255)
|
|
|
+ CTime int64 `orm:"-" json:"c_time"` // int(11)
|
|
|
+ CreatedAt time.Time `orm:"column(created_at);null;auto_now_add;type(datetime)" json:"-"` // datetime
|
|
|
+ UpdatedAt time.Time `orm:"column(updated_at);null;auto_now;type(datetime)" json:"-"` // datetime
|
|
|
+}
|
|
|
+
|
|
|
+func (self *CentBalance) TableName() string {
|
|
|
+ return cent_balances_tablename
|
|
|
+}
|
|
|
+
|
|
|
+func (self *CentBalance) Create(wxUId, c int64, s, rId, remark string) (item *CentBalance) {
|
|
|
+ item = &CentBalance{
|
|
|
+ Count: c,
|
|
|
+ WxUId: wxUId,
|
|
|
+ Source: s,
|
|
|
+ RelateId: rId,
|
|
|
+ Remark: remark,
|
|
|
+ }
|
|
|
+ id, err := orm.NewOrm().Insert(item)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("Create CentBalance err=[%s]", err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ item.Id = id
|
|
|
+ return item
|
|
|
+}
|
|
|
+
|
|
|
+//返回source名称
|
|
|
+func (self *CentBalance) GetSourceName() string {
|
|
|
+ sourceName := ""
|
|
|
+ switch self.Source {
|
|
|
+ case CENT_ONE_DAY:
|
|
|
+ sourceName = CENT_ONE_DAY_NAME
|
|
|
+ case CENT_TWO_DAY:
|
|
|
+ sourceName = CENT_TWO_DAY_NAME
|
|
|
+ case CENT_THREE_DAY:
|
|
|
+ sourceName = CENT_THREE_DAY_NAME
|
|
|
+ case CENT_FOUR_DAY:
|
|
|
+ sourceName = CENT_FOUR_DAY_NAME
|
|
|
+ case CENT_FIVE_DAY:
|
|
|
+ sourceName = CENT_FIVE_DAY_NAME
|
|
|
+ case CENT_SIX_DAY:
|
|
|
+ sourceName = CENT_SIX_DAY_NAME
|
|
|
+ case CENT_SEVEN_DAY:
|
|
|
+ sourceName = CENT_SEVEN_DAY_NAME
|
|
|
+ case CENT_FIRST_LOGIN:
|
|
|
+ sourceName = CENT_FIRST_LOGIN_NAME
|
|
|
+ case CENT_NEW_MAN:
|
|
|
+ sourceName = CENT_NEW_MAN_NAME
|
|
|
+ }
|
|
|
+ return sourceName
|
|
|
+}
|
|
|
+
|
|
|
+//获取某人的积分流列表
|
|
|
+func GetCentBalanceListByWxUId(wxUId, page, perPage int64, useCache bool) (list []*CentBalance) {
|
|
|
+ k := fmt.Sprintf("cent_model.GetCentBalanceListByWxUId(%d).page(%d).perPage(%d)", wxUId, page, perPage)
|
|
|
+ if useCache {
|
|
|
+ if s, ok := cache.Cache.Get(k).([]*CentBalance); ok {
|
|
|
+ return s
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ balance := new(CentBalance)
|
|
|
+ qs := orm.NewOrm().QueryTable(balance)
|
|
|
+ qs = qs.Filter("wx_uid", wxUId)
|
|
|
+
|
|
|
+ if _, err := qs.OrderBy("-created_at", "-id").
|
|
|
+ Limit(perPage, (page-1)*perPage).All(&list); err != nil {
|
|
|
+ beego.BeeLogger.Debug("get cash balance with wxUId=%d, err=[%s]", wxUId, err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, item := range list {
|
|
|
+ item.SourceName = item.GetSourceName()
|
|
|
+ }
|
|
|
+
|
|
|
+ cache.Cache.Put(k, list, 10*time.Minute)
|
|
|
+ return list
|
|
|
+}
|
|
|
+
|
|
|
+//获取某人的积分流列表总条数
|
|
|
+func GetCentBalanceCountByWxUId(wxUId int64) int64 {
|
|
|
+ item := new(CentBalance)
|
|
|
+ o := orm.NewOrm()
|
|
|
+ count, _ := o.QueryTable(item).Filter("wx_uid", wxUId).Count()
|
|
|
+ return count
|
|
|
+}
|
|
|
+
|
|
|
+//账户进账总额
|
|
|
+func GetCentEnterBalance(wxUId int64) int64 {
|
|
|
+ type Ret struct {
|
|
|
+ Count int64
|
|
|
+ }
|
|
|
+ ret := &Ret{}
|
|
|
+ o := orm.NewOrm()
|
|
|
+ tbn := new(CentBalance).TableName()
|
|
|
+ sql := fmt.Sprintf("SELECT sum(`count`) as count FROM `%s` WHERE wx_uid=? and count > 0;", tbn)
|
|
|
+ err := o.Raw(sql, wxUId).QueryRow(ret)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("GetCentEnterBalance, wxUser:%d err=[%s]", wxUId, err)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ if ret.Count < 0 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return ret.Count
|
|
|
+}
|
|
|
+
|
|
|
+//账户出账总额
|
|
|
+func GetCentOutBalance(wxUId int64) int64 {
|
|
|
+ type Ret struct {
|
|
|
+ Count int64
|
|
|
+ }
|
|
|
+ ret := &Ret{}
|
|
|
+ o := orm.NewOrm()
|
|
|
+ tbn := new(CentBalance).TableName()
|
|
|
+ sql := fmt.Sprintf("SELECT sum(`count`) as count FROM `%s` WHERE wx_uid=? and count < 0;", tbn)
|
|
|
+ err := o.Raw(sql, wxUId).QueryRow(ret)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("GetCentOuterBalance, wxUser:%d err=[%s]", wxUId, err)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ if ret.Count < 0 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return ret.Count
|
|
|
+}
|
|
|
+
|
|
|
+//账户余额
|
|
|
+func GetCentTotalBalance(wxUId int64) int64 {
|
|
|
+ type Ret struct {
|
|
|
+ Count int64
|
|
|
+ }
|
|
|
+ ret := &Ret{}
|
|
|
+ o := orm.NewOrm()
|
|
|
+ tbn := new(CentBalance).TableName()
|
|
|
+ sql := fmt.Sprintf("SELECT sum(`count`) as count FROM `%s` WHERE wx_uid=?;", tbn)
|
|
|
+ err := o.Raw(sql, wxUId).QueryRow(ret)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("GetCentTotalBalance, wxUser:%d err=[%s]", wxUId, err)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ if ret.Count < 0 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return ret.Count
|
|
|
+}
|
|
|
+
|
|
|
+func GetCentBalanceByWxUIdAndRIdAndSource(wxUId int64, rId, source string) *CentBalance {
|
|
|
+ item := &CentBalance{}
|
|
|
+ if err := orm.NewOrm().QueryTable(item).
|
|
|
+ Filter("wx_uid", wxUId).
|
|
|
+ Filter("source", source).
|
|
|
+ Filter("relate_id", rId).OrderBy("-created_at").Limit(1).
|
|
|
+ One(item); err != nil {
|
|
|
+ beego.BeeLogger.Info("GetCentBalanceByWxUIdAndRIdAndSource(%d,%s,%s), err=%s",
|
|
|
+ wxUId, rId, source, err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return item
|
|
|
+}
|
|
|
+
|
|
|
+func GetCentBalanceBySourceAndRId(source, rId string) *CentBalance {
|
|
|
+ item := &CentBalance{}
|
|
|
+ if err := orm.NewOrm().QueryTable(item).
|
|
|
+ Filter("source", source).
|
|
|
+ Filter("relate_id", rId).Limit(1).
|
|
|
+ One(item); err != nil {
|
|
|
+ beego.BeeLogger.Info("GetCentBalanceBySourceAndRId(%s,%s), err=%s",
|
|
|
+ source, rId, err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return item
|
|
|
+}
|
|
|
+
|
|
|
+func GetCentById(id int64) *CentBalance {
|
|
|
+ item := &CentBalance{}
|
|
|
+ if err := orm.NewOrm().QueryTable(item).
|
|
|
+ Filter("id", id).Limit(1).
|
|
|
+ One(item); err != nil {
|
|
|
+ beego.BeeLogger.Debug("GetCentById(%s), err=%s",
|
|
|
+ id, err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return item
|
|
|
+}
|