From fe63a372ad079dd4b4a1bcdffcbbe42f745c320e Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Fri, 22 May 2020 14:58:32 +0800 Subject: [PATCH] fix --- controllers/client/reward.go | 5 +++-- controllers/pc/reward.go | 2 +- models/reward_history.go | 9 ++++++++- services/pay/handle.go | 35 ++++++++++++++++++++++------------- services/pay/order.go | 31 ++++++++++++++++--------------- utils/utils.go | 21 +++++++++++++++++++++ 6 files changed, 71 insertions(+), 32 deletions(-) diff --git a/controllers/client/reward.go b/controllers/client/reward.go index f58ac23..78a0c8f 100644 --- a/controllers/client/reward.go +++ b/controllers/client/reward.go @@ -61,7 +61,7 @@ func (t *RewardCtl) Reward() { t.CheckErr(err) } - history := &models.RewardHistory{ + history := models.RewardHistory{ OutTradeNo: res["out_trade_no"].(string), ActivityId: activityId, RewardServerId: rewardServer.Id, @@ -72,6 +72,7 @@ func (t *RewardCtl) Reward() { RehearsalId: activity.RehearsalId, Status: -1, ReviewTime: 0, + ExpireTime: time.Now().Add(24 * time.Hour).Unix(), Type: _type, } @@ -79,7 +80,7 @@ func (t *RewardCtl) Reward() { history.Status = 0 } - _, err = models.Add(history) + _, err = models.Add(&history) t.CheckErr(err) res["rehearsal_id"] = activity.RehearsalId diff --git a/controllers/pc/reward.go b/controllers/pc/reward.go index db37150..61be1fa 100644 --- a/controllers/pc/reward.go +++ b/controllers/pc/reward.go @@ -92,7 +92,7 @@ func (t *RewardCtl) Review() { _, err = models.Update(v.Id, v, "status") t.CheckErr(err) - if activity.RehearsalId != 0 { // 彩排不需要金额 + if v.RehearsalId != 0 && v.Type == 0 { // 彩排不需要金额 continue } diff --git a/models/reward_history.go b/models/reward_history.go index 7ea15cf..e86ea0a 100644 --- a/models/reward_history.go +++ b/models/reward_history.go @@ -23,8 +23,9 @@ type RewardHistory struct { UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户得id') INT(11)"` Content string `json:"content" xorm:"not null comment('内容') text"` Amount float64 `json:"amount" xorm:"not null default(0.00) comment('金额') DECIMAL(18)"` - Status int `json:"status" xorm:"not null default(0) comment('-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废') INT(11)"` + Status int `json:"status" xorm:"not null default(0) comment('-1未支付,0未审核,1未通过,2已通过,3已推送,4已退款') INT(11)"` ReviewTime int64 `json:"review_time" xorm:"not null default(0) comment('审核时间') INT(11)"` + ExpireTime int64 `json:"expire_time" xorm:"not null default(0) comment('过期时间') INT(11)"` Type int `json:"type" xorm:"not null default 0 comment('线下h5/直播 = 0/1') TINYINT(1)"` Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` @@ -61,6 +62,12 @@ func GetRewardHistoryByIds(ids interface{}) ([]*RewardHistory, error) { return result, err } +func GetExpireRewardHistory() ([]*RewardHistory, error) { + result := make([]*RewardHistory, 0) + err := core.GetXormAuto().Where("is_delete=0 and status=0 and expire_time<=", time.Now().Unix()).Find(&result) + return result, err +} + func (t *RewardHistory) SumMoney(serverId, rehearsalId interface{}) (float64, error) { return core.GetXormAuto().Where("reward_server_id=? and status=3 and is_delete=0 "+ " and rehearsal_id=?", serverId, rehearsalId).Sum(t, "amount") diff --git a/services/pay/handle.go b/services/pay/handle.go index 71223a3..fcb01fa 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -6,6 +6,7 @@ import ( "hudongzhuanjia/logger" "hudongzhuanjia/models" im_service "hudongzhuanjia/services/im" + "hudongzhuanjia/utils" "hudongzhuanjia/utils/define" "math" "time" @@ -13,6 +14,7 @@ import ( func init() { go loopUnifiedOrder() + go utils.HandleTicker(1*time.Hour, HandleReward) // 打赏24小时退款 } var orderDelayQueue = make(chan *models.UserOrder, math.MaxInt8) @@ -270,20 +272,27 @@ func HandleFailed(order *models.UserOrder) error { return nil } -// 定时任务 -// 1个小时查询一次 -func HandleTicker() { - defer func() { - if err := recover(); err != nil { - logger.Error("定时任务错误原因", err) +// 处理打赏 +func HandleReward() error { + result, err := models.GetExpireRewardHistory() + if err != nil { + logger.Error("获取过期打赏错误原因", err) + return err + } + for _, v := range result { + if v.RehearsalId != 0 && v.Type == 0 { // 欧轩互动h5彩排 + continue } - HandleTicker() - }() - ticker := time.NewTicker(1 * time.Hour) - for { - select { - case <-ticker.C: - + _, err := Refund("欧轩互动-打赏过期退款", v.OutTradeNo) + if err != nil { + logger.Error("欧轩互动打赏过期退款", v.OutTradeNo) + return err + } + v.Status = 4 + _, err = models.Update(v.Id, v, "status") + if err != nil { + logger.Error("欧轩互动打赏过期状态改变错误") } } + return nil } diff --git a/services/pay/order.go b/services/pay/order.go index 0bc1295..0a328b9 100644 --- a/services/pay/order.go +++ b/services/pay/order.go @@ -228,14 +228,15 @@ func Close(outTradeNo string) error { return nil } -//const CALLBACK_REFUND_URL = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund" +const CallbackRefundUrl = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund" -func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { +func Refund(reason, outTradeNo string) (map[string]string, error) { userOrder := new(models.UserOrder) exist, err := userOrder.GetByOutTradeNo(outTradeNo) if err != nil { return nil, err } + if !exist { return nil, fmt.Errorf("订单不存在") } @@ -243,28 +244,28 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { outRefundNo := utils.RandomStr(64) nonceStr := utils.RandomStr(32) - res, err := pay.Refund2(client, &pay.RefundRequest{ - TransactionId: "", - OutTradeNo: outTradeNo, - OutRefundNo: outRefundNo, - TotalFee: userOrder.TotalFee, - RefundFee: userOrder.TotalFee, - NonceStr: nonceStr, - SignType: pay_core.SignType_MD5, - RefundFeeType: "CNY", - RefundDesc: reason, - }) + body := make(map[string]string, 0) + body["out_trade_no"] = outTradeNo + body["out_refund_no"] = outRefundNo + body["total_fee"] = fmt.Sprint(userOrder.TotalFee) + body["refund_fee"] = fmt.Sprint(userOrder.TotalFee) + body["nonce_str"] = nonceStr + body["sign_type"] = pay_core.SignType_MD5 + body["refund_desc"] = reason + body["notify_url"] = CallbackRefundUrl + + res, err := pay.Refund(client, body) // if err != nil { return nil, err } userOrder.Status = 3 - _, err = userOrder.UpdateStatusByOutTradeNo(outTradeNo, userOrder.Status) + _, err = models.Update(userOrder.Id, userOrder, "status") if err != nil { return nil, err } - PutOrderDelayQueue(userOrder) // 退款查询 + go PutOrderDelayQueue(userOrder) // 退款查询 return res, nil } diff --git a/utils/utils.go b/utils/utils.go index 0952e18..792c5f8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "fmt" "github.com/skip2/go-qrcode" + "hudongzhuanjia/logger" "hudongzhuanjia/utils/define" "io/ioutil" "math/rand" @@ -136,3 +137,23 @@ func RandomInt(length int) int { num, _ := strconv.Atoi(string(result)) return num } + +// 定时任务 +func HandleTicker(ticker time.Duration, f func() error) { + defer func() { + if err := recover(); err != nil { + logger.Error("定时任务错误panic原因", err) + } + HandleTicker(ticker, f) + }() + t := time.NewTicker(ticker) + for { + select { + case <-t.C: + err := f() + if err != nil { + logger.Error("定时任务错误原因", err) + } + } + } +}