task.go 7.2 KB

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