完善用户资料发放优惠卷

This commit is contained in:
wucongxing8150 2024-09-02 13:59:13 +08:00
parent 94d25b4ec4
commit 7285da9a79
4 changed files with 74 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import (
"hepa-calc-api/api/model"
"hepa-calc-api/api/requests"
"hepa-calc-api/api/responses"
"hepa-calc-api/api/service"
"hepa-calc-api/extend/app"
"hepa-calc-api/global"
"hepa-calc-api/utils"
@ -218,6 +219,15 @@ func (r *UserCase) PutUserCase(c *gin.Context) {
}
}
// 发放用户完善资料优惠卷
couponService := service.CouponService{}
res := couponService.GrantFillUserInfoCoupon(tx, userId)
if res != true {
tx.Rollback()
responses.FailWithMessage("操作失败", c)
return
}
// app数据
appData.PatientUuid = user.AppIden
_, err = app.UpdateUserCase(appData)

View File

@ -133,3 +133,15 @@ func (r *CouponDao) Dec(tx *gorm.DB, couponId int64, field string, numeral int)
}
return nil
}
// GetFillUserInfoCouponList 发放用户完善资料优惠卷列表
func (r *CouponDao) GetFillUserInfoCouponList() (m []*model.Coupon, err error) {
err = global.Db.
Where("coupon_status = ?", 1).
Where("distribution_object in ?", []int{1, 4}).
Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}

View File

@ -7,23 +7,24 @@ import (
)
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"`
CouponType int `gorm:"column:coupon_type;type:varchar(255);comment:优惠卷类型1:无门槛 2:满减)" json:"coupon_type"`
CouponStatus int `gorm:"column:coupon_status;type:tinyint(1);default:1;comment:状态1:正常 2:强制失效 3:结束 4:删除)" json:"coupon_status"`
ApplicationScope int `gorm:"column:application_scope;type:tinyint(1);default:1;comment:适用范围1:全场通用 2:单项 3:会员)" json:"application_scope"`
IsMutex int `gorm:"column:is_mutex;type:tinyint(1);default:1;comment:是否互斥0:否 1:是)互斥情况下无法和其他优惠卷同时使用" json:"is_mutex"`
CouponCount int `gorm:"column:coupon_count;type:int(10);default:1;comment:发放数量;NOT NULL" json:"coupon_count"`
CouponTakeCount int `gorm:"column:coupon_take_count;type:int(10);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) unsigned;default:0.00;comment:优惠卷金额" json:"coupon_price"`
WithAmount float64 `gorm:"column:with_amount;type:decimal(10,2);comment:符合满减标准金额(优惠卷类型为满减时使用)" json:"with_amount"`
ValidType int `gorm:"column:valid_type;type:tinyint(1);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"`
SystemMemberIds string `gorm:"column:system_member_ids;type:bigint(19);comment:会员id适用范围为会员时生效如果此项为null则表示所有会员通用" json:"system_member_ids"`
CouponDesc string `gorm:"column:coupon_desc;type:varchar(200);comment:优惠卷描述" json:"coupon_desc"`
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"`
CouponType int `gorm:"column:coupon_type;type:varchar(255);comment:优惠卷类型1:无门槛 2:满减)" json:"coupon_type"`
CouponStatus int `gorm:"column:coupon_status;type:tinyint(1);default:1;comment:状态1:正常 2:强制失效 3:结束 4:删除)" json:"coupon_status"`
ApplicationScope int `gorm:"column:application_scope;type:tinyint(1);default:1;comment:适用范围1:全场通用 2:单项 3:会员)" json:"application_scope"`
DistributionObject int `gorm:"column:distribution_object;type:tinyint(1);default:1;comment:发放对象1:全部用户 2:新注册用户 3:会员 4:完善资料)" json:"distribution_object"`
IsMutex int `gorm:"column:is_mutex;type:tinyint(1);default:1;comment:是否互斥0:否 1:是)互斥情况下无法和其他优惠卷同时使用" json:"is_mutex"`
CouponCount int `gorm:"column:coupon_count;type:int(10);default:1;comment:发放数量;NOT NULL" json:"coupon_count"`
CouponTakeCount int `gorm:"column:coupon_take_count;type:int(10);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) unsigned;default:0.00;comment:优惠卷金额" json:"coupon_price"`
WithAmount float64 `gorm:"column:with_amount;type:decimal(10,2);comment:符合满减标准金额(优惠卷类型为满减时使用)" json:"with_amount"`
ValidType int `gorm:"column:valid_type;type:tinyint(1);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"`
SystemMemberIds string `gorm:"column:system_member_ids;type:bigint(19);comment:会员id适用范围为会员时生效如果此项为null则表示所有会员通用" json:"system_member_ids"`
CouponDesc string `gorm:"column:coupon_desc;type:varchar(200);comment:优惠卷描述" json:"coupon_desc"`
Model
}

View File

@ -6,6 +6,7 @@ import (
"hepa-calc-api/api/dao"
"hepa-calc-api/api/model"
"hepa-calc-api/extend/rabbitMq"
"hepa-calc-api/utils"
"time"
)
@ -13,6 +14,7 @@ type CouponService struct {
}
// GrantUserCoupon 发放优惠卷
// grantQuantity 发放数量
func (r *CouponService) GrantUserCoupon(tx *gorm.DB, userId, couponId int64, grantQuantity int) bool {
// 获取优惠卷数据
couponDao := dao.CouponDao{}
@ -31,7 +33,7 @@ func (r *CouponService) GrantUserCoupon(tx *gorm.DB, userId, couponId int64, gra
now := time.Now().Add(10 * 60 * time.Second)
validEndTime := time.Time(*coupon.ValidEndTime)
if validEndTime.Before(now) {
return false
return true
}
}
@ -46,8 +48,18 @@ func (r *CouponService) GrantUserCoupon(tx *gorm.DB, userId, couponId int64, gra
return false
}
// 检测用户是否已有该优惠卷
userCouponDao := dao.UserCouponDao{}
maps := make(map[string]interface{})
maps["user_id"] = userId
maps["coupon_id"] = coupon.CouponId
userCoupon, _ := userCouponDao.GetUserCoupon(maps)
if userCoupon != nil {
return true
}
// 添加用户优惠卷表
userCoupon := &model.UserCoupon{
userCoupon = &model.UserCoupon{
UserId: userId,
CouponId: couponId,
UserCouponStatus: 0,
@ -67,7 +79,6 @@ func (r *CouponService) GrantUserCoupon(tx *gorm.DB, userId, couponId int64, gra
userCoupon.ValidEndTime = &validEndTime
}
userCouponDao := dao.UserCouponDao{}
userCoupon, err = userCouponDao.AddUserCoupon(tx, userCoupon)
if err != nil {
return false
@ -114,3 +125,23 @@ func (r *CouponService) GrantUserCoupon(tx *gorm.DB, userId, couponId int64, gra
return true
}
// GrantFillUserInfoCoupon 发放用户完善资料优惠卷
func (r *CouponService) GrantFillUserInfoCoupon(tx *gorm.DB, userId int64) bool {
// 发放用户完善资料优惠卷
couponDao := dao.CouponDao{}
coupons, _ := couponDao.GetFillUserInfoCouponList()
if len(coupons) == 0 {
return true
}
// 发放优惠卷
for _, coupon := range coupons {
res := r.GrantUserCoupon(tx, userId, coupon.CouponId, 1)
if res == false {
utils.LogJsonErr("发放用户完善资料优惠卷失败:", nil)
}
}
return true
}