diff --git a/api/dto/Coupon.go b/api/dto/Coupon.go index 02e1788..bdf9eb3 100644 --- a/api/dto/Coupon.go +++ b/api/dto/Coupon.go @@ -3,6 +3,7 @@ package dto import ( "fmt" "hepa-calc-api/api/model" + "strings" ) type CouponDto struct { @@ -21,6 +22,7 @@ type CouponDto struct { ValidDays int `json:"valid_days"` // 自领取之日起有效天数 ValidStartTime model.LocalTime `json:"valid_start_time"` // 开始使用时间 ValidEndTime model.LocalTime `json:"valid_end_time"` // 结束使用时间 + SystemMemberIds []*string `json:"system_member_ids"` // 会员id(适用范围为会员时生效,如果此项为null,则表示所有会员通用) CouponDesc string `json:"coupon_desc"` // 优惠券描述 CreatedAt model.LocalTime `json:"created_at"` // 创建时间 UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 @@ -49,3 +51,21 @@ func GetCouponDto(m *model.Coupon) *CouponDto { UpdatedAt: m.UpdatedAt, } } + +// LoadSystemMemberIds 加载会员id +func (r *CouponDto) LoadSystemMemberIds(systemMemberIds string) *CouponDto { + if systemMemberIds != "" { + s := strings.Split(systemMemberIds, ",") + + response := make([]*string, len(s)) + + for i, v := range s { + h := fmt.Sprintf("%v", v) + response[i] = &h + } + + r.SystemMemberIds = response + } + + return r +} diff --git a/api/model/Coupon.go b/api/model/Coupon.go index 7a7075a..2ceee05 100644 --- a/api/model/Coupon.go +++ b/api/model/Coupon.go @@ -22,8 +22,7 @@ type Coupon struct { 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"` - QuestionId int64 `gorm:"column:question_id;type:bigint(19);comment:问题id(适用范围为单项时生效,如果此项为null,则表示所有单项通用)" json:"question_id"` - SystemMemberId int64 `gorm:"column:system_member_id;type:bigint(19);comment:会员id(适用范围为会员时生效,如果此项为null,则表示所有会员通用)" json:"system_member_id"` + 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 } diff --git a/api/service/UserCoupon.go b/api/service/UserCoupon.go index 0a06344..76dd7ba 100644 --- a/api/service/UserCoupon.go +++ b/api/service/UserCoupon.go @@ -2,10 +2,12 @@ package service import ( "errors" + "fmt" "gorm.io/gorm" "hepa-calc-api/api/dao" "hepa-calc-api/api/dto" "hepa-calc-api/api/model" + "strings" "time" ) @@ -58,10 +60,6 @@ func (r *UserCouponService) CheckUserCoupon(m *model.UserCoupon, id int64, order if m.Coupon.ApplicationScope != 1 && m.Coupon.ApplicationScope != 2 { return false, errors.New("优惠卷无法使用") } - - if id != m.Coupon.QuestionId { - return false, errors.New("优惠卷无法使用") - } } // 会员 @@ -70,7 +68,9 @@ func (r *UserCouponService) CheckUserCoupon(m *model.UserCoupon, id int64, order return false, errors.New("优惠卷无法使用") } - if id != m.Coupon.SystemMemberId { + systemMemberIds := fmt.Sprintf("%d", id) + res := strings.Contains(m.Coupon.SystemMemberIds, systemMemberIds) + if res == false { return false, errors.New("优惠卷无法使用") } }