package activity import ( "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/go-logger/logger" "app.yhyue.com/moapp/message/db" "app.yhyue.com/moapp/message/model" "app.yhyue.com/moapp/message/util" "encoding/json" "fmt" "time" ) const ( //表 tableTaskInfo = "jyactivities.lottery_task_info" tableTaskUser = "jyactivities.lottery_task_user" tableActivityInfo = "jyactivities.activity_info" ) // 任务信息 type TaskInfo struct { Id int64 `json:"id"` Name string `json:"name"` ActiveId int64 `json:"active_id"` //活动id Qualification string `json:"qualification"` //获取抽奖资格次数; CycleNum int `json:"cycle_num"` //任务周期(每天/每周/每月/活动周期) 可执行任务次数:n次;-1:不限制 CycleUnit int `json:"cycle_unit"` //默认0:当天;;1:当周;2:当月;3:活动周期 ActivityStartTime string `json:"start_time"` //活动开始时间 ActivityEndTime string `json:"end_time"` //活动结束时间 } // 信息主题 type MsgBody struct { Phone string UserId string MgoUserId string PositionId int64 ActiveId int64 TaskInfoId int64 OrderCode string } // LotteryDrawTask 抽奖任务 func LotteryDrawTask(msg *model.Message) { var msgBody MsgBody if msg.E_body != nil { b, err := json.Marshal(msg.E_body) if err == nil { err = json.Unmarshal(b, &msgBody) } if err != nil { logger.Info(fmt.Sprintf("任务完成参数信息有误:%s", msg.E_userId)) return } } //判断用户是否存在 data, ok := db.Mgo.FindById("user", msg.E_userId, `{"l_registedate":1,"s_phone":1,"s_m_phone":1}`) if data == nil || len(*data) == 0 || !ok { logger.Info(fmt.Sprintf("未找到用户%s, %v。", msg.E_userId, msgBody.TaskInfoId)) return } //任务信息 taskInfos := db.Mysql.SelectBySql(fmt.Sprintf("SELECT lti.*,ai.start_time,ai.end_time FROM %s lti LEFT JOIN %s ai ON lti.active_id = ai.id WHERE lti.active_id = ? AND lti.id = ? AND ai.end_time >= NOW() ORDER BY lti.create_date ORDER BY lti.create_date DESC", tableTaskInfo, tableActivityInfo), msgBody.ActiveId, msgBody.TaskInfoId) if taskInfos == nil || len(*taskInfos) == 0 { logger.Info(fmt.Sprintf("没有当前需要完成的任务信息:%v", msgBody)) return } //任务信息 var taskInfo TaskInfo b, err := json.Marshal((*taskInfos)[0]) if err == nil { err = json.Unmarshal(b, &taskInfo) } if err != nil { logger.Info(fmt.Sprintf("任务信息异常:%v,err:%s", msgBody, err.Error())) return } var ( now = time.Now() timeRange = util.TimeRange{} ) //判断任务有效期内,此次任务是否已完成 switch taskInfo.CycleUnit { case 1: //当周 timeRange = util.GetWeekRange(now) case 2: //当月 timeRange = util.GetMonthRange(now) case 3: //活动周期 startTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityStartTime, time.Local) emdTime, _ := time.ParseInLocation(date.Date_Full_Layout, taskInfo.ActivityEndTime, time.Local) timeRange = util.TimeRange{ StartTime: startTime, EndTime: emdTime, StartUnix: startTime.Unix(), EndUnix: emdTime.Unix(), } default: //当天 timeRange = util.GetDayRange(now) } //当前周期内 是否已完成任务 if taskInfo.CycleNum > 0 { if count := db.Mysql.CountBySql(fmt.Sprintf(`SELECT COUNT(ltu.id) FROM %s ltu WHERE ltu.active_id = ? AND ltu.task_id = ? AND ltu.state = 0 AND ltu.create_date < ? AND ltu.create_date > ? `, tableTaskUser), taskInfo.ActiveId, taskInfo.Id, timeRange.EndTime.Format(date.Date_Full_Layout), timeRange.StartTime.Format(date.Date_Full_Layout)); taskInfo.CycleNum <= int(count) { logger.Info(fmt.Sprintf("用户:%s ,此任务:%s ,在 %s 已完成", msgBody.Phone, taskInfo.Name, timeRange.StartTime.Format(date.Date_Short_Layout))) return } } //二次验证 switch taskInfo.Id { case 1: //签到赠送剑鱼币任务 case 2: //设置关键词任务 var hasKeys bool //查看当前用户是否有订阅词 res := db.Compatible.Select(msgBody.UserId, `{"o_vipjy":1,"o_member_jy":1,"o_jy":"1"}`) if res != nil && len(*res) > 0 { obj, _ := (*res)["o_jy"].(map[string]interface{}) aKey, _ := obj["a_key"].([]interface{}) if len(aKey) > 0 { hasKeys = true } if !hasKeys { obj, _ = (*res)["o_vipjy"].(map[string]interface{}) if obj == nil { obj, _ = (*res)["o_member_jy"].(map[string]interface{}) } if obj != nil { itmes, _ := obj["a_items"].([]interface{}) for _, v := range itmes { item, _ := v.(map[string]interface{}) keys, _ := item["a_key"].([]interface{}) if len(keys) > 0 { hasKeys = true break } } } } } //没有设置关键词 if !hasKeys { logger.Info(fmt.Sprintf("当前用户:%s,手机号: %s 没有设置订阅词", msgBody.UserId, msgBody.Phone)) return } case 3: //招标采购搜索任务 case 4: //购买/续费/升级超级订阅会员 case 5: //购买/续费/升级大会员 case 6: //给朋友分享活动 } if id := db.Mysql.Insert(tableTaskUser, map[string]interface{}{ "active_id": msgBody.ActiveId, "task_id": msgBody.TaskInfoId, "phone": msgBody.Phone, "position_id": msgBody.PositionId, "mgo_user_id": msgBody.MgoUserId, "order_code": msgBody.OrderCode, "state": 0, "end_date": timeRange.EndTime.Format(date.Date_Full_Layout), "update_date": time.Now().Format(date.Date_Full_Layout), "create_date": time.Now().Format(date.Date_Full_Layout), }); id < 0 { logger.Info(fmt.Sprintf("保存任务记录异常:%v", msgBody)) } }