task.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package activity
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "net/rpc"
  7. "strings"
  8. "app.yhyue.com/moapp/message/config"
  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/db"
  14. "app.yhyue.com/moapp/message/model"
  15. mrpc "app.yhyue.com/moapp/message/rpc"
  16. "github.com/gogf/gf/v2/os/gcfg"
  17. "github.com/gogf/gf/v2/os/gctx"
  18. "github.com/gogf/gf/v2/util/gconv"
  19. )
  20. /*
  21. 新手任务/限时任务
  22. */
  23. // Task 完成任务
  24. func Task(msg *model.Message) {
  25. //活动时间
  26. taskStartTime := gcfg.Instance().MustGet(gctx.New(), "taskStartTime", nil).Int64()
  27. log.Println(msg.E_body)
  28. num := gconv.Int64(msg.E_body["num"])
  29. code := gconv.Int64(msg.E_body["code"]) //1005
  30. types := gconv.String(msg.E_body["type"])
  31. baseUserId := gconv.Int64(msg.E_body["baseUserId"])
  32. positionId := gconv.Int64(msg.E_body["positionId"])
  33. now := date.NowFormat(date.Date_Full_Layout)
  34. query := map[string]interface{}{
  35. "type": types,
  36. "user_id": baseUserId,
  37. }
  38. //判断用户是否有任务
  39. data, ok := db.Mgo.FindOne("user", map[string]interface{}{"base_user_id": baseUserId})
  40. if data == nil || len(*data) == 0 || !ok {
  41. logger.Info(fmt.Sprintf("未找到用户%s,无法增加积分%v。", msg.E_userId, code))
  42. return
  43. }
  44. l_registedate := gconv.Int64((*data)["l_registedate"])
  45. isNew := l_registedate > taskStartTime //是否注册时间处于新手任务开始时间
  46. taskData := config.TaskConf.OldTask
  47. if isNew {
  48. taskData = config.TaskConf.NewTask
  49. }
  50. //判断用户是否有该任务
  51. bl := false
  52. for _, v := range taskData {
  53. if bl {
  54. continue
  55. }
  56. if v.Type == types {
  57. bl = true
  58. }
  59. }
  60. if !bl {
  61. logger.Info(fmt.Sprintf("用户%s没有该任务%v,不再增加积分。", msg.E_userId, code))
  62. return
  63. }
  64. //获取用户下所有的任务
  65. taskMsgMap := map[string]string{}
  66. success_status := 0
  67. end_time := ""
  68. taskAllMsg := db.Mysql.SelectBySql(`select a.task_id,a.name,a.status,a.type,b.success_status,b.end_time from integral_task_detail a
  69. left join integral_task b on a.task_id =b.id
  70. where a.user_id =?`, baseUserId)
  71. if taskAllMsg != nil && len(*taskAllMsg) > 0 {
  72. //用户有任务
  73. for _, v := range *taskAllMsg {
  74. success_status = gconv.Int(v["success_status"])
  75. end_time = gconv.String(v["end_time"])
  76. if success_status == 1 {
  77. logger.Info(fmt.Sprintf("用户%s已经完成挑战,默认任务完成%v,不再增加积分。", msg.E_userId, code))
  78. return
  79. }
  80. task_id := gconv.Int64(v["task_id"])
  81. typ := gconv.String(v["type"])
  82. status := gconv.String(v["status"])
  83. taskMsgMap[typ] = fmt.Sprintf("%v_%s", task_id, status)
  84. }
  85. }
  86. //判断是否赠送过积分,送过则不送
  87. if db.Mysql.CountBySql(`select count(1) from jypoints.integral_flow where userId =? and pointType =? and sort =1;`, msg.E_userId, code) > 0 {
  88. logger.Info(fmt.Sprintf("用户%s已经增加过积分%v,不再增加积分。", msg.E_userId, code))
  89. return
  90. }
  91. //增加积分
  92. mrpc.IntegralHarvest(msg.E_userId, num, code, msg.E_time)
  93. //判断任务状态
  94. if taskMsgMap[types] == "" {
  95. //如果等于空证明没有初始化
  96. db.Tidb.ExecTx("创建任务", func(tx *sql.Tx) bool {
  97. taskId := db.Tidb.InsertByTx(tx, "integral_task", map[string]interface{}{
  98. "user_id": baseUserId,
  99. "position_id": positionId,
  100. "type": common.If(isNew, 2, 1),
  101. "create_time": now,
  102. })
  103. //任务明细
  104. for _, v := range taskData {
  105. if v.Type == types {
  106. db.Tidb.InsertByTx(tx, "integral_task_detail", map[string]interface{}{
  107. "task_id": taskId,
  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. }
  123. }
  124. return true
  125. })
  126. } else {
  127. //修改任务状态
  128. taskId := ""
  129. taskTypeArr := strings.Split(taskMsgMap[types], "_")
  130. if len(taskTypeArr) > 1 {
  131. taskId = taskTypeArr[0]
  132. status := taskTypeArr[1]
  133. if status == "0" {
  134. db.Tidb.Update("integral_task_detail", query, map[string]interface{}{
  135. "status": 1,
  136. })
  137. taskMsgMap[types] = fmt.Sprintf("%s_%v", taskId, 1)
  138. }
  139. }
  140. //判断是否已经完成所有任务
  141. if len(taskMsgMap) == len(taskData) && end_time != "" { //已初始化完所有任务
  142. finishStatus := true
  143. //
  144. for _, v := range taskMsgMap {
  145. vArr := strings.Split(v, "_")
  146. status := vArr[1]
  147. if status == "0" {
  148. //有未完成任务 不增加积分
  149. finishStatus = false
  150. }
  151. }
  152. endtime, _ := time.Parse(date.Date_Full_Layout, end_time)
  153. if finishStatus && success_status == 0 && time.Now().Before(endtime) {
  154. //判断是否完成所有任务且开启确认挑战
  155. mrpc.SubVipHarvest(msg.E_userId, 7, "")
  156. db.Tidb.Update("integral_task", map[string]interface{}{"id": gconv.Int64(taskId)}, map[string]interface{}{
  157. "success_status": 1,
  158. })
  159. }
  160. }
  161. }
  162. }
  163. //获取任务
  164. func GetTaskRpc(isNew bool) (p *jrpc.TaskDataResp) {
  165. defer common.Catch()
  166. var repl jrpc.TaskDataResp
  167. client, err := rpc.DialHTTP("tcp", gcfg.Instance().MustGet(gctx.New(), "rpc.pointrpc").String())
  168. if err != nil {
  169. logger.Info("tcp err", err)
  170. return &repl
  171. }
  172. defer client.Close()
  173. err = client.Call("JyPointRpc.GetTask", jrpc.TaskData{IsNew: isNew}, &repl)
  174. if err != nil {
  175. logger.Info("JyPointRpc err:", err)
  176. return &repl
  177. }
  178. return &repl
  179. }