task.go 9.0 KB

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