lotteryDrawTask.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package activity
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/go-logger/logger"
  6. "app.yhyue.com/moapp/message/db"
  7. "app.yhyue.com/moapp/message/model"
  8. "app.yhyue.com/moapp/message/util"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/os/gctx"
  13. "regexp"
  14. "time"
  15. )
  16. const (
  17. //表
  18. tableTaskInfo = "jyactivities.lottery_task_info"
  19. tableTaskUser = "jyactivities.lottery_task_user"
  20. tableActivityInfo = "jyactivities.activity_info"
  21. )
  22. // 任务信息
  23. type TaskInfo struct {
  24. Id int64 `json:"id"`
  25. Name string `json:"name"`
  26. ActiveId int64 `json:"active_id"` //活动id
  27. Qualification int `json:"qualification"` //获取抽奖资格次数;
  28. CycleNum int `json:"cycle_num"` //任务周期(每天/每周/每月/活动周期) 可执行任务次数:n次;-1:不限制
  29. CycleUnit int `json:"cycle_unit"` //默认0:当天;;1:当周;2:当月;3:活动周期
  30. ActivityStartTime string `json:"start_time"` //活动开始时间
  31. ActivityEndTime string `json:"end_time"` //活动结束时间
  32. ActivityName string `json:"activity_name"` //活动名称
  33. }
  34. // 信息主题
  35. type MsgBody struct {
  36. Phone string
  37. UserId string
  38. MgoUserId string
  39. PositionId int64
  40. ActiveId int64
  41. TaskInfoId int64
  42. OrderCode string
  43. }
  44. // LotteryDrawTask 抽奖任务
  45. func LotteryDrawTask(msg *model.Message) {
  46. var msgBody MsgBody
  47. if msg.E_body != nil {
  48. b, err := json.Marshal(msg.E_body)
  49. if err == nil {
  50. err = json.Unmarshal(b, &msgBody)
  51. }
  52. if err != nil {
  53. logger.Info(fmt.Sprintf("任务完成参数信息有误:%s", msg.E_userId))
  54. return
  55. }
  56. }
  57. //判断用户是否存在
  58. data, ok := db.Mgo.FindById("user", msg.E_userId, `{"l_registedate":1,"s_phone":1,"s_m_phone":1}`)
  59. if data == nil || len(*data) == 0 || !ok {
  60. logger.Info(fmt.Sprintf("未找到用户:%s, 任务id:%d。", msg.E_userId, msgBody.TaskInfoId))
  61. return
  62. }
  63. //任务信息
  64. 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.start_time < NOW() AND ai.end_time >= NOW() ORDER BY lti.create_date DESC", tableTaskInfo, tableActivityInfo), msgBody.ActiveId, msgBody.TaskInfoId)
  65. if taskInfos == nil || len(*taskInfos) == 0 {
  66. logger.Info(fmt.Sprintf("没有当前需要完成的任务信息:%v", msgBody))
  67. return
  68. }
  69. //任务信息
  70. var taskInfo TaskInfo
  71. b, err := json.Marshal((*taskInfos)[0])
  72. if err == nil {
  73. err = json.Unmarshal(b, &taskInfo)
  74. }
  75. if err != nil {
  76. logger.Info(fmt.Sprintf("任务信息异常:%v,err:%s", msgBody, err.Error()))
  77. return
  78. }
  79. var (
  80. now = time.Now()
  81. timeRange = util.TimeRange{}
  82. )
  83. //判断任务有效期内,此次任务是否已完成
  84. switch taskInfo.CycleUnit {
  85. case 1: //当周
  86. timeRange = util.GetWeekRange(now)
  87. case 2: //当月
  88. timeRange = util.GetMonthRange(now)
  89. case 3: //活动周期
  90. startTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityStartTime, time.Local)
  91. emdTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityEndTime, time.Local)
  92. timeRange = util.TimeRange{
  93. StartTime: startTime,
  94. EndTime: emdTime,
  95. StartUnix: startTime.Unix(),
  96. EndUnix: emdTime.Unix(),
  97. }
  98. default: //当天
  99. timeRange = util.GetDayRange(now)
  100. }
  101. //当前周期内 是否已完成任务
  102. if taskInfo.CycleNum > 0 {
  103. tasks := db.Mysql.SelectBySql(fmt.Sprintf(`SELECT ltu.create_date FROM %s ltu WHERE ltu.active_id = ? AND ltu.task_id = ? 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))
  104. if tasks != nil && taskInfo.CycleNum <= len(*tasks) {
  105. createDate := common.InterfaceToStr((*tasks)[0]["create_date"])
  106. logger.Info(fmt.Sprintf("用户:%s ,此任务:%s ,在 %s 已完成", msgBody.Phone, taskInfo.Name, createDate))
  107. return
  108. }
  109. } else if msgBody.OrderCode != "" {
  110. tasks := db.Mysql.SelectBySql(fmt.Sprintf(`SELECT ltu.create_date FROM %s ltu WHERE ltu.active_id = ? AND ltu.position_id = ? AND ltu.order_code = ?`, tableTaskUser), taskInfo.ActiveId, msgBody.PositionId, msgBody.OrderCode)
  111. //TODO 判重 担心订单有更新
  112. if tasks != nil && len(*tasks) > 0 {
  113. createDate := common.InterfaceToStr((*tasks)[0]["create_date"])
  114. logger.Info(fmt.Sprintf("用户:%s ,此订单:%s ,在 %s 已新增抽奖机会", msgBody.Phone, msgBody.OrderCode, createDate))
  115. return
  116. }
  117. }
  118. //二次验证
  119. switch taskInfo.Id {
  120. case 1: //签到赠送剑鱼币任务
  121. case 2: //设置关键词任务
  122. var hasKeys bool
  123. //查看当前用户是否有订阅词
  124. res := db.Compatible.Select(msgBody.UserId, `{"o_vipjy":1,"o_member_jy":1,"o_jy":"1"}`)
  125. if res != nil && len(*res) > 0 {
  126. obj, _ := (*res)["o_jy"].(map[string]interface{})
  127. aKey, _ := obj["a_key"].([]interface{})
  128. if len(aKey) > 0 {
  129. hasKeys = true
  130. }
  131. if !hasKeys {
  132. obj, _ = (*res)["o_vipjy"].(map[string]interface{})
  133. if obj == nil {
  134. obj, _ = (*res)["o_member_jy"].(map[string]interface{})
  135. }
  136. if obj != nil {
  137. itmes, _ := obj["a_items"].([]interface{})
  138. for _, v := range itmes {
  139. item, _ := v.(map[string]interface{})
  140. keys, _ := item["a_key"].([]interface{})
  141. if len(keys) > 0 {
  142. hasKeys = true
  143. break
  144. }
  145. }
  146. }
  147. }
  148. }
  149. //没有设置关键词
  150. if !hasKeys {
  151. logger.Info(fmt.Sprintf("当前用户:%s,手机号: %s 没有设置订阅词", msgBody.UserId, msgBody.Phone))
  152. return
  153. }
  154. case 3: //招标采购搜索任务
  155. case 4: //购买/续费/升级超级订阅会员
  156. case 5: //购买/续费/升级大会员
  157. // TODO 完成任务 赠送两次 抽奖机会
  158. case 6: //给朋友分享活动
  159. }
  160. insertMap := map[string]interface{}{
  161. "active_id": msgBody.ActiveId,
  162. "task_id": msgBody.TaskInfoId,
  163. "phone": msgBody.Phone,
  164. "position_id": msgBody.PositionId,
  165. "mgo_user_id": msgBody.MgoUserId,
  166. "order_code": msgBody.OrderCode,
  167. "state": 0,
  168. "end_date": timeRange.EndTime.Format(date.Date_Full_Layout),
  169. "update_date": time.Now().Format(date.Date_Full_Layout),
  170. "create_date": time.Now().Format(date.Date_Full_Layout),
  171. }
  172. for i := 0; i < taskInfo.Qualification; i++ {
  173. if id := db.Mysql.Insert(tableTaskUser, insertMap); id < 0 {
  174. logger.Info(fmt.Sprintf("保存任务记录异常:%v", msgBody))
  175. // TODO 保存任务记录异常 告警
  176. if webhookURL := g.Cfg().MustGet(gctx.New(), "webhookURL").Strings(); len(webhookURL) > 0 {
  177. content := fmt.Sprintf(`保存任务记录异常:\n活动名称:%s \n手机号:%s \n职位id:%d \n任务id:%d \n任务名称:%s`,
  178. taskInfo.ActivityName, msgBody.Phone, msgBody.PositionId, msgBody.TaskInfoId, taskInfo.Name)
  179. util.SendMsgByWXURL(content, webhookURL)
  180. }
  181. }
  182. }
  183. }
  184. type lotteryBody struct {
  185. NickName string `json:"nick_name"`
  186. Phone string `json:"phone"`
  187. MgoUserId string `json:"mgo_user_id"`
  188. PositionId int64 `json:"position_id"`
  189. ActiveId int64 `json:"active_id"`
  190. ActiveName string `json:"active_name"`
  191. PrizeId int64 `json:"prize_id"`
  192. PrizeName string `json:"prize_name"`
  193. }
  194. // LotteryWinning 中奖信息
  195. func LotteryWinning(msg *model.Message) {
  196. //TODO sse 通知用户 中奖信息
  197. var lb lotteryBody
  198. if msg.E_body != nil {
  199. b, err := json.Marshal(msg.E_body)
  200. if err == nil {
  201. err = json.Unmarshal(b, &lb)
  202. }
  203. if err != nil {
  204. logger.Info(fmt.Sprintf("中奖参数信息有误:%s", msg.E_userId))
  205. return
  206. }
  207. }
  208. var userName = lb.NickName
  209. if lb.Phone != "" {
  210. var PhoneReg = regexp.MustCompile(`^(100\d{8}|1[3-9]\d{9})$`)
  211. if PhoneReg.MatchString(lb.Phone) {
  212. phone := []rune(lb.Phone)
  213. userName = string(phone[0:3]) + "****" + string(phone[(len(phone)-4):])
  214. }
  215. }
  216. logger.Info(" sse 中奖信息通知:", msg)
  217. util.SseBroadcast.SendToUsers(model.SseMessage{
  218. Prize: lb.PrizeName,
  219. User: userName,
  220. State: model.AllTarget,
  221. Time: time.Now().Format(date.Date_Full_Layout),
  222. Remark: fmt.Sprintf("恭喜 %s 抽中 %s ", userName, lb.PrizeName),
  223. })
  224. }