invitebean.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package model
  2. import (
  3. "active/constant"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "time"
  9. "git.jiaxianghudong.com/go/logs"
  10. "git.jiaxianghudong.com/webs/pkg/rds"
  11. "gopkg.in/yaml.v2"
  12. )
  13. var InviteBeanConf = &InviteBeanConfig{}
  14. var InviteBeanConf2 = &InviteBeanConfig2{}
  15. var invitebeanExpireAt int32
  16. // 邮件内容
  17. type appDownloadMail struct {
  18. Title string `json:"title" yaml:"title" config:"title"`
  19. Content string `json:"content" yaml:"content" config:"content"`
  20. Awards [][]int32 `json:"awards" yaml:"awards" config:"awards"`
  21. }
  22. type InviteBeanConfig struct {
  23. Count int64 `json:"count" yaml:"count"`
  24. AwardList []Award `json:"award_list" yaml:"award_list"`
  25. Appdownload_Opentag []string `json:"appdownload_opentag" yaml:"appdownload_opentag" ` //落地页app下载开放的平台
  26. Appdownload_Code string `json:"appdownload_code" yaml:"appdownload_code"` //落地页app下载带的标识码
  27. Appdownload_Mail appDownloadMail `json:"appdownload_mail" yaml:"appdownload_mail"` //落地页app下载邮件
  28. }
  29. type InviteBeanConfig2 struct {
  30. Count int64 `json:"count" yaml:"count"`
  31. AwardList2 []Award `json:"award_list" yaml:"award_list2"`
  32. Appdownload_Opentag []string `json:"appdownload_opentag" yaml:"appdownload_opentag" ` //落地页app下载开放的平台
  33. Appdownload_Code string `json:"appdownload_code" yaml:"appdownload_code"` //落地页app下载带的标识码
  34. Appdownload_Mail appDownloadMail `json:"appdownload_mail" yaml:"appdownload_mail"` //落地页app下载邮件
  35. Versionupdate_Mail appDownloadMail `json:"versionupdate_mail" yaml:"versionupdate_mail"` //版本更新邮件
  36. Appdownload_Opentag_Jx []string `json:"appdownload_opentag_jx" yaml:"appdownload_opentag_jx" ` //家乡包落地页app下载开放的平台
  37. Appdownload_Code_Jx string `json:"appdownload_code_jx" yaml:"appdownload_code_jx"` //家乡包落地页app下载带的标识码
  38. Appdownload_Mail_Jx appDownloadMail `json:"appdownload_mail_jx" yaml:"appdownload_mail_jx"` //家乡包落地页app下载邮件
  39. }
  40. type InviteRecord struct {
  41. Record []*Record `json:"record"`
  42. }
  43. type Record struct {
  44. UserInfo
  45. Status bool `json:"status"`
  46. Reason string `json:"reason"`
  47. Time int64 `json:"time"`
  48. }
  49. type Award struct {
  50. ShareType int64 `json:"share_type" yaml:"share_type"`
  51. Count int64 `json:"count" yaml:"count"`
  52. IsReceive bool `json:"is_receive"`
  53. Min int64 `json:"-" yaml:"min"`
  54. Max int64 `json:"-" yaml:"max"`
  55. Data []Data `json:"data" yaml:"data"`
  56. UserInfoList []UserInfo `json:"user_info_list"`
  57. }
  58. //获取奖品信息
  59. type Data struct {
  60. Id int64 `json:"id" yaml:"id"` //奖品id
  61. Count int64 `json:"count" yaml:"count"`
  62. Tag string `json:"tag" yaml:"tag"`
  63. Min int64 `json:"-" yaml:"min"`
  64. Max int64 `json:"-" yaml:"max"`
  65. }
  66. type UserInfo struct {
  67. UserId int64 `json:"user_id"`
  68. Url string `json:"url"`
  69. NickName string `json:"nick_name"`
  70. Sex int64 `json:"sex"` // 0女1男
  71. }
  72. func (this *InviteBeanConfig) GetConf(file string) {
  73. yamlFile, err := ioutil.ReadFile(fmt.Sprintf("./yaml/%v", file))
  74. if err != nil {
  75. log.Printf("yamlFile.Get err #%v ", err)
  76. }
  77. err = yaml.Unmarshal(yamlFile, this)
  78. if err != nil {
  79. log.Fatalf("Unmarshal: %v", err)
  80. }
  81. return
  82. }
  83. func (this *InviteBeanConfig2) GetConf(file string) {
  84. yamlFile, err := ioutil.ReadFile(fmt.Sprintf("./yaml/%v", file))
  85. if err != nil {
  86. log.Printf("yamlFile.Get err #%v ", err)
  87. }
  88. err = yaml.Unmarshal(yamlFile, this)
  89. if err != nil {
  90. log.Fatalf("Unmarshal: %v", err)
  91. }
  92. //log.Printf("%+v \n", *this)
  93. return
  94. }
  95. type InviteBeanList struct {
  96. List []InviteBean `json:"list"`
  97. }
  98. type InviteBean struct {
  99. IsReceive bool `json:"is_receive"`
  100. UserInfoList []UserInfo `json:"user_info_list"`
  101. }
  102. //获得储存信息数据
  103. func GetInviteBeanData(UserID string, tm string, endTm string) *InviteBeanList {
  104. //log.Println("key:",fmt.Sprintf(constant.INVITEBEANDATA,tm),UserID)
  105. data := rds.Redis.HGet(fmt.Sprintf(constant.INVITEBEANDATA, tm), UserID).Val()
  106. inviteBeanList := &InviteBeanList{}
  107. if data != "" { //有值状态下
  108. err := json.Unmarshal([]byte(data), inviteBeanList)
  109. if err != nil {
  110. logs.Errorf("redpacket[%s] GetInviteBeanData json err:%v", UserID, err)
  111. return inviteBeanList
  112. }
  113. } else { //如果没有值的话
  114. //log.Println("new ",UserID)
  115. for _ = range InviteBeanConf.AwardList {
  116. inviteBeanList.List = append(inviteBeanList.List, InviteBean{})
  117. }
  118. }
  119. return inviteBeanList
  120. }
  121. func GetInviteRecord(userID string, tm string) *InviteRecord {
  122. data := rds.Redis.HGet(fmt.Sprintf(constant.INVITERECORD, tm), userID).Val()
  123. inviteRecord := &InviteRecord{}
  124. if data != "" { //有值状态下
  125. json.Unmarshal([]byte(data), inviteRecord)
  126. }
  127. return inviteRecord
  128. }
  129. func (this *InviteRecord) SetInviteRecord(userID string, tm string, endTm string) {
  130. data, _ := json.Marshal(this)
  131. rds.Redis.HSet(fmt.Sprintf(constant.INVITERECORD, tm), userID, string(data))
  132. end, _ := time.Parse("2006-01-02 15:04:05", endTm)
  133. expireAt := end.Add(constant.REBATEDATATIMEOUT)
  134. rds.Redis.ExpireAt(fmt.Sprintf(constant.INVITERECORD, tm), expireAt)
  135. }
  136. //设置数据值
  137. func (this *InviteBeanList) SetInviteBeanData(UserID string, tm string, endTm string) bool {
  138. data, _ := json.Marshal(this)
  139. rds.Redis.HSet(fmt.Sprintf(constant.INVITEBEANDATA, tm), UserID, string(data))
  140. //if atomic.CompareAndSwapInt32(&invitebeanExpireAt, 0, 1) {
  141. end, _ := time.Parse("2006-01-02 15:04:05", endTm)
  142. expireAt := end.Add(constant.REBATEDATATIMEOUT)
  143. // log.Printf("---更新领取百万豆数据过期时间: expireAt:%v \n", expireAt)
  144. rds.Redis.ExpireAt(fmt.Sprintf(constant.INVITEBEANDATA, tm), expireAt)
  145. rds.Redis.ExpireAt(fmt.Sprintf(constant.INVITEBEANUSER, tm), expireAt)
  146. //log.Printf("---更新领取百万豆被邀请人数据过期时间: expireAt:%v \n", expireAt)
  147. //}
  148. return true
  149. }
  150. //得到受邀用户id值,来判断是否被邀请过了
  151. func GetInviteBeanUser(UserID string, tm string) string {
  152. data := rds.Redis.HGet(fmt.Sprintf(constant.INVITEBEANUSER, tm), UserID).Val()
  153. return data
  154. }
  155. //设置收到邀请的人数
  156. func SetInviteBeanUser(UserID string, tm string, endTm string) bool {
  157. rds.Redis.HSet(fmt.Sprintf(constant.INVITEBEANUSER, tm), UserID, "1") //
  158. return true
  159. }