package task import ( "database/sql" "encoding/json" "fmt" "log" "strconv" "time" "app.yhyue.com/moapp/MessageCenter/rpc/type/message" . "app.yhyue.com/moapp/jybase/api" qu "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/go-xweb/xweb" "app.yhyue.com/moapp/message/config" "app.yhyue.com/moapp/message/db" "app.yhyue.com/moapp/message/handler/activity" "app.yhyue.com/moapp/message/model" . "app.yhyue.com/moapp/message/rpc" . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p" "github.com/gogf/gf/v2/util/gconv" ) type Task struct { *xweb.Action task xweb.Mapper `xweb:"/task"` //获取任务 confirmChallenge xweb.Mapper `xweb:"/confirmChallenge"` //确认挑战 } type TaskInfo struct { Name string `json:"name"` Desc string `json:"desc"` PcHref string `json:"pcHref"` AppHref string `json:"appHref"` WxHref string `json:"wxHref"` Point int `json:"point"` Icon string `json:"icon"` Type string `json:"type"` Status int `json:"status"` FinishTime string `json:"finishTime"` } //InitTask 初始化任务 func (this *Task) InitTask(isNew bool) []*config.TaskStruct { if isNew { return config.TaskConf.NewTask } return config.TaskConf.OldTask } //Task 获取任务 func (this *Task) Task() { sessVal := this.Session().GetMultiple() mgoUserId := gconv.String(sessVal["mgoUserId"]) userId := gconv.String(sessVal["userId"]) positionId := gconv.Int64(sessVal["positionId"]) baseUserId := gconv.Int64(sessVal["base_user_id"]) now := date.NowFormat(date.Date_Full_Layout) // rData, errMsg := func() (interface{}, error) { info := map[string]interface{}{} data := []*TaskInfo{} dataM := map[string]*TaskInfo{} //完成状态 0未开始/过期 1正在进行 2已完成 status := 0 //查询用户任务情况 taskDetail := db.Tidb.SelectBySql(`select * from integral_task_detail where user_id =?`, baseUserId) if taskDetail != nil && len(*taskDetail) > 0 { for _, v := range *taskDetail { name := gconv.String(v["name"]) desc := gconv.String(v["description"]) pcHref := gconv.String(v["pc_href"]) appHref := gconv.String(v["app_href"]) wxHref := gconv.String(v["wx_href"]) point := gconv.Int(v["point"]) icon := gconv.String(v["icon"]) types := gconv.String(v["type"]) detailStatus := gconv.Int(v["status"]) finishTime := gconv.String(v["finish_time"]) tf := &TaskInfo{ Name: name, Desc: desc, PcHref: pcHref, AppHref: appHref, WxHref: wxHref, Point: point, Icon: icon, Type: types, Status: detailStatus, FinishTime: finishTime, } dataM[types] = tf } } userMsg, ok := db.Mgo.FindById("user", mgoUserId, `{"l_registedate":1,"s_m_phone":1,"s_phone":1,"s_myemail":1,"s_nickname":1,"s_headimageurl":1,"s_password":1,"s_company":1,"s_unionid":1,"o_jy":1,"o_vipjy":1,"o_member_jy":1,"i_app_login_task":1}`) if userMsg == nil || len(*userMsg) == 0 || !ok { return nil, fmt.Errorf("未查询到用户") } //注册时间 判断是新手任务还是老用户限时任务 l_registedate := gconv.Int64((*userMsg)["l_registedate"]) isNew := l_registedate > config.TaskConf.TaskStartTime //是否注册时间处于新手任务开始时间 ts := this.InitTask(isNew) //初始化任务 for _, v := range ts { if dataM[v.Type] == nil { dataM[v.Type] = &TaskInfo{ Name: v.Name, Desc: v.Desc, PcHref: v.PcHref, AppHref: v.AppHref, WxHref: v.WxHref, Point: v.Point, Icon: v.Icon, Type: v.Type, Status: 0, } } } mail := gconv.String((*userMsg)["s_myemail"]) if !isNew { //邮箱是否绑定 if mail != "" { dataM[model.BindMail].Status = 1 dataM[model.BindMail].FinishTime = now activity.Task(&model.Message{ E_code: "task", E_userId: userId, E_time: time.Now().Unix(), E_app: "jyweb_node2", E_body: map[string]interface{}{ "code": 1009, //首次订阅 "types": "bindMail", "baseUserId": baseUserId, "positionId": positionId, }, }) } } //判断是否已经创建任务 integralTaskData := db.Tidb.SelectBySql(`select * from integral_task where user_id =? limit 1`, baseUserId) if integralTaskData == nil || len(*integralTaskData) <= 0 { 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": qu.If(isNew, 2, 1), "create_time": now, }) fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"} args := []interface{}{} //任务明细 for _, v := range dataM { args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type) } db.Tidb.InsertBatchByTx(tx, "integral_task_detail", fields, args) return true }) } else { //部分初始化 taskId := gconv.Int64((*integralTaskData)[0]["id"]) fields := []string{"task_id", "user_id", "position_id", "name", "description", "icon", "point", "pc_href", "wx_href", "app_href", "status", "finish_time", "create_time", "type"} args := []interface{}{} for _, v := range dataM { if v.Status == 0 { if db.Tidb.CountBySql(`select count(1) from integral_task_detail where user_id =? and type =?`, baseUserId, v.Type) > 0 { continue } //任务明细 args = append(args, taskId, baseUserId, positionId, v.Name, v.Desc, v.Icon, v.Point, v.PcHref, v.WxHref, v.AppHref, v.Status, qu.If(v.FinishTime == "", nil, v.FinishTime), now, v.Type) } } if len(args) > 0 { db.Tidb.InsertBatch("integral_task_detail", fields, args) } if (*integralTaskData)[0]["end_time"] != nil { et := gconv.String((*integralTaskData)[0]["end_time"]) loc, _ := time.LoadLocation("Asia/Shanghai") endtime, _ := time.ParseInLocation(date.Date_Full_Layout, et, loc) if time.Now().Before(endtime) { //当前时间未超过结束时间 status = 1 } else { status = 3 } info["endTime"] = endtime.Unix() } success_status := gconv.Int((*integralTaskData)[0]["success_status"]) if success_status == 1 { //已完成挑战 status = 2 } } // for _, v := range dataM { for _, v := range ts { data = append(data, &TaskInfo{ Name: dataM[v.Type].Name, Desc: dataM[v.Type].Desc, PcHref: dataM[v.Type].PcHref, AppHref: dataM[v.Type].AppHref, WxHref: dataM[v.Type].WxHref, Point: dataM[v.Type].Point, Icon: dataM[v.Type].Icon, Type: dataM[v.Type].Type, Status: dataM[v.Type].Status, FinishTime: dataM[v.Type].FinishTime, }) } if isNew { info["newbieTask"] = data info["taskType"] = 2 } else { info["limitedTask"] = data info["taskType"] = 1 } //查询完成状态 info["status"] = status return info, nil }() if errMsg != nil { log.Printf("%s Task %s异常:%s\n", userId, errMsg.Error()) } this.ServeJson(NewResult(rData, errMsg)) } //EntSubscribe 判断企业是否订阅 func EntSubscribe(entUserId, entId int64) int { if entId > 0 { //企业 data, ok := db.Mgo.FindOne("entniche_rule", map[string]interface{}{ "i_entid": entId, "i_userid": entUserId, }) if data == nil || len(*data) <= 0 || !ok { return 0 } o_entniche := gconv.Map((*data)["o_entniche"]) a_items := gconv.Maps(o_entniche["a_items"]) for _, v := range a_items { a_key := gconv.Maps(v["a_key"]) if len(a_key) > 0 { return 1 } } } return 0 } func (this *Task) ConfirmChallenge() { sessVal := this.Session().GetMultiple() positionId := gconv.Int64(sessVal["positionId"]) baseUserId := gconv.Int64(sessVal["base_user_id"]) rData, errMsg := func() (interface{}, error) { infoMap := map[string]interface{}{} if string(this.Body()) == "" { return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1001) } body := xweb.FilterXSS(string(this.Body())) //接收参数 json.Unmarshal([]byte(body), &infoMap) if len(infoMap) == 0 { return map[string]interface{}{"status": -1}, fmt.Errorf(Error_msg_1002) } if gconv.Int(infoMap["challenge"]) == 1 { dayLater := time.Now().Add(time.Duration(config.TaskConf.TaskDayTime-1) * time.Hour * 24) endTime := time.Date(dayLater.Year(), dayLater.Month(), dayLater.Day(), 23, 59, 59, 0, time.Local) if db.Tidb.Update("integral_task", map[string]interface{}{ "position_id": positionId, "user_id": baseUserId, }, map[string]interface{}{ "end_time": endTime.Format(date.Date_Full_Layout), }) { go func() { //用户点击“确认挑战”给用户发消息 userId := gconv.String(sessVal["mgoUserId"]) wxUrl := "/front/sess/" + Se.EncodeString(userId+",_id,identityKeep,") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl) appUrl := "/jyapp/free/sess/" + Se.EncodeString(userId+",id,"+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(config.PushConfig.Messages.ConfirmChallenge.MobileUrl) req := &message.MultipleSaveMsgReq{ UserIds: userId, Title: config.PushConfig.Messages.ConfirmChallenge.Title, Content: config.PushConfig.Messages.ConfirmChallenge.Content, MsgType: config.PushConfig.Messages.ConfirmChallenge.MsgType, Link: config.PushConfig.Messages.ConfirmChallenge.PcUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl + "," + config.PushConfig.Messages.ConfirmChallenge.MobileUrl, Appid: config.PushConfig.Messages.ConfirmChallenge.Appid, AppPushUrl: appUrl, WxPushUrl: config.PushConfig.Webdomain + wxUrl, IosPushUrl: appUrl, } SendMsg("确认挑战", req) }() return map[string]interface{}{"status": 1}, nil } } return map[string]interface{}{"status": 1}, nil }() if errMsg != nil { log.Printf("%s UserAccount ConfirmChallenge 异常:%s\n", baseUserId, errMsg.Error()) } this.ServeJson(NewResult(rData, errMsg)) }