新增了:发放系统优惠卷
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/dto"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/extend/rabbitMq"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"strconv"
|
||||
@@ -475,3 +477,235 @@ func (r *CouponService) PutSystemCouponStatus(couponId int64, req requests.PutSy
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GrantSystemCoupon 发放系统优惠卷
|
||||
func (r *CouponService) GrantSystemCoupon(couponId, adminUserId int64, req requests.GrantSystemCoupon) (bool, error) {
|
||||
if req.Type == 2 {
|
||||
return false, errors.New("暂时只支持具体用户发放")
|
||||
}
|
||||
|
||||
couponDao := dao.CouponDao{}
|
||||
coupon, err := couponDao.GetCouponById(couponId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if coupon.CouponStatus != 1 {
|
||||
return false, errors.New("优惠卷非正常状态,不支持发放")
|
||||
}
|
||||
|
||||
// 检测订单过期时间
|
||||
now := time.Now()
|
||||
validEndTime := time.Time(*coupon.ValidEndTime)
|
||||
if now.After(validEndTime) {
|
||||
return false, errors.New("优惠卷已过期,不支持发放")
|
||||
}
|
||||
|
||||
// 检测优惠卷剩余数量
|
||||
remainingQuantity := coupon.CouponCount - coupon.CouponTakeCount
|
||||
if remainingQuantity <= 0 {
|
||||
return false, errors.New("优惠卷数量不足,无法发放")
|
||||
}
|
||||
|
||||
// 检测优惠卷可发放数量
|
||||
if req.TotalQuantity >= remainingQuantity {
|
||||
return false, errors.New("优惠卷数量不足,无法发放")
|
||||
}
|
||||
|
||||
// 检测优惠卷过期时间
|
||||
now = now.Add(time.Minute * 10)
|
||||
if coupon.ValidType == 1 {
|
||||
// 1:绝对时效
|
||||
validStartTime := time.Time(*coupon.ValidStartTime)
|
||||
if now.Before(validStartTime) {
|
||||
return false, errors.New("优惠卷还未启用")
|
||||
}
|
||||
|
||||
validEndTime := time.Time(*coupon.ValidEndTime)
|
||||
if now.After(validEndTime) {
|
||||
return false, errors.New("优惠卷即将过期,不支持发放")
|
||||
}
|
||||
}
|
||||
|
||||
// 获取优惠卷发放记录表
|
||||
couponGrantDao := dao.CouponGrantDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["coupon_id"] = coupon.CouponId
|
||||
maps["grant_result"] = 2
|
||||
couponGrants, _ := couponGrantDao.GetCouponGrantList(maps)
|
||||
if len(couponGrants) > 0 {
|
||||
return false, errors.New("请等待上次发放结束后再执行此操作")
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
userId, err := strconv.ParseInt(req.UserId, 10, 64)
|
||||
if err != nil {
|
||||
return false, errors.New("发放失败")
|
||||
}
|
||||
|
||||
userDao := dao.UserDao{}
|
||||
user, _ := userDao.GetUserById(userId)
|
||||
if user == nil {
|
||||
return false, errors.New("用户错误")
|
||||
}
|
||||
|
||||
// 获取患者数据
|
||||
userPatientDao := dao.UserPatientDao{}
|
||||
userPatient, _ := userPatientDao.GetUserPatientByUserId(userId)
|
||||
if userPatient == nil {
|
||||
return false, errors.New("用户错误")
|
||||
}
|
||||
|
||||
// 检测用户是否已领取该优惠卷
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
maps = make(map[string]interface{})
|
||||
maps["user_id"] = user.UserId
|
||||
maps["coupon_id"] = coupon.CouponId
|
||||
maps["user_coupon_status"] = 0
|
||||
userCoupons, _ := userCouponDao.GetUserCouponList(maps)
|
||||
if len(userCoupons) >= req.SingleQuantity {
|
||||
return false, errors.New("用户已有该优惠卷")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < req.SingleQuantity; i++ {
|
||||
// 添加用户优惠卷表
|
||||
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 false, errors.New("发放失败")
|
||||
}
|
||||
|
||||
// 增加优惠卷发放数量
|
||||
err = couponDao.Inc(tx, couponId, "coupon_take_count", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, 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 false, errors.New("发放失败")
|
||||
}
|
||||
|
||||
popupModel.PopupContent = string(popupContentData)
|
||||
|
||||
popupDao := dao.PopupDao{}
|
||||
popup, _ := popupDao.AddPopup(tx, popupModel)
|
||||
if popup == nil {
|
||||
tx.Rollback()
|
||||
return false, 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 false, 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
|
||||
}
|
||||
|
||||
err = rabbitMQ.PublishWithDelay("user.coupon.expired.delay.queue", "amqp.delay.direct", "UserCouponExpired", data, delay)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
// 发送通知
|
||||
res, _ := PatientDistributeCoupon(coupon.CouponName, user.UserId)
|
||||
if !res {
|
||||
utils.LogJsonErr("CouponService-GrantSystemCoupon", "优惠卷通知发送失败")
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// 添加优惠卷发放记录表
|
||||
couponGrantModel := &model.CouponGrant{
|
||||
CouponId: coupon.CouponId,
|
||||
GrantType: req.Type,
|
||||
UserId: userId,
|
||||
TotalQuantity: req.TotalQuantity,
|
||||
GrantQuantity: req.SingleQuantity,
|
||||
GrantResult: 1,
|
||||
StopReason: "",
|
||||
AdminUserId: adminUserId,
|
||||
}
|
||||
|
||||
_, err = couponGrantDao.AddCouponGrant(tx, couponGrantModel)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hospital-admin-api/extend/rabbitMq"
|
||||
)
|
||||
|
||||
// PatientDistributeCoupon 患者-优惠劵发放-站内
|
||||
func PatientDistributeCoupon(couponName string, userId int64) (bool, error) {
|
||||
// 建立队列连接
|
||||
rabbitMQ, err := rabbitMq.NewRabbitMQClient()
|
||||
if err != nil {
|
||||
return false, errors.New("内部错误")
|
||||
}
|
||||
|
||||
defer rabbitMQ.Close()
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["user_id"] = fmt.Sprintf("%d", userId)
|
||||
data["notice_type"] = 3
|
||||
data["notice_system_type"] = 2
|
||||
data["from_name"] = "肝胆小秘书"
|
||||
data["notice_brief_title"] = "有新的优惠券已下发至您的账户,点击查看详情。"
|
||||
data["notice_title"] = fmt.Sprintf("【%s】已到账", couponName)
|
||||
data["notice_content"] = "有新的优惠劵已下发至您的账户中,点击查看详情!"
|
||||
data["link_type"] = 7
|
||||
|
||||
err = rabbitMQ.Publish("send.station.message.queue", "amqp.direct", "SendStationMessage", data)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user