task.go 9.1 KB

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