发放优惠卷时增加了通知

This commit is contained in:
wucongxing8150 2024-05-29 15:37:06 +08:00
parent 444d4907b4
commit 032730ba3e
3 changed files with 59 additions and 11 deletions

View File

@ -1 +1,36 @@
package service
import (
"errors"
"fmt"
"hospital-open-api/extend/rabbitMq"
)
// PatientDistributeCoupon 患者-优惠劵发放-站内
func PatientDistributeCoupon(couponName string, userId int64) (bool, error) {
// 建立队列连接
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
if err != nil {
return false, errors.New("内部错误")
}
defer rabbitMQ.Close()
data := make(map[string]interface{})
data["user_id"] = fmt.Sprintf("%d", userId)
data["notice_type"] = 3
data["notice_system_type"] = 2
data["from_name"] = "肝胆小秘书"
data["notice_brief_title"] = "有新的优惠券已下发至您的账户,点击查看详情。"
data["notice_title"] = fmt.Sprintf("【%s】已到账", couponName)
data["notice_content"] = "有新的优惠劵已下发至您的账户中,点击查看详情!"
data["link_type"] = 7
err = rabbitMQ.Publish("send.station.message.queue", "amqp.direct", "SendStationMessage", data)
if err != nil {
return false, err
}
return true, nil
}

View File

@ -8,8 +8,10 @@ import (
dtoV1 "hospital-open-api/api/dto/v1"
"hospital-open-api/api/model"
requestsV1 "hospital-open-api/api/requests/v1"
"hospital-open-api/api/service"
"hospital-open-api/extend/rabbitMq"
"hospital-open-api/global"
"hospital-open-api/utils"
"strconv"
"time"
)
@ -58,6 +60,11 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
// 建立队列连接
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
if err != nil {
tx.Rollback()
return g, errors.New("内部错误")
}
defer rabbitMQ.Close()
// 获取优惠卷数据
@ -124,7 +131,7 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
now := time.Now()
// 检测优惠卷过期时间
if coupon.CouponType == 1 {
if coupon.ValidType == 1 {
// 1:绝对时效
validStartTime := time.Time(coupon.ValidStartTime)
if now.Before(validStartTime) {
@ -244,11 +251,18 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re
delay = 10 * time.Second
}
err = rabbitMQ.PublishWithDelay("amqp.delay.direct", "UserCouponExpired", data, delay)
err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay)
if err != nil {
tx.Rollback()
return nil, err
}
// 发送通知
res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId)
if !res {
utils.LogJsonInfo("优惠卷通知发送失败")
}
}
}

View File

@ -39,20 +39,19 @@ func NewRabbitMQClient() (*RabbitMQ, error) {
}
// Publish 生产
func (r *RabbitMQ) Publish(queueName, exchangeName string, message interface{}) error {
q, err := r.channel.QueueDeclare(
func (r *RabbitMQ) Publish(queueName, exchangeName, routingKey string, message interface{}) error {
_, err := r.channel.QueueDeclare(
queueName, // 队列名字
false, // 消息是否持久化
true, // 消息是否持久化
false, // 不使用的时候删除队列
false, // 排他
false, // 是否等待服务器确认
false, // 是否排他
false, // 是否阻塞
nil, // arguments
)
if err != nil {
return err
}
// Serialize the message
body, err := json.Marshal(message)
if err != nil {
return err
@ -60,7 +59,7 @@ func (r *RabbitMQ) Publish(queueName, exchangeName string, message interface{})
err = r.channel.Publish(
exchangeName, // exchange交换机名字
q.Name, //
routingKey, //
false, // 是否为无法路由的消息进行返回处理
false, // 是否对路由到无消费者队列的消息进行返回处理 RabbitMQ 3.0 废弃
amqp.Publishing{
@ -74,9 +73,9 @@ func (r *RabbitMQ) Publish(queueName, exchangeName string, message interface{})
return nil
}
func (r *RabbitMQ) PublishWithDelay(exchangeName, routingKey string, message interface{}, delay time.Duration) error {
func (r *RabbitMQ) PublishWithDelay(queueName, exchangeName, routingKey string, message interface{}, delay time.Duration) error {
err := r.channel.ExchangeDeclare(
exchangeName, // name
queueName, // name
"x-delayed-message", // type
true, // durable
false, // auto-deleted