package reservation import ( utilNsq "dashoo.cn/micro_libary/nsq" "encoding/json" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/glog" "github.com/nsqio/go-nsq" "lims_adapter/common" "time" ) // AutoSignOut 自动写入签退时间 type AutoSignOut struct { Id int `json:"id"` Tenant string `json:"tenant"` } func (a *AutoSignOut) HandleMessage(message *nsq.Message) error { if message.Body != nil { glog.Info("收到会议室预约的延时消息,内容为:", string(message.Body)) err := json.Unmarshal(message.Body, a) if err != nil { glog.Error("nsq body json unmarshal err:", err.Error()) } if a == nil { glog.Error("json unmarshal is null") } if a.Tenant == "" || a.Id == 0 { glog.Error("参数错误, 本次操作终止执行") } err = NewSrv(a.Tenant).AutoEnding(a.Id) if err != nil { glog.Error("auto press error:", err) } return nil } return nil } // ReceiverReservation 延迟消息接收 func ReceiverReservation(topic string) { // 从配置文件中获取主题 nsqChannel := utilNsq.NsqChannlNameForSrv() consumer, err := utilNsq.NewConsumerForHandler(topic, "reservation"+nsqChannel, new(AutoSignOut)) if err != nil { glog.Error("create nsq consumer err:", err.Error()) } <-consumer.StopChan } // AutoProcess 发送延迟消息 func AutoProcess(id int, tenant string, delayTime time.Duration) { topic := g.Cfg().GetString("nsq.nsqReservationAutoProcess") expire := AutoSignOut{ Tenant: tenant, Id: id, } msg, err := json.Marshal(expire) if err != nil { glog.Error("appoint auto process json marshal err:", err.Error()) } // 发送延时消息 err = common.NsqProducer.DeferredPublish(topic, delayTime, msg) if err != nil { glog.Error("发送预约类的延时消息失败,err:", err.Error()) } glog.Info("发送延时消息, 内容为:", string(msg), "延时时间为:", delayTime) }