task.go 5.5 KB

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