task.go 6.9 KB

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