Compare commits
10 Commits
20e6ca30eb
...
b6d99cda23
| Author | SHA1 | Date | |
|---|---|---|---|
| b6d99cda23 | |||
| 7af51c49ce | |||
| d6e28d292e | |||
| f0f79d6435 | |||
| 75ed34e791 | |||
| f5694cb227 | |||
| 032730ba3e | |||
| 444d4907b4 | |||
| e425546627 | |||
| a04e59c3de |
@ -11,12 +11,12 @@ import (
|
||||
|
||||
type Coupon struct{}
|
||||
|
||||
// GetMultiDoctor 获取多点执业医生详情
|
||||
func (r *Coupon) GetMultiDoctor(c *gin.Context) {
|
||||
userDoctorRequest := requestsV1.UserDoctorRequest{}
|
||||
req := userDoctorRequest.GetMultiDoctor
|
||||
// ReceiveCoupon 领取优惠卷
|
||||
func (r *Coupon) ReceiveCoupon(c *gin.Context) {
|
||||
couponRequest := requestsV1.CouponRequest{}
|
||||
req := couponRequest.ReceiveCoupon
|
||||
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
@ -28,12 +28,12 @@ func (r *Coupon) GetMultiDoctor(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
userDoctorService := serviceV1.UserDoctorService{}
|
||||
getMultiDoctorResponses, err := userDoctorService.GetMultiDoctor(req)
|
||||
couponService := serviceV1.CouponService{}
|
||||
ReceiveCouponResponses, err := couponService.ReceiveCoupon(req)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.OkWithData(getMultiDoctorResponses, c)
|
||||
responses.OkWithData(ReceiveCouponResponses, c)
|
||||
}
|
||||
|
||||
91
api/dao/coupon.go
Normal file
91
api/dao/coupon.go
Normal file
@ -0,0 +1,91 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hospital-open-api/api/model"
|
||||
"hospital-open-api/global"
|
||||
)
|
||||
|
||||
type CouponDao struct {
|
||||
}
|
||||
|
||||
// GetCouponById 获取数据-id
|
||||
func (r *CouponDao) GetCouponById(couponId int64) (m *model.Coupon, err error) {
|
||||
err = global.Db.First(&m, couponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetCouponPreloadById 获取数据-加载全部关联-id
|
||||
func (r *CouponDao) GetCouponPreloadById(couponId int64) (m *model.Coupon, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, couponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// EditCoupon 修改
|
||||
func (r *CouponDao) EditCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.Coupon{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditCouponByOrderServicePackageId 修改-id
|
||||
func (r *CouponDao) EditCouponByOrderServicePackageId(tx *gorm.DB, couponId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCouponList 获取列表
|
||||
func (r *CouponDao) GetCouponList(maps interface{}) (m []*model.Coupon, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddCoupon 新增
|
||||
func (r *CouponDao) AddCoupon(tx *gorm.DB, model *model.Coupon) (*model.Coupon, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *CouponDao) Inc(tx *gorm.DB, couponId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *CouponDao) Dec(tx *gorm.DB, couponId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditCouponById 修改
|
||||
func (r *CouponDao) EditCouponById(tx *gorm.DB, couponId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
64
api/dao/popup.go
Normal file
64
api/dao/popup.go
Normal file
@ -0,0 +1,64 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hospital-open-api/api/model"
|
||||
"hospital-open-api/global"
|
||||
)
|
||||
|
||||
type PopupDao struct {
|
||||
}
|
||||
|
||||
// GetPopupById 获取数据-id
|
||||
func (r *PopupDao) GetPopupById(popupId int64) (m *model.Popup, err error) {
|
||||
err = global.Db.First(&m, popupId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetPopupPreloadById 获取数据-加载全部关联-id
|
||||
func (r *PopupDao) GetPopupPreloadById(popupId int64) (m *model.Popup, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, popupId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// EditPopup 修改
|
||||
func (r *PopupDao) EditPopup(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.Popup{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditPopupByOrderServicePackageId 修改-id
|
||||
func (r *PopupDao) EditPopupByOrderServicePackageId(tx *gorm.DB, popupId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Popup{}).Where("popup_id = ?", popupId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPopupList 获取列表
|
||||
func (r *PopupDao) GetPopupList(maps interface{}) (m []*model.Popup, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddPopup 新增
|
||||
func (r *PopupDao) AddPopup(tx *gorm.DB, model *model.Popup) (*model.Popup, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
@ -71,3 +71,12 @@ func (r *UserDao) GetUserList(maps interface{}) (m []*model.User, err error) {
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUser 获取用户
|
||||
func (r *UserDao) GetUser(maps interface{}) (m *model.User, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@ -73,9 +73,18 @@ func (r *UserCouponDao) GetUserCouponList(maps interface{}) (m []*model.UserCoup
|
||||
}
|
||||
|
||||
// AddUserCoupon 新增用户优惠卷
|
||||
func (r *UserCouponDao) AddUserCoupon(tx *gorm.DB, model *model.UserCoupon) (*model.UserCoupon, error) {
|
||||
func (r *UserCouponDao) AddUserCoupon(tx *gorm.DB, model *model.UserCoupon) (m *model.UserCoupon, err error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetUserCoupon 获取
|
||||
func (r *UserCouponDao) GetUserCoupon(maps interface{}) (m *model.UserCoupon, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
90
api/dao/userPatient.go
Normal file
90
api/dao/userPatient.go
Normal file
@ -0,0 +1,90 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hospital-open-api/api/model"
|
||||
"hospital-open-api/global"
|
||||
)
|
||||
|
||||
type UserPatientDao struct {
|
||||
}
|
||||
|
||||
// GetUserPatientById 获取患者数据-患者id
|
||||
func (r *UserPatientDao) GetUserPatientById(patientId int64) (m *model.UserPatient, err error) {
|
||||
err = global.Db.First(&m, patientId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUserPatientByUserId 获取患者数据-id
|
||||
func (r *UserPatientDao) GetUserPatientByUserId(userId int64) (m *model.UserPatient, err error) {
|
||||
err = global.Db.Where("user_id = ?", userId).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUserPatientPreloadById 获取患者数据-加载全部关联-患者id
|
||||
func (r *UserPatientDao) GetUserPatientPreloadById(patientId int64) (m *model.UserPatient, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, patientId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteUserPatient 删除患者
|
||||
func (r *UserPatientDao) DeleteUserPatient(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.UserPatient{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUserPatientById 删除患者-患者id
|
||||
func (r *UserPatientDao) DeleteUserPatientById(tx *gorm.DB, patientId int64) error {
|
||||
if err := tx.Delete(&model.UserPatient{}, patientId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUserPatient 修改患者
|
||||
func (r *UserPatientDao) EditUserPatient(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.UserPatient{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUserPatientById 修改患者-患者id
|
||||
func (r *UserPatientDao) EditUserPatientById(tx *gorm.DB, patientId int64, data interface{}) error {
|
||||
err := tx.Model(&model.UserPatient{}).Where("patient_id = ?", patientId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserPatientList 获取患者列表
|
||||
func (r *UserPatientDao) GetUserPatientList(maps interface{}) (m []*model.UserPatient, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddUserPatient 新增患者
|
||||
func (r *UserPatientDao) AddUserPatient(tx *gorm.DB, model *model.UserPatient) (*model.UserPatient, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
7
api/dto/v1/coupon.go
Normal file
7
api/dto/v1/coupon.go
Normal file
@ -0,0 +1,7 @@
|
||||
package v1
|
||||
|
||||
type ReceiveCouponDto struct {
|
||||
Status int `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
@ -68,6 +68,7 @@ func Auth() gin.HandlerFunc {
|
||||
|
||||
concatenatedParamsStr := ""
|
||||
requestParams, ok := paramsRaw.(map[string]string)
|
||||
fmt.Println(requestParams)
|
||||
if ok || len(requestParams) > 0 {
|
||||
// 将请求参数按照参数名升序排序
|
||||
sortedKeys := make([]string, 0, len(requestParams))
|
||||
|
||||
58
api/model/coupon.go
Normal file
58
api/model/coupon.go
Normal file
@ -0,0 +1,58 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"hospital-open-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Coupon 优惠卷表
|
||||
type Coupon struct {
|
||||
CouponId int64 `gorm:"column:coupon_id;type:bigint(19);primary_key;comment:主键id" json:"coupon_id"`
|
||||
CouponName string `gorm:"column:coupon_name;type:varchar(255);comment:优惠卷名称" json:"coupon_name"`
|
||||
CouponIcon string `gorm:"column:coupon_icon;type:varchar(255);comment:优惠卷图片" json:"coupon_icon"`
|
||||
CouponClient int `gorm:"column:coupon_client;type:tinyint(1);comment:使用平台(1:小程序);NOT NULL" json:"coupon_client"`
|
||||
CouponType int `gorm:"column:coupon_type;type:tinyint(1);comment:优惠卷类型(1:无门槛 2:满减 3:数量);NOT NULL" json:"coupon_type"`
|
||||
CouponStatus int `gorm:"column:coupon_status;type:tinyint(1);default:1;comment:状态(1:正常 2:强制失效 3:结束 4:删除)" json:"coupon_status"`
|
||||
DistributionObject int `gorm:"column:distribution_object;type:tinyint(1);default:1;comment:发放对象(1:全部用户 2:新注册用户 3:会员 4:近期消费 5:近期购药 6:存量用户 7:健康包服务用户)" json:"distribution_object"`
|
||||
ApplicationScope int `gorm:"column:application_scope;type:tinyint(1);default:1;comment:适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品)" json:"application_scope"`
|
||||
InquiryType string `gorm:"column:inquiry_type;type:varchar(100);comment:关联问诊类型,application_scope=问诊时存在生效,逗号分隔(1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药 6:检测)" json:"inquiry_type"`
|
||||
BrandId int64 `gorm:"column:brand_id;type:bigint(19);comment:关联品牌id(如不限制品牌,此项为空)" json:"brand_id"`
|
||||
IsMutex int `gorm:"column:is_mutex;type:tinyint(1);default:1;comment:是否互斥(0:否 1:是)互斥情况下无法和其他优惠卷同时使用" json:"is_mutex"`
|
||||
IsDisplay int `gorm:"column:is_display;type:tinyint(1);comment:是否展示(0:否 1:是)" json:"is_display"`
|
||||
DistributionWithDay int `gorm:"column:distribution_with_day;type:int(11);default:0;comment:发放关联天数(发放对象为近期消费等类型时规定天数)" json:"distribution_with_day"`
|
||||
MinUsableNumber int `gorm:"column:min_usable_number;type:int(11);default:1;comment:单商品最小可使用数量(默认为1,类型为数量时使用,如需限制优惠卷使用数量,请填写此处)" json:"min_usable_number"`
|
||||
CouponCount int `gorm:"column:coupon_count;type:int(10);default:0;comment:发放数量" json:"coupon_count"`
|
||||
CouponTakeCount int `gorm:"column:coupon_take_count;type:int(10);default:0;comment:已领取数量" json:"coupon_take_count"`
|
||||
CouponUsedCount int `gorm:"column:coupon_used_count;type:int(10);default:0;comment:已使用数量" json:"coupon_used_count"`
|
||||
CouponPrice float64 `gorm:"column:coupon_price;type:decimal(10,2);default:0.00;comment:优惠卷金额" json:"coupon_price"`
|
||||
WithAmount float64 `gorm:"column:with_amount;type:decimal(10,2);default:0.00;comment:符合满减标准金额(优惠卷类型为满减时使用)" json:"with_amount"`
|
||||
ValidType int `gorm:"column:valid_type;type:tinyint(4);comment:有效类型(1:绝对时效,xxx-xxx时间段有效 2:相对时效 n天内有效);NOT NULL" json:"valid_type"`
|
||||
ValidDays int `gorm:"column:valid_days;type:int(3);comment:自领取之日起有效天数" json:"valid_days"`
|
||||
ValidStartTime LocalTime `gorm:"column:valid_start_time;type:datetime;comment:开始使用时间" json:"valid_start_time"`
|
||||
ValidEndTime LocalTime `gorm:"column:valid_end_time;type:datetime;comment:结束使用时间" json:"valid_end_time"`
|
||||
ProductId string `gorm:"column:product_id;type:text;comment:关联商品id,逗号分隔,指定商品时,填入此项。" json:"product_id"`
|
||||
ReissueIntervalDays int `gorm:"column:reissue_interval_days;type:int(5);default:0;comment:确认收货后的再次发放间隔天数(如果设置为 0,则表示不再次发放。当适用范围为商品时生效)" json:"reissue_interval_days"`
|
||||
IsReissuableAfterExpire int `gorm:"column:is_reissuable_after_expire;type:tinyint(1);default:0;comment:过期之后是否允许再次发放(0:否 1:是)" json:"is_reissuable_after_expire"`
|
||||
IsPopup int `gorm:"column:is_popup;type:tinyint(1);default:0;comment:是否首页弹窗(0:否 1:是)" json:"is_popup"`
|
||||
CouponDesc string `gorm:"column:coupon_desc;type:text;comment:优惠卷描述" json:"coupon_desc"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Coupon) TableName() string {
|
||||
return "gdxz_coupon"
|
||||
}
|
||||
|
||||
func (m *Coupon) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.CouponId == 0 {
|
||||
m.CouponId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
40
api/model/popup.go
Normal file
40
api/model/popup.go
Normal file
@ -0,0 +1,40 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"hospital-open-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Popup 弹窗表
|
||||
type Popup struct {
|
||||
PopupId int64 `gorm:"column:popup_id;type:bigint(19);primary_key;comment:主键id" json:"popup_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id" json:"user_id"`
|
||||
AppType int `gorm:"column:app_type;type:tinyint(4);comment:应用程序类型(1:小程序 2:app);NOT NULL" json:"app_type"`
|
||||
ClientType int `gorm:"column:client_type;type:tinyint(4);comment:客户端类型(1:患者端 2:医生端 3:药师端);NOT NULL" json:"client_type"`
|
||||
Status int `gorm:"column:status;type:tinyint(4);default:0;comment:状态(0:未弹 1:已弹)" json:"status"`
|
||||
PopupType int `gorm:"column:popup_type;type:tinyint(4);comment:弹窗类型(1:结算费用规则 2:新优惠卷弹窗);NOT NULL" json:"popup_type"`
|
||||
PopupTitle string `gorm:"column:popup_title;type:varchar(255);comment:标题" json:"popup_title"`
|
||||
PopupContent string `gorm:"column:popup_content;type:varchar(1000);comment:内容" json:"popup_content"`
|
||||
PopupImg string `gorm:"column:popup_img;type:varchar(255);comment:封面图片" json:"popup_img"`
|
||||
PopupLink string `gorm:"column:popup_link;type:varchar(255);comment:跳转地址" json:"popup_link"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Popup) TableName() string {
|
||||
return "gdxz_popup"
|
||||
}
|
||||
|
||||
func (m *Popup) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.PopupId == 0 {
|
||||
m.PopupId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -8,14 +8,14 @@ import (
|
||||
|
||||
// UserCoupon 用户优惠卷表
|
||||
type UserCoupon struct {
|
||||
UserCouponId int64 `gorm:"column:user_coupon_id;type:bigint(19);primary_key;comment:主键id" json:"user_coupon_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
PatientId int64 `gorm:"column:patient_id;type:bigint(19);comment:患者id" json:"patient_id"`
|
||||
CouponId int64 `gorm:"column:coupon_id;type:bigint(19);comment:优惠卷id;NOT NULL" json:"coupon_id"`
|
||||
UserCouponStatus int `gorm:"column:user_coupon_status;type:tinyint(1);default:0;comment:状态(0:未使用 1:已使用 3:已过期)" json:"user_coupon_status"`
|
||||
CouponUseDate time.Time `gorm:"column:coupon_use_date;type:datetime;comment:使用时间" json:"coupon_use_date"`
|
||||
ValidStartTime time.Time `gorm:"column:valid_start_time;type:datetime;comment:有效使用时间" json:"valid_start_time"`
|
||||
ValidEndTime time.Time `gorm:"column:valid_end_time;type:datetime;comment:过期使用时间" json:"valid_end_time"`
|
||||
UserCouponId int64 `gorm:"column:user_coupon_id;type:bigint(19);primary_key;comment:主键id" json:"user_coupon_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
PatientId int64 `gorm:"column:patient_id;type:bigint(19);comment:患者id" json:"patient_id"`
|
||||
CouponId int64 `gorm:"column:coupon_id;type:bigint(19);comment:优惠卷id;NOT NULL" json:"coupon_id"`
|
||||
UserCouponStatus int `gorm:"column:user_coupon_status;type:tinyint(1);default:0;comment:状态(0:未使用 1:已使用 3:已过期)" json:"user_coupon_status"`
|
||||
CouponUseDate *time.Time `gorm:"column:coupon_use_date;type:datetime;comment:使用时间" json:"coupon_use_date"`
|
||||
ValidStartTime time.Time `gorm:"column:valid_start_time;type:datetime;comment:有效使用时间" json:"valid_start_time"`
|
||||
ValidEndTime time.Time `gorm:"column:valid_end_time;type:datetime;comment:过期使用时间" json:"valid_end_time"`
|
||||
Model
|
||||
}
|
||||
|
||||
|
||||
41
api/model/userPatient.go
Normal file
41
api/model/userPatient.go
Normal file
@ -0,0 +1,41 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"hospital-open-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserPatient 用户-患者表
|
||||
type UserPatient struct {
|
||||
PatientId int64 `gorm:"column:patient_id;type:bigint(19);primary_key;comment:主键id" json:"patient_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
UserName string `gorm:"column:user_name;type:varchar(100);comment:用户名称" json:"user_name"`
|
||||
OpenId string `gorm:"column:open_id;type:varchar(100);comment:微信open_id" json:"open_id"`
|
||||
UnionId string `gorm:"column:union_id;type:varchar(100);comment:微信开放平台唯一标识" json:"union_id"`
|
||||
WxSessionKey string `gorm:"column:wx_session_key;type:varchar(255);comment:微信会话密钥" json:"wx_session_key"`
|
||||
Status int `gorm:"column:status;type:tinyint(1);default:1;comment:状态(0:禁用 1:正常 2:删除)" json:"status"`
|
||||
IdcardStatus int `gorm:"column:idcard_status;type:tinyint(1);default:0;comment:实名认证状态(0:未认证 1:认证通过 2:认证失败)" json:"idcard_status"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
|
||||
DisableReason string `gorm:"column:disable_reason;type:varchar(255);comment:禁用理由" json:"disable_reason"`
|
||||
User *User `gorm:"foreignKey:UserId;references:user_id" json:"user"` // 用户
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *UserPatient) TableName() string {
|
||||
return "gdxz_user_patient"
|
||||
}
|
||||
|
||||
func (m *UserPatient) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.PatientId == 0 {
|
||||
m.PatientId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
12
api/requests/v1/Coupon.go
Normal file
12
api/requests/v1/Coupon.go
Normal file
@ -0,0 +1,12 @@
|
||||
package v1
|
||||
|
||||
type CouponRequest struct {
|
||||
ReceiveCoupon // 领取优惠卷
|
||||
}
|
||||
|
||||
// ReceiveCoupon 领取优惠卷
|
||||
type ReceiveCoupon struct {
|
||||
Mobile string `json:"mobile" form:"mobile" validate:"required,Mobile" label:"手机号"`
|
||||
CouponId string `json:"coupon_id" form:"coupon_id" validate:"required" label:"优惠卷id"`
|
||||
Quantity int `json:"quantity" form:"quantity" validate:"required" label:"优惠卷数量"`
|
||||
}
|
||||
@ -107,8 +107,8 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 优惠卷
|
||||
couponGroup := v1Group.Group("/coupon")
|
||||
{
|
||||
// 领取某一优惠卷
|
||||
couponGroup.POST("/receive/:coupon_id", api.V1.Coupon.GetMultiDoctor)
|
||||
// 领取优惠卷
|
||||
couponGroup.POST("/receive", api.V1.Coupon.ReceiveCoupon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
api/service/MessagePush.go
Normal file
36
api/service/MessagePush.go
Normal file
@ -0,0 +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
|
||||
|
||||
}
|
||||
309
api/service/v1/Coupon.go
Normal file
309
api/service/v1/Coupon.go
Normal file
@ -0,0 +1,309 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hospital-open-api/api/dao"
|
||||
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"
|
||||
)
|
||||
|
||||
// CouponService 优惠卷
|
||||
type CouponService struct {
|
||||
}
|
||||
|
||||
// ReceiveCoupon 领取优惠卷
|
||||
func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.ReceiveCouponDto, err error) {
|
||||
// 获取用户数据
|
||||
userDao := dao.UserDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["mobile"] = req.Mobile
|
||||
maps["user_type"] = 1
|
||||
user, _ := userDao.GetUser(maps)
|
||||
if user == nil {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 2,
|
||||
Message: "未注册互联网医院患者端",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 获取患者数据
|
||||
userPatientDao := dao.UserPatientDao{}
|
||||
userPatient, _ := userPatientDao.GetUserPatientByUserId(user.UserId)
|
||||
if userPatient == nil {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 2,
|
||||
Message: "未注册互联网医院患者端",
|
||||
Data: nil,
|
||||
}
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
couponId, err := strconv.ParseInt(req.CouponId, 10, 64)
|
||||
if err != nil {
|
||||
return g, errors.New("内部错误")
|
||||
}
|
||||
|
||||
// 获取优惠卷数据
|
||||
couponDao := dao.CouponDao{}
|
||||
coupon, _ := couponDao.GetCouponById(couponId)
|
||||
if coupon == nil {
|
||||
return nil, errors.New("非法优惠卷")
|
||||
}
|
||||
|
||||
// 检测优惠卷状态
|
||||
if coupon.CouponStatus != 1 {
|
||||
return nil, errors.New("存在错误优惠卷")
|
||||
}
|
||||
|
||||
// 检测发放优惠卷数量
|
||||
if req.Quantity <= 0 {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量错误",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 检测优惠卷剩余数量
|
||||
remainingQuantity := coupon.CouponCount - coupon.CouponTakeCount
|
||||
if remainingQuantity <= 0 {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量不足,领取失败",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
if remainingQuantity < req.Quantity {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷数量不足,领取失败",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 获取当前时间
|
||||
now := time.Now()
|
||||
|
||||
// 检测优惠卷过期时间
|
||||
if coupon.ValidType == 1 {
|
||||
// 1:绝对时效
|
||||
validStartTime := time.Time(coupon.ValidStartTime)
|
||||
if now.Before(validStartTime) {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷还未启用",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
validEndTime := time.Time(coupon.ValidEndTime)
|
||||
if now.After(validEndTime) {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷已过期",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 即将过期检测
|
||||
now = now.Add(time.Minute * 10)
|
||||
// 1:绝对时效
|
||||
validStartTime = time.Time(coupon.ValidStartTime)
|
||||
if now.Before(validStartTime) {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷还未启用",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
validEndTime = time.Time(coupon.ValidEndTime)
|
||||
if now.After(validEndTime) {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "优惠卷即将过期",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 检测用户是否已领取该优惠卷
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
maps = make(map[string]interface{})
|
||||
maps["user_id"] = user.UserId
|
||||
maps["coupon_id"] = coupon.CouponId
|
||||
maps["user_coupon_status"] = 0
|
||||
userCoupons, _ := userCouponDao.GetUserCouponList(maps)
|
||||
if len(userCoupons) > 0 {
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 0,
|
||||
Message: "重复领取",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 增加优惠卷发放数量
|
||||
err = couponDao.Inc(tx, couponId, "coupon_take_count", req.Quantity)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
}
|
||||
|
||||
// 检测优惠卷是否全部发放完毕
|
||||
if (req.Quantity - remainingQuantity) == 0 {
|
||||
maps := make(map[string]interface{})
|
||||
maps["coupon_status"] = 3
|
||||
err := couponDao.EditCouponById(tx, coupon.CouponId, maps)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < req.Quantity; i++ {
|
||||
// 添加用户优惠卷表
|
||||
UserCouponModel := &model.UserCoupon{
|
||||
UserId: user.UserId,
|
||||
PatientId: userPatient.PatientId,
|
||||
CouponId: couponId,
|
||||
CouponUseDate: nil,
|
||||
}
|
||||
|
||||
// 有效类型(1:绝对时效,xxx-xxx时间段有效 2:相对时效 n天内有效)
|
||||
if coupon.ValidType == 1 {
|
||||
UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime)
|
||||
UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime)
|
||||
}
|
||||
|
||||
if coupon.ValidType == 2 {
|
||||
UserCouponModel.ValidStartTime = now
|
||||
UserCouponModel.ValidEndTime = now.AddDate(0, 0, coupon.ValidDays)
|
||||
}
|
||||
|
||||
userCoupon, _ := userCouponDao.AddUserCoupon(tx, UserCouponModel)
|
||||
if userCoupon == nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
}
|
||||
|
||||
// 同类型优惠卷只添加一次弹窗
|
||||
if i == 0 {
|
||||
// 添加弹窗表
|
||||
if coupon.IsPopup == 1 {
|
||||
popupModel := &model.Popup{
|
||||
UserId: user.UserId,
|
||||
AppType: 1,
|
||||
ClientType: 1,
|
||||
PopupType: 2,
|
||||
PopupTitle: "新人红包福利",
|
||||
PopupContent: "",
|
||||
}
|
||||
|
||||
popupContent := make(map[string]interface{})
|
||||
popupContent["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
|
||||
popupContent["coupon_price"] = coupon.CouponPrice
|
||||
popupContent["application_scope"] = coupon.ApplicationScope
|
||||
popupContent["inquiry_type"] = coupon.InquiryType
|
||||
popupContent["valid_type"] = coupon.ValidType
|
||||
popupContent["valid_days"] = coupon.ValidDays
|
||||
popupContent["valid_start_time"] = coupon.ValidStartTime
|
||||
popupContent["valid_end_time"] = coupon.ValidEndTime
|
||||
|
||||
popupContentData, err := json.Marshal(popupContent)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
}
|
||||
|
||||
popupModel.PopupContent = string(popupContentData)
|
||||
|
||||
popupDao := dao.PopupDao{}
|
||||
popup, _ := popupDao.AddPopup(tx, popupModel)
|
||||
if popup == nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("发放失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 增加优惠卷过期队列
|
||||
// 计算当天的结束时间
|
||||
year, month, day := now.Date()
|
||||
location := now.Location()
|
||||
endOfDay := time.Date(year, month, day, 23, 59, 59, 0, location)
|
||||
if userCoupon.ValidEndTime.Before(endOfDay) {
|
||||
// 需添加队列
|
||||
// 建立队列连接
|
||||
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return g, errors.New("内部错误")
|
||||
}
|
||||
|
||||
defer rabbitMQ.Close()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId)
|
||||
|
||||
delay := userCoupon.ValidEndTime.Sub(time.Now())
|
||||
|
||||
if delay < 10 {
|
||||
delay = 10 * time.Second
|
||||
}
|
||||
|
||||
err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
// 发送通知
|
||||
res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId)
|
||||
if !res {
|
||||
utils.LogJsonError("优惠卷通知发送失败")
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
g = &dtoV1.ReceiveCouponDto{
|
||||
Status: 1,
|
||||
Message: "成功",
|
||||
Data: nil,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
10
config.yaml
10
config.yaml
@ -72,4 +72,12 @@ wechat:
|
||||
doctor-app-secret: 817665d3763637fe66d56548f8484622
|
||||
mch-id: 1636644248
|
||||
v3-api-secret: gdxz292sjSOadN3m2pCda03NfCsmNadY
|
||||
mch-certificate-serial-number: 7DEC0E6C57E0DC71F077F02F52406566AF39BEBB
|
||||
mch-certificate-serial-number: 7DEC0E6C57E0DC71F077F02F52406566AF39BEBB
|
||||
|
||||
# [rabbitMq]
|
||||
amqp:
|
||||
host: 127.0.0.1
|
||||
port: 5672
|
||||
user: gdxz_2022rabbitmq
|
||||
password: qwr2p&¥e@3.2p
|
||||
vhost: gdxz_2022rabbitmq
|
||||
|
||||
9
config/amqp.go
Normal file
9
config/amqp.go
Normal file
@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
type Amqp struct {
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址
|
||||
Port int `mapstructure:"port" json:"port" yaml:"port"` // 服务器端口
|
||||
Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
|
||||
User string `mapstructure:"user" json:"user" yaml:"user"`
|
||||
Vhost string `mapstructure:"vhost" json:"vhost" yaml:"vhost"`
|
||||
}
|
||||
@ -15,4 +15,5 @@ type Config struct {
|
||||
Im Im `mapstructure:"im" json:"im" yaml:"im"`
|
||||
Dysms Dysms `mapstructure:"dysms" json:"dysms" yaml:"dysms"`
|
||||
Wechat Wechat `mapstructure:"wechat" json:"wechat" yaml:"wechat"`
|
||||
Amqp Amqp `mapstructure:"amqp" json:"amqp" yaml:"amqp"`
|
||||
}
|
||||
|
||||
155
extend/rabbitMq/rabbitMq.go
Normal file
155
extend/rabbitMq/rabbitMq.go
Normal file
@ -0,0 +1,155 @@
|
||||
package rabbitMq
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/streadway/amqp"
|
||||
"hospital-open-api/config"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RabbitMQ struct {
|
||||
conn *amqp.Connection
|
||||
channel *amqp.Channel
|
||||
}
|
||||
|
||||
// NewRabbitMQClient 初始化客户端
|
||||
func NewRabbitMQClient() (*RabbitMQ, error) {
|
||||
m := config.C.Amqp
|
||||
|
||||
user := url.QueryEscape(m.User)
|
||||
password := url.QueryEscape(m.Password)
|
||||
|
||||
dsn := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", user, password, m.Host, m.Port, m.Vhost)
|
||||
conn, err := amqp.Dial(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RabbitMQ{
|
||||
conn: conn,
|
||||
channel: ch,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Publish 生产
|
||||
func (r *RabbitMQ) Publish(queueName, exchangeName, routingKey string, message interface{}) error {
|
||||
_, err := r.channel.QueueDeclare(
|
||||
queueName, // 队列名字
|
||||
true, // 消息是否持久化
|
||||
false, // 不使用的时候删除队列
|
||||
false, // 是否排他
|
||||
false, // 是否阻塞
|
||||
nil, // arguments
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.channel.Publish(
|
||||
exchangeName, // exchange(交换机名字)
|
||||
routingKey, //
|
||||
false, // 是否为无法路由的消息进行返回处理
|
||||
false, // 是否对路由到无消费者队列的消息进行返回处理 RabbitMQ 3.0 废弃
|
||||
amqp.Publishing{
|
||||
ContentType: "text/plain",
|
||||
Body: body, // 消息内容
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) PublishWithDelay(queueName, exchangeName, routingKey string, message interface{}, delay time.Duration) error {
|
||||
err := r.channel.ExchangeDeclare(
|
||||
queueName, // name
|
||||
"x-delayed-message", // type
|
||||
true, // durable
|
||||
false, // auto-deleted
|
||||
false, // internal
|
||||
false, // 阻塞处理
|
||||
amqp.Table{"x-delayed-type": "direct"}, // arguments
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.channel.Publish(
|
||||
exchangeName, // exchange
|
||||
routingKey, // routing key
|
||||
false, // 是否为无法路由的消息进行返回处理
|
||||
false, // 是否对路由到无消费者队列的消息进行返回处理 RabbitMQ 3.0 废弃
|
||||
amqp.Publishing{
|
||||
ContentType: "text/plain",
|
||||
Body: body,
|
||||
Headers: amqp.Table{"x-delay": int32(delay / time.Millisecond)},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Consume 消费
|
||||
func (r *RabbitMQ) Consume(queueName string) (<-chan amqp.Delivery, error) {
|
||||
q, err := r.channel.QueueDeclare(
|
||||
queueName, // 队列名称
|
||||
false, // 是否持久化
|
||||
false, // 是否自动删除队列
|
||||
false, // 排他
|
||||
false, // 阻塞处理
|
||||
nil, // arguments
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
msgs, err := r.channel.Consume(
|
||||
q.Name, // queue
|
||||
"", // consumer
|
||||
true, // auto-ack
|
||||
false, // exclusive
|
||||
false, // no-local
|
||||
false, // no-wait
|
||||
nil, // args
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return msgs, nil
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) Close() {
|
||||
if r.channel != nil {
|
||||
err := r.channel.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if r.conn != nil {
|
||||
err := r.conn.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
1
go.mod
1
go.mod
@ -64,6 +64,7 @@ require (
|
||||
github.com/spf13/cast v1.5.1 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/streadway/amqp v1.1.0 // indirect
|
||||
github.com/subosito/gotenv v1.4.2 // indirect
|
||||
github.com/tjfoc/gmsm v1.3.2 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@ -277,6 +277,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.16.0 h1:rGGH0XDZhdUOryiDWjmIvUSWpbNqisK8Wk0Vyefw8hc=
|
||||
github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg=
|
||||
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
|
||||
github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
|
||||
@ -15,9 +15,22 @@ func LogJsonInfo(v interface{}) {
|
||||
}
|
||||
|
||||
jsonString := string(jsonData)
|
||||
fmt.Println(jsonString)
|
||||
|
||||
global.Logger.WithFields(logrus.Fields{
|
||||
"data": jsonString,
|
||||
}).Info("info")
|
||||
}
|
||||
|
||||
func LogJsonError(v interface{}) {
|
||||
jsonData, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling struct to JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonString := string(jsonData)
|
||||
|
||||
global.Logger.WithFields(logrus.Fields{
|
||||
"data": jsonString,
|
||||
}).Info("error")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user