From 75ed34e7914f45ef5f2e573c7d3e00467bcf421b Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Fri, 31 May 2024 10:28:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=8D=B7=E7=9A=84=E5=85=A5=E5=8F=82=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/requests/v1/Coupon.go | 7 +- api/service/v1/Coupon.go | 375 +++++++++++++++++++------------------- 2 files changed, 184 insertions(+), 198 deletions(-) diff --git a/api/requests/v1/Coupon.go b/api/requests/v1/Coupon.go index 71fdb92..a3acce9 100644 --- a/api/requests/v1/Coupon.go +++ b/api/requests/v1/Coupon.go @@ -6,12 +6,7 @@ type CouponRequest struct { // 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 { + Mobile string `json:"mobile" form:"mobile" validate:"required,Mobile" label:"手机号"` 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/service/v1/Coupon.go b/api/service/v1/Coupon.go index 51a0b13..2016fcc 100644 --- a/api/service/v1/Coupon.go +++ b/api/service/v1/Coupon.go @@ -50,6 +50,97 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re return g, err } + // 将 id 转换为 int64 类型 + couponId, err := strconv.ParseInt(req.CouponId, 10, 64) + if err != nil { + return g, errors.New("内部错误") + } + + // 获取优惠卷数据 + couponDao := dao.CouponDao{} + coupon, _ := couponDao.GetCouponById(couponId) + if coupon == nil { + return nil, errors.New("非法优惠卷") + } + + // 检测优惠卷状态 + if coupon.CouponStatus != 1 { + return nil, errors.New("存在错误优惠卷") + } + + // 检测发放优惠卷数量 + if req.Quantity <= 0 { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量错误", + Data: nil, + } + return g, nil + } + + // 检测优惠卷剩余数量 + remainingQuantity := coupon.CouponCount - coupon.CouponTakeCount + if remainingQuantity <= 0 { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量不足,领取失败", + Data: nil, + } + return g, nil + } + + if remainingQuantity < req.Quantity { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷数量不足,领取失败", + Data: nil, + } + return g, nil + } + + // 获取当前时间 + now := time.Now() + + // 检测优惠卷过期时间 + if coupon.ValidType == 1 { + // 1:绝对时效 + validStartTime := time.Time(coupon.ValidStartTime) + if now.Before(validStartTime) { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷还未启用", + Data: nil, + } + return g, nil + } + + validEndTime := time.Time(coupon.ValidEndTime) + if now.After(validEndTime) { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "优惠卷已过期", + Data: nil, + } + return g, nil + } + } + + // 检测用户是否已领取该优惠卷 + userCouponDao := dao.UserCouponDao{} + + maps = make(map[string]interface{}) + maps["user_id"] = user.UserId + maps["coupon_id"] = coupon.CouponId + userCoupon, _ := userCouponDao.GetUserCoupon(maps) + if userCoupon != nil { + g = &dtoV1.ReceiveCouponDto{ + Status: 0, + Message: "重复领取", + Data: nil, + } + return g, nil + } + // 开始事务 tx := global.Db.Begin() defer func() { @@ -58,213 +149,113 @@ func (r *CouponService) ReceiveCoupon(req requestsV1.ReceiveCoupon) (g *dtoV1.Re } }() - // 建立队列连接 - rabbitMQ, err := rabbitMq.NewRabbitMQClient() - if err != nil { - tx.Rollback() - return g, errors.New("内部错误") + // 添加用户优惠卷表 + UserCouponModel := &model.UserCoupon{ + UserId: user.UserId, + PatientId: userPatient.PatientId, + CouponId: couponId, + CouponUseDate: nil, } - defer rabbitMQ.Close() + // 有效类型(1:绝对时效,xxx-xxx时间段有效 2:相对时效 n天内有效) + if coupon.ValidType == 1 { + UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime) + UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime) + } - // 获取优惠卷数据 - 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 coupon.ValidType == 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", req.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) + + popupDao := dao.PopupDao{} + 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.Before(endOfDay) { + // 需添加队列 + // 建立队列连接 + rabbitMQ, err := rabbitMq.NewRabbitMQClient() if err != nil { tx.Rollback() return g, errors.New("内部错误") } - coupon, _ := couponDao.GetCouponById(couponId) - if coupon == nil { - tx.Rollback() - return nil, errors.New("非法优惠卷") + defer rabbitMQ.Close() + + data := make(map[string]interface{}) + data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId) + + delay := userCoupon.ValidEndTime.Sub(time.Now()) + + if delay < 10 { + delay = 10 * time.Second } - // 检测优惠卷状态 - 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.ValidType == 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.ValidType == 1 { - UserCouponModel.ValidStartTime = time.Time(coupon.ValidStartTime) - UserCouponModel.ValidEndTime = time.Time(coupon.ValidEndTime) - } - - if coupon.ValidType == 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) + err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay) if err != nil { tx.Rollback() - return nil, errors.New("发放失败") + return nil, err } - // 添加弹窗表 - if coupon.IsPopup == 1 { - popupModel := &model.Popup{ - UserId: user.UserId, - AppType: 1, - ClientType: 1, - PopupType: 2, - PopupTitle: "新人红包福利", - PopupContent: "", + go func() { + // 发送通知 + res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId) + if !res { + utils.LogJsonError("优惠卷通知发送失败") } - - 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.Before(endOfDay) { - // 需添加队列 - data := make(map[string]interface{}) - data["user_coupon_id"] = fmt.Sprintf("%d", userCoupon.UserCouponId) - - delay := userCoupon.ValidEndTime.Sub(time.Now()) - - if delay < 10 { - delay = 10 * time.Second - } - - err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay) - if err != nil { - tx.Rollback() - return nil, err - } - - go func() { - // 发送通知 - res, _ := service.PatientDistributeCoupon(coupon.CouponName, user.UserId) - if !res { - utils.LogJsonError("优惠卷通知发送失败") - } - }() - } + }() } tx.Commit()