123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package activity
- import (
- "database/sql"
- "fmt"
- "log"
- "net/rpc"
- "strconv"
- "strings"
- "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 := gcfg.Instance().MustGet(gctx.New(), "taskStartTime", nil).Int64()
- log.Println(msg.E_body)
- // num := gconv.Int64(msg.E_body["num"])
- 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,
- }
- //判断用户是否有任务
- 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
- }
- fmt.Println(v.Type, types, "==", v.Type == types)
- if v.Type == types {
- num = v.Point
- bl = true
- }
- }
- if !bl {
- logger.Info(fmt.Sprintf("用户%s没有该任务%v,不再增加积分。", msg.E_userId, code))
- return
- }
- //获取用户下所有的任务
- taskMsgMap := map[string]string{}
- success_status := 0
- end_time := ""
- 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
- left join integral_task b on a.task_id =b.id
- where a.user_id =?`, baseUserId)
- if taskAllMsg != nil && len(*taskAllMsg) > 0 {
- //用户有任务
- for _, v := range *taskAllMsg {
- success_status = gconv.Int(v["success_status"])
- end_time = gconv.String(v["end_time"])
- if success_status == 1 {
- logger.Info(fmt.Sprintf("用户%s已经完成挑战,默认任务完成%v,不再增加积分。", msg.E_userId, code))
- return
- }
- task_id := gconv.Int64(v["task_id"])
- typ := gconv.String(v["type"])
- status := gconv.String(v["status"])
- taskMsgMap[typ] = fmt.Sprintf("%v_%s", task_id, status)
- }
- }
- //判断是否赠送过积分,送过则不送
- if db.Mysql.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
- }
- //增加积分
- mrpc.IntegralHarvest(msg.E_userId, gconv.Int64(num), code, msg.E_time)
- //判断任务状态
- if taskMsgMap[types] == "" {
- //如果等于空证明没有初始化
- db.Tidb.ExecTx("创建任务", func(tx *sql.Tx) bool {
- taskId := db.Tidb.InsertByTx(tx, "integral_task", map[string]interface{}{
- "user_id": baseUserId,
- "position_id": positionId,
- "type": common.If(isNew, 2, 1),
- "create_time": now,
- })
- //任务明细
- for _, v := range taskData {
- if v.Type == types {
- db.Tidb.InsertByTx(tx, "integral_task_detail", map[string]interface{}{
- "task_id": taskId,
- "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,
- })
- }
- }
- return true
- })
- } else {
- //修改任务状态
- taskId := ""
- taskTypeArr := strings.Split(taskMsgMap[types], "_")
- fmt.Println("taskTypeArr:", taskTypeArr)
- if len(taskTypeArr) > 1 {
- taskId = taskTypeArr[0]
- status := taskTypeArr[1]
- if status == "0" {
- db.Tidb.Update("integral_task_detail", query, map[string]interface{}{
- "status": 1,
- })
- taskMsgMap[types] = fmt.Sprintf("%s_%v", taskId, 1)
- }
- }
- //判断是否已经完成所有任务
- if len(taskMsgMap) == len(taskData) && end_time != "" { //已初始化完所有任务
- finishStatus := true
- //
- for _, v := range taskMsgMap {
- vArr := strings.Split(v, "_")
- status := vArr[1]
- if status == "0" {
- //有未完成任务 不增加积分
- finishStatus = false
- }
- }
- endtime, _ := time.Parse(date.Date_Full_Layout, end_time)
- 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": gconv.Int64(taskId)}, 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: config.PushConfig.Messages.GetVip.Content,
- MsgType: config.PushConfig.Messages.GetVip.MsgType,
- Link: config.PushConfig.Messages.GetVip.PcUrl + "," + 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
- }
|