Forráskód Böngészése

cent many table manage func

abiao 5 éve
szülő
commit
9b5ec68f82

+ 173 - 0
go/gopath/src/fohow.com/apps/models/economic_model/cent_economic.go

@@ -0,0 +1,173 @@
+package economic_model
+
+import (
+	"fmt"
+	"time"
+
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+
+	"fohow.com/cache"
+)
+
+const (
+	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      = "新人注册"
+	EXCXHANGE_PRODUCT      = "exchange_product"
+	EXCXHANGE_PRODUCT_NAME = "购物扣除"
+	PROMOTION_SEND         = "promotion_send"
+	PROMOTION_SEND_NAME    = "促销活动赠送"
+	PRE_KEY_DAILY          = "day_sign"
+)
+
+//积分余额表
+type CentEconomic 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 *CentEconomic) GetTable(WxUserId int64) string {
+	split := WxUserId % 10 // TODO: 这里先粗暴的分 10 张表
+	return fmt.Sprintf("cent%d_balance", split)
+}
+
+func (self *CentEconomic) Create(wxUId, c int64, s, rId, remark string) (item *CentEconomic) {
+	item = &CentEconomic{
+		Count:    c,
+		WxUId:    wxUId,
+		Source:   s,
+		RelateId: rId,
+		Remark:   remark,
+	}
+	table := self.GetTable(wxUId)
+	sql := fmt.Sprintf("insert into %s(count,wx_uid,source,relate_id,remark)values(?,?,?,?,?)", table)
+
+	o := orm.NewOrm()
+	result, err := o.Raw(sql, item.Count, item.WxUId, item.Source, item.RelateId, item.Remark).Exec()
+	if err != nil {
+		beego.BeeLogger.Debug("insert into cent economice with wxUId=%d, err=[%s]", wxUId, err)
+		return nil
+	}
+	item.Id, _ = result.LastInsertId()
+	return item
+}
+
+//返回source名称
+func (self *CentEconomic) 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
+	case EXCXHANGE_PRODUCT:
+		sourceName = EXCXHANGE_PRODUCT_NAME
+	case PROMOTION_SEND:
+		sourceName = PROMOTION_SEND_NAME
+	}
+	return sourceName
+}
+
+//获取某人的积分流列表
+func GetCentEconomicListByWxUId(wxUId, page, perPage int64, useCache bool) (list []*CentEconomic) {
+	k := fmt.Sprintf("economic_model.GetCentEconomicListByWxUId(%d).page(%d).perPage(%d)", wxUId, page, perPage)
+	if useCache {
+		if s, ok := cache.Cache.Get(k).([]*CentEconomic); ok {
+			return s
+		}
+	}
+	sql := `
+		select id,wx_uid,count,source,relate_id,remark,created_at,updated_at from
+		%s
+		where  wx_uid = ? ORDER BY id desc LIMIT ? OFFSET ?; ;
+		`
+	//table := CentEconomic{}.GetTable(wxUId)
+	table := new(CentEconomic).GetTable(wxUId)
+	query := fmt.Sprintf(sql, table)
+
+	_, err := orm.NewOrm().Raw(query, wxUId, page, perPage).QueryRows(&list)
+	if err != nil {
+		beego.BeeLogger.Warn("three_wx_model.GetCentEconomicListByWxUId(%d) err=%s", wxUId, err)
+		return nil
+	}
+	cache.Cache.Put(k, list, 10*time.Minute)
+	return list
+}
+
+//获取某人的积分流列表总条数
+func GetCentEconomicCountByWxUId(wxUId int64) int64 {
+	type Ret struct {
+		Count int64
+	}
+	ret := &Ret{}
+	o := orm.NewOrm()
+	tbn := new(CentEconomic).GetTable(wxUId)
+	sql := fmt.Sprintf("SELECT count(1) as count FROM `%s` WHERE wx_uid=?;", tbn)
+	err := o.Raw(sql, wxUId).QueryRow(ret)
+	if err != nil {
+		beego.BeeLogger.Error("GetCentEconomicCountByWxUId, 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(CentEconomic).GetTable(wxUId)
+	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
+}

+ 9 - 0
go/gopath/src/fohow.com/apps/models/economic_model/init.go

@@ -0,0 +1,9 @@
+package economic_model
+
+import (
+	"github.com/astaxie/beego/orm"
+)
+
+func init() {
+	orm.RegisterModel(new(CentEconomic), new(MainEconomic))
+}

+ 69 - 0
go/gopath/src/fohow.com/apps/models/economic_model/main_economic.go

@@ -0,0 +1,69 @@
+package economic_model
+
+import (
+	"fmt"
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+	"time"
+	// "fohow.com/libs/wx_mp"
+)
+
+const (
+	main_economics_tablename = "main_economics"
+)
+
+type MainEconomic struct {
+	Id        int64     `orm:"column(id);pk"                                  json:"id"`     // int(11)
+	Cent      int64     `orm:"column(cent);null"                              json:"cent"`   // int(11)
+	WxUId     int64     `orm:"column(wx_uid);null"                            json:"wx_uid"` // varchar(20)
+	IsWrong   bool      `orm:"column(is_wrong)"                               json:"is_wrong"`
+	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`     // datetime
+	CTime     int64     `orm:"-"                                              json:"ctime"` // datetime
+	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`     // datetime
+}
+
+func (self *MainEconomic) TableName() string {
+	return main_economics_tablename
+}
+
+func (self *MainEconomic) Create(wxUId, c int64) (item *MainEconomic) {
+	item = &MainEconomic{
+		Cent:  c,
+		WxUId: wxUId,
+	}
+	id, err := orm.NewOrm().Insert(item)
+	if err != nil {
+		beego.BeeLogger.Error("Create MainEconomic err=[%s]", err)
+		return nil
+	}
+	item.Id = id
+	return item
+}
+
+func GetMainEconomicByWxUId(wxUId int64, useCache bool) *MainEconomic {
+	k := fmt.Sprintf("economic_model.GetMainEconomicByWxUId(%d)", wxUId)
+	if useCache {
+		if usr, ok := cache.Cache.Get(k).(*MainEconomic); ok {
+			return usr
+		}
+	}
+	usr := new(MainEconomic)
+	o := orm.NewOrm()
+	err := o.QueryTable(usr).Filter("wx_uid", wxUId).Limit(1).One(usr)
+	if err != nil {
+		beego.Debug("GetMainEconomicByWxUId(%d), err=[%s]", wxUId, err)
+		return nil
+	} else {
+		cache.Cache.Put(k, usr, 24*time.Hour)
+		return usr
+	}
+}
+
+func (self *MainEconomic) Save() error {
+	if _, err := orm.NewOrm().Update(self); err != nil {
+		beego.BeeLogger.Error("Save MainEconomic id=[%d] .err=[%s]", self.Id, err)
+		return err
+	}
+	return nil
+}