修改了时间计算方法
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"hepa-calc-admin-api/api/dao"
|
||||
"hepa-calc-admin-api/api/model"
|
||||
"hepa-calc-admin-api/config"
|
||||
"hepa-calc-admin-api/extend/rabbitMq"
|
||||
"hepa-calc-admin-api/extend/weChat"
|
||||
"hepa-calc-admin-api/global"
|
||||
"hepa-calc-admin-api/utils"
|
||||
@@ -20,190 +19,6 @@ import (
|
||||
type OrderSingleService struct {
|
||||
}
|
||||
|
||||
// AddOrderSingle 创建单项订单
|
||||
// payChannel:支付渠道(1:h5支付 2:app支付 3:会员支付)
|
||||
func (r *OrderSingleService) AddOrderSingle(tx *gorm.DB, UserId, QuestionId int64, UserCouponId *int64, payChannel int, orderPrice float64) (orderSingle *model.OrderSingle, err error) {
|
||||
// 检测并发请求
|
||||
redisKey := "AddOrderSingle" + fmt.Sprintf("%d", UserId) + fmt.Sprintf("%d", QuestionId)
|
||||
res, _ := global.Redis.Get(context.Background(), redisKey).Result()
|
||||
if res != "" {
|
||||
return nil, errors.New("请勿重复操作")
|
||||
}
|
||||
|
||||
defer func(redisKey string) {
|
||||
global.Redis.Del(context.Background(), redisKey)
|
||||
}(redisKey)
|
||||
|
||||
// 添加缓存
|
||||
_, err = global.Redis.Set(context.Background(), redisKey, "1", (10)*time.Second).Result()
|
||||
if err != nil {
|
||||
return nil, errors.New("生成订单失败")
|
||||
}
|
||||
|
||||
// 获取题目数据
|
||||
questionDao := dao.QuestionDao{}
|
||||
question, err := questionDao.GetQuestionById(QuestionId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题目异常")
|
||||
}
|
||||
|
||||
// 检测题目
|
||||
questionService := &QuestionService{}
|
||||
isNormal, err := questionService.CheckQuestion(question)
|
||||
if err != nil || isNormal == false {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var amountTotal *float64 // 总金额
|
||||
var couponAmountTotal float64 // 优惠卷总金额
|
||||
var paymentAmountTotal float64 // 实际付款金额
|
||||
var orderStatus int // 订单状态(1:待支付 2:已完成 3:已取消)
|
||||
var payStatus int // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
var payTime *time.Time // 支付时间
|
||||
var escrowTradeNo string // 第三方支付流水号
|
||||
|
||||
// 获取问题最终价格
|
||||
amountTotal, err = questionService.GetUserBuyPrice(UserId, QuestionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if amountTotal == nil {
|
||||
return nil, errors.New("价格错误")
|
||||
}
|
||||
|
||||
// 检测用户优惠卷
|
||||
var userCoupon *model.UserCoupon
|
||||
if UserCouponId != nil {
|
||||
// 获取优惠卷数据
|
||||
UserCouponDao := dao.UserCouponDao{}
|
||||
userCoupon, err = UserCouponDao.GetUserCouponPreloadById(*UserCouponId)
|
||||
if err != nil {
|
||||
return nil, errors.New("优惠券异常")
|
||||
}
|
||||
|
||||
userCouponService := &UserCouponService{}
|
||||
isCanUse, err := userCouponService.CheckUserCoupon(userCoupon, QuestionId, 1, *amountTotal)
|
||||
if err != nil || isCanUse == false {
|
||||
return nil, errors.New("价格异常")
|
||||
}
|
||||
|
||||
// 优惠卷总金额
|
||||
couponAmountTotal = userCoupon.Coupon.CouponPrice
|
||||
}
|
||||
|
||||
// 会员支付
|
||||
if payChannel == 3 {
|
||||
paymentAmountTotal = 0 // 实际付款金额
|
||||
orderStatus = 2 // 订单状态(1:待支付 2:已完成 3:已取消)
|
||||
payStatus = 2 // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
|
||||
now := time.Now()
|
||||
payTime = &now // 支付时间
|
||||
|
||||
escrowTradeNo = "GD" + global.Snowflake.Generate().String() // 第三方支付流水号
|
||||
} else {
|
||||
// 实际付款金额
|
||||
paymentAmountTotal = *amountTotal - couponAmountTotal
|
||||
if orderPrice != paymentAmountTotal {
|
||||
return nil, errors.New("价格异常")
|
||||
}
|
||||
|
||||
orderStatus = 1 // 订单状态(1:待支付 2:已完成 3:已取消)
|
||||
payStatus = 1 // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
payTime = nil // 支付时间
|
||||
}
|
||||
|
||||
// 生成订单号
|
||||
orderNo := global.Snowflake.Generate().String()
|
||||
|
||||
// 创建订单
|
||||
orderSingle = &model.OrderSingle{
|
||||
UserId: UserId,
|
||||
QuestionId: QuestionId,
|
||||
OrderStatus: orderStatus,
|
||||
IsDelete: 0,
|
||||
PayChannel: payChannel,
|
||||
PayStatus: payStatus,
|
||||
PayTime: payTime,
|
||||
RefundStatus: 0,
|
||||
OrderNo: orderNo,
|
||||
EscrowTradeNo: escrowTradeNo,
|
||||
AmountTotal: *amountTotal,
|
||||
CouponAmountTotal: couponAmountTotal,
|
||||
PaymentAmountTotal: paymentAmountTotal,
|
||||
CancelStatus: 0,
|
||||
CancelTime: nil,
|
||||
CancelRemarks: "",
|
||||
OrderRemarks: "",
|
||||
}
|
||||
|
||||
orderSingleDao := dao.OrderSingleDao{}
|
||||
orderSingle, err = orderSingleDao.AddOrderSingle(tx, orderSingle)
|
||||
if err != nil {
|
||||
return nil, errors.New("订单创建失败")
|
||||
}
|
||||
|
||||
// 创建优惠卷表
|
||||
if userCoupon != nil {
|
||||
orderSingleCoupon := &model.OrderSingleCoupon{
|
||||
OrderId: orderSingle.OrderId,
|
||||
UserCouponId: *UserCouponId,
|
||||
CouponName: userCoupon.Coupon.CouponName,
|
||||
CouponUsePrice: userCoupon.Coupon.CouponPrice,
|
||||
}
|
||||
|
||||
orderSingleCouponDao := dao.OrderSingleCouponDao{}
|
||||
orderSingleCoupon, err = orderSingleCouponDao.AddOrderSingleCoupon(tx, orderSingleCoupon)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, errors.New("订单创建失败")
|
||||
}
|
||||
|
||||
// 修改优惠卷使用状态
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
userCouponData := make(map[string]interface{})
|
||||
userCouponData["user_coupon_status"] = 1
|
||||
userCouponData["coupon_use_date"] = time.Now().Format("2006-01-02 15:04:05")
|
||||
err := userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, userCouponData)
|
||||
if err != nil {
|
||||
return nil, errors.New("订单创建失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 增加未支付取消订单延迟队列
|
||||
if payChannel == 1 || payChannel == 2 {
|
||||
delay := 30 * time.Minute
|
||||
|
||||
if config.C.Env == "dev" {
|
||||
delay = 3 * time.Minute
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["order_id"] = fmt.Sprintf("%d", orderSingle.OrderId)
|
||||
data["order_no"] = orderSingle.OrderNo
|
||||
data["user_id"] = fmt.Sprintf("%d", orderSingle.UserId)
|
||||
data["order_type"] = 1
|
||||
data["pay_channel"] = orderSingle.PayChannel
|
||||
|
||||
p := rabbitMq.PublishS{
|
||||
QueueName: "cancel.unpay.order.delay.queue",
|
||||
ExchangeName: "amqp.delay.direct",
|
||||
RoutingKey: "CancelUnPayOrder",
|
||||
Message: data,
|
||||
Delay: delay,
|
||||
}
|
||||
err := p.PublishWithDelay()
|
||||
if err != nil {
|
||||
utils.LogJsonErr("添加处理取消未支付订单队列失败:", err.Error())
|
||||
return nil, errors.New("订单创建失败")
|
||||
}
|
||||
}
|
||||
|
||||
return orderSingle, nil
|
||||
}
|
||||
|
||||
// CancelOrderSingle 取消单项订单
|
||||
// cancelReason:订单取消原因(1:主动取消 2:后台取消 3:支付超时取消)
|
||||
func (r *OrderSingleService) CancelOrderSingle(tx *gorm.DB, userId, orderId int64, cancelReason int) (bool, error) {
|
||||
|
||||
@@ -143,42 +143,6 @@ func (r *QuestionService) GetQuestionBuyCount(userId, questionId int64) (c int,
|
||||
return int(buyCount), nil
|
||||
}
|
||||
|
||||
// GetUserBuyPrice 获取问题最终价格
|
||||
func (r *QuestionService) GetUserBuyPrice(userId, questionId int64) (p *float64, err error) {
|
||||
// 获取问题详情
|
||||
questionDao := dao.QuestionDao{}
|
||||
question, err := questionDao.GetQuestionById(questionId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题目异常")
|
||||
}
|
||||
|
||||
// 检测用户是否购买过该问题
|
||||
isFirstBuy := r.CheckUserBuyQuestion(userId, questionId)
|
||||
if isFirstBuy == false {
|
||||
// 未购买过
|
||||
systemSingleDao := dao.SystemSingleDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
systemSingle, err := systemSingleDao.GetSystemSingle(maps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p = &systemSingle.FirstTimePrice
|
||||
}
|
||||
|
||||
// 处理问题优惠价格
|
||||
if p == nil {
|
||||
p = r.HandleQuestionDiscountPrice(question.DiscountPrice, question.DiscountEndTime)
|
||||
}
|
||||
|
||||
if p == nil {
|
||||
p = &question.Price
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// CheckQuestion 检测题目
|
||||
func (r *QuestionService) CheckQuestion(m *model.Question) (bool, error) {
|
||||
if m.QuestionStatus != 1 {
|
||||
|
||||
@@ -3,9 +3,7 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"hepa-calc-admin-api/api/dao"
|
||||
"hepa-calc-admin-api/api/model"
|
||||
"hepa-calc-admin-api/extend/aliyun"
|
||||
"io"
|
||||
"math/rand"
|
||||
@@ -59,20 +57,6 @@ func (r *UserService) HandleUserAvatar(wxAvatar string) (avatar string, err erro
|
||||
return ossPath, nil
|
||||
}
|
||||
|
||||
// CheckUserMember 检测用户会员
|
||||
func (r *UserService) CheckUserMember(user *model.User) bool {
|
||||
if user.IsMember == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if user.MemberExpireDate.Before(now) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// CheckUserBuyOrderMember 检测用户是否购买过会员
|
||||
func (r *UserService) CheckUserBuyOrderMember(userId int64) bool {
|
||||
orderMemberDao := dao.OrderMemberDao{}
|
||||
@@ -83,25 +67,3 @@ func (r *UserService) CheckUserBuyOrderMember(userId int64) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// AddUserMemberValidDate 增加用户会员过期时间
|
||||
func (r *UserService) AddUserMemberValidDate(tx *gorm.DB, user *model.User, d int) bool {
|
||||
userData := make(map[string]interface{})
|
||||
if user.IsMember == 0 {
|
||||
userData["is_member"] = 1
|
||||
}
|
||||
|
||||
if user.MemberExpireDate == nil {
|
||||
userData["is_member"] = time.Now().Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
userData["is_member"] = user.MemberExpireDate.Add(time.Duration(d) * 24 * time.Hour)
|
||||
}
|
||||
|
||||
userDao := dao.UserDao{}
|
||||
err := userDao.EditUserById(tx, user.UserId, userData)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"hepa-calc-admin-api/api/dao"
|
||||
"hepa-calc-admin-api/api/dto"
|
||||
"hepa-calc-admin-api/api/model"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -116,93 +115,3 @@ func (r *UserCouponService) ReturnUserCoupon(tx *gorm.DB, userCouponId int64) bo
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user