diff --git a/config.json b/config.json index eba0c8a..1e77734 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "port": "20181", - "host": "127.0.0.1", + "host": "192.168.1.103", "cross_domain": "*", "post_max_memory": 1024000, "api_router": "/PcClient/*", diff --git a/controllers/client/live.go b/controllers/client/live.go index 4dcb655..5bc8542 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -3,7 +3,9 @@ package client import ( "hudongzhuanjia/controllers" "hudongzhuanjia/libs/filter" + "hudongzhuanjia/libs/im" "hudongzhuanjia/models" + im_service "hudongzhuanjia/services/im" pay_service "hudongzhuanjia/services/pay" red_envelope_service "hudongzhuanjia/services/red_envelope" "hudongzhuanjia/utils" @@ -97,6 +99,11 @@ func (t *LiveCtl) SendLiveRedPack() { amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额 areaId := t.MustGetInt64("area_id") + if amount/float64(num) < 0.3 { // 平均每个红包不得小于0.3 + t.ERROR("每个红包不得小于0.3元", code.MSG_ERR) + return + } + activity := new(models.Activity) exist, err := models.GetById(activity, activityId) t.CheckErr(err) @@ -112,29 +119,50 @@ func (t *LiveCtl) SendLiveRedPack() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, activityId, userId) - t.CheckErr(err) + var res map[string]interface{} + if activity.RehearsalId != 0 { + res, err = pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, activityId, userId) + t.CheckErr(err) + } rule := models.LiveRedEnvelopeRule{} - rule.OutTradeNo = res["out_trade_no"].(string) - rule.Amount = amount - rule.UserId = userId + rule.OutTradeNo = res["out_trade_no"].(string) // 彩排为空 rule.ActivityId = activityId + rule.ActivityId = activity.RehearsalId + rule.UserId = userId + rule.Amount = amount + rule.Num = num rule.Prompt = filter.Replace(prompt) rule.IsDelete = false + if activity.RehearsalId == 0 { + rule.Status = 0 + } else { + rule.Status = 1 // 彩排 + } rule.UpdatedAt = time.Now() rule.CreatedAt = time.Now() _, err = rule.Add() t.CheckErr(err) + if activity.RehearsalId != 0 { // 彩排不过钱 + err = im_service.SendGroupCustomMessage(activityId, im.NoticeLiveRedPackStart, + map[string]interface{}{ + "live_red_envelope_rule_id": rule.Id, + "prompt": rule.Prompt, + "timestamp": time.Now().Unix(), + }) + t.CheckErr(err) + } + records := red_envelope_service.GenRedPack(int(amount*100), num) for _, v := range records { record := new(models.ShakeRedEnvelopeRecord) + record.ActivityId = activityId + record.RehearsalId = activity.RehearsalId record.AreaId = area.Id record.AreaName = area.Name record.RedEnvelopeType = 1 record.LiveRedEnvelopeRuleId = rule.Id - record.ActivityId = activityId record.Name = user.Nickname + "发红包" record.UserId = 0 record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2) @@ -144,27 +172,32 @@ func (t *LiveCtl) SendLiveRedPack() { t.CheckErr(err) } + res["rehearsal_id"] = activity.RehearsalId res["live_red_envelope_rule_id"] = rule.Id t.JSON(res) } // 领取红包 func (t *LiveCtl) GetLiveRedPack() { - liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id") + ruleId := t.MustGetInt64("live_red_pack_rule_id") userId := t.MustGetUID() - if liveRedPackInfoId == 0 { - t.ERROR("live_red_pack_info_id不为0", code.MSG_ERR_Param) + rule := new(models.LiveRedEnvelopeRule) + exist, err := models.GetById(rule, ruleId) + t.CheckErr(err) + t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在") + if rule.Status != 1 { + t.ERROR("红包规则尚未生效", code.MSG_SHAKERB_RULE_NOT_EXIST) return } user := models.User{} - exist, err := models.GetById(&user, userId) + exist, err = models.GetById(&user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户") redPack := new(models.ShakeRedEnvelopeRecord) - exist, err = redPack.GetLiveRedPackRecord(liveRedPackInfoId) + exist, err = redPack.GetLiveRedPackRecord(ruleId) t.CheckErr(err) if !exist { // 通知其他的人 @@ -182,7 +215,10 @@ func (t *LiveCtl) GetLiveRedPack() { t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT) return } - pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60) + + if rule.RehearsalId == 0 { // 非彩排 + go pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60) + } t.JSON(redPack) } diff --git a/controllers/client/reward.go b/controllers/client/reward.go index 7c5202e..5cdcf7e 100644 --- a/controllers/client/reward.go +++ b/controllers/client/reward.go @@ -38,6 +38,7 @@ func (t *RewardCtl) Reward() { exist, err := models.GetById(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + //查询该活动的的打赏服务id rewardServer := new(models.RewardServer) exist, err = rewardServer.GetByActivityId(activityId) @@ -52,8 +53,11 @@ func (t *RewardCtl) Reward() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - res, err := pay_service.UnifiedOrder("欧轩互动-打赏支付", user.Openid, int64(amount*100), 2, user.Id, activityId) - t.CheckErr(err) + var res map[string]interface{} + if activity.RehearsalId == 0 { + res, err = pay_service.UnifiedOrder("欧轩互动-打赏支付", user.Openid, int64(amount*100), 2, user.Id, activityId) + t.CheckErr(err) + } _, err = core.GetXormAuto().InsertOne(&models.RewardHistory{ OutTradeNo: res["out_trade_no"].(string), diff --git a/log/hdzj.log b/log/hdzj.log index 5edb19c..3c86a30 100644 --- a/log/hdzj.log +++ b/log/hdzj.log @@ -31,3 +31,47 @@ 2020-04-24 16:18:18.277 ERROR logger/logger.go:92 check err {"error": "Error 1048: Column 'cancel_time' cannot be null"} 2020-04-24 16:51:38.582 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "FAIL当前使用此业务的用户过多,请稍后再试", "交易订单号": "6VxH4qIla22QsgBQDZRuOHZVTbBVtGhx"} 2020-04-24 17:15:07.448 ERROR logger/logger.go:92 check err {"error": "No content found to be updated"} +2020-05-07 12:00:04.491 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:05.530 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:06.550 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:07.574 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:08.598 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:09.618 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:10.638 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:11.658 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:12.678 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:13.703 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:14.723 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:15.743 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:16.768 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:17.791 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:18.815 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:19.856 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:20.967 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:22.091 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:23.170 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:24.213 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:25.236 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:26.258 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:27.279 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:28.303 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:29.323 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:30.347 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:31.399 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:32.423 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:33.443 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:34.463 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:35.483 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:36.503 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:37.618 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:38.691 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:39.832 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:40.975 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:42.000 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:43.059 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:44.084 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "Xvrl1vsxxFhW0JFv6o2M0Jo58sNt5EUt"} +2020-05-07 12:00:45.108 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "EFmDbFGxUhaQwPxJyKMLKHo4X1ZkPW2z"} +2020-05-07 12:00:46.132 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "uoG5LOiHOn33qVhDIvHlRnfVxLXkkFWC"} +2020-05-07 12:00:47.151 ERROR logger/logger.go:92 查询订单出现错误 {"错误原因": "订单不存在", "交易订单号": "TLXnC3wQ7Ybd7Cd9bUGtFk3tTc6DM9CN"} +2020-05-07 12:00:57.542 ERROR logger/logger.go:92 打赏状态发送变化: err->Params type error, out_trade_no GjpJb0V0iJ1Z3yOFJQJailW3boAUWbvF +2020-05-07 12:03:33.742 ERROR logger/logger.go:92 打赏状态发送变化: err->Params type error, out_trade_no GjpJb0V0iJ1Z3yOFJQJailW3boAUWbvF diff --git a/models/base.go b/models/base.go index 655e5b5..39c6004 100644 --- a/models/base.go +++ b/models/base.go @@ -66,3 +66,6 @@ func AliasTableName(bean interface{}, alias string) string { func GetById(bean interface{}, id int64) (bool, error) { return core.GetXormAuto().Where("is_delete=0 and id=?", id).Get(bean) } +func Add(bean interface{}) (int64, error) { + return core.GetXormAuto().InsertOne(bean) +} diff --git a/models/bully_screen_history.go b/models/bully_screen_history.go index 1e2989b..05b662c 100644 --- a/models/bully_screen_history.go +++ b/models/bully_screen_history.go @@ -34,6 +34,10 @@ func (t *BullyScreenHistory) TableName() string { return BullyScreenHistoryTableName } +func (t *BullyScreenHistory) GetByOutTradeNo(outTradeNo string) (bool, error) { + return core.GetXormAuto().Where("is_delete=0 and out_trade_no=?", outTradeNo).Get(t) +} + func (t *BullyScreenHistory) UpdateStatus(id int64, status int) (int64, error) { t.Status = status return core.GetXormAuto().ID(id).Cols("status").Update(t) diff --git a/models/live_red_envelope_rule.go b/models/live_red_envelope_rule.go index d1f23b5..2697d11 100644 --- a/models/live_red_envelope_rule.go +++ b/models/live_red_envelope_rule.go @@ -14,14 +14,15 @@ type LiveRedEnvelopeRule struct { CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` - UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` - GroupId string `json:"group_id" xorm:"not null default '' comment('聊天室地址') VARCHAR(128)"` - Prompt string `json:"prompt" xorm:"not null default 0 comment('祝福语') VARCHAR(255)"` - Amount float64 `json:"amount" xorm:"not null default 0 comment('红包金额') DECIMAL(18)"` - OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('订单号') VARCHAR(128)"` - Error string `json:"error" xorm:"not null default '' comment('出现错误') VARCHAR(255)"` - Status int `json:"status" xorm:"not null default 0 comment('-1尚未支付0支付成功1已推送2已作废')"` + UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"` + ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` + RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id') INT(11)"` + GroupId string `json:"group_id" xorm:"not null default '' comment('聊天室地址') VARCHAR(128)"` + Prompt string `json:"prompt" xorm:"not null default 0 comment('祝福语') VARCHAR(255)"` + Amount float64 `json:"amount" xorm:"not null default 0 comment('红包金额') DECIMAL(18)"` + Num int `json:"num" xorm:"not null default 0 comment('红包个数') INT(11)"` + OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('订单号') VARCHAR(128)"` + Status int `json:"status" xorm:"not null default 0 comment('-1已作废0尚未支付1支付成功/已推送')"` } func (t *LiveRedEnvelopeRule) TableName() string { @@ -38,5 +39,5 @@ func (t *LiveRedEnvelopeRule) GetByOutTradeNo(outTradeNo string) (bool, error) { func (t *LiveRedEnvelopeRule) UpdateStatusByOutTradeNo(outTradeNo string, status int) (int64, error) { t.Status = status - return core.GetXormAuto().Where("is_delete=0 and status=0 and out_trade_no=?", outTradeNo).Update(t) + return core.GetXormAuto().Where("is_delete=0 and out_trade_no=?", status, outTradeNo).Update(t) } diff --git a/models/reward_history.go b/models/reward_history.go index ce1d07c..7071746 100644 --- a/models/reward_history.go +++ b/models/reward_history.go @@ -44,11 +44,6 @@ func (t *RewardHistory) UpdateStatus(id int64, status int) (int64, error) { return core.GetXormAuto().Where("id=?", id).Cols("status").Update(t) } -func (t *RewardHistory) UpdateStatusByOutTradeNo(outTradeNo string, status int) (int64, error) { - t.Status = status - return core.GetXormAuto().Where("is_delete=0 and status=-1 and out_trade_no=?", outTradeNo).Update(status) -} - func (t *RewardHistory) UpdateStatusByIds(ids []int64, status int) error { if len(ids) > 0 { _, err := core.GetXormAuto().In("id", ids).Cols("status"). diff --git a/models/user_order.go b/models/user_order.go index 72a8c72..e7ee28b 100644 --- a/models/user_order.go +++ b/models/user_order.go @@ -77,6 +77,7 @@ func (t *UserOrder) UpdateRefundByOutTradeNo(outTradeNo interface{}) (int64, err func GetUserOrdersByStatus(expire string, status ...int) ([]*UserOrder, error) { orders := make([]*UserOrder, 0) - err := core.GetXormAuto().Where("is_delete=0 and time_expire <= ?", expire).In("status", status).Find(&orders) + err := core.GetXormAuto().Where("is_delete=0 and time_expire <= ?", expire). + In("status", status).Desc("created_at").Find(&orders) return orders, err } diff --git a/services/pay/handle.go b/services/pay/handle.go index 93c9a07..1e31524 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -2,7 +2,10 @@ package pay_service import ( "errors" + "fmt" + "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/libs/im" + "hudongzhuanjia/logger" "hudongzhuanjia/models" im_service "hudongzhuanjia/services/im" "time" @@ -11,11 +14,9 @@ import ( //处理支付成功之后的回调问题 // 直播红包 -func HandleSuccess(order *models.UserOrder) error { - if order == nil { - return errors.New("订单信息不存在") - } - if order.GoodType == 4 { // 直播商品 +func HandleSuccess(param *orderDelayQueueParam) (err error) { + var exist bool + if param.Order.GoodType == 4 { // 直播商品 customerOrder := new(models.CustomerOrder) //exist, err := customerOrder.GetByOutTradeNO(order.OutTradeNo) //if err != nil || !exist { @@ -26,7 +27,7 @@ func HandleSuccess(order *models.UserOrder) error { return errors.New("商品已经支付") } - _, err := customerOrder.UpdateStatusBy(order.OutTradeNo, 0, 1) + _, err := customerOrder.UpdateStatusBy(param.Order.OutTradeNo, 0, 1) if err != nil { return errors.New("更新状态发送错误") } @@ -74,27 +75,87 @@ func HandleSuccess(order *models.UserOrder) error { // } //} - } else if order.GoodType == 3 { // 直播红包 + } else if param.Order.GoodType == 3 { // 直播红包 info := new(models.LiveRedEnvelopeRule) - exist, err := info.GetByOutTradeNo(order.OutTradeNo) - if err != nil || !exist { - return errors.New("直播红包信息异常") + _, 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 + } + 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 } - return im_service.SendGroupCustomMessage(order.ActivityId, im.NoticeLiveRedPackStart, + err = im_service.SendGroupCustomMessage(param.Order.ActivityId, im.NoticeLiveRedPackStart, map[string]interface{}{ "live_red_envelope_rule_id": info.Id, "prompt": info.Prompt, "timestamp": time.Now().Unix(), }) - } else if order.GoodType == 2 { // 打赏 + 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()) + } + } else if param.Order.GoodType == 2 { // 打赏 history := new(models.RewardHistory) - _, err := history.UpdateStatusByOutTradeNo(order.OutTradeNo, 0) - return err - } else if order.GoodType == 1 { // 霸屏 + 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 = 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()) + } + } else if param.Order.GoodType == 1 { // 霸屏 history := new(models.BullyScreenHistory) - _, err := history.UpdateStatusByOutTradeNo(order.OutTradeNo, 0) - return err + 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 = 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 +} + +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 nil + return } diff --git a/services/pay/order.go b/services/pay/order.go index 399df3e..a07d82c 100644 --- a/services/pay/order.go +++ b/services/pay/order.go @@ -23,39 +23,31 @@ func init() { var orderDelayQueue = make(chan *orderDelayQueueParam, math.MaxInt8) type orderDelayQueueParam struct { - First bool `json:"first"` - Expires int64 `json:"expires"` - Delay int `json:"delay"` - OutTradeNo string `json:"out_trade_no"` - Body string `json:"body"` - Amount int `json:"amount"` // 金额 - Status int `json:"status"` // 0 订单 3 退款 - OpenId string `json:"open_id"` // 被操作人 + Order *models.UserOrder + First bool `json:"first"` + Expires int64 `json:"expires"` + Delay int `json:"delay"` } -func PutOrderDelayQueue(body, outTradeNo, openId string, amount, status int, expires int64, delay int) { +func PutOrderDelayQueue(order *models.UserOrder, expires int64, delay int) { if expires == 0 { expires = time.Now().Add(12 * time.Hour).Unix() // 2 个小时 } if delay == 0 { - delay = 100 + delay = 1000 } orderDelayQueue <- &orderDelayQueueParam{ - First: true, - Expires: expires, - Delay: delay, - OutTradeNo: outTradeNo, - Body: body, - Amount: amount, - Status: status, - OpenId: openId, + First: true, + Expires: expires, + Delay: delay, + Order: order, } } func loopUnifiedOrder() { - orders, err := models.GetUserOrdersByStatus(core2.FormatTime(time.Now()), 0, 3) + orders, err := models.GetUserOrdersByStatus(core2.FormatTime(time.Now().Add(12*time.Hour)), 0, 3) if err != nil { panic(err) } @@ -65,7 +57,8 @@ func loopUnifiedOrder() { if err != nil { expire = time.Now().Add(12 * time.Hour) } - PutOrderDelayQueue(order.Body, order.OutTradeNo, order.OpenId, int(order.TotalFee), order.Status, expire.Unix(), 0) + + PutOrderDelayQueue(order, expire.Add(12*time.Hour).Unix(), 0) } defer func() { @@ -83,11 +76,12 @@ func loopUnifiedOrder() { panic("通道异常关闭") } if param.Expires <= time.Now().Unix() { - //if param.Status == 0 { - //order := new(models.UserOrder) - //_, err = order.UpdateStatusByOutTradeNo(param.OutTradeNo, 7) - //Close(param.OutTradeNo) // 超时关闭订单 - //} + if param.Order.Status == 0 { + order := new(models.UserOrder) + _, err = order.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 7) + Close(param.Order.OutTradeNo) // 超时关闭订单 + go HandleFailed(param) + } continue // 超时 } @@ -97,32 +91,33 @@ func loopUnifiedOrder() { } param.First = false - if param.Status == 0 { - res, err := OrderQuery(param.OutTradeNo) + if param.Order.Status == 0 { + res, err := OrderQuery(param.Order.OutTradeNo) // 出现错误 if err != nil { logger.Error("查询订单出现错误", zap.String("错误原因", err.Error()), - zap.String("交易订单号", param.OutTradeNo)) + zap.String("交易订单号", param.Order.OutTradeNo)) orderDelayQueue <- param // 重新进入队列 continue } if res.Query.TradeState == CODE_TRADE_SUCCESS { - go HandleSuccess(res.Order) + param.Order = res.Order + go HandleSuccess(param) } else if res.Query.TradeState == CODE_TRADE_REFUND { - param.Status = 3 + param.Order.Status = 3 orderDelayQueue <- param continue } else if res.Query.TradeState == CODE_TRADE_NOTPAY || res.Query.TradeState == CODE_TRADE_USERPAYING { orderDelayQueue <- param continue } - } else if param.Status == 3 { - _, err = QueryRefund(param.OutTradeNo) + } else if param.Order.Status == 3 { + _, err = QueryRefund(param.Order.OutTradeNo) if err != nil { logger.Error("退款订单查询错误", zap.String("错误原因", err.Error()), - zap.String("交易订单号", param.OutTradeNo)) + zap.String("交易订单号", param.Order.OutTradeNo)) continue } } else { @@ -180,7 +175,7 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) userOrder.IsDelete = false userOrder.CreatedAt = time.Now() userOrder.UpdatedAt = time.Now() - if _, err = userOrder.AddUserOrder(); err != nil { + if _, err = models.Add(userOrder); err != nil { return nil, err } @@ -189,7 +184,7 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) pac := "prepay_id=" + resp.PrepayId paySign := core2.JsapiSign(client.AppId(), timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) - go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) + go PutOrderDelayQueue(userOrder, 0, 0) return map[string]interface{}{ "appid": Appid, "timestamp": timestamp, @@ -217,7 +212,7 @@ func ReOrder(outTradeNo string) (map[string]interface{}, error) { //获取H5支付需要的paySign pac := "prepay_id=" + userOrder.PrepayId paySign := core2.JsapiSign(Appid, timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) - go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) + go PutOrderDelayQueue(userOrder, 0, 0) return map[string]interface{}{ "appid": Appid, "timestamp": timestamp, @@ -404,7 +399,7 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { return nil, err } - PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) // 退款查询 + PutOrderDelayQueue(userOrder, 0, 0) // 退款查询 return res, nil }