package activity import ( "app.yhyue.com/moapp/message/handler/award" "fmt" "github.com/gogf/gf/v2/os/gtime" "log" "net/rpc" "strconv" "time" "app.yhyue.com/moapp/MessageCenter/rpc/type/message" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/go-logger/logger" jrpc "app.yhyue.com/moapp/jybase/rpc" "app.yhyue.com/moapp/message/config" "app.yhyue.com/moapp/message/db" "app.yhyue.com/moapp/message/model" . "app.yhyue.com/moapp/message/rpc" mrpc "app.yhyue.com/moapp/message/rpc" . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p" "github.com/gogf/gf/v2/os/gcfg" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/util/gconv" ) /* 新手任务/限时任务 */ // Task 完成任务 func Task(msg *model.Message) { //活动时间 taskStartTime := config.TaskConf.TaskStartTime log.Println(msg.E_body, taskStartTime) num := 0 code := gconv.Int64(msg.E_body["code"]) //1005 types := gconv.String(msg.E_body["types"]) baseUserId := gconv.Int64(msg.E_body["baseUserId"]) positionId := gconv.Int64(msg.E_body["positionId"]) now := date.NowFormat(date.Date_Full_Layout) query := map[string]interface{}{ "type": types, "user_id": baseUserId, } // p647新绑定用户送7天超级订阅 startTime := gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "newRegister.startTime").String(), "2006-01-02T15:04:05Z").Time entTime := gtime.NewFromStrLayout(gcfg.Instance().MustGet(gctx.New(), "newRegister.entTime").String(), "2006-01-02T15:04:05Z").Time if time.Now().After(startTime) && time.Now().Before(entTime) && gconv.Bool(msg.E_body["isOnlyBind"]) { _ = award.GivenSubVip(msg.E_userId, award.SubVip{ Num: gcfg.Instance().MustGet(gctx.New(), "newRegister.supVip", nil).Int64(), ActivityCode: "newRegister", Date: msg.E_time, Desc: "2024年新用户注册赠送7天超级订阅", }) SendMsg("新绑定手机号用户", &message.MultipleSaveMsgReq{ UserIds: msg.E_userId, Title: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.title", nil).String(), Content: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.content", nil).String(), MsgType: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.msgType", nil).Int64(), Appid: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.appid", nil).String(), Link: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.pcUrl", nil).String() + "," + gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", nil).String() + "," + gcfg.Instance().MustGet(gctx.New(), "newRegister.message.wxUrl", nil).String() + "," + gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", nil).String(), AppPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", nil).String(), WxPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.wxUrl", nil).String(), IosPushUrl: gcfg.Instance().MustGet(gctx.New(), "newRegister.message.mobileUrl", nil).String(), }) } //判断用户是否有任务 data, ok := db.Mgo.FindById("user", msg.E_userId, `{"l_registedate":1}`) if data == nil || len(*data) == 0 || !ok { logger.Info(fmt.Sprintf("未找到用户%s,无法增加积分%v。", msg.E_userId, code)) return } l_registedate := gconv.Int64((*data)["l_registedate"]) isNew := l_registedate > taskStartTime //是否注册时间处于新手任务开始时间 taskData := config.TaskConf.OldTask if isNew { taskData = config.TaskConf.NewTask } //判断用户是否有该任务 bl := false for _, v := range taskData { if bl { continue } if v.Type == types { num = v.Point bl = true } } if !bl { logger.Info(fmt.Sprintf("用户%s没有该任务%v,不再增加积分。", msg.E_userId, code)) return } var task_id int64 success_status := 0 end_time := "" for i := 0; i < 5; i++ { task_id, success_status, end_time = createTask(baseUserId, positionId, isNew, now) if task_id > 0 { break } time.Sleep(time.Second) } if task_id <= 0 { logger.Error("没有取到task_id", msg.E_userId, baseUserId, code) return } else if success_status == 1 { logger.Info(fmt.Sprintf("用户%s已经完成挑战,默认任务完成%v,不再增加积分。", msg.E_userId, code)) return } else if db.TidbPoint.CountBySql(`select count(1) from jypoints.integral_flow where userId =? and pointType =? and sort =1;`, msg.E_userId, code) > 0 { //判断是否赠送过积分,送过则不送 logger.Info(fmt.Sprintf("用户%s已经增加过积分%v,不再增加积分。", msg.E_userId, code)) return } //获取用户下所有的任务 taskMsgMap := map[string]int{} taskAllMsg := db.Tidb.SelectBySql(`select name,status,type from integral_task_detail where user_id=?`, baseUserId) if taskAllMsg != nil && len(*taskAllMsg) > 0 { //用户有任务 for _, v := range *taskAllMsg { taskMsgMap[gconv.String(v["type"])] = gconv.Int(v["status"]) } } //增加积分 mrpc.IntegralHarvest(msg.E_userId, gconv.Int64(num), code, msg.E_time) //判断任务状态 fmt.Println(msg.E_userId, taskMsgMap[types]) if _, exists := taskMsgMap[types]; !exists { //任务明细 for _, v := range taskData { if v.Type == types { db.Tidb.Insert("integral_task_detail", map[string]interface{}{ "task_id": task_id, "user_id": baseUserId, "position_id": positionId, "name": v.Name, "description": v.Desc, "icon": v.Icon, "point": v.Point, "pc_href": v.PcHref, "wx_href": v.WxHref, "app_href": v.AppHref, "status": 1, //完成任务 "finish_time": now, "create_time": now, "type": v.Type, }) break } } } //修改任务状态 fmt.Println(msg.E_userId, types, "update") status := taskMsgMap[types] fmt.Println(msg.E_userId, status, "status", status == 0) if status == 0 { db.Tidb.Update("integral_task_detail", query, map[string]interface{}{ "status": 1, "finish_time": time.Now().Format(date.Date_Full_Layout), }) taskMsgMap[types] = 1 } //判断是否已经完成所有任务 if len(taskMsgMap) == len(taskData) && end_time != "" { //已初始化完所有任务 finishStatus := true // for _, v := range taskMsgMap { if v == 0 { //有未完成任务 不增加积分 finishStatus = false } } endtime, _ := time.Parse(date.Date_Full_Layout, end_time) logger.Info("STATUS:", msg.E_userId, finishStatus, success_status == 0, time.Now().Before(endtime)) if finishStatus && success_status == 0 && time.Now().Before(endtime) { //判断是否完成所有任务且开启确认挑战 if mrpc.SubVipHarvest(msg.E_userId, 7, "") == nil { if db.Tidb.Update("integral_task", map[string]interface{}{"id": task_id}, map[string]interface{}{ "success_status": 1, }) { go func() { wxUrl := "/front/sess/" + Se.EncodeString(msg.E_userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl) appUrl := "/jyapp/free/sess/" + Se.EncodeString(msg.E_userId+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.GetVip.MobileUrl) SendMsg("获赠七天超级订阅服务", &message.MultipleSaveMsgReq{ UserIds: msg.E_userId, Title: config.PushConfig.Messages.GetVip.Title, Content: fmt.Sprintf(config.PushConfig.Messages.GetVip.Content, common.If(isNew, "新手", "限时").(string)), MsgType: config.PushConfig.Messages.GetVip.MsgType, Link: config.PushConfig.Messages.GetVip.PcUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl + "," + config.PushConfig.Messages.GetVip.MobileUrl, Appid: config.PushConfig.Messages.GetVip.Appid, AppPushUrl: appUrl, WxPushUrl: config.PushConfig.Webdomain + wxUrl, IosPushUrl: appUrl, }) }() } } } } } // 获取任务 func GetTaskRpc(isNew bool) (p *jrpc.TaskDataResp) { defer common.Catch() var repl jrpc.TaskDataResp client, err := rpc.DialHTTP("tcp", gcfg.Instance().MustGet(gctx.New(), "rpc.pointrpc").String()) if err != nil { logger.Info("tcp err", err) return &repl } defer client.Close() err = client.Call("JyPointRpc.GetTask", jrpc.TaskData{IsNew: isNew}, &repl) if err != nil { logger.Info("JyPointRpc err:", err) return &repl } return &repl } func createTask(baseUserId, positionId int64, isNew bool, now string) (int64, int, string) { list := db.Tidb.SelectBySql(`select id,success_status,end_time from integral_task where user_id=?`, baseUserId) if list == nil { return -1, 0, "" } else if len(*list) == 0 { id := db.Tidb.Insert("integral_task", map[string]interface{}{ "user_id": baseUserId, "position_id": positionId, "type": common.If(isNew, 2, 1), "create_time": now, }) return id, 0, "" } else { return common.Int64All((*list)[0]["id"]), common.IntAll((*list)[0]["success_status"]), common.ObjToString((*list)[0]["end_time"]) } }