invite.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package user_model
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "github.com/astaxie/beego/orm"
  7. "fohow.com/cache"
  8. )
  9. const (
  10. INVITE_BENEFIT_RATE = 5 //佣金比例, 支付金额的10%。
  11. invite_orders_tablename = "invite_benefit_orders"
  12. SOURCE_BINDING = "binding" //用户绑定
  13. SOURCE_PRODUCT_BENEFIT = "product_benefit" //商品佣金
  14. FX_SOURCE_PRODUCT_BENEFIT = "fx_product_benefit" //二级商品佣金
  15. SOURCE_PROJECT_BENEFIT = "project_benefit" //申请店长佣金
  16. BALANCE_BENEFIT = "balance_benefit" //提货券充值佣金
  17. SOURCE_SHOP_BENEFIT = "shop_benefit" //申请店长佣金
  18. SECOND_RELATE_ID_PREFIT = "SECOND_"
  19. )
  20. type InviteOrder struct {
  21. Id int64 `orm:"column(id);pk" json:"id"` // int(11)
  22. BenefitWxUId int64 `orm:"column(benefit_wx_uid)" json:"benefit_wx_uid"` // int(11)
  23. WxUId int64 `orm:"column(wx_uid)" json:"wx_uid"` // int(11)
  24. IndWxUId int64 `orm:"column(ind_wx_uid)" json:"ind_wx_uid"`
  25. Count int64 `orm:"column(count)" json:"count"` // int(11)
  26. Amount int64 `orm:"column(amount)" json:"amount"` // int(11)
  27. Source string `orm:"column(source);null" json:"source"` // varchar(64)
  28. RelateId string `orm:"column(relate_id);null" json:"relate_id"` // varchar(255)
  29. IsEnterBalance bool `orm:"column(is_enter_balance)" json:"is_enter_balance"`
  30. EnterTime time.Time `orm:"column(enter_time);type(datetime)" json:"enter_time"`
  31. CreatedAt time.Time `orm:"column(created_at);null;auto_now_add;type(datetime)" json:"-"` // datetime
  32. UpdatedAt time.Time `orm:"column(updated_at);null;auto_now;type(datetime)" json:"-"` // datetime
  33. }
  34. func (self *InviteOrder) TableName() string {
  35. return invite_orders_tablename
  36. }
  37. func (self *InviteOrder) Create(bUId, wxUId, indWxUId, count, amount int64, source, relateId string) *InviteOrder {
  38. item := &InviteOrder{
  39. BenefitWxUId: bUId,
  40. WxUId: wxUId,
  41. IndWxUId: indWxUId,
  42. Count: count,
  43. Amount: amount,
  44. Source: source,
  45. RelateId: relateId,
  46. }
  47. id, err := orm.NewOrm().Insert(item)
  48. if err != nil {
  49. beego.BeeLogger.Error("insert InviteOrder err=[%s]", err)
  50. return nil
  51. }
  52. item.Id = id
  53. return item
  54. }
  55. func (self *InviteOrder) Save() error {
  56. if _, err := orm.NewOrm().Update(self); err != nil {
  57. beego.BeeLogger.Error("Save InviteOrder id=[%d] .err=[%s]", self.Id, err)
  58. return err
  59. }
  60. return nil
  61. }
  62. func GetInviteOrderByWxUIdAndSourceAndRId(wxUId int64, source, rId string) *InviteOrder {
  63. item := &InviteOrder{}
  64. if err := orm.NewOrm().QueryTable(item).
  65. Filter("wx_uid", wxUId).
  66. Filter("source", source).
  67. Filter("relate_id", rId).Limit(1).
  68. One(item); err != nil {
  69. beego.BeeLogger.Info("GetInviteOrderByWxUIdAndSourceAndRId(%d,%s,%s), err=%s",
  70. wxUId, source, rId, err)
  71. return nil
  72. }
  73. return item
  74. }
  75. func GetInviteOrderByBUIdAndWxUIdAndIndWxUIdAndSource(bUId, wxUId, indWxUId int64, source string) *InviteOrder {
  76. item := &InviteOrder{}
  77. if err := orm.NewOrm().QueryTable(item).
  78. Filter("benefit_wx_uid", bUId).
  79. Filter("wx_uid", wxUId).
  80. Filter("ind_wx_uid", indWxUId).
  81. Filter("source", source).Limit(1).
  82. One(item); err != nil {
  83. beego.BeeLogger.Info("GetInviteOrderByBUIdAndWxUIdAndIndWxUIdAndSource(%d, %d, %d,%s), err=%s",
  84. bUId, wxUId, indWxUId, source, err)
  85. return nil
  86. }
  87. return item
  88. }
  89. func GetInviteOrderByRId(rId string) *InviteOrder {
  90. item := &InviteOrder{}
  91. if err := orm.NewOrm().QueryTable(item).
  92. Filter("relate_id", rId).Limit(1).
  93. One(item); err != nil {
  94. beego.BeeLogger.Info("GetInviteOrderByRId(%s), err=%s",
  95. rId, err)
  96. return nil
  97. }
  98. return item
  99. }
  100. //邀请的下线
  101. type Invitee struct {
  102. Id int64 `orm:"column(id)" json:"id"` //微信ID
  103. Total int64 `orm:"column(total)" json:"total"` //佣金总额
  104. Amount int64 `orm:"column(amount)" json:"amount"` //消费总额
  105. Nickname string `orm:"column(nickname)" json:"nickname"` //昵称
  106. Head string `orm:"column(head)" json:"head"` //头像
  107. WxUId int64 `orm:"column(wx_uid)" json:"wx_uid"` //用户Id
  108. ShowInviteMode int64 `orm:"column(show_invite_mode)" json:"show_invite_mode"` // 群主标记
  109. //团队业绩
  110. SaleGroup float64 `orm:"column(sale_group);null" json:"sale_group"` // int(11)
  111. SaleGroupSum float64 `orm:"column(sale_group_sum)" json:"sale_group_sum"` // datetime
  112. NeInvitee interface{} `orm:"-" json:"ne_invitee"` //WxUId下一级邀请关系
  113. NeInviteeCount int64 `orm:"-" json:"ne_invitee_count"` //WxUId下一级邀请总数
  114. Status int64 `orm:"-" json:"status"` //代理状态
  115. }
  116. //邀请的一级关系- 总佣金
  117. func GetInviteListByInviteId(inviteId, page, perPage int64, useCache bool) (list []*Invitee) {
  118. k := fmt.Sprintf("user_model.GetInviteListByInviteId(%d).page(%d).perPage(%d)", inviteId, page, perPage)
  119. if useCache {
  120. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  121. return s
  122. }
  123. }
  124. o := orm.NewOrm()
  125. tbn := new(InviteOrder).TableName()
  126. 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)
  127. _, err := o.Raw(sql, inviteId).QueryRows(&list)
  128. if err != nil {
  129. beego.BeeLogger.Error("GetInviteListByInviteId err=[%s], invite id:%d", inviteId, err)
  130. return nil
  131. }
  132. for _, item := range list {
  133. item.Head = GetFullImgUrl(item.Head)
  134. }
  135. cache.Cache.Put(k, list, 1*time.Minute)
  136. return list
  137. }
  138. //获取群主邀请关系
  139. func GetIntroListByIntroInner(introInnerNo string, page, perPage int64, useCache bool) (list []*Invitee) {
  140. k := fmt.Sprintf("user_model.GetIntroListByIntroInner(%s).page(%d).perPage(%d)", introInnerNo, page, perPage)
  141. if useCache {
  142. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  143. return s
  144. }
  145. }
  146. o := orm.NewOrm()
  147. sql := `
  148. SELECT
  149. b.total,
  150. a.nickname,
  151. a.head,
  152. a.id,
  153. a.show_invite_mode
  154. FROM
  155. wx_users a
  156. 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
  157. WHERE
  158. a.intro_inner_no LIKE %s
  159. ORDER BY
  160. b.total DESC,
  161. a.intro_inner_no
  162. LIMIT %d,%d;
  163. `
  164. sql = fmt.Sprintf(sql, "'%"+introInnerNo+"%'", (page-1)*perPage, perPage)
  165. _, err := o.Raw(sql).QueryRows(&list)
  166. if err != nil {
  167. beego.BeeLogger.Error("GetIntroListByIntroInner err=[%s], introInnerNo id:%s", err, introInnerNo)
  168. return nil
  169. }
  170. for _, item := range list {
  171. item.Head = GetFullImgUrl(item.Head)
  172. }
  173. cache.Cache.Put(k, list, 1*time.Minute)
  174. return list
  175. }
  176. //获取我的推广显示
  177. func GetMyIntroListByInviteId(inviteId, page, perPage int64, useCache bool) (list []*Invitee) {
  178. k := fmt.Sprintf("user_model.GetMyIntroListByInviteId(%d).page(%d).perPage(%d)", inviteId, page, perPage)
  179. if useCache {
  180. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  181. return s
  182. }
  183. }
  184. o := orm.NewOrm()
  185. sql := `
  186. SELECT
  187. b.total,
  188. a.nickname,
  189. a.head,
  190. a.sale_group,
  191. a.sale_group_sum,
  192. a.id,
  193. a.show_invite_mode
  194. FROM
  195. wx_users a
  196. 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
  197. WHERE
  198. a.intro_user_id=%d or a.invite_id=%d
  199. ORDER BY
  200. a.sale_group DESC
  201. LIMIT %d,%d;
  202. `
  203. sql = fmt.Sprintf(sql, inviteId, inviteId, (page-1)*perPage, perPage)
  204. _, err := o.Raw(sql).QueryRows(&list)
  205. if err != nil {
  206. beego.BeeLogger.Error("GetMyIntroListByInviteId err=[%s], inviteId id:%d", err, inviteId)
  207. return nil
  208. }
  209. for _, item := range list {
  210. //beego.BeeLogger.Warn("%v", item)
  211. item.Head = GetFullImgUrl(item.Head)
  212. //代理状态判断---普通会员(status=0),则显示 申请代理
  213. if item.ShowInviteMode == 0 {
  214. item.Status = 0
  215. record := GetAgentApplyByWxUId(item.Id)
  216. if record != nil {
  217. item.Status = 1
  218. }
  219. } else {
  220. item.Status = 2
  221. }
  222. }
  223. cache.Cache.Put(k, list, 1*time.Minute)
  224. return list
  225. }
  226. //群内人员总数
  227. func GetMyIntroCountByInviteId(inviteId int64) int64 {
  228. type Ret struct {
  229. Count int64 `json:"count"` //人数
  230. }
  231. ret := &Ret{}
  232. o := orm.NewOrm()
  233. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
  234. err := o.Raw(sql).QueryRow(ret)
  235. if err != nil {
  236. beego.BeeLogger.Error("GetMyIntroCountByInviteId, inviteId:%d, err=[%s]", inviteId, err)
  237. return 0
  238. }
  239. if ret.Count < 0 {
  240. return 0
  241. }
  242. return ret.Count
  243. }
  244. //邀请的一级关系- 每月的佣金
  245. func GetInviteListByInviteIdAndTime(inviteId, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  246. k := fmt.Sprintf("user_model.GetInviteListByInviteIdAndTime(%d).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
  247. if useCache {
  248. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  249. return s
  250. }
  251. }
  252. o := orm.NewOrm()
  253. tbn := new(InviteOrder).TableName()
  254. sql := `
  255. select sum(b1.count) as total,
  256. sum(b1.amount) as amount,
  257. u.nickname,
  258. u.head,
  259. o.wx_uid
  260. from %s o
  261. LEFT JOIN(
  262. select *
  263. from %s
  264. WHERE benefit_wx_uid= ?
  265. and date_format(date_add(created_at, interval 8 HOUR), '%s')= ? as b1 on o.id= b1.id
  266. LEFT JOIN wx_users u on u.id= o.wx_uid
  267. where o.benefit_wx_uid= ?
  268. GROUP BY o.wx_uid
  269. ORDER BY total desc
  270. limit %d,%d;
  271. `
  272. sql = fmt.Sprintf(sql, tbn, tbn, "%Y%m", (page-1)*perPage, perPage)
  273. _, err := o.Raw(sql, inviteId, createTime.Format("200601"), inviteId).QueryRows(&list)
  274. if err != nil {
  275. beego.BeeLogger.Error("GetInviteListByInviteIdAndTime err=[%s], invite id:%d, createTime: %s", inviteId, createTime.Format("2006-01-02"), err)
  276. return nil
  277. }
  278. for _, item := range list {
  279. item.Head = GetFullImgUrl(item.Head)
  280. }
  281. cache.Cache.Put(k, list, 1*time.Minute)
  282. return list
  283. }
  284. //群主关系- 每月的佣金
  285. func GetIntroListByInnerNoAndTime(innerNo string, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  286. k := fmt.Sprintf("user_model.GetIntroListByInnerNoAndTime(%s).page(%d).perPage(%d)", innerNo, createTime.Format("2006-01-02"), page, perPage)
  287. if useCache {
  288. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  289. return s
  290. }
  291. }
  292. o := orm.NewOrm()
  293. //tbn := new(InviteOrder).TableName()
  294. sql := `
  295. SELECT
  296. b.total,
  297. a.nickname,
  298. a.head,
  299. a.id,
  300. a.show_invite_mode
  301. FROM
  302. wx_users a
  303. 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
  304. WHERE
  305. a.intro_inner_no LIKE %s
  306. ORDER BY
  307. b.total DESC,
  308. a.intro_inner_no
  309. LIMIT %d,%d;
  310. `
  311. sql = fmt.Sprintf(sql, "%Y%m", "'%"+innerNo+"%'", (page-1)*perPage, perPage)
  312. _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
  313. if err != nil {
  314. beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], innerNo:%s, createTime: %s", err, innerNo, createTime.Format("2006-01-02"))
  315. return nil
  316. }
  317. for _, item := range list {
  318. item.Head = GetFullImgUrl(item.Head)
  319. }
  320. cache.Cache.Put(k, list, 1*time.Minute)
  321. return list
  322. }
  323. //邀请好友人数
  324. func GetInviteListCountByInviteId(inviteId int64) int64 {
  325. type Ret struct {
  326. Count int64 `json:"count"` //人数
  327. }
  328. ret := &Ret{}
  329. o := orm.NewOrm()
  330. tbn := new(InviteOrder).TableName()
  331. sql := fmt.Sprintf("select count(DISTINCT wx_uid) as count from %s where benefit_wx_uid = ?;", tbn)
  332. err := o.Raw(sql, inviteId).QueryRow(ret)
  333. if err != nil {
  334. beego.BeeLogger.Error("GetInviteListCountByInviteId, id:%d, err=[%s]", inviteId, err)
  335. return 0
  336. }
  337. if ret.Count < 0 {
  338. return 0
  339. }
  340. return ret.Count
  341. }
  342. //群主关系- 每月的佣金
  343. func GetMyMonthIntroListByInviteId(inviteId int64, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  344. k := fmt.Sprintf("user_model.GetMyMonthIntroListByInviteId(%d).Time(%s).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
  345. if useCache {
  346. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  347. return s
  348. }
  349. }
  350. o := orm.NewOrm()
  351. //tbn := new(InviteOrder).TableName()
  352. sql := `
  353. SELECT
  354. b.total,
  355. a.nickname,
  356. a.head,
  357. a.id,
  358. a.sale_group,
  359. a.sale_group_sum,
  360. a.show_invite_mode
  361. FROM
  362. wx_users a
  363. 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
  364. WHERE
  365. a.intro_user_id=%d or a.invite_id=%d
  366. ORDER BY
  367. a.sale_group DESC
  368. LIMIT %d,%d;
  369. `
  370. sql = fmt.Sprintf(sql, "%Y%m", inviteId, inviteId, (page-1)*perPage, perPage)
  371. _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
  372. if err != nil {
  373. beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], inviteId:%d, createTime: %s", err, inviteId, createTime.Format("2006-01-02"))
  374. return nil
  375. }
  376. for _, item := range list {
  377. item.Head = GetFullImgUrl(item.Head)
  378. //代理状态判断---普通会员(status=0),则显示 申请代理
  379. if item.ShowInviteMode == 0 {
  380. item.Status = 0
  381. record := GetAgentApplyByWxUId(item.Id)
  382. if record != nil {
  383. item.Status = 1
  384. }
  385. } else {
  386. item.Status = 2
  387. }
  388. }
  389. cache.Cache.Put(k, list, 1*time.Minute)
  390. return list
  391. }
  392. //群内人员总数
  393. func GetMyMonthIntroListCount(inviteId int64) int64 {
  394. type Ret struct {
  395. Count int64 `json:"count"` //人数
  396. }
  397. ret := &Ret{}
  398. o := orm.NewOrm()
  399. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
  400. err := o.Raw(sql).QueryRow(ret)
  401. if err != nil {
  402. beego.BeeLogger.Error("GetMyMonthIntroListCount, inviteId:%d, err=[%s]", inviteId, err)
  403. return 0
  404. }
  405. if ret.Count < 0 {
  406. return 0
  407. }
  408. return ret.Count
  409. }
  410. //群内人员总数
  411. func GetIntroCountByIntroInner(introInner string) int64 {
  412. type Ret struct {
  413. Count int64 `json:"count"` //人数
  414. }
  415. ret := &Ret{}
  416. o := orm.NewOrm()
  417. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_inner_no like %s ;", "'%"+introInner+"%'")
  418. err := o.Raw(sql).QueryRow(ret)
  419. if err != nil {
  420. beego.BeeLogger.Error("GetIntroCountByIntroInner, introInner:%d, err=[%s]", introInner, err)
  421. return 0
  422. }
  423. if ret.Count < 0 {
  424. return 0
  425. }
  426. return ret.Count
  427. }
  428. //分佣总额
  429. func GetInviteCountByInviteId(inviteId int64) int64 {
  430. type Ret struct {
  431. Count int64 `json:"count"` //人数
  432. }
  433. ret := &Ret{}
  434. o := orm.NewOrm()
  435. tbn := new(InviteOrder).TableName()
  436. sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
  437. err := o.Raw(sql, inviteId).QueryRow(ret)
  438. if err != nil {
  439. beego.BeeLogger.Error("GetInviteCountByInviteId, id:%d, err=[%s]", inviteId, err)
  440. return 0
  441. }
  442. if ret.Count < 0 {
  443. return 0
  444. }
  445. return ret.Count
  446. }
  447. //群主分佣总额
  448. func GetIntroCountByIntroId(introId int64) int64 {
  449. type Ret struct {
  450. Count int64 `json:"count"` //人数
  451. }
  452. ret := &Ret{}
  453. o := orm.NewOrm()
  454. tbn := new(InviteOrder).TableName()
  455. sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
  456. err := o.Raw(sql, introId).QueryRow(ret)
  457. if err != nil {
  458. beego.BeeLogger.Error("GetIntroCountByIntroId, id:%d, err=[%s]", introId, err)
  459. return 0
  460. }
  461. if ret.Count < 0 {
  462. return 0
  463. }
  464. return ret.Count
  465. }
  466. //某月的分佣总额
  467. func GetInviteCountByInviteIdAndTime(inviteId int64, createTime time.Time) int64 {
  468. type Ret struct {
  469. Count int64 `json:"count"` //金额
  470. }
  471. ret := &Ret{}
  472. o := orm.NewOrm()
  473. tbn := new(InviteOrder).TableName()
  474. 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")
  475. err := o.Raw(sql, inviteId, createTime.Format("200601")).QueryRow(ret)
  476. if err != nil {
  477. beego.BeeLogger.Error("GetInviteCountByInviteIdAndTime, id:%d, createTime: %s, err=[%s]", inviteId, createTime.Format("200601"), err)
  478. return 0
  479. }
  480. if ret.Count < 0 {
  481. return 0
  482. }
  483. return ret.Count
  484. }
  485. //某月wx_uid邀请的二级下线里产生佣金的人数
  486. func GetInviteConsumeCountByBeWxUIdAndWxUIdAndTime(benefitWxUid, wxUId int64, createTime time.Time) int64 {
  487. type Ret struct {
  488. Count int64 `json:"count"` //人数
  489. }
  490. ret := &Ret{}
  491. o := orm.NewOrm()
  492. tbn := new(InviteOrder).TableName()
  493. 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")
  494. err := o.Raw(sql, benefitWxUid, wxUId, createTime.Format("200601")).QueryRow(ret)
  495. if err != nil {
  496. beego.BeeLogger.Error("GetInviteConsumeCountByWxUIdAndTime, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
  497. return 0
  498. }
  499. if ret.Count < 0 {
  500. return 0
  501. }
  502. return ret.Count
  503. }
  504. //wx_uid邀请的二级下线里产生佣金的人数
  505. func GetInviteConsumeCountByBeWxUIdAndWxUId(benefitWxUid, wxUId int64) int64 {
  506. type Ret struct {
  507. Count int64 `json:"count"` //人数
  508. }
  509. ret := &Ret{}
  510. o := orm.NewOrm()
  511. tbn := new(InviteOrder).TableName()
  512. 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")
  513. err := o.Raw(sql, benefitWxUid, wxUId).QueryRow(ret)
  514. if err != nil {
  515. beego.BeeLogger.Error("GetInviteConsumeCountByBeWxUIdAndWxUId, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
  516. return 0
  517. }
  518. if ret.Count < 0 {
  519. return 0
  520. }
  521. return ret.Count
  522. }
  523. //查找用户最新的商品/项目佣金记录(包括一级和二级)
  524. func GetLastInviteOrderByBeWxUIdAndSource(benefitWxUid int64, source string) *InviteOrder {
  525. item := &InviteOrder{}
  526. if err := orm.NewOrm().QueryTable(item).
  527. Filter("benefit_wx_uid", benefitWxUid).
  528. Filter("source", source).OrderBy("-id").Limit(1).
  529. One(item); err != nil {
  530. beego.BeeLogger.Info("GetLastInviteOrderByBeWxUIdAndSource(%d,%s), err=%s",
  531. benefitWxUid, source, err)
  532. return nil
  533. }
  534. return item
  535. }
  536. //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
  537. func GetIndWxUIdCountByBenefitWxUIdAndWxUIdAndIndWxUId(benefitWxId, wxUId, indWxUId int64, useCache bool) int64 {
  538. k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d)", benefitWxId, wxUId, indWxUId)
  539. if useCache {
  540. if num, ok := cache.Cache.Get(k).(int64); ok {
  541. return num
  542. }
  543. }
  544. type Ret struct {
  545. Count int64 `json:"count"` //人数
  546. }
  547. ret := &Ret{}
  548. o := orm.NewOrm()
  549. sql :=
  550. `
  551. SELECT
  552. sum(o.count) AS count
  553. FROM
  554. invite_benefit_orders o
  555. WHERE
  556. o.benefit_wx_uid = ?
  557. AND o.wx_uid = ?
  558. AND o.ind_wx_uid = ?;
  559. `
  560. err := o.Raw(sql, benefitWxId, wxUId, indWxUId).QueryRow(ret)
  561. if err != nil {
  562. beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d), err=[%s]", benefitWxId, wxUId, indWxUId, err)
  563. return 0
  564. }
  565. if ret.Count < 0 {
  566. return 0
  567. }
  568. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  569. return ret.Count
  570. }
  571. //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
  572. func GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(benefitWxId, wxUId, indWxUId int64, createdTime time.Time, useCache bool) int64 {
  573. k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s)", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"))
  574. if useCache {
  575. if num, ok := cache.Cache.Get(k).(int64); ok {
  576. return num
  577. }
  578. }
  579. type Ret struct {
  580. Count int64 `json:"count"` //人数
  581. }
  582. ret := &Ret{}
  583. o := orm.NewOrm()
  584. sql :=
  585. `
  586. SELECT
  587. sum(o.count) AS count
  588. FROM
  589. invite_benefit_orders o
  590. WHERE
  591. o.benefit_wx_uid = ?
  592. AND o.wx_uid = ?
  593. AND o.ind_wx_uid = ?
  594. and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?
  595. `
  596. err := o.Raw(sql, benefitWxId, wxUId, indWxUId, createdTime.Format("200601")).QueryRow(ret)
  597. if err != nil {
  598. beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s), err=[%s]", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"), err)
  599. return 0
  600. }
  601. if ret.Count < 0 {
  602. return 0
  603. }
  604. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  605. return ret.Count
  606. }
  607. //wxUId及下级-贡献的总奖励金
  608. func GetInvitedTotalCountByBenefitWxUIdAndWxUId(benefitWxId, wxUId int64, useCache bool) int64 {
  609. k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d)", benefitWxId, wxUId)
  610. if useCache {
  611. if num, ok := cache.Cache.Get(k).(int64); ok {
  612. return num
  613. }
  614. }
  615. type Ret struct {
  616. Count int64 `json:"count"`
  617. }
  618. ret := &Ret{}
  619. o := orm.NewOrm()
  620. sql :=
  621. `
  622. SELECT
  623. sum(o.count) AS count
  624. FROM
  625. invite_benefit_orders o
  626. WHERE
  627. o.benefit_wx_uid = ?
  628. AND o.wx_uid = ?;
  629. `
  630. err := o.Raw(sql, benefitWxId, wxUId).QueryRow(ret)
  631. if err != nil {
  632. beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d), err=[%s]", benefitWxId, wxUId, err)
  633. return 0
  634. }
  635. if ret.Count < 0 {
  636. return 0
  637. }
  638. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  639. return ret.Count
  640. }
  641. //wxUId及下级-贡献的本月奖励金
  642. func GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(benefitWxId, wxUId int64, createdTime time.Time, useCache bool) int64 {
  643. k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s)", benefitWxId, wxUId, createdTime.Format("200601"))
  644. if useCache {
  645. if num, ok := cache.Cache.Get(k).(int64); ok {
  646. return num
  647. }
  648. }
  649. type Ret struct {
  650. Count int64 `json:"count"`
  651. }
  652. ret := &Ret{}
  653. o := orm.NewOrm()
  654. sql :=
  655. `
  656. SELECT
  657. sum(o.count) AS count
  658. FROM
  659. invite_benefit_orders o
  660. WHERE
  661. o.benefit_wx_uid = ?
  662. AND o.wx_uid = ?
  663. and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?;
  664. `
  665. err := o.Raw(sql, benefitWxId, wxUId, createdTime.Format("200601")).QueryRow(ret)
  666. if err != nil {
  667. beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s), err=[%s]", benefitWxId, wxUId, createdTime.Format("200601"), err)
  668. return 0
  669. }
  670. if ret.Count < 0 {
  671. return 0
  672. }
  673. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  674. return ret.Count
  675. }