task.go 9.5 KB

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