invite.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. and a.openid!=""
  200. ORDER BY
  201. a.sale_group DESC
  202. LIMIT %d,%d;
  203. `
  204. sql = fmt.Sprintf(sql, inviteId, inviteId, (page-1)*perPage, perPage)
  205. _, err := o.Raw(sql).QueryRows(&list)
  206. if err != nil {
  207. beego.BeeLogger.Error("GetMyIntroListByInviteId err=[%s], inviteId id:%d", err, inviteId)
  208. return nil
  209. }
  210. for _, item := range list {
  211. //beego.BeeLogger.Warn("%v", item)
  212. item.Head = GetFullImgUrl(item.Head)
  213. //代理状态判断---普通会员(status=0),则显示 申请代理
  214. if item.ShowInviteMode == 0 {
  215. item.Status = 0
  216. record := GetAgentApplyByWxUId(item.Id)
  217. if record != nil {
  218. item.Status = 1
  219. }
  220. } else {
  221. item.Status = 2
  222. }
  223. }
  224. cache.Cache.Put(k, list, 1*time.Minute)
  225. return list
  226. }
  227. //群内人员总数
  228. func GetMyIntroCountByInviteId(inviteId int64) int64 {
  229. type Ret struct {
  230. Count int64 `json:"count"` //人数
  231. }
  232. ret := &Ret{}
  233. o := orm.NewOrm()
  234. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where openid!='' and intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
  235. err := o.Raw(sql).QueryRow(ret)
  236. if err != nil {
  237. beego.BeeLogger.Error("GetMyIntroCountByInviteId, inviteId:%d, err=[%s]", inviteId, err)
  238. return 0
  239. }
  240. if ret.Count < 0 {
  241. return 0
  242. }
  243. return ret.Count
  244. }
  245. //邀请的一级关系- 每月的佣金
  246. func GetInviteListByInviteIdAndTime(inviteId, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  247. k := fmt.Sprintf("user_model.GetInviteListByInviteIdAndTime(%d).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
  248. if useCache {
  249. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  250. return s
  251. }
  252. }
  253. o := orm.NewOrm()
  254. tbn := new(InviteOrder).TableName()
  255. sql := `
  256. select sum(b1.count) as total,
  257. sum(b1.amount) as amount,
  258. u.nickname,
  259. u.head,
  260. o.wx_uid
  261. from %s o
  262. LEFT JOIN(
  263. select *
  264. from %s
  265. WHERE benefit_wx_uid= ?
  266. and date_format(date_add(created_at, interval 8 HOUR), '%s')= ? as b1 on o.id= b1.id
  267. LEFT JOIN wx_users u on u.id= o.wx_uid
  268. where o.benefit_wx_uid= ?
  269. GROUP BY o.wx_uid
  270. ORDER BY total desc
  271. limit %d,%d;
  272. `
  273. sql = fmt.Sprintf(sql, tbn, tbn, "%Y%m", (page-1)*perPage, perPage)
  274. _, err := o.Raw(sql, inviteId, createTime.Format("200601"), inviteId).QueryRows(&list)
  275. if err != nil {
  276. beego.BeeLogger.Error("GetInviteListByInviteIdAndTime err=[%s], invite id:%d, createTime: %s", inviteId, createTime.Format("2006-01-02"), err)
  277. return nil
  278. }
  279. for _, item := range list {
  280. item.Head = GetFullImgUrl(item.Head)
  281. }
  282. cache.Cache.Put(k, list, 1*time.Minute)
  283. return list
  284. }
  285. //群主关系- 每月的佣金
  286. func GetIntroListByInnerNoAndTime(innerNo string, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  287. k := fmt.Sprintf("user_model.GetIntroListByInnerNoAndTime(%s).page(%d).perPage(%d)", innerNo, createTime.Format("2006-01-02"), page, perPage)
  288. if useCache {
  289. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  290. return s
  291. }
  292. }
  293. o := orm.NewOrm()
  294. //tbn := new(InviteOrder).TableName()
  295. sql := `
  296. SELECT
  297. b.total,
  298. a.nickname,
  299. a.head,
  300. a.id,
  301. a.show_invite_mode
  302. FROM
  303. wx_users a
  304. 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
  305. WHERE
  306. a.intro_inner_no LIKE %s
  307. ORDER BY
  308. b.total DESC,
  309. a.intro_inner_no
  310. LIMIT %d,%d;
  311. `
  312. sql = fmt.Sprintf(sql, "%Y%m", "'%"+innerNo+"%'", (page-1)*perPage, perPage)
  313. _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
  314. if err != nil {
  315. beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], innerNo:%s, createTime: %s", err, innerNo, createTime.Format("2006-01-02"))
  316. return nil
  317. }
  318. for _, item := range list {
  319. item.Head = GetFullImgUrl(item.Head)
  320. }
  321. cache.Cache.Put(k, list, 1*time.Minute)
  322. return list
  323. }
  324. //邀请好友人数
  325. func GetInviteListCountByInviteId(inviteId int64) int64 {
  326. type Ret struct {
  327. Count int64 `json:"count"` //人数
  328. }
  329. ret := &Ret{}
  330. o := orm.NewOrm()
  331. tbn := new(InviteOrder).TableName()
  332. sql := fmt.Sprintf("select count(DISTINCT wx_uid) as count from %s where benefit_wx_uid = ?;", tbn)
  333. err := o.Raw(sql, inviteId).QueryRow(ret)
  334. if err != nil {
  335. beego.BeeLogger.Error("GetInviteListCountByInviteId, id:%d, err=[%s]", inviteId, err)
  336. return 0
  337. }
  338. if ret.Count < 0 {
  339. return 0
  340. }
  341. return ret.Count
  342. }
  343. //群主关系- 每月的佣金
  344. func GetMyMonthIntroListByInviteId(inviteId int64, page, perPage int64, createTime time.Time, useCache bool) (list []*Invitee) {
  345. k := fmt.Sprintf("user_model.GetMyMonthIntroListByInviteId(%d).Time(%s).page(%d).perPage(%d)", inviteId, createTime.Format("2006-01-02"), page, perPage)
  346. if useCache {
  347. if s, ok := cache.Cache.Get(k).([]*Invitee); ok {
  348. return s
  349. }
  350. }
  351. o := orm.NewOrm()
  352. //tbn := new(InviteOrder).TableName()
  353. sql := `
  354. SELECT
  355. b.total,
  356. a.nickname,
  357. a.head,
  358. a.id,
  359. a.sale_group,
  360. a.sale_group_sum,
  361. a.show_invite_mode
  362. FROM
  363. wx_users a
  364. 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
  365. WHERE
  366. a.intro_user_id=%d or a.invite_id=%d
  367. and a.openid!=""
  368. ORDER BY
  369. a.sale_group DESC
  370. LIMIT %d,%d;
  371. `
  372. sql = fmt.Sprintf(sql, "%Y%m", inviteId, inviteId, (page-1)*perPage, perPage)
  373. _, err := o.Raw(sql, createTime.Format("200601")).QueryRows(&list)
  374. if err != nil {
  375. beego.BeeLogger.Error("GetIntroListByInnerNoAndTime err=[%s], inviteId:%d, createTime: %s", err, inviteId, createTime.Format("2006-01-02"))
  376. return nil
  377. }
  378. for _, item := range list {
  379. item.Head = GetFullImgUrl(item.Head)
  380. //代理状态判断---普通会员(status=0),则显示 申请代理
  381. if item.ShowInviteMode == 0 {
  382. item.Status = 0
  383. record := GetAgentApplyByWxUId(item.Id)
  384. if record != nil {
  385. item.Status = 1
  386. }
  387. } else {
  388. item.Status = 2
  389. }
  390. }
  391. cache.Cache.Put(k, list, 1*time.Minute)
  392. return list
  393. }
  394. //群内人员总数
  395. func GetMyMonthIntroListCount(inviteId int64) int64 {
  396. type Ret struct {
  397. Count int64 `json:"count"` //人数
  398. }
  399. ret := &Ret{}
  400. o := orm.NewOrm()
  401. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where openid!='' and intro_user_id=%d or invite_id=%d ;", inviteId, inviteId)
  402. err := o.Raw(sql).QueryRow(ret)
  403. if err != nil {
  404. beego.BeeLogger.Error("GetMyMonthIntroListCount, inviteId:%d, err=[%s]", inviteId, err)
  405. return 0
  406. }
  407. if ret.Count < 0 {
  408. return 0
  409. }
  410. return ret.Count
  411. }
  412. //群内人员总数
  413. func GetIntroCountByIntroInner(introInner string) int64 {
  414. type Ret struct {
  415. Count int64 `json:"count"` //人数
  416. }
  417. ret := &Ret{}
  418. o := orm.NewOrm()
  419. sql := fmt.Sprintf("select count(DISTINCT id) as count from wx_users where intro_inner_no like %s ;", "'%"+introInner+"%'")
  420. err := o.Raw(sql).QueryRow(ret)
  421. if err != nil {
  422. beego.BeeLogger.Error("GetIntroCountByIntroInner, introInner:%d, err=[%s]", introInner, err)
  423. return 0
  424. }
  425. if ret.Count < 0 {
  426. return 0
  427. }
  428. return ret.Count
  429. }
  430. //分佣总额
  431. func GetInviteCountByInviteId(inviteId int64) int64 {
  432. type Ret struct {
  433. Count int64 `json:"count"` //人数
  434. }
  435. ret := &Ret{}
  436. o := orm.NewOrm()
  437. tbn := new(InviteOrder).TableName()
  438. sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
  439. err := o.Raw(sql, inviteId).QueryRow(ret)
  440. if err != nil {
  441. beego.BeeLogger.Error("GetInviteCountByInviteId, id:%d, err=[%s]", inviteId, err)
  442. return 0
  443. }
  444. if ret.Count < 0 {
  445. return 0
  446. }
  447. return ret.Count
  448. }
  449. //群主分佣总额
  450. func GetIntroCountByIntroId(introId int64) int64 {
  451. type Ret struct {
  452. Count int64 `json:"count"` //人数
  453. }
  454. ret := &Ret{}
  455. o := orm.NewOrm()
  456. tbn := new(InviteOrder).TableName()
  457. sql := fmt.Sprintf("select sum(count) as count from %s where benefit_wx_uid = ?;", tbn)
  458. err := o.Raw(sql, introId).QueryRow(ret)
  459. if err != nil {
  460. beego.BeeLogger.Error("GetIntroCountByIntroId, id:%d, err=[%s]", introId, err)
  461. return 0
  462. }
  463. if ret.Count < 0 {
  464. return 0
  465. }
  466. return ret.Count
  467. }
  468. //某月的分佣总额
  469. func GetInviteCountByInviteIdAndTime(inviteId int64, createTime time.Time) int64 {
  470. type Ret struct {
  471. Count int64 `json:"count"` //金额
  472. }
  473. ret := &Ret{}
  474. o := orm.NewOrm()
  475. tbn := new(InviteOrder).TableName()
  476. 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")
  477. err := o.Raw(sql, inviteId, createTime.Format("200601")).QueryRow(ret)
  478. if err != nil {
  479. beego.BeeLogger.Error("GetInviteCountByInviteIdAndTime, id:%d, createTime: %s, err=[%s]", inviteId, createTime.Format("200601"), err)
  480. return 0
  481. }
  482. if ret.Count < 0 {
  483. return 0
  484. }
  485. return ret.Count
  486. }
  487. //某月wx_uid邀请的二级下线里产生佣金的人数
  488. func GetInviteConsumeCountByBeWxUIdAndWxUIdAndTime(benefitWxUid, wxUId int64, createTime time.Time) int64 {
  489. type Ret struct {
  490. Count int64 `json:"count"` //人数
  491. }
  492. ret := &Ret{}
  493. o := orm.NewOrm()
  494. tbn := new(InviteOrder).TableName()
  495. 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")
  496. err := o.Raw(sql, benefitWxUid, wxUId, createTime.Format("200601")).QueryRow(ret)
  497. if err != nil {
  498. beego.BeeLogger.Error("GetInviteConsumeCountByWxUIdAndTime, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
  499. return 0
  500. }
  501. if ret.Count < 0 {
  502. return 0
  503. }
  504. return ret.Count
  505. }
  506. //wx_uid邀请的二级下线里产生佣金的人数
  507. func GetInviteConsumeCountByBeWxUIdAndWxUId(benefitWxUid, wxUId int64) int64 {
  508. type Ret struct {
  509. Count int64 `json:"count"` //人数
  510. }
  511. ret := &Ret{}
  512. o := orm.NewOrm()
  513. tbn := new(InviteOrder).TableName()
  514. 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")
  515. err := o.Raw(sql, benefitWxUid, wxUId).QueryRow(ret)
  516. if err != nil {
  517. beego.BeeLogger.Error("GetInviteConsumeCountByBeWxUIdAndWxUId, benefitWxUid:%d, wxUId:%d, err=[%s]", benefitWxUid, wxUId, err)
  518. return 0
  519. }
  520. if ret.Count < 0 {
  521. return 0
  522. }
  523. return ret.Count
  524. }
  525. //查找用户最新的商品/项目佣金记录(包括一级和二级)
  526. func GetLastInviteOrderByBeWxUIdAndSource(benefitWxUid int64, source string) *InviteOrder {
  527. item := &InviteOrder{}
  528. if err := orm.NewOrm().QueryTable(item).
  529. Filter("benefit_wx_uid", benefitWxUid).
  530. Filter("source", source).OrderBy("-id").Limit(1).
  531. One(item); err != nil {
  532. beego.BeeLogger.Info("GetLastInviteOrderByBeWxUIdAndSource(%d,%s), err=%s",
  533. benefitWxUid, source, err)
  534. return nil
  535. }
  536. return item
  537. }
  538. //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
  539. func GetIndWxUIdCountByBenefitWxUIdAndWxUIdAndIndWxUId(benefitWxId, wxUId, indWxUId int64, useCache bool) int64 {
  540. k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d)", benefitWxId, wxUId, indWxUId)
  541. if useCache {
  542. if num, ok := cache.Cache.Get(k).(int64); ok {
  543. return num
  544. }
  545. }
  546. type Ret struct {
  547. Count int64 `json:"count"` //人数
  548. }
  549. ret := &Ret{}
  550. o := orm.NewOrm()
  551. sql :=
  552. `
  553. SELECT
  554. sum(o.count) AS count
  555. FROM
  556. invite_benefit_orders o
  557. WHERE
  558. o.benefit_wx_uid = ?
  559. AND o.wx_uid = ?
  560. AND o.ind_wx_uid = ?;
  561. `
  562. err := o.Raw(sql, benefitWxId, wxUId, indWxUId).QueryRow(ret)
  563. if err != nil {
  564. beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenefitWxUIdAndWxUId(%d, %d, %d), err=[%s]", benefitWxId, wxUId, indWxUId, err)
  565. return 0
  566. }
  567. if ret.Count < 0 {
  568. return 0
  569. }
  570. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  571. return ret.Count
  572. }
  573. //benefitWxId下级wxUId的下级indWxUId给benefit贡献的奖励金总和
  574. func GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(benefitWxId, wxUId, indWxUId int64, createdTime time.Time, useCache bool) int64 {
  575. k := fmt.Sprintf("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s)", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"))
  576. if useCache {
  577. if num, ok := cache.Cache.Get(k).(int64); ok {
  578. return num
  579. }
  580. }
  581. type Ret struct {
  582. Count int64 `json:"count"` //人数
  583. }
  584. ret := &Ret{}
  585. o := orm.NewOrm()
  586. sql :=
  587. `
  588. SELECT
  589. sum(o.count) AS count
  590. FROM
  591. invite_benefit_orders o
  592. WHERE
  593. o.benefit_wx_uid = ?
  594. AND o.wx_uid = ?
  595. AND o.ind_wx_uid = ?
  596. and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?
  597. `
  598. err := o.Raw(sql, benefitWxId, wxUId, indWxUId, createdTime.Format("200601")).QueryRow(ret)
  599. if err != nil {
  600. beego.BeeLogger.Error("user_model.GetIndWxUIdCountByBenWxUIdAndWxUIdAndIndWxUIdAndTime(%d, %d, %d, %s), err=[%s]", benefitWxId, wxUId, indWxUId, createdTime.Format("2006-01-02"), err)
  601. return 0
  602. }
  603. if ret.Count < 0 {
  604. return 0
  605. }
  606. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  607. return ret.Count
  608. }
  609. //wxUId及下级-贡献的总奖励金
  610. func GetInvitedTotalCountByBenefitWxUIdAndWxUId(benefitWxId, wxUId int64, useCache bool) int64 {
  611. k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d)", benefitWxId, wxUId)
  612. if useCache {
  613. if num, ok := cache.Cache.Get(k).(int64); ok {
  614. return num
  615. }
  616. }
  617. type Ret struct {
  618. Count int64 `json:"count"`
  619. }
  620. ret := &Ret{}
  621. o := orm.NewOrm()
  622. sql :=
  623. `
  624. SELECT
  625. sum(o.count) AS count
  626. FROM
  627. invite_benefit_orders o
  628. WHERE
  629. o.benefit_wx_uid = ?
  630. AND o.wx_uid = ?;
  631. `
  632. err := o.Raw(sql, benefitWxId, wxUId).QueryRow(ret)
  633. if err != nil {
  634. beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUId(%d, %d), err=[%s]", benefitWxId, wxUId, err)
  635. return 0
  636. }
  637. if ret.Count < 0 {
  638. return 0
  639. }
  640. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  641. return ret.Count
  642. }
  643. //wxUId及下级-贡献的本月奖励金
  644. func GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(benefitWxId, wxUId int64, createdTime time.Time, useCache bool) int64 {
  645. k := fmt.Sprintf("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s)", benefitWxId, wxUId, createdTime.Format("200601"))
  646. if useCache {
  647. if num, ok := cache.Cache.Get(k).(int64); ok {
  648. return num
  649. }
  650. }
  651. type Ret struct {
  652. Count int64 `json:"count"`
  653. }
  654. ret := &Ret{}
  655. o := orm.NewOrm()
  656. sql :=
  657. `
  658. SELECT
  659. sum(o.count) AS count
  660. FROM
  661. invite_benefit_orders o
  662. WHERE
  663. o.benefit_wx_uid = ?
  664. AND o.wx_uid = ?
  665. and DATE_FORMAT(DATE_ADD(o.created_at,INTERVAL 8 HOUR),'%Y%m') = ?;
  666. `
  667. err := o.Raw(sql, benefitWxId, wxUId, createdTime.Format("200601")).QueryRow(ret)
  668. if err != nil {
  669. beego.BeeLogger.Error("user_model.GetInvitedTotalCountByBenefitWxUIdAndWxUIdAndTime(%d, %d, %s), err=[%s]", benefitWxId, wxUId, createdTime.Format("200601"), err)
  670. return 0
  671. }
  672. if ret.Count < 0 {
  673. return 0
  674. }
  675. cache.Cache.Put(k, ret.Count, 10*time.Minute)
  676. return ret.Count
  677. }