diff --git a/api/controller/v1/coupon.go b/api/controller/v1/coupon.go index 4c77480..9ff78da 100644 --- a/api/controller/v1/coupon.go +++ b/api/controller/v1/coupon.go @@ -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) } diff --git a/api/dao/coupon.go b/api/dao/coupon.go new file mode 100644 index 0000000..9c6d4fe --- /dev/null +++ b/api/dao/coupon.go @@ -0,0 +1,82 @@ +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 +} diff --git a/api/dao/popup.go b/api/dao/popup.go new file mode 100644 index 0000000..8a72ca5 --- /dev/null +++ b/api/dao/popup.go @@ -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 +} diff --git a/api/dao/user.go b/api/dao/user.go index 1caa419..84883c2 100644 --- a/api/dao/user.go +++ b/api/dao/user.go @@ -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 +} diff --git a/api/dao/userCoupon.go b/api/dao/userCoupon.go index 602edbe..3e700bc 100644 --- a/api/dao/userCoupon.go +++ b/api/dao/userCoupon.go @@ -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 +} diff --git a/api/dao/userPatient.go b/api/dao/userPatient.go new file mode 100644 index 0000000..4eeb4a9 --- /dev/null +++ b/api/dao/userPatient.go @@ -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 +} diff --git a/api/dto/v1/coupon.go b/api/dto/v1/coupon.go new file mode 100644 index 0000000..d918b00 --- /dev/null +++ b/api/dto/v1/coupon.go @@ -0,0 +1,7 @@ +package v1 + +type ReceiveCouponDto struct { + Status int `json:"status"` + Message string `json:"message"` + Data interface{} `json:"data"` +} diff --git a/api/model/coupon.go b/api/model/coupon.go new file mode 100644 index 0000000..bad7f90 --- /dev/null +++ b/api/model/coupon.go @@ -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 +} diff --git a/api/model/popup.go b/api/model/popup.go new file mode 100644 index 0000000..a3ad91f --- /dev/null +++ b/api/model/popup.go @@ -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 +} diff --git a/api/model/userCoupon.go b/api/model/userCoupon.go index cfcb024..e41a6d1 100644 --- a/api/model/userCoupon.go +++ b/api/model/userCoupon.go @@ -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 } diff --git a/api/model/userPatient.go b/api/model/userPatient.go new file mode 100644 index 0000000..b625845 --- /dev/null +++ b/api/model/userPatient.go @@ -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 +} diff --git a/api/requests/v1/Coupon.go b/api/requests/v1/Coupon.go new file mode 100644 index 0000000..71fdb92 --- /dev/null +++ b/api/requests/v1/Coupon.go @@ -0,0 +1,17 @@ +package v1 + +type CouponRequest struct { + ReceiveCoupon // 领取优惠卷 +} + +// ReceiveCoupon 领取优惠卷 +type ReceiveCoupon struct { + Mobile string `json:"mobile" form:"mobile" validate:"required,Mobile" label:"手机号"` + ReceiveCouponCoupon []*ReceiveCouponCoupon `json:"coupon" form:"coupon" validate:"required" label:"优惠卷"` // 领取优惠卷,优惠卷部分入参数据 +} + +// ReceiveCouponCoupon 领取优惠卷,优惠卷部分入参数据 +type ReceiveCouponCoupon struct { + CouponId string `json:"coupon_id" form:"coupon_id" validate:"required" label:"优惠卷id"` + Quantity int `json:"quantity" form:"quantity" validate:"required" label:"优惠卷数量"` +} diff --git a/api/router/router.go b/api/router/router.go index 082d822..f509917 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -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) } } } diff --git a/api/service/v1/Coupon.go b/api/service/v1/Coupon.go new file mode 100644 index 0000000..eaf5141 --- /dev/null +++ b/api/service/v1/Coupon.go @@ -0,0 +1,244 @@ +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/global" + "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 + } + + // 开始事务 + tx := global.Db.Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + // 获取优惠卷数据 + couponDao := dao.CouponDao{} + userCouponDao := dao.UserCouponDao{} + popupDao := dao.PopupDao{} + for _, v := range req.ReceiveCouponCoupon { + // 将 id 转换为 int64 类型 + couponId, err := strconv.ParseInt(v.CouponId, 10, 64) + if err != nil { + tx.Rollback() + return g, errors.New("内部错误") + } + + coupon, _ := couponDao.GetCouponById(couponId) + if coupon == nil { + tx.Rollback() + return nil, errors.New("非法优惠卷") + } + + // 检测优惠卷状态 + if coupon.CouponStatus != 1 { + tx.Rollback() + return nil, errors.New("存在错误优惠卷") + } + + // 检测发放优惠卷数量 + if v.Quantity <= 0 { + tx.Rollback() + + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量错误", + Data: nil, + } + return g, nil + } + + // 检测优惠卷剩余数量 + remainingQuantity := coupon.CouponCount - coupon.CouponTakeCount + if remainingQuantity <= 0 { + tx.Rollback() + + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量不足,领取失败", + Data: nil, + } + return g, nil + } + + if remainingQuantity < v.Quantity { + tx.Rollback() + + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量不足,领取失败", + Data: nil, + } + return g, nil + } + + // 获取当前时间 + now := time.Now() + + // 检测优惠卷过期时间 + if coupon.CouponType == 1 { + // 1:绝对时效 + validStartTime := time.Time(coupon.ValidStartTime) + if now.Before(validStartTime) { + tx.Rollback() + + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷还未启用", + Data: nil, + } + return g, nil + } + + validEndTime := time.Time(coupon.ValidEndTime) + if now.After(validEndTime) { + tx.Rollback() + + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷已过期", + Data: nil, + } + return g, nil + } + } + + // 检测用户是否已领取该优惠卷 + maps := make(map[string]interface{}) + maps["user_id"] = user.UserId + maps["coupon_id"] = coupon.CouponId + userCoupon, _ := userCouponDao.GetUserCoupon(maps) + if userCoupon != nil { + continue + } + + // 添加用户优惠卷表 + UserCouponModel := &model.UserCoupon{ + UserId: user.UserId, + PatientId: userPatient.PatientId, + CouponId: couponId, + CouponUseDate: nil, + } + + // 有效类型(1:绝对时效,xxx-xxx时间段有效 2:相对时效 n天内有效) + if coupon.CouponType == 1 { + UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime) + UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime) + } + + if coupon.CouponType == 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("发放失败") + } + + // 增加优惠卷发放数量 + err = couponDao.Inc(tx, couponId, "coupon_take_count", v.Quantity) + if err != nil { + tx.Rollback() + return nil, errors.New("发放失败") + } + + // 添加弹窗表 + 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) + + 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.After(endOfDay) { + // 需添加队列 + } + } + + tx.Commit() + + g = &dtoV1.ReceiveCouponDto{ + Status: 1, + Message: "成功", + Data: nil, + } + return g, nil +} diff --git a/config.yaml b/config.yaml index ecf9c8b..465c6ed 100644 --- a/config.yaml +++ b/config.yaml @@ -72,4 +72,12 @@ wechat: doctor-app-secret: 817665d3763637fe66d56548f8484622 mch-id: 1636644248 v3-api-secret: gdxz292sjSOadN3m2pCda03NfCsmNadY - mch-certificate-serial-number: 7DEC0E6C57E0DC71F077F02F52406566AF39BEBB \ No newline at end of file + 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 diff --git a/config/amqp.go b/config/amqp.go new file mode 100644 index 0000000..f7d1c63 --- /dev/null +++ b/config/amqp.go @@ -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"` +} diff --git a/config/config.go b/config/config.go index 23e0b19..7ae910b 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` } diff --git a/core/amqp.go b/core/amqp.go new file mode 100644 index 0000000..156318e --- /dev/null +++ b/core/amqp.go @@ -0,0 +1,42 @@ +package core + +import ( + "fmt" + "github.com/streadway/amqp" + "hospital-open-api/config" + "hospital-open-api/global" + "net/url" +) + +func Amqp() { + 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 { + panic("rabbitMq初始化失败! " + err.Error()) + } + + defer func(conn *amqp.Connection) { + _ = conn.Close() + }(conn) + + ch, err := conn.Channel() + if err != nil { + panic("rabbitMq初始化失败! " + err.Error()) + } + + defer func(ch *amqp.Channel) { + _ = ch.Close() + }(ch) + + global.AmqpConn = conn + global.AmqpChannel = ch + + fmt.Println("初始化rabbitMq成功......") +} diff --git a/global/global.go b/global/global.go index 662c098..0b6f721 100644 --- a/global/global.go +++ b/global/global.go @@ -6,15 +6,18 @@ import ( "github.com/go-playground/validator/v10" "github.com/go-redis/redis/v8" "github.com/sirupsen/logrus" + "github.com/streadway/amqp" "gorm.io/gorm" ) // 全局变量 var ( - Db *gorm.DB // 数据库 - Logger *logrus.Logger // 日志 - Redis *redis.Client // redis - Validate *validator.Validate // 验证器 - Trans ut.Translator // Validate/v10 全局验证器 - Snowflake *snowflake.Node // 雪花算法 + Db *gorm.DB // 数据库 + Logger *logrus.Logger // 日志 + Redis *redis.Client // redis + Validate *validator.Validate // 验证器 + Trans ut.Translator // Validate/v10 全局验证器 + Snowflake *snowflake.Node // 雪花算法 + AmqpConn *amqp.Connection // rabbitMq队列 + AmqpChannel *amqp.Channel // rabbitMq队列 ) diff --git a/go.mod b/go.mod index 7efaa86..327d7d5 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index f82cdae..44cbe0d 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index e4f0154..0879514 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,9 @@ func main() { // 加载雪花算法 core.Snowflake() + // 加载队列 + core.Amqp() + // 初始化路由-加载中间件 r := router.Init()