117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"hepa-calc-api/api/dao"
|
|
"hepa-calc-api/api/model"
|
|
"time"
|
|
)
|
|
|
|
type UserCouponService struct {
|
|
}
|
|
|
|
// CheckUserCoupon 检测用户优惠卷
|
|
func (r *UserCouponService) CheckUserCoupon(m *model.UserCoupon, id int64, orderType int, amountTotal float64) (bool, error) {
|
|
if m.UserCouponStatus == 1 {
|
|
return false, errors.New("优惠卷异常")
|
|
}
|
|
|
|
if m.UserCouponStatus == 2 {
|
|
return false, errors.New("优惠卷已过期,无法使用")
|
|
}
|
|
|
|
now := time.Now()
|
|
validEndTime := time.Time(m.ValidEndTime)
|
|
if validEndTime.Before(now) {
|
|
return false, errors.New("优惠卷已过期,无法使用")
|
|
}
|
|
|
|
if m.Coupon == nil {
|
|
return false, errors.New("优惠卷异常")
|
|
}
|
|
|
|
// 检测优惠卷状态
|
|
if m.Coupon.CouponStatus == 2 {
|
|
return false, errors.New("优惠卷已失效,无法使用")
|
|
}
|
|
|
|
if m.Coupon.CouponStatus == 3 {
|
|
return false, errors.New("优惠卷无法使用")
|
|
}
|
|
|
|
if m.Coupon.CouponStatus == 4 {
|
|
return false, errors.New("优惠卷异常,无法使用")
|
|
}
|
|
|
|
// 检测价格
|
|
if m.Coupon.CouponType == 2 {
|
|
if m.Coupon.WithAmount > amountTotal {
|
|
return false, errors.New("优惠卷不符合满减金额标准,无法使用")
|
|
}
|
|
}
|
|
|
|
// 单项
|
|
if orderType == 1 {
|
|
if m.Coupon.ApplicationScope != 1 && m.Coupon.ApplicationScope != 2 {
|
|
return false, errors.New("优惠卷无法使用")
|
|
}
|
|
|
|
if id != m.Coupon.QuestionId {
|
|
return false, errors.New("优惠卷无法使用")
|
|
}
|
|
}
|
|
|
|
// 会员
|
|
if orderType == 2 {
|
|
if m.Coupon.ApplicationScope != 1 && m.Coupon.ApplicationScope != 3 {
|
|
return false, errors.New("优惠卷无法使用")
|
|
}
|
|
|
|
if id != m.Coupon.SystemMemberId {
|
|
return false, errors.New("优惠卷无法使用")
|
|
}
|
|
}
|
|
|
|
// 检测优惠劵过期时间
|
|
if m.Coupon.ValidType == 1 {
|
|
validEndTime = time.Time(m.Coupon.ValidEndTime)
|
|
if validEndTime.Before(now) {
|
|
return false, errors.New("优惠卷已过期,无法使用")
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// ReturnUserCoupon 退还优惠卷
|
|
func (r *UserCouponService) ReturnUserCoupon(tx *gorm.DB, userCouponId int64) bool {
|
|
// 获取优惠卷数据
|
|
UserCouponDao := dao.UserCouponDao{}
|
|
userCoupon, err := UserCouponDao.GetUserCouponPreloadById(userCouponId)
|
|
if err != nil {
|
|
// 无该优惠卷数据,无需处理
|
|
return true
|
|
}
|
|
|
|
userCouponDao := dao.UserCouponDao{}
|
|
userCouponData := make(map[string]interface{})
|
|
|
|
// 检测优惠卷过期时间。判断是否需要退还
|
|
now := time.Now()
|
|
validEndTime := time.Time(userCoupon.ValidEndTime)
|
|
if validEndTime.Before(now) {
|
|
userCouponData["user_coupon_status"] = 3
|
|
} else {
|
|
userCouponData["user_coupon_status"] = 0
|
|
}
|
|
|
|
userCouponData["coupon_use_date"] = nil
|
|
err = userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, userCouponData)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|