task.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package activity
  2. import (
  3. "fmt"
  4. "log"
  5. "net/rpc"
  6. "strconv"
  7. "time"
  8. "app.yhyue.com/moapp/message/handler/award"
  9. "github.com/gogf/gf/v2/os/gtime"
  10. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  11. "app.yhyue.com/moapp/jybase/common"
  12. "app.yhyue.com/moapp/jybase/date"
  13. "app.yhyue.com/moapp/jybase/go-logger/logger"
  14. jrpc "app.yhyue.com/moapp/jybase/rpc"
  15. "app.yhyue.com/moapp/message/config"
  16. "app.yhyue.com/moapp/message/db"
  17. "app.yhyue.com/moapp/message/model"
  18. . "app.yhyue.com/moapp/message/rpc"
  19. mrpc "app.yhyue.com/moapp/message/rpc"
  20. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  21. "github.com/gogf/gf/v2/os/gcfg"
  22. "github.com/gogf/gf/v2/os/gctx"
  23. "github.com/gogf/gf/v2/util/gconv"
  24. )
  25. /*
  26. 新手任务/限时任务
  27. */
  28. // Task 完成任务
  29. func Task(msg *model.Message) {
  30. //活动时间
  31. taskStartTime := config.TaskConf.TaskStartTime
  32. log.Println(msg.E_body, taskStartTime)
  33. num := 0
  34. code := gconv.Int64(msg.E_body["code"]) //1005
  35. types := gconv.String(msg.E_body["types"])
  36. baseUserId := gconv.Int64(msg.E_body["baseUserId"])
  37. positionId := gconv.Int64(msg.E_body["positionId"])
  38. now := date.NowFormat(date.Date_Full_Layout)
  39. query := map[string]interface{}{
  40. "type": types,
  41. "user_id": baseUserId,
  42. }
  43. // p647新绑定用户送7天超级订阅
  44. startTime := gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "newRegister.startTime").String(), "2006-01-02T15:04:05Z").Time
  45. entTime := gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "newRegister.entTime").String(), "2006-01-02T15:04:05Z").Time
  46. if time.Now().After(startTime) && time.Now().Before(entTime) && gconv.Bool(msg.E_body["isOnlyBind"]) {
  47. // 查询该用户活动时间内是否赠送过
  48. c := db.Mysql.CountBySql("SELECT count(1) FROM dataexport_order WHERE user_id = ? AND distribution_channel = 'x054'", msg.E_userId)
  49. if c <= 0 {
  50. _ = award.GivenSubVip(msg.E_userId, award.SubVip{
  51. Num: gcfg.Instance().MustGet(gctx.New(), "newRegister.supVip", nil).Int64(),
  52. ActivityCode: "newRegister",
  53. Date: msg.E_time,
  54. Desc: "2024年新用户注册赠送7天超级订阅",
  55. })
  56. err := SendMsg("新绑定手机号用户", &message.MultipleSaveMsgReq{
  57. UserIds: msg.E_userId,
  58. Title: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.title", "").String(),
  59. Content: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.content", "").String(),
  60. MsgType: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.msgType").Int64(),
  61. Appid: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.appid", "").String(),
  62. Link: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.pcUrl", "").String() + "," +
  63. gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", "").String() + "," +
  64. gcfg.Instance().MustGet(gctx.New(), "newRegister.message.wxUrl", "").String() + "," +
  65. gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", "").String(),
  66. AppPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", "").String(),
  67. WxPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.wxUrl", "").String(),
  68. IosPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", "").String(),
  69. })
  70. if err != nil {
  71. logger.Info("SendMsg Fail ", err)
  72. }
  73. } else {
  74. logger.Info("该用户活动时间内已经绑定过手机号", msg.E_userId)
  75. }
  76. }
  77. //判断用户是否有任务
  78. data, ok := db.Mgo.FindById("user", msg.E_userId, `{"l_registedate":1}`)
  79. if data == nil || len(*data) == 0 || !ok {
  80. logger.Info(fmt.Sprintf("未找到用户%s,无法增加积分%v。", msg.E_userId, code))
  81. return
  82. }
  83. l_registedate := gconv.Int64((*data)["l_registedate"])
  84. isNew := l_registedate > taskStartTime //是否注册时间处于新手任务开始时间
  85. taskData := config.TaskConf.OldTask
  86. if isNew {
  87. taskData = config.TaskConf.NewTask
  88. }
  89. //判断用户是否有该任务
  90. bl := false
  91. for _, v := range taskData {
  92. if bl {
  93. continue
  94. }
  95. if v.Type == types {
  96. num = v.Point
  97. bl = true
  98. }
  99. }
  100. if !bl {
  101. logger.Info(fmt.Sprintf("用户%s没有该任务%v,不再增加积分。", msg.E_userId, code))
  102. return
  103. }
  104. var task_id int64
  105. success_status := 0
  106. end_time := ""
  107. for i := 0; i < 5; i++ {
  108. task_id, success_status, end_time = createTask(baseUserId, positionId, isNew, now)
  109. if task_id > 0 {
  110. break
  111. }
  112. time.Sleep(time.Second)
  113. }
  114. if task_id <= 0 {
  115. logger.Error("没有取到task_id", msg.E_userId, baseUserId, code)
  116. return
  117. } else if success_status == 1 {
  118. logger.Info(fmt.Sprintf("用户%s已经完成挑战,默认任务完成%v,不再增加积分。", msg.E_userId, code))
  119. return
  120. } else if db.TidbPoint.CountBySql(`select count(1) from jypoints.integral_flow where userId =? and pointType =? and sort =1;`, msg.E_userId, code) > 0 {
  121. //判断是否赠送过积分,送过则不送
  122. logger.Info(fmt.Sprintf("用户%s已经增加过积分%v,不再增加积分。", msg.E_userId, code))
  123. return
  124. }
  125. //获取用户下所有的任务
  126. taskMsgMap := map[string]int{}
  127. taskAllMsg := db.Tidb.SelectBySql(`select name,status,type from integral_task_detail where user_id=?`, baseUserId)
  128. if taskAllMsg != nil && len(*taskAllMsg) > 0 {
  129. //用户有任务
  130. for _, v := range *taskAllMsg {
  131. taskMsgMap[gconv.String(v["type"])] = gconv.Int(v["status"])
  132. }
  133. }
  134. //增加积分
  135. mrpc.IntegralHarvest(msg.E_userId, gconv.Int64(num), code, msg.E_time)
  136. //判断任务状态
  137. fmt.Println(msg.E_userId, taskMsgMap[types])
  138. if _, exists := taskMsgMap[types]; !exists {
  139. //任务明细
  140. for _, v := range taskData {
  141. if v.Type == types {
  142. db.Tidb.Insert("integral_task_detail", map[string]interface{}{
  143. "task_id": task_id,
  144. "user_id": baseUserId,
  145. "position_id": positionId,
  146. "name": v.Name,
  147. "description": v.Desc,
  148. "icon": v.Icon,
  149. "point": v.Point,
  150. "pc_href": v.PcHref,
  151. "wx_href": v.WxHref,
  152. "app_href": v.AppHref,
  153. "status": 1, //完成任务
  154. "finish_time": now,
  155. "create_time": now,
  156. "type": v.Type,
  157. })
  158. break
  159. }
  160. }
  161. }
  162. //修改任务状态
  163. fmt.Println(msg.E_userId, types, "update")
  164. status := taskMsgMap[types]
  165. fmt.Println(msg.E_userId, status, "status", status == 0)
  166. if status == 0 {
  167. db.Tidb.Update("integral_task_detail", query, map[string]interface{}{
  168. "status": 1,
  169. "finish_time": time.Now().Format(date.Date_Full_Layout),
  170. })
  171. taskMsgMap[types] = 1
  172. }
  173. //判断是否已经完成所有任务
  174. if len(taskMsgMap) == len(taskData) && end_time != "" { //已初始化完所有任务
  175. finishStatus := true
  176. //
  177. for _, v := range taskMsgMap {
  178. if v == 0 {
  179. //有未完成任务 不增加积分
  180. finishStatus = false
  181. }
  182. }
  183. endtime, _ := time.Parse(date.Date_Full_Layout, end_time)
  184. logger.Info("STATUS:", msg.E_userId, finishStatus, success_status == 0, time.Now().Before(endtime))
  185. if finishStatus && success_status == 0 && time.Now().Before(endtime) {
  186. //判断是否完成所有任务且开启确认挑战
  187. if mrpc.SubVipHarvest(msg.E_userId, 7, "") == nil {
  188. if db.Tidb.Update("integral_task", map[string]interface{}{"id": task_id}, map[string]interface{}{
  189. "success_status": 1,
  190. }) {
  191. go func() {
  192. wxUrl := "/front/sess/" + Se.EncodeString(msg.E_userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  193. appUrl := "/jyapp/free/sess/" + Se.EncodeString(msg.E_userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl)
  194. SendMsg("获赠七天超级订阅服务", &message.MultipleSaveMsgReq{
  195. UserIds: msg.E_userId,
  196. Title: config.PushConfig.Messages.GetVip.Title,
  197. Content: fmt.Sprintf(config.PushConfig.Messages.GetVip.Content, common.If(isNew, "新手", "限时").(string)),
  198. MsgType: config.PushConfig.Messages.GetVip.MsgType,
  199. Link: config.PushConfig.Messages.GetVip.PcUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl,
  200. Appid: config.PushConfig.Messages.GetVip.Appid,
  201. AppPushUrl: appUrl,
  202. WxPushUrl: config.PushConfig.Webdomain + wxUrl,
  203. IosPushUrl: appUrl,
  204. })
  205. }()
  206. }
  207. }
  208. }
  209. }
  210. }
  211. // 获取任务
  212. func GetTaskRpc(isNew bool) (p *jrpc.TaskDataResp) {
  213. defer common.Catch()
  214. var repl jrpc.TaskDataResp
  215. client, err := rpc.DialHTTP("tcp", gcfg.Instance().MustGet(gctx.New(), "rpc.pointrpc").String())
  216. if err != nil {
  217. logger.Info("tcp err", err)
  218. return &repl
  219. }
  220. defer client.Close()
  221. err = client.Call("JyPointRpc.GetTask", jrpc.TaskData{IsNew: isNew}, &repl)
  222. if err != nil {
  223. logger.Info("JyPointRpc err:", err)
  224. return &repl
  225. }
  226. return &repl
  227. }
  228. func createTask(baseUserId, positionId int64, isNew bool, now string) (int64, int, string) {
  229. list := db.Tidb.SelectBySql(`select id,success_status,end_time from integral_task where user_id=?`, baseUserId)
  230. if list == nil {
  231. return -1, 0, ""
  232. } else if len(*list) == 0 {
  233. id := db.Tidb.Insert("integral_task", map[string]interface{}{
  234. "user_id": baseUserId,
  235. "position_id": positionId,
  236. "type": common.If(isNew, 2, 1),
  237. "create_time": now,
  238. })
  239. return id, 0, ""
  240. } else {
  241. return common.Int64All((*list)[0]["id"]), common.IntAll((*list)[0]["success_status"]), common.ObjToString((*list)[0]["end_time"])
  242. }
  243. }