| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 |
- package user_model
- import (
- "fmt"
- "time"
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/orm"
- "fohow.com/cache"
- )
- const (
- INVITE_BENEFIT_RATE = 5 //佣金比例, 支付金额的10%。
- invite_orders_tablename = "invite_benefit_orders"
- SOURCE_BINDING = "binding" //用户绑定
- SOURCE_PRODUCT_BENEFIT = "product_benefit" //商品佣金
- FX_SOURCE_PRODUCT_BENEFIT = "fx_product_benefit" //二级商品佣金
- SOURCE_PROJECT_BENEFIT = "project_benefit" //申请店长佣金
- BALANCE_BENEFIT = "balance_benefit" //提货券充值佣金
- SOURCE_SHOP_BENEFIT = "shop_benefit" //申请店长佣金
- SECOND_RELATE_ID_PREFIT = "SECOND_"
- )
- type InviteOrder struct {
- Id int64 `orm:"column(id);pk" json:"id"` // int(11)
- BenefitWxUId int64 `orm:"column(benefit_wx_uid)" json:"benefit_wx_uid"` // int(11)
- WxUId int64 `orm:"column(wx_uid)" json:"wx_uid"` // int(11)
- IndWxUId int64 `orm:"column(ind_wx_uid)" json:"ind_wx_uid"`
- Count int64 `orm:"column(count)" json:"count"` // int(11)
- Amount int64 `orm:"column(amount)" json:"amount"` // int(11)
- Source string `orm:"column(source);null" json:"source"` // varchar(64)
- RelateId string `orm:"column(relate_id);null" json:"relate_id"` // varchar(255)
- IsEnterBalance bool `orm:"column(is_enter_balance)" json:"is_enter_balance"`
- EnterTime time.Time `orm:"column(enter_time);type(datetime)" json:"enter_time"`
- 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 *InviteOrder) TableName() string {
- return invite_orders_tablename
- }
- func (self *InviteOrder) Create(bUId, wxUId, indWxUId, count, amount int64, source, relateId string) *InviteOrder {
- item := &InviteOrder{
- BenefitWxUId: bUId,
- WxUId: wxUId,
- IndWxUId: indWxUId,
- Count: count,
- Amount: amount,
- Source: source,
- RelateId: relateId,
- }
- id, err := orm.NewOrm().Insert(item)
- if err != nil {
- beego.BeeLogger.Error("insert InviteOrder err=[%s]", err)
- return nil
- }
- item.Id = id
- return item
- }
- func (self *InviteOrder) Save() error {
- if _, err := orm.NewOrm().Update(self); err != nil {
- beego.BeeLogger.Error("Save InviteOrder id=[%d] .err=[%s]", self.Id, err)
- return err
- }
- return nil
- }
- func GetInviteOrderByWxUIdAndSourceAndRId(wxUId int64, source, rId string) *InviteOrder {
- item := &InviteOrder{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("wx_uid", wxUId).
- Filter("source", source).
- Filter("relate_id", rId).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetInviteOrderByWxUIdAndSourceAndRId(%d,%s,%s), err=%s",
- wxUId, source, rId, err)
- return nil
- }
- return item
- }
- func GetInviteOrderByBUIdAndWxUIdAndIndWxUIdAndSource(bUId, wxUId, indWxUId int64, source string) *InviteOrder {
- item := &InviteOrder{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("benefit_wx_uid", bUId).
- Filter("wx_uid", wxUId).
- Filter("ind_wx_uid", indWxUId).
- Filter("source", source).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetInviteOrderByBUIdAndWxUIdAndIndWxUIdAndSource(%d, %d, %d,%s), err=%s",
- bUId, wxUId, indWxUId, source, err)
- return nil
- }
- return item
- }
- func GetInviteOrderByRId(rId string) *InviteOrder {
- item := &InviteOrder{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("relate_id", rId).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetInviteOrderByRId(%s), err=%s",
- rId, err)
- return nil
- }
- return item
- }
- //邀请的下线
- type Invitee struct {
- Id int64 `orm:"column(id)" json:"id"` //微信ID
- Total int64 `orm:"column(total)" json:"total"` //佣金总额
- Amount int64 `orm:"column(amount)" json:"amount"` //消费总额
- Nickname string `orm:"column(nickname)" json:"nickname"` //昵称
- Head string `orm:"column(head)" json:"head"` //头像
- WxUId int64 `orm:"column(wx_uid)" json:"wx_uid"` //用户Id
- ShowInviteMode int64 `orm:"column(show_invite_mode)" json:"show_invite_mode"` // 群主标记
- //团队业绩
- SaleGroup float64 `orm:"column(sale_group);null" json:"sale_group"` // int(11)
- SaleGroupSum float64 `orm:"column(sale_group_sum)" json:"sale_group_sum"` // datetime
- NeInvitee interface{} `orm:"-" json:"ne_invitee"` //WxUId下一级邀请关系
- NeInviteeCount int64 `orm:"-" json:"ne_invitee_count"` //WxUId下一级邀请总数
- Status int64 `orm:"-" json:"status"` //代理状态
- }
- //邀请的一级关系- 总佣金
- func GetInviteListByInviteId(inviteId, page, perPage int64, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetInviteListByInviteId(%d).page(%d).perPage(%d)", inviteId, page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select sum(count) as total, sum(amount) as amount, wx_users.nickname, wx_users.head, wx_uid, wx_users.sale_group, wx_users.sale_group_sum from %s left join wx_users on wx_users.id = %s.wx_uid where benefit_wx_uid = ? group by wx_uid order by total desc limit %d,%d", tbn, tbn, (page-1)*perPage, perPage)
- _, err := o.Raw(sql, inviteId).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetInviteListByInviteId err=[%s], invite id:%d", inviteId, err)
- return nil
- }
- for _, item := range list {
- item.Head = GetFullImgUrl(item.Head)
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //获取群主邀请关系
- func GetIntroListByIntroInner(introInnerNo string, page, perPage int64, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetIntroListByIntroInner(%s).page(%d).perPage(%d)", introInnerNo, page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- sql := `
- SELECT
- b.total,
- a.nickname,
- a.head,
- a.id,
- a.show_invite_mode
- FROM
- wx_users a
- LEFT JOIN ( SELECT sum( count ) AS total, sum( amount ) AS amount, benefit_wx_uid AS wx_uid from invite_benefit_orders WHERE 1 group by benefit_wx_uid ) b ON a.id = b.wx_uid
- WHERE
- a.intro_inner_no LIKE %s
- ORDER BY
- b.total DESC,
- a.intro_inner_no
- LIMIT %d,%d;
- `
- sql = fmt.Sprintf(sql, "'%"+introInnerNo+"%'", (page-1)*perPage, perPage)
- _, err := o.Raw(sql).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetIntroListByIntroInner err=[%s], introInnerNo id:%s", err, introInnerNo)
- return nil
- }
- for _, item := range list {
- item.Head = GetFullImgUrl(item.Head)
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //获取我的推广显示
- func GetMyIntroListByInviteId(inviteId, page, perPage int64, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetMyIntroListByInviteId(%d).page(%d).perPage(%d)", inviteId, page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- sql := `
- SELECT
- b.total,
- a.nickname,
- a.head,
- a.sale_group,
- a.sale_group_sum,
- a.id,
- a.show_invite_mode
- FROM
- wx_users a
- LEFT JOIN ( SELECT sum( count ) AS total, sum( amount ) AS amount, benefit_wx_uid AS wx_uid from invite_benefit_orders WHERE 1 group by benefit_wx_uid ) b ON a.id = b.wx_uid
- WHERE
- a.intro_user_id=%d or a.invite_id=%d
- ORDER BY
- a.sale_group DESC
- LIMIT %d,%d;
- `
- sql = fmt.Sprintf(sql, inviteId, inviteId, (page-1)*perPage, perPage)
- _, err := o.Raw(sql).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetMyIntroListByInviteId err=[%s], inviteId id:%d", err, inviteId)
- return nil
- }
- for _, item := range list {
- //beego.BeeLogger.Warn("%v", item)
- item.Head = GetFullImgUrl(item.Head)
- //代理状态判断---普通会员(status=0),则显示 申请代理
- if item.ShowInviteMode == 0 {
- item.Status = 0
- record := GetAgentApplyByWxUId(item.Id)
- if record != nil {
- item.Status = 1
- }
- } else {
- item.Status = 2
- }
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //群内人员总数
- func GetMyIntroCountByInviteId(inviteId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
- err := o.Raw(sql).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetMyIntroCountByInviteId, inviteId:%d, err=[%s]", inviteId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //邀请的一级关系- 每月的佣金
- func GetInviteListByInviteIdAndTime(inviteId, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetInviteListByInviteIdAndTime(%d).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := `
- select sum(b1.count) as total,
- sum(b1.amount) as amount,
- u.nickname,
- u.head,
- o.wx_uid
- from %s o
- LEFT JOIN(
- select *
- from %s
- WHERE benefit_wx_uid= ?
- and date_format(date_add(created_at, interval 8 HOUR), '%s')= ? as b1 on o.id= b1.id
- LEFT JOIN wx_users u on u.id= o.wx_uid
- where o.benefit_wx_uid= ?
- GROUP BY o.wx_uid
- ORDER BY total desc
- limit %d,%d;
- `
- sql = fmt.Sprintf(sql, tbn, tbn, "%Y%m", (page-1)*perPage, perPage)
- _, err := o.Raw(sql, inviteId, createTime.Format("200601"), inviteId).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetInviteListByInviteIdAndTime err=[%s], invite id:%d, createTime: %s", inviteId, createTime.Format("2006-01-02"), err)
- return nil
- }
- for _, item := range list {
- item.Head = GetFullImgUrl(item.Head)
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //群主关系- 每月的佣金
- func GetIntroListByInnerNoAndTime(innerNo string, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetIntroListByInnerNoAndTime(%s).page(%d).perPage(%d)", innerNo, createTime.Format("2006-01-02"), page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- //tbn := new(InviteOrder).TableName()
- sql := `
- SELECT
- b.total,
- a.nickname,
- a.head,
- a.id,
- a.show_invite_mode
- FROM
- wx_users a
- LEFT JOIN ( SELECT sum( count ) AS total, sum( amount ) AS amount, benefit_wx_uid AS wx_uid FROM invite_benefit_orders WHERE date_format(date_add(created_at, interval 8 HOUR), '%s')= ? GROUP BY benefit_wx_uid ) b ON a.id = b.wx_uid
- WHERE
- a.intro_inner_no LIKE %s
- ORDER BY
- b.total DESC,
- a.intro_inner_no
- LIMIT %d,%d;
- `
- sql = fmt.Sprintf(sql, "%Y%m", "'%"+innerNo+"%'", (page-1)*perPage, perPage)
- _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], innerNo:%s, createTime: %s", err, innerNo, createTime.Format("2006-01-02"))
- return nil
- }
- for _, item := range list {
- item.Head = GetFullImgUrl(item.Head)
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //邀请好友人数
- func GetInviteListCountByInviteId(inviteId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select count(DISTINCT wx_uid) as count from %s where benefit_wx_uid = ?;", tbn)
- err := o.Raw(sql, inviteId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetInviteListCountByInviteId, id:%d, err=[%s]", inviteId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //群主关系- 每月的佣金
- func GetMyMonthIntroListByInviteId(inviteId int64, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
- k := fmt.Sprintf("user_model.GetMyMonthIntroListByInviteId(%d).Time(%s).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
- return s
- }
- }
- o := orm.NewOrm()
- //tbn := new(InviteOrder).TableName()
- sql := `
- SELECT
- b.total,
- a.nickname,
- a.head,
- a.id,
- a.sale_group,
- a.sale_group_sum,
- a.show_invite_mode
- FROM
- wx_users a
- LEFT JOIN ( SELECT sum( count ) AS total, sum( amount ) AS amount, benefit_wx_uid AS wx_uid FROM invite_benefit_orders WHERE date_format(date_add(created_at, interval 8 HOUR), '%s')= ? GROUP BY benefit_wx_uid ) b ON a.id = b.wx_uid
- WHERE
- a.intro_user_id=%d or a.invite_id=%d
- ORDER BY
- a.sale_group DESC
- LIMIT %d,%d;
- `
- sql = fmt.Sprintf(sql, "%Y%m", inviteId, inviteId, (page-1)*perPage, perPage)
- _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
- if err != nil {
- beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], inviteId:%d, createTime: %s", err, inviteId, createTime.Format("2006-01-02"))
- return nil
- }
- for _, item := range list {
- item.Head = GetFullImgUrl(item.Head)
- //代理状态判断---普通会员(status=0),则显示 申请代理
- if item.ShowInviteMode == 0 {
- item.Status = 0
- record := GetAgentApplyByWxUId(item.Id)
- if record != nil {
- item.Status = 1
- }
- } else {
- item.Status = 2
- }
- }
- cache.Cache.Put(k, list, 1*time.Minute)
- return list
- }
- //群内人员总数
- func GetMyMonthIntroListCount(inviteId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
- err := o.Raw(sql).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetMyMonthIntroListCount, inviteId:%d, err=[%s]", inviteId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //群内人员总数
- func GetIntroCountByIntroInner(introInner string) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_inner_no like %s ;", "'%"+introInner+"%'")
- err := o.Raw(sql).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetIntroCountByIntroInner, introInner:%d, err=[%s]", introInner, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //分佣总额
- func GetInviteCountByInviteId(inviteId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
- err := o.Raw(sql, inviteId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetInviteCountByInviteId, id:%d, err=[%s]", inviteId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //群主分佣总额
- func GetIntroCountByIntroId(introId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
- err := o.Raw(sql, introId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetIntroCountByIntroId, id:%d, err=[%s]", introId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //某月的分佣总额
- func GetInviteCountByInviteIdAndTime(inviteId int64, createTime time.Time) int64 {
- type Ret struct {
- Count int64 `json:"count"` //金额
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ? and date_format(date_add(created_at, interval 8 HOUR), '%s') = ?;", tbn, "%Y%m")
- err := o.Raw(sql, inviteId, createTime.Format("200601")).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetInviteCountByInviteIdAndTime, id:%d, createTime: %s, err=[%s]", inviteId, createTime.Format("200601"), err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //某月wx_uid邀请的二级下线里产生佣金的人数
- func GetInviteConsumeCountByBeWxUIdAndWxUIdAndTime(benefitWxUid, wxUId int64, createTime time.Time) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select count(DISTINCT ind_wx_uid) as count from %s where benefit_wx_uid = ? and wx_uid = ? and wx_uid != ind_wx_uid and DATE_FORMAT(DATE_ADD(created_at,INTERVAL 8 HOUR),'%s') = ? and count > 0;", tbn, "%Y%m")
- err := o.Raw(sql, benefitWxUid, wxUId, createTime.Format("200601")).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetInviteConsumeCountByWxUIdAndTime, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //wx_uid邀请的二级下线里产生佣金的人数
- func GetInviteConsumeCountByBeWxUIdAndWxUId(benefitWxUid, wxUId int64) int64 {
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(InviteOrder).TableName()
- sql := fmt.Sprintf("select count(DISTINCT ind_wx_uid) as count from %s where benefit_wx_uid = ? and wx_uid = ? and wx_uid != ind_wx_uid and count > 0;", tbn, "%Y%m")
- err := o.Raw(sql, benefitWxUid, wxUId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("GetInviteConsumeCountByBeWxUIdAndWxUId, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //查找用户最新的商品/项目佣金记录(包括一级和二级)
- func GetLastInviteOrderByBeWxUIdAndSource(benefitWxUid int64, source string) *InviteOrder {
- item := &InviteOrder{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("benefit_wx_uid", benefitWxUid).
- Filter("source", source).OrderBy("-id").Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetLastInviteOrderByBeWxUIdAndSource(%d,%s), err=%s",
- benefitWxUid, source, err)
- return nil
- }
- return item
- }
- //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
- func GetIndWxUIdCountByBenefitWxUIdAndWxUIdAndIndWxUId(benefitWxId, wxUId, indWxUId int64, useCache bool) int64 {
- k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d)", benefitWxId, wxUId, indWxUId)
- if useCache {
- if num, ok := cache.Cache.Get(k).(int64); ok {
- return num
- }
- }
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql :=
- `
- SELECT
- sum(o.count) AS count
- FROM
- invite_benefit_orders o
- WHERE
- o.benefit_wx_uid = ?
- AND o.wx_uid = ?
- AND o.ind_wx_uid = ?;
-
- `
- err := o.Raw(sql, benefitWxId, wxUId, indWxUId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d), err=[%s]", benefitWxId, wxUId, indWxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- cache.Cache.Put(k, ret.Count, 10*time.Minute)
- return ret.Count
- }
- //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
- func GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(benefitWxId, wxUId, indWxUId int64, createdTime time.Time, useCache bool) int64 {
- k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s)", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"))
- if useCache {
- if num, ok := cache.Cache.Get(k).(int64); ok {
- return num
- }
- }
- type Ret struct {
- Count int64 `json:"count"` //人数
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql :=
- `
- SELECT
- sum(o.count) AS count
- FROM
- invite_benefit_orders o
- WHERE
- o.benefit_wx_uid = ?
- AND o.wx_uid = ?
- AND o.ind_wx_uid = ?
- and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?
-
- `
- err := o.Raw(sql, benefitWxId, wxUId, indWxUId, createdTime.Format("200601")).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s), err=[%s]", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"), err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- cache.Cache.Put(k, ret.Count, 10*time.Minute)
- return ret.Count
- }
- //wxUId及下级-贡献的总奖励金
- func GetInvitedTotalCountByBenefitWxUIdAndWxUId(benefitWxId, wxUId int64, useCache bool) int64 {
- k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d)", benefitWxId, wxUId)
- if useCache {
- if num, ok := cache.Cache.Get(k).(int64); ok {
- return num
- }
- }
- type Ret struct {
- Count int64 `json:"count"`
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql :=
- `
- SELECT
- sum(o.count) AS count
- FROM
- invite_benefit_orders o
- WHERE
- o.benefit_wx_uid = ?
- AND o.wx_uid = ?;
-
- `
- err := o.Raw(sql, benefitWxId, wxUId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d), err=[%s]", benefitWxId, wxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- cache.Cache.Put(k, ret.Count, 10*time.Minute)
- return ret.Count
- }
- //wxUId及下级-贡献的本月奖励金
- func GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(benefitWxId, wxUId int64, createdTime time.Time, useCache bool) int64 {
- k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s)", benefitWxId, wxUId, createdTime.Format("200601"))
- if useCache {
- if num, ok := cache.Cache.Get(k).(int64); ok {
- return num
- }
- }
- type Ret struct {
- Count int64 `json:"count"`
- }
- ret := &Ret{}
- o := orm.NewOrm()
- sql :=
- `
- SELECT
- sum(o.count) AS count
- FROM
- invite_benefit_orders o
- WHERE
- o.benefit_wx_uid = ?
- AND o.wx_uid = ?
- and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?;
-
- `
- err := o.Raw(sql, benefitWxId, wxUId, createdTime.Format("200601")).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s), err=[%s]", benefitWxId, wxUId, createdTime.Format("200601"), err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- cache.Cache.Put(k, ret.Count, 10*time.Minute)
- return ret.Count
- }
|