黄梓健 5 years ago
parent
commit
fe63a372ad
  1. 5
      controllers/client/reward.go
  2. 2
      controllers/pc/reward.go
  3. 9
      models/reward_history.go
  4. 35
      services/pay/handle.go
  5. 31
      services/pay/order.go
  6. 21
      utils/utils.go

5
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

2
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
}

9
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")

35
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
}

31
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
}

21
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)
}
}
}
}
Loading…
Cancel
Save