|
|
package pay_service
import ( "errors" "git.ouxuan.net/tommy/osmanthuswine/src/core" "github.com/xormplus/xorm" "hudongzhuanjia/models" "hudongzhuanjia/services/im" "hudongzhuanjia/utils" "hudongzhuanjia/utils/define" "time" )
func init() { go utils.HandleTicker(30*time.Minute, HandleReward) // 打赏24小时退款
go utils.HandleTicker(30*time.Second, HandleGoodOrder) // 订单关闭信息
}
//处理支付成功之后的回调问题
func HandleSuccess(order *models.UserOrder) error { if order.GoodType == 4 { // 直播商品
activity := new(models.Activity) exist, err := models.Get(activity, order.ActivityId) if err != nil || !exist { return errors.New("互动信息异常") } s := core.GetXormAuto().NewSession() defer s.Close() err = s.Begin() if err != nil { s.Rollback() return err } cOrder := models.CustomerOrder{} exist, err = s.Where("is_delete=0 and out_trade_no=?", order.OutTradeNo).Get(cOrder) if err != nil || !exist || cOrder.Status != 0 { s.Rollback() return errors.New("客户订单异常") } cOrder.Status = 1 _, err = s.Where("out_trade_no=?", order.OutTradeNo).Cols("").Update(&cOrder) if err != nil { s.Rollback() return errors.New("订单状态异常") }
subs, err := models.GetCustomerOrderSubsByOrderId(cOrder.Id) if err != nil { s.Rollback() return err }
gift := models.OrderGift{} exist, err = s.Where("is_delete=0 and order_gift_switch=? "+"and activity_id=?", define.StatusOpen, activity.Id).Get(&gift) if err != nil { s.Rollback() return err } if exist { prize := new(models.UserPrize) prize.UserId = order.UserId prize.ActivityId = order.ActivityId prize.RehearsalId = activity.RehearsalId prize.CustomerOrderId = cOrder.Id prize.ActivityName = activity.Name prize.ArchId = activity.ArchId prize.PrizeName = gift.GiftName prize.PrizeImg = gift.GiftPicUrl prize.PrizeType = 4 prize.IsDelete = false prize.Status = 1 if gift.Num <= 0 { // 不限制
_, err = s.InsertOne(prize) if err != nil { s.Rollback() return err } cOrder.UserPrizeId = prize.Id _, err := s.ID(cOrder.Id).NoAutoCondition().Cols("user_prize_id").Update(&cOrder) if err != nil { s.Rollback() return err } } else if gift.Num > 0 { count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0", activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&gift) if err != nil { s.Rollback() return err } if gift.Num > int(count) { // 足够
_, err = s.InsertOne(prize) if err != nil { s.Rollback() return err } cOrder.UserPrizeId = prize.Id _, err := s.ID(cOrder.Id).NoAutoCondition().Cols("user_prize_id").Update(&cOrder) if err != nil { s.Rollback() return err } } } } err = s.Commit() if err != nil { s.Rollback() return err } for _, sub := range subs { area := models.AreaStore{} exist, _ := area.GetByUserId(cOrder.ActivityId, cOrder.BuyerId) admin := 0 if exist { admin = 1 } go im_service.SendGroupCustomMessage(cOrder.BuyerId, cOrder.ActivityId, im_service.NoticeLiveGoodBuy, map[string]interface{}{ "admin": admin, "goods_pic_url": sub["goods_pic_url"], "goods_num": sub["goods_num"], "good_name": sub["good_name"], "good_price": sub["good_price"], "goods_id": sub["goods_id"], "timestamp": time.Now().Unix(), }) } } else if order.GoodType == 3 { // 直播红包
info := new(models.LiveRedEnvelopeRule) _, err := info.UpdateStatusByOutTradeNo(order.OutTradeNo, 1) if err != nil { return err } exist, err := info.GetByOutTradeNo(order.OutTradeNo) if err != nil || !exist || info.Status != 1 { return errors.New("订单状态异常") }
exist, _ = new(models.AreaStore).GetByUserId(info.ActivityId, info.UserId) admin := 0 if exist { admin = 1 } go im_service.SendGroupCustomMessage(info.UserId, order.ActivityId, im_service.NoticeLiveRedPackStart, map[string]interface{}{ "admin": admin, "live_red_envelope_rule_id": info.Id, "prompt": info.Prompt, "timestamp": time.Now().Unix(), }) } else if order.GoodType == 2 { // 打赏
history := new(models.RewardHistory) exist, err := history.GetByOutTradeNo(order.OutTradeNo) if err != nil || !exist || history.Status != -1 { return errors.New("订单状态异常") } _, err = history.UpdateStatus(history.Id, 0) if err != nil { return err } } else if order.GoodType == 1 { // 霸屏
history := new(models.BullyScreenHistory) exist, err := history.GetByOutTradeNo(order.OutTradeNo) if err != nil || !exist || history.Status != -1 { return errors.New("订单状态异常") }
_, err = history.UpdateStatus(history.Id, 0) if err != nil { return err } } return nil }
// 处理商品订单超时的状态
func HandleGoodOrder() error { orders, err := models.GetExpiredAtLiveCustomerOrder() if err != nil { return err } for _, v := range orders { if v.Type != 1 || v.Status != 0 { continue } res, err := OrderQuery(v.OutTradeNo) if err != nil { return err } if res["trade_state"] == define.CODE_TRADE_SUCCESS { v.Status = 1 _, err = models.Update(v.Id, v, "status") if err != nil { return err } } else if res["trade_state"] == define.CODE_TRADE_NOTPAY { err = Close(v.OutTradeNo) if err != nil { return err } v.Status = 9 v.CancelTime = time.Now() _, err = models.Update(v.Id, v, "status", "cancel_time") if err != nil { return err } }
//subs, err := models.GetSubOrdersByOrderId(v.Id)
//if err != nil {
// return err
//}
//for _, sub := range subs {
// _, err = new(models.CustomerGoods).IncrStockById(sub.GoodsId, sub.GoodsNum)
// if err != nil {
// return err
// }
//}
} return nil }
// 处理打赏
func HandleReward() error { result, err := models.GetExpireRewardHistory() if err != nil { return err } for _, v := range result { if v.RehearsalId != 0 && v.Type == 0 { // 欧轩互动h5彩排
continue } _, err := Refund("欧轩互动-打赏过期退款", v.OutTradeNo) if err != nil { return err } v.Status = 4 _, err = models.Update(v.Id, v, "status") if err != nil { return err } } return nil }
func HandleCancelOrder(outTradeNo string) error { return models.Commit(func(session *xorm.Session) error { order := new(models.CustomerOrder) exist, err := session.Where("is_delete=0 and status=0 and out_trade_no=?", outTradeNo).Get(order) if err != nil { return err } if !exist || order.Status != 0 { return errors.New("客户订单异常") } order.Status = 9 order.CancelTime = time.Now() _, err = session.Where("is_delete=0 and out_trade_no=?", outTradeNo). Cols("status, cancel_time").Update(order) if err != nil { return err } subs := make([]*models.CustomerOrderSub, 0) err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs) if err != nil { return err } //for _, sub := range subs {
// _, err = session.ID(sub.GoodsId).NoAutoCondition().Incr("stock", sub.GoodsNum).
// Update(&models.CustomerGoods{})
// if err != nil {
// return err
// }
//}
return nil }) }
|