lotteryDrawTask.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package activity
  2. import (
  3. "app.yhyue.com/moapp/jybase/date"
  4. "app.yhyue.com/moapp/jybase/go-logger/logger"
  5. "app.yhyue.com/moapp/message/db"
  6. "app.yhyue.com/moapp/message/model"
  7. "app.yhyue.com/moapp/message/util"
  8. "encoding/json"
  9. "fmt"
  10. "time"
  11. )
  12. const (
  13. //表
  14. tableTaskInfo = "jyactivities.lottery_task_info"
  15. tableTaskUser = "jyactivities.lottery_task_user"
  16. tableActivityInfo = "jyactivities.activity_info"
  17. )
  18. // 任务信息
  19. type TaskInfo struct {
  20. Id int64 `json:"id"`
  21. Name string `json:"name"`
  22. ActiveId int64 `json:"active_id"` //活动id
  23. Qualification string `json:"qualification"` //获取抽奖资格次数;
  24. CycleNum int `json:"cycle_num"` //任务周期(每天/每周/每月/活动周期) 可执行任务次数:n次;-1:不限制
  25. CycleUnit int `json:"cycle_unit"` //默认0:当天;;1:当周;2:当月;3:活动周期
  26. ActivityStartTime string `json:"start_time"` //活动开始时间
  27. ActivityEndTime string `json:"end_time"` //活动结束时间
  28. }
  29. // 信息主题
  30. type MsgBody struct {
  31. Phone string
  32. UserId string
  33. MgoUserId string
  34. PositionId int64
  35. ActiveId int64
  36. TaskInfoId int64
  37. OrderCode string
  38. }
  39. // LotteryDrawTask 抽奖任务
  40. func LotteryDrawTask(msg *model.Message) {
  41. var msgBody MsgBody
  42. if msg.E_body != nil {
  43. b, err := json.Marshal(msg.E_body)
  44. if err == nil {
  45. err = json.Unmarshal(b, &msgBody)
  46. }
  47. if err != nil {
  48. logger.Info(fmt.Sprintf("任务完成参数信息有误:%s", msg.E_userId))
  49. return
  50. }
  51. }
  52. //判断用户是否存在
  53. data, ok := db.Mgo.FindById("user", msg.E_userId, `{"l_registedate":1,"s_phone":1,"s_m_phone":1}`)
  54. if data == nil || len(*data) == 0 || !ok {
  55. logger.Info(fmt.Sprintf("未找到用户:%s, 任务id:%d。", msg.E_userId, msgBody.TaskInfoId))
  56. return
  57. }
  58. //任务信息
  59. taskInfos := db.Mysql.SelectBySql(fmt.Sprintf("SELECT lti.*,ai.start_time,ai.end_time FROM %s lti LEFT JOIN %s ai ON lti.active_id = ai.id WHERE lti.active_id = ? AND lti.id = ? AND ai.end_time >= NOW() ORDER BY lti.create_date DESC", tableTaskInfo, tableActivityInfo), msgBody.ActiveId, msgBody.TaskInfoId)
  60. if taskInfos == nil || len(*taskInfos) == 0 {
  61. logger.Info(fmt.Sprintf("没有当前需要完成的任务信息:%v", msgBody))
  62. return
  63. }
  64. //任务信息
  65. var taskInfo TaskInfo
  66. b, err := json.Marshal((*taskInfos)[0])
  67. if err == nil {
  68. err = json.Unmarshal(b, &taskInfo)
  69. }
  70. if err != nil {
  71. logger.Info(fmt.Sprintf("任务信息异常:%v,err:%s", msgBody, err.Error()))
  72. return
  73. }
  74. var (
  75. now = time.Now()
  76. timeRange = util.TimeRange{}
  77. )
  78. //判断任务有效期内,此次任务是否已完成
  79. switch taskInfo.CycleUnit {
  80. case 1: //当周
  81. timeRange = util.GetWeekRange(now)
  82. case 2: //当月
  83. timeRange = util.GetMonthRange(now)
  84. case 3: //活动周期
  85. startTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityStartTime, time.Local)
  86. emdTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityEndTime, time.Local)
  87. timeRange = util.TimeRange{
  88. StartTime: startTime,
  89. EndTime: emdTime,
  90. StartUnix: startTime.Unix(),
  91. EndUnix: emdTime.Unix(),
  92. }
  93. default: //当天
  94. timeRange = util.GetDayRange(now)
  95. }
  96. //当前周期内 是否已完成任务
  97. if taskInfo.CycleNum > 0 {
  98. if count := db.Mysql.CountBySql(fmt.Sprintf(`SELECT COUNT(ltu.id) FROM %s ltu WHERE ltu.active_id = ? AND ltu.task_id = ? AND ltu.state = 0 AND ltu.create_date < ? AND ltu.create_date > ? `, tableTaskUser), taskInfo.ActiveId, taskInfo.Id, timeRange.EndTime.Format(date.Date_Full_Layout), timeRange.StartTime.Format(date.Date_Full_Layout)); taskInfo.CycleNum <= int(count) {
  99. logger.Info(fmt.Sprintf("用户:%s ,此任务:%s ,在 %s 已完成", msgBody.Phone, taskInfo.Name, timeRange.StartTime.Format(date.Date_Short_Layout)))
  100. return
  101. }
  102. }
  103. //二次验证
  104. switch taskInfo.Id {
  105. case 1: //签到赠送剑鱼币任务
  106. case 2: //设置关键词任务
  107. var hasKeys bool
  108. //查看当前用户是否有订阅词
  109. res := db.Compatible.Select(msgBody.UserId, `{"o_vipjy":1,"o_member_jy":1,"o_jy":"1"}`)
  110. if res != nil && len(*res) > 0 {
  111. obj, _ := (*res)["o_jy"].(map[string]interface{})
  112. aKey, _ := obj["a_key"].([]interface{})
  113. if len(aKey) > 0 {
  114. hasKeys = true
  115. }
  116. if !hasKeys {
  117. obj, _ = (*res)["o_vipjy"].(map[string]interface{})
  118. if obj == nil {
  119. obj, _ = (*res)["o_member_jy"].(map[string]interface{})
  120. }
  121. if obj != nil {
  122. itmes, _ := obj["a_items"].([]interface{})
  123. for _, v := range itmes {
  124. item, _ := v.(map[string]interface{})
  125. keys, _ := item["a_key"].([]interface{})
  126. if len(keys) > 0 {
  127. hasKeys = true
  128. break
  129. }
  130. }
  131. }
  132. }
  133. }
  134. //没有设置关键词
  135. if !hasKeys {
  136. logger.Info(fmt.Sprintf("当前用户:%s,手机号: %s 没有设置订阅词", msgBody.UserId, msgBody.Phone))
  137. return
  138. }
  139. case 3: //招标采购搜索任务
  140. case 4: //购买/续费/升级超级订阅会员
  141. case 5: //购买/续费/升级大会员
  142. case 6: //给朋友分享活动
  143. }
  144. if id := db.Mysql.Insert(tableTaskUser, map[string]interface{}{
  145. "active_id": msgBody.ActiveId,
  146. "task_id": msgBody.TaskInfoId,
  147. "phone": msgBody.Phone,
  148. "position_id": msgBody.PositionId,
  149. "mgo_user_id": msgBody.MgoUserId,
  150. "order_code": msgBody.OrderCode,
  151. "state": 0,
  152. "end_date": timeRange.EndTime.Format(date.Date_Full_Layout),
  153. "update_date": time.Now().Format(date.Date_Full_Layout),
  154. "create_date": time.Now().Format(date.Date_Full_Layout),
  155. }); id < 0 {
  156. logger.Info(fmt.Sprintf("保存任务记录异常:%v", msgBody))
  157. }
  158. }