// singleBatch
package getui

import (
"encoding/json"
"gugegin/tools"
"log"
"strings"
)

//【toSingle】执行cid批量单推
type Batch struct {
Is_async bool
Msg_list []Single
}

func Sba() {
cids := []string{"0373e181f3b1b36f1c725ea8e4f9492d"}
rd := tools.RandomStr(5)

noti := Notification{
Title: "Batch推标题" + rd,
Body: "Batch推内容" + rd,
Click_type: "none",
}
pus := Push_message{
Notification: noti,
}
aud := Audience{
Cid: cids,
}
signe := Single{
Request_id: tools.RandomStr(30),
Audience: aud,
Push_message: pus,
}

// sgs := []Single{}
// sgs = append(sgs, signe)

SingleBatch([]Single{signe})
}

//不同的内容推送给不同的用户,同时执行多次
func SingleBatch(mlist []Single) {
url := BASEURL + "/push/single/batch/cid"
log.Println(url)

batch := Batch{
Is_async: true,
Msg_list: mlist,
}
reqBody := strings.ToLower(tools.StructToJSON(batch))
s := tools.ClientPost2(url, strings.NewReader(reqBody), TokenValue)
sResult := PushResult{}
err := json.Unmarshal([]byte(s), &sResult)
if err != nil {
log.Println(err)
}
if sResult.Code == 0 {
log.Println("推送成功")
}

if sResult.Code == 10001 {
log.Println("--------SingleBatch----token过期或无效------")
//token过期
ObtainToken()
SingleBatch(mlist)
}
log.Println("--------SingleBatch-----msg-----", sResult.Msg)
}