lotteryDrawTask.go 6.3 KB

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