package logs

import (
	"bp.jydev.jianyu360.cn/BP/jynsq/gonsq"
	"encoding/json"
	"log"
)

type NoticeConfig struct {
	isOpen          bool //是否开启提示
	IsJsonEncode    bool
	Addr, topic     string
	Id, Title, Text string
}
type Notice struct {
	p *gonsq.Producer
	c NoticeConfig
}

func newNotice(c NoticeConfig) (n *Notice, err error) {
	n = &Notice{
		c: c,
	}
	var producer *gonsq.Producer
	if c.isOpen {
		producer, err = gonsq.NewProducer(c.Addr, c.topic, c.IsJsonEncode)
		if err != nil {
			return
		}
		n.p = producer
	}
	return
}

// SendWarnMsg 发送异常通知
func (n *Notice) SendWarnMsg(detail map[string]interface{}) error {
	if n.p == nil {
		return nil
	}
	m := &gonsq.Msg{n.c.Id, n.c.Title, n.c.Text, detail}
	bs, err := json.Marshal(m)
	if err != nil {
		return err
	}
	log.Println("模拟发送信息----->", string(bs))
	return nil
	if err := n.p.Publish(bs); err != nil {
		log.Println("nsq连接失败", err)
	}
	return nil
}
