task.go 7.1 KB

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