package svc import ( "applet/app/db" "applet/app/md" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "fmt" "strings" "time" "xorm.io/xorm" ) func NoSignSend(eg *xorm.Engine) { PessimismLockValue := "NoSignSend" ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { return } defer ch.Release() key := fmt.Sprintf("NoSignSend") //TODO::增加“悲观锁”防止串行 getString, _ := cache.GetString(key) if getString == PessimismLockValue { fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次未执行完") return } cache.SetEx(key, PessimismLockValue, 3600*8) //8小时 notice := make([]model.JpushNotice, 0) eg.Where("type='sign' and (jpush_open=1 or sms_open=1)").Desc("day").Find(¬ice) for _, v := range notice { commNoSignSend(eg, ch, 1, v) } cache.Del(key) } func commNoSignSend(eg *xorm.Engine, ch *rabbit.Channel, p int, data model.JpushNotice) { now := time.Now().Unix() lastLoginAt := now - int64(data.Day)*3600 loginNoticeTime := now - int64(data.NoticeDay)*3600 sql := ` SELECT u.id,u.phone,u.nickname,unt.id as unt_id,unt.sign_time,unt.sign_notice_time FROM user_notice_time unt LEFT JOIN user u on u.id=unt.uid WHERE unt.sign_time<'%d' and unt.sign_notice_time<'%d' and unt.sign_time>0 %s ` sql = fmt.Sprintf(sql, lastLoginAt, loginNoticeTime, "limit "+utils.IntToStr((p-1)*1000)+",1000") userList, _ := db.QueryNativeString(eg, sql) if len(userList) == 0 { return } extra := "" for _, v := range userList { hour := (now - utils.StrToInt64(v["sign_time"])) / 3600 data.Content = strings.ReplaceAll(data.Content, "{昵称}", v["nickname"]) data.Content = strings.ReplaceAll(data.Content, "{手机号}", v["phone"]) data.Content = strings.ReplaceAll(data.Content, "{小时}", utils.Int64ToStr(hour)) extra = "{\"hour\":\"" + utils.Int64ToStr(hour) + "\"}" if data.JpushOpen == 1 { arg := md.JpushRecordFundData{UserId: v["id"], Target: "1", Platform: "all", Title: data.Title, Content: data.Content} err := ch.PublishV2(md.EggJpushRecordQueueExchange, arg, md.EggJpushRecordQueue) if err != nil { ch.PublishV2(md.EggJpushRecordQueueExchange, arg, md.EggJpushRecordQueue) } } if data.SmsOpen == 1 { arg1 := md.AliyunSmsRecordFundData{Phone: v["phone"], Title: data.Title, Content: data.Content, Code: data.SmsCode, Extra: extra} err := ch.PublishV2(md.EggAliyunSmsRecordQueueExchange, arg1, md.EggAliyunSmsRecordQueue) if err != nil { ch.PublishV2(md.EggAliyunSmsRecordQueueExchange, arg1, md.EggAliyunSmsRecordQueue) } } eg.Where("id=?", v["unt_id"]).Cols("sign_notice_time").Update(&model.UserNoticeTime{SignNoticeTime: int(time.Now().Unix())}) } if len(userList) == 1000 { p++ commNoSignSend(eg, ch, p, data) return } return }