bidderPlan.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package activity
  2. import (
  3. "app.yhyue.com/moapp/jybase/go-logger/logger"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "app.yhyue.com/moapp/message/db"
  6. "app.yhyue.com/moapp/message/handler/award"
  7. "app.yhyue.com/moapp/message/rpc"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/gogf/gf/v2/os/gcfg"
  11. "github.com/gogf/gf/v2/os/gctx"
  12. "github.com/gogf/gf/v2/os/gtime"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. "time"
  15. )
  16. /*
  17. 投标人专属免费计划
  18. 待解决问题:连点处理
  19. */
  20. type BidderPlan struct {
  21. St, Ed time.Time //活动开始时间、结束时间
  22. Missions map[string]struct { //任务
  23. Points int `json:"points"`
  24. Schedule int `json:"schedule"`
  25. }
  26. ScheduleReward map[int]struct { //进度奖励
  27. SubVip int `json:"subvip"`
  28. Schedule int `json:"schedule"`
  29. }
  30. }
  31. const (
  32. BidderPlanActivityCode = "bidderPlan"
  33. BidderPlanRedis = "main"
  34. //活动累计进度
  35. BidderPlanScheduleCache = "bidderPlanScheduleValue_%s"
  36. //奖励领取状态
  37. BidderPlanScheduleAwardStatus = "bidderPlanScheduleAward_%s_%d"
  38. BidderPlanSubscribeMissionsStatus = "bidderPlanActivitySubscribeCacheKey_%s"
  39. BidderPlanInviteMissionsStatus = "bidderPlanActivityInviteCacheKey_%s"
  40. BidderPlanImproveInfoMissionsStatus = "bidderPlanActivityImproveInfoCacheKey_%s"
  41. )
  42. var JyBidderPlan *BidderPlan
  43. func init() {
  44. JyBidderPlan = &BidderPlan{
  45. St: gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "bidderPlan.dateRange.st").String(), "2006-01-02T15:04:05Z").Time,
  46. Ed: gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "bidderPlan.dateRange.ed").String(), "2006-01-02T15:04:05Z").Time,
  47. }
  48. //加载任务
  49. if loadMissionsErr := gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions").Scan(&JyBidderPlan.Missions); loadMissionsErr != nil {
  50. logger.Error(fmt.Sprintf("JyBidderPlan 加载任务异常 %v", loadMissionsErr))
  51. }
  52. //加载进度奖励
  53. if loadScheduleErr := gcfg.Instance().MustGet(gctx.New(), "bidderPlan.scheduleReward").Scan(&JyBidderPlan.ScheduleReward); loadScheduleErr != nil {
  54. logger.Error(fmt.Sprintf("JyBidderPlan 加载进度异常 %v", loadScheduleErr))
  55. }
  56. }
  57. // InActivity 是否在活动时间内
  58. func (BP *BidderPlan) InActivity() bool {
  59. now := time.Now()
  60. return now.After(JyBidderPlan.St) && now.Before(JyBidderPlan.Ed)
  61. }
  62. // cacheLong 缓存时间
  63. func (BP *BidderPlan) cacheLong() int {
  64. //return int(JyBidderPlan.Ed.Unix()-time.Now().Unix()))+ 60 * 60 * 24
  65. return 60 * 60 * 24 * 30
  66. }
  67. // GetSchedule 查询活动任务进度
  68. func (BP *BidderPlan) GetSchedule(userId string) int {
  69. return -redis.GetInt(BidderPlanRedis, fmt.Sprintf(BidderPlanScheduleCache, userId))
  70. }
  71. func (BP *BidderPlan) GetScheduleQuery(userId string, schedule int) (status int) {
  72. if BP.GetSchedule(userId) < schedule {
  73. return 0
  74. }
  75. key := fmt.Sprintf(BidderPlanScheduleAwardStatus, userId, schedule)
  76. if redis.Get(BidderPlanRedis, key) == nil {
  77. status = gconv.Int(redis.Incr(BidderPlanRedis, key))
  78. _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
  79. } else {
  80. status = redis.GetInt(BidderPlanRedis, key)
  81. }
  82. if status > 1 {
  83. status = 1
  84. }
  85. return
  86. }
  87. // ScheduleGiven 活动任务进度奖励
  88. func (BP *BidderPlan) ScheduleGiven(userId string, schedule int) error {
  89. if !BP.InActivity() {
  90. return fmt.Errorf("活动已结束")
  91. }
  92. if BP.GetScheduleQuery(userId, schedule) != 1 {
  93. return fmt.Errorf("非领取状态")
  94. }
  95. scheduleAward, ok := BP.ScheduleReward[schedule]
  96. if !ok {
  97. return fmt.Errorf("未知请求")
  98. }
  99. if err := award.GivenSubVip(userId, award.SubVip{
  100. Num: gconv.Int64(scheduleAward.SubVip),
  101. ActivityCode: BidderPlanActivityCode,
  102. Desc: fmt.Sprintf("达到%d剑鱼币", schedule),
  103. Date: time.Now().Unix(),
  104. }); err != nil {
  105. return fmt.Errorf("领取奖励异常")
  106. }
  107. key := fmt.Sprintf(BidderPlanScheduleAwardStatus, userId, schedule)
  108. redis.Put(BidderPlanRedis, key, -1, BP.cacheLong())
  109. return nil
  110. }
  111. // AddSchedule 增加活动进度
  112. func (BP *BidderPlan) AddSchedule(userId string, value int) (after int) {
  113. if value > 0 {
  114. key := fmt.Sprintf(BidderPlanScheduleCache, userId)
  115. after = -gconv.Int(redis.Decrby(BidderPlanRedis, key, value))
  116. if value == after { //首次设置过期值
  117. _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
  118. }
  119. }
  120. return
  121. }
  122. // FreeSubscribe 免费订阅
  123. type FreeSubscribe struct {
  124. OAreaP map[string]interface{} `json:"o_area_p"`
  125. AKey []interface{} `json:"a_key"`
  126. }
  127. // MissionsSubscribeQuery 订阅任务
  128. // status:0 未完成 1 待领取 -1已领取
  129. func (BP *BidderPlan) MissionsSubscribeQuery(userId string) (status int) {
  130. //非7天超级订阅或存在订阅内容则完成任务
  131. finish := func() bool {
  132. uData, ok := db.Mgo.FindById("user", userId, `{"o_jy":1,"i_vip_status":1,"i_member_status":1}`)
  133. if !ok || uData == nil || len(*uData) == 0 {
  134. return false
  135. }
  136. if gconv.Int((*uData)["i_vip_status"]) > 0 { //超级订阅
  137. return true
  138. } else if gconv.Int((*uData)["i_member_status"]) > 0 { //大会员
  139. return true
  140. }
  141. //查询免费订阅是否完成
  142. vStatus, err := rpc.GetUserVipBaseMsgByRpc(userId)
  143. if err != nil {
  144. return false
  145. }
  146. if vStatus.EntnicheStatus > 0 {
  147. return true
  148. } else {
  149. bytes := gconv.Bytes((*uData)["o_jy"])
  150. if bytes == nil || len(bytes) == 0 {
  151. return false
  152. }
  153. freeSub := FreeSubscribe{}
  154. if err = json.Unmarshal(bytes, &freeSub); err != nil {
  155. fmt.Println(err)
  156. return false
  157. }
  158. if len(freeSub.AKey) > 0 && len(freeSub.OAreaP) > 0 {
  159. return true
  160. }
  161. }
  162. return false
  163. }()
  164. if !finish {
  165. return 0
  166. }
  167. key := fmt.Sprintf(BidderPlanSubscribeMissionsStatus, userId)
  168. if redis.Get(BidderPlanRedis, key) == nil {
  169. status = gconv.Int(redis.Incr(BidderPlanRedis, key))
  170. _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
  171. } else {
  172. status = redis.GetInt(BidderPlanRedis, key)
  173. }
  174. if status > 1 {
  175. status = 1
  176. }
  177. return
  178. }
  179. // MissionsSubscribeGiven 订阅任务领取奖励
  180. func (BP *BidderPlan) MissionsSubscribeGiven(userId string) error {
  181. if !BP.InActivity() {
  182. return fmt.Errorf("活动已结束")
  183. }
  184. status := BP.MissionsSubscribeQuery(userId)
  185. if status != 1 {
  186. return fmt.Errorf("非领取状态")
  187. }
  188. if givenPointsErr := award.GivenPoints(userId, award.Points{
  189. Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.subscribe.points").Int64(),
  190. Type: 1006,
  191. ActivityCode: BidderPlanActivityCode,
  192. Desc: "免费订阅任务完成",
  193. Date: time.Now().Unix(),
  194. }); givenPointsErr != nil {
  195. return fmt.Errorf("领取奖励异常")
  196. }
  197. //设置已领取
  198. redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanSubscribeMissionsStatus, userId), -1, BP.cacheLong())
  199. //增加任务进度
  200. BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.subscribe.schedule").Int())
  201. return nil
  202. }
  203. // MissionsInviteQuery 邀请任务状态查询
  204. // status:0 未完成 1 待领取 -1已领取
  205. func (BP *BidderPlan) MissionsInviteQuery(userId string) (status int) {
  206. status = redis.GetInt(BidderPlanRedis, fmt.Sprintf(BidderPlanInviteMissionsStatus, userId))
  207. if status > 1 {
  208. status = 1
  209. }
  210. return status
  211. }
  212. // MissionsInviteGiven 邀请任务奖励领取
  213. func (BP *BidderPlan) MissionsInviteGiven(userId string) error {
  214. if !BP.InActivity() {
  215. return fmt.Errorf("活动已结束")
  216. }
  217. status := BP.MissionsInviteQuery(userId)
  218. if status != 1 {
  219. return fmt.Errorf("非领取状态")
  220. }
  221. //领取
  222. if givenPointsErr := award.GivenPoints(userId, award.Points{
  223. Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.invite.points").Int64(),
  224. Type: 1007,
  225. ActivityCode: BidderPlanActivityCode,
  226. Desc: "邀请领好礼任务完成",
  227. Date: time.Now().Unix(),
  228. }); givenPointsErr != nil {
  229. return fmt.Errorf("领取奖励异常")
  230. }
  231. //设置已领取
  232. redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanInviteMissionsStatus, userId), -1, BP.cacheLong())
  233. //增加任务进度
  234. BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.invite.schedule").Int())
  235. return nil
  236. }
  237. // MissionsImproveInfoQuery 完善信息任务
  238. // status:0 未完成 1 待领取 -1已领取
  239. func (BP *BidderPlan) MissionsImproveInfoQuery(userId string) (status int) {
  240. hasMsg := db.Mgo.Count("saleLeads", map[string]interface{}{
  241. "userid": userId,
  242. }) > 0
  243. if !hasMsg {
  244. return 0
  245. }
  246. key := fmt.Sprintf(BidderPlanImproveInfoMissionsStatus, userId)
  247. if redis.Get(BidderPlanRedis, key) == nil {
  248. status = gconv.Int(redis.Incr(BidderPlanRedis, key))
  249. _ = redis.SetExpire(BidderPlanRedis, key, BP.cacheLong())
  250. } else {
  251. status = redis.GetInt(BidderPlanRedis, key)
  252. }
  253. if status > 1 {
  254. status = 1
  255. }
  256. return
  257. }
  258. // MissionsImproveInfoGiven 完善信息奖励领取
  259. func (BP *BidderPlan) MissionsImproveInfoGiven(userId string) error {
  260. if !BP.InActivity() {
  261. return fmt.Errorf("活动已结束")
  262. }
  263. status := BP.MissionsImproveInfoQuery(userId)
  264. if status != 1 {
  265. return fmt.Errorf("非领取状态")
  266. }
  267. if givenPointsErr := award.GivenPoints(userId, award.Points{
  268. Num: gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.improveInfo.points").Int64(),
  269. Type: 1008,
  270. ActivityCode: BidderPlanActivityCode,
  271. Desc: "完善信息任务完成",
  272. Date: time.Now().Unix(),
  273. }); givenPointsErr != nil {
  274. return fmt.Errorf("领取奖励异常")
  275. }
  276. //设置已领取
  277. redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanImproveInfoMissionsStatus, userId), -1, BP.cacheLong())
  278. //增加任务进度
  279. BP.AddSchedule(userId, gcfg.Instance().MustGet(gctx.New(), "bidderPlan.missions.improveInfo.schedule").Int())
  280. return nil
  281. }