互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.2 KiB

package pay_service
import (
"errors"
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/logger"
"hudongzhuanjia/models"
im_service "hudongzhuanjia/services/im"
"time"
)
//处理支付成功之后的回调问题
// 直播红包
func HandleSuccess(param *OrderDelayQueueParam) error {
if param.Order.GoodType == 4 { // 直播商品
customerOrder := new(models.CustomerOrder)
_, err := customerOrder.UpdateStatusBy(param.Order.OutTradeNo, 0, 1)
if err != nil {
return errors.New("更新状态发送错误")
}
customerOrder.GetByOutTradeNO(param.Order.OutTradeNo)
if customerOrder.Status != 1 {
return errors.New("商品订单状态信息出现异常")
}
subs, err := models.GetCustomerOrderSubsByOrderNos(customerOrder.OrderNo)
if err != nil {
logger.Error("商品子订单查询异常", err)
return err
}
for _, sub := range subs {
err = im_service.SendGroupCustomMessage(customerOrder.BuyerId, param.Order.ActivityId, im_service.NoticeLiveGoodBuy,
map[string]interface{}{
"goods_pic_url": sub.GoodsPicUrl,
"goods_num": sub.GoodsNum,
"good_name": sub.GoodName,
"good_price": sub.GoodPrice,
"goods_id": sub.GoodsId,
"timestamp": time.Now().Unix(),
})
if err != nil {
logger.Error("直播商品子订单信息发送异常异常", err)
return err
}
}
activity := new(models.Activity)
exist, err := models.Get(activity, param.Order.ActivityId)
if err != nil || !exist {
return errors.New("互动信息异常")
}
gift := new(models.OrderGift)
exist, err = gift.GetByActivityId(param.Order.ActivityId)
if err != nil {
err = fmt.Errorf("获取订单送礼错误: err-> %v, activity_id-> %v", err, activity.Id)
logger.Error(err)
return err
}
if !exist {
return nil
}
prize := new(models.UserPrize)
prize.UserId = param.Order.UserId
prize.ActivityId = param.Order.ActivityId
prize.RehearsalId = activity.RehearsalId
prize.ActivityName = activity.Name
prize.PrizeName = gift.GiftName
prize.PrizeImg = gift.GiftPicUrl
prize.PrizeType = 4
prize.IsDelete = false
prize.Status = 1
prize.CreatedAt = time.Now()
prize.UpdatedAt = time.Now()
if gift.Num > 0 {
count, err := new(models.CustomerOrder).
Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
if err != nil {
err = fmt.Errorf("订单人数统计失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
logger.Error(err)
return err
}
if gift.Num <= int(count) { // 人数足够
return nil
}
}
_, err = prize.Add()
if err != nil {
err = fmt.Errorf("奖品添加失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
logger.Error(err)
return err
}
customerOrder.UserPrizeId = prize.Id
_, err = models.Update(customerOrder, "user_prize_id")
if err != nil {
err = fmt.Errorf("奖品记录添加失败: err-> %v, out_trade_no-> %v, prize_id->%v", err, param.Order.OutTradeNo, prize.Id)
logger.Error(err)
return err
}
} else if param.Order.GoodType == 3 { // 直播红包
info := new(models.LiveRedEnvelopeRule)
_, err := info.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 1)
if err != nil {
err = fmt.Errorf("状态改变出现错误: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
exist, err := info.GetByOutTradeNo(param.Order.OutTradeNo)
if err != nil || !exist || info.Status != 1 {
err = fmt.Errorf("直播红包信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
err, exist, info.Status, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
err = im_service.SendGroupCustomMessage(info.UserId, param.Order.ActivityId, im_service.NoticeLiveRedPackStart,
map[string]interface{}{
"live_red_envelope_rule_id": info.Id,
"prompt": info.Prompt,
"timestamp": time.Now().Unix(),
})
if err != nil {
err = fmt.Errorf("发送腾讯im信息出现错误: err->%v, info_id->%v, out_trade_no->%v",
err, info.Id, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
} else if param.Order.GoodType == 2 { // 打赏
history := new(models.RewardHistory)
exist, err := history.GetByOutTradeNo(param.Order.OutTradeNo)
if err != nil || !exist || history.Status != -1 {
err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
_, err = history.UpdateStatus(history.Id, 0)
if err != nil {
err = fmt.Errorf("打赏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
} else if param.Order.GoodType == 1 { // 霸屏
history := new(models.BullyScreenHistory)
exist, err := history.GetByOutTradeNo(param.Order.OutTradeNo)
if err != nil || !exist || history.Status != -1 {
err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
return err
}
_, err = history.UpdateStatus(history.Id, 0)
if err != nil {
err = fmt.Errorf("霸屏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
}
}
return nil
}
func HandleFailed(param *OrderDelayQueueParam) (err error) {
var exist bool
// 退还库存
if param.Order.GoodType == 4 {
order := new(models.CustomerOrder)
exist, err = order.GetByOutTradeNO(param.Order.OutTradeNo)
if err != nil || !exist {
err = fmt.Errorf("不存在客户订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
logger.Error(err.Error())
return
}
subs := make([]*models.CustomerOrderSub, 0)
err = core.GetXormAuto().Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
if err != nil {
err = fmt.Errorf("不存在客户子订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
}
for _, sub := range subs {
_, err = core.GetXormAuto().Where("id=?", sub.GoodsId).
Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
if err != nil {
err = fmt.Errorf("库存返回失败: err->%v, out_trade_no->%v, sub_id->%v", err, param.Order.OutTradeNo, sub.Id)
}
}
}
return
}