hepa-calc-api/api/service/UserCoupon.go

209 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"errors"
"gorm.io/gorm"
"hepa-calc-api/api/dao"
"hepa-calc-api/api/dto"
"hepa-calc-api/api/model"
"time"
)
type UserCouponService struct {
}
// CheckUserCoupon 检测用户优惠卷
// orderType:类型1:单项 2:会员)
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
}
// GetUserUsableQuestionCoupon 获取用户可使用优惠卷-单项
func (r *UserCouponService) GetUserUsableQuestionCoupon(userId, questionId int64, amountTotal float64) (g []*dto.UserCouponDto, err error) {
// 获取用户数据
userDao := dao.UserDao{}
user, err := userDao.GetUserById(userId)
if err != nil || user == nil {
return nil, errors.New("用户错误")
}
// 检测用户会员
userService := &UserService{}
isMember := userService.CheckUserMember(user)
if isMember == true {
// 会员无需使用优惠卷
return nil, nil
}
// 获取用户优惠卷
UserCouponDao := dao.UserCouponDao{}
maps := make(map[string]interface{})
maps["user_id"] = userId
maps["user_coupon_status"] = 0
userCoupons, err := UserCouponDao.GetUserCouponPreloadList(maps)
if err != nil {
return nil, errors.New("优惠券异常")
}
//定义返回数据
var responses []*model.UserCoupon
for _, userCoupon := range userCoupons {
isCanUse, err := r.CheckUserCoupon(userCoupon, questionId, 1, amountTotal)
if err != nil || isCanUse == false {
continue
}
responses = append(responses, userCoupon)
}
g = dto.GetUserCouponListDto(responses)
return g, nil
}
// GetUserUsableMemberCoupon 获取用户可使用优惠卷-会员
func (r *UserCouponService) GetUserUsableMemberCoupon(userId, systemMemberId int64, amountTotal float64) (g []*dto.UserCouponDto, err error) {
// 获取用户数据
userDao := dao.UserDao{}
user, err := userDao.GetUserById(userId)
if err != nil || user == nil {
return nil, errors.New("用户错误")
}
// 检测用户会员
userService := &UserService{}
isMember := userService.CheckUserMember(user)
if isMember == true {
// 会员无需使用优惠卷
return nil, nil
}
// 获取用户优惠卷
UserCouponDao := dao.UserCouponDao{}
maps := make(map[string]interface{})
maps["user_id"] = userId
maps["user_coupon_status"] = 0
userCoupons, err := UserCouponDao.GetUserCouponPreloadList(maps)
if err != nil {
return nil, errors.New("优惠券异常")
}
//定义返回数据
var responses []*model.UserCoupon
for _, userCoupon := range userCoupons {
isCanUse, err := r.CheckUserCoupon(userCoupon, systemMemberId, 2, amountTotal)
if err != nil || isCanUse == false {
continue
}
responses = append(responses, userCoupon)
}
g = dto.GetUserCouponListDto(responses)
return g, nil
}