From 639d24abe495db669bdcc53a10738b5b67fda58d Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Thu, 14 May 2020 14:23:08 +0800 Subject: [PATCH] rehearsal_id --- controllers/client/good.go | 83 +++++++++++++++++++++--------------- controllers/client/live.go | 33 ++------------ controllers/client/reward.go | 3 +- controllers/client/sign.go | 20 +++------ controllers/common/im.go | 6 +-- controllers/pc/lottery_draw.go | 24 +++++++++++ controllers/pc/reward.go | 3 +- controllers/pc/shake_red_envelope.go | 5 +-- libs/im/im.go | 2 +- libs/im/status.go | 10 ----- models/area_store.go | 1 + services/im/im.go | 24 ++++++++--- services/pay/handle.go | 3 +- test/pay_test.go | 2 +- 14 files changed, 111 insertions(+), 108 deletions(-) diff --git a/controllers/client/good.go b/controllers/client/good.go index bb0d24b..89fe656 100644 --- a/controllers/client/good.go +++ b/controllers/client/good.go @@ -149,39 +149,47 @@ func (t *GoodCtl) Order() { defer session.Close() session.Begin() - subOrders := make([]*models.CustomerOrderSub, 0) - var price = 0 + goodIds := make([]interface{}, 0) for _, g := range param.Goods { - good := new(models.CustomerGoods) - exist, err = session.Where("is_delete=0 and id=?", g["id"]).Get(good) - if err != nil || !exist { - session.Rollback() - t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) - } - - if good.Stock-g["num"] < 0 { - session.Rollback() - t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) - } + goodIds = append(goodIds, g["id"]) + } + goods := make([]*models.CustomerGoods, 0) + err = session.Where("is_delete=0").In("id", goodIds).Find(&goods) + if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 { + session.Rollback() + t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) + } - if good.Stock > 0 { - _, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good) - if err != nil { - session.Rollback() - t.CheckErr(err) + subOrders := make([]*models.CustomerOrderSub, 0) + var price = 0 + // 检测库存 + for _, good := range goods { + for _, g := range param.Goods { + if good.Id == int64(g["id"]) { + if good.Stock-g["num"] < 0 { + session.Rollback() + t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) + } else { + _, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good) + if err != nil { + session.Rollback() + t.CheckErr(err) + } + price += int(good.Price*100) * g["num"] + subOrders = append(subOrders, &models.CustomerOrderSub{ + IsDelete: false, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + OrderNo: "", + GoodsId: good.Id, + GoodsNum: g["num"], + GoodName: good.Name, + GoodPrice: good.Price, + }) + break + } } } - price += int(good.Price*100) * g["num"] - subOrders = append(subOrders, &models.CustomerOrderSub{ - IsDelete: false, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - OrderNo: "", - GoodsId: good.Id, - GoodsNum: g["num"], - GoodName: good.Name, - GoodPrice: good.Price, - }) } res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, @@ -222,16 +230,21 @@ func (t *GoodCtl) Order() { session.Rollback() t.CheckErr(err) } + + // 批量插入 + beans := make([]interface{}, 0) for _, subOrder := range subOrders { subOrder.OrderNo = order.OrderNo - _, err = session.InsertOne(subOrder) - if err != nil { - session.Rollback() - t.CheckErr(err) - } + beans = append(beans, subOrder) + } + session.Insert(beans...) + if err != nil { + session.Rollback() + t.CheckErr(err) } - session.Commit() + err = session.Commit() + t.CheckErr(err) res["order_id"] = order.Id t.JSON(res) diff --git a/controllers/client/live.go b/controllers/client/live.go index de92aff..43f798e 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -1,13 +1,10 @@ package client import ( - "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "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" @@ -131,15 +128,11 @@ func (t *LiveCtl) SendLiveRedPack() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - var res = make(map[string]interface{}, 0) - res["out_trade_no"] = "" - if activity.RehearsalId == 0 { - res, err = pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, userId, activityId) - t.CheckErr(err) - } + res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, userId, activityId) + t.CheckErr(err) rule := new(models.LiveRedEnvelopeRule) - rule.OutTradeNo = res["out_trade_no"].(string) // 彩排为空 + rule.OutTradeNo = res["out_trade_no"].(string) rule.ActivityId = activityId rule.RehearsalId = activity.RehearsalId rule.AreaId = areaId @@ -148,26 +141,12 @@ func (t *LiveCtl) SendLiveRedPack() { rule.Num = num rule.Prompt = filter.Replace(prompt) rule.IsDelete = false - if activity.RehearsalId == 0 { - rule.Status = 0 - } else { - rule.Status = 1 // 彩排 - } + rule.Status = 0 rule.UpdatedAt = time.Now() rule.CreatedAt = time.Now() _, err = models.Add(rule) t.CheckErr(err) - if activity.RehearsalId != 0 { // 彩排不过钱 - err = im_service.SendGroupCustomMessage(fmt.Sprint(user.Id), 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) @@ -230,9 +209,5 @@ func (t *LiveCtl) GetLiveRedPack() { return } - 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 c24b57d..a86eed8 100644 --- a/controllers/client/reward.go +++ b/controllers/client/reward.go @@ -23,6 +23,7 @@ func (t *RewardCtl) Reward() { content := t.MustGet("content") amount := t.MustGetDouble("amount") uid := t.MustGetUID() + _type := t.DefaultInt("type", 0) if amount <= 0 { t.ERROR("打赏金额不能小于0", code.MSG_ERR_Param) @@ -56,7 +57,7 @@ func (t *RewardCtl) Reward() { var res = make(map[string]interface{}, 0) res["out_trade_no"] = "" - if activity.RehearsalId == 0 { + if activity.RehearsalId == 0 || _type == 1 { // 直播不用彩排 res, err = pay_service.UnifiedOrder("欧轩互动-打赏支付", user.Openid, int64(amount*100), 2, user.Id, activityId) t.CheckErr(err) } diff --git a/controllers/client/sign.go b/controllers/client/sign.go index 43fafe5..acf83d5 100644 --- a/controllers/client/sign.go +++ b/controllers/client/sign.go @@ -94,15 +94,8 @@ func (t *SignCtl) Setting() { func (t *SignCtl) Sign() { uid := t.MustGetUID() activityId := t.MustGetInt64("activity_id") - _type := t.DefaultInt("_type", 0) // 默认 0 - - // 适配直播 - var areaId int64 = 0 - if _type == 0 { - areaId = t.MustGetAreaId() - } else { - areaId = t.MustGetInt64("area_id") - } + _type := t.DefaultInt("type", 0) // 默认 0 + areaId := t.MustGetAreaId() //根据activity_id查找主活动的信息 activity := new(models.Activity) @@ -110,11 +103,10 @@ func (t *SignCtl) Sign() { t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") - // 根据areaId - customer := new(models.Customer) - exist, err = customer.GetByActivityIdAndAreaId(activityId, areaId) + area := new(models.AreaStore) + exist, err = models.GetById(area, areaId) t.CheckErr(err) - t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") + t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") user := new(models.User) exist, err = models.GetById(user, uid) @@ -168,7 +160,7 @@ func (t *SignCtl) Sign() { go ws_send_service.SendSign(fmt.Sprintf("%d", activity.Id), define.TYPE_CUSTOMER, activity.CustomerId, map[string]interface{}{ "type": "sign_up", - "customer_id": customer.Id, + "customer_id": area.CustomerId, "data": map[string]interface{}{ "avatar": user.Avatar, "sign_total": signTotal + 1, diff --git a/controllers/common/im.go b/controllers/common/im.go index 2f1f9c2..351adf7 100644 --- a/controllers/common/im.go +++ b/controllers/common/im.go @@ -1,9 +1,7 @@ package common import ( - "fmt" "hudongzhuanjia/controllers" - "hudongzhuanjia/libs/im" im_service "hudongzhuanjia/services/im" "time" ) @@ -16,7 +14,7 @@ func (t *ImTestCtl) SendNotice() { activityId := t.MustGetInt64("activity_id") status := t.MustGetInt("status") - im_service.SendGroupCustomMessage(fmt.Sprint(1), activityId, im.NoticeStatus(status), map[string]interface{}{ + im_service.SendGroupCustomMessage(1, activityId, im_service.NoticeStatus(status), map[string]interface{}{ "customer_id": 16, "shake_red_envelope_rule_id": 5, "timestamp": time.Now().Unix(), @@ -28,7 +26,7 @@ func (t *ImTestCtl) SendRedPack() { activityId := t.MustGetInt64("activity_id") prompt := t.MustGet("prompt") - im_service.SendGroupCustomMessage(fmt.Sprint(1), activityId, im.NoticeLiveRedPackStart, + im_service.SendGroupCustomMessage(1, activityId, im_service.NoticeLiveRedPackStart, map[string]interface{}{ "live_red_envelope_rule_id": 1, "prompt": prompt, diff --git a/controllers/pc/lottery_draw.go b/controllers/pc/lottery_draw.go index cd8797e..b5bd2ac 100644 --- a/controllers/pc/lottery_draw.go +++ b/controllers/pc/lottery_draw.go @@ -5,6 +5,7 @@ import ( "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" + im_service "hudongzhuanjia/services/im" lottery_service "hudongzhuanjia/services/lottery" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" @@ -21,6 +22,7 @@ type LotteryDrawCtl struct { //开始抽奖 func (t *LotteryDrawCtl) Start() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") + activityId := t.MustGetInt64("activity_id") ladder := new(models.LotteryDrawRuleLadder) exist, err := models.GetById(ladder, ladderId) @@ -36,12 +38,19 @@ func (t *LotteryDrawCtl) Start() { "id=": ladderId, }, ladder, "status", "updated_at")) + // 通知直播开始抽奖 + im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStart, + map[string]interface{}{ + "lottery_draw_ladder_id": ladder.Id, + "timestamp": time.Now().Unix(), + }) t.SUCCESS("操作成功") } //停止抽奖 func (t *LotteryDrawCtl) Stop() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") + activityId := t.MustGetInt64("activity_id") ladder := new(models.LotteryDrawRuleLadder) exist, err := models.GetById(ladder, ladderId) @@ -58,6 +67,13 @@ func (t *LotteryDrawCtl) Stop() { "id=": ladderId, }, ladder, "status", "updated_at")) + // 通知直播开始抽奖 + im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStop, + map[string]interface{}{ + "lottery_draw_ladder_id": ladder.Id, + "timestamp": time.Now().Unix(), + }) + t.SUCCESS("操作成功") } @@ -379,6 +395,14 @@ func (t *LotteryDrawCtl) Lottery() { t.CheckErr(err) } + // 通知直播开始抽奖 + im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawResult, + map[string]interface{}{ + "winners": winners, + "lottery_draw_ladder_id": ladder.Id, + "timestamp": time.Now().Unix(), + }) + t.RAW(winners) } diff --git a/controllers/pc/reward.go b/controllers/pc/reward.go index cd4605f..8d3226a 100644 --- a/controllers/pc/reward.go +++ b/controllers/pc/reward.go @@ -3,7 +3,6 @@ package pc import ( "fmt" "hudongzhuanjia/controllers" - "hudongzhuanjia/libs/im" "hudongzhuanjia/models" bully_reward_service "hudongzhuanjia/services/bully_reward" im_service "hudongzhuanjia/services/im" @@ -115,7 +114,7 @@ func (t *RewardCtl) Review() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - go im_service.SendGroupCustomMessage(fmt.Sprint(user.Id), v.ActivityId, im.NoticeReward, + go im_service.SendGroupCustomMessage(user.Id, v.ActivityId, im_service.NoticeReward, map[string]interface{}{ "amount": v.Amount, "content": v.Content, diff --git a/controllers/pc/shake_red_envelope.go b/controllers/pc/shake_red_envelope.go index 082129c..a24d2e2 100644 --- a/controllers/pc/shake_red_envelope.go +++ b/controllers/pc/shake_red_envelope.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" - "hudongzhuanjia/libs/im" "hudongzhuanjia/models" im_service "hudongzhuanjia/services/im" red_envelope_service "hudongzhuanjia/services/red_envelope" @@ -87,7 +86,7 @@ func (t *ShakeRedEnvelopeCtl) Start() { err = red_envelope_service.GenRedEnvelope(activity.Id, activity.RehearsalId, rule) t.CheckErr(err) - go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im.NoticeShakeRedPackStart, + go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackStart, map[string]interface{}{ "customer_id": uid, "shake_red_envelope_rule_id": rule.Id, @@ -116,7 +115,7 @@ func (t *ShakeRedEnvelopeCtl) Stop() { _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus) t.CheckErr(err) - go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im.NoticeShakeRedPackEnd, + go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackEnd, map[string]interface{}{ "customer_id": uid, "shake_red_envelope_rule_id": rule.Id, diff --git a/libs/im/im.go b/libs/im/im.go index a3e4ad7..7a9383a 100644 --- a/libs/im/im.go +++ b/libs/im/im.go @@ -46,7 +46,7 @@ type SendGroupSystemNotificationParam struct { ToMembersAccount []string `json:"ToMembers_Account,omitempty"` } -func SendGroupSystemNotification(groupId string, status NoticeStatus, data map[string]interface{}, members ...string) error { +func SendGroupSystemNotification(groupId string, status int, data map[string]interface{}, members ...string) error { sig, err := GenSig("admin") if err != nil { return err diff --git a/libs/im/status.go b/libs/im/status.go index f3b0bab..9f48406 100644 --- a/libs/im/status.go +++ b/libs/im/status.go @@ -1,11 +1 @@ package im - -// 定义一些通知或者数据状态 -type NoticeStatus int - -const NoticeLiveRedPackStart NoticeStatus = 256 // 通知直播用户红包开始了 -const NoticeLiveRedPackEnd NoticeStatus = 257 // 通知直播用户红包结束了 -const NoticeLiveRedPackGet NoticeStatus = 258 // 某人摇中红包 -const NoticeShakeRedPackStart NoticeStatus = 259 // 通知摇红包开始了 -const NoticeShakeRedPackEnd NoticeStatus = 260 // 通知摇红包结束了 -const NoticeReward NoticeStatus = 261 // 打赏 diff --git a/models/area_store.go b/models/area_store.go index 4f0d658..c7c4bff 100644 --- a/models/area_store.go +++ b/models/area_store.go @@ -15,6 +15,7 @@ type AreaStore struct { Type string `json:"type" xorm:"not null default('') comment('地区类型') VARCHAR(255)"` Address string `json:"address" xorm:"not null default('') comment('地址') VARCHAR(255)"` ActivityId int64 `json:"activity_id" xorm:"not null comment('主活动id') BIGINT(20)"` + CustomerId int64 `json:"customer_id" xorm:"not null default 0 comment('客户id') INT(11)"` IsMainArea bool `json:"is_main_area" xorm:"not null default(0) comment('是否主地区1是') TINYINT(1)"` IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('软删除') TINYINT(1)"` CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` diff --git a/services/im/im.go b/services/im/im.go index 37979bb..2420871 100644 --- a/services/im/im.go +++ b/services/im/im.go @@ -8,10 +8,22 @@ import ( "hudongzhuanjia/logger" "hudongzhuanjia/models" "hudongzhuanjia/utils" - "strconv" ) -func SendNoticeByActivityId(activityId int64, _type im.NoticeStatus, data map[string]interface{}, members ...string) error { +// 定义一些通知或者数据状态 +type NoticeStatus int + +const NoticeLiveRedPackStart NoticeStatus = 256 // 通知直播用户红包开始了 +const NoticeLiveRedPackEnd NoticeStatus = 257 // 通知直播用户红包结束了 +const NoticeLiveRedPackGet NoticeStatus = 258 // 某人摇中红包 +const NoticeShakeRedPackStart NoticeStatus = 259 // 通知摇红包开始了 +const NoticeShakeRedPackEnd NoticeStatus = 260 // 通知摇红包结束了 +const NoticeReward NoticeStatus = 261 // 打赏 +const NoticeLotteryDrawStart = 262 // 抽奖开始 +const NoticeLotteryDrawStop = 263 // 抽奖开始 +const NoticeLotteryDrawResult = 264 // 抽奖结果 + +func SendNoticeByActivityId(activityId int64, _type NoticeStatus, data map[string]interface{}, members ...string) error { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) if err != nil { @@ -21,10 +33,10 @@ func SendNoticeByActivityId(activityId int64, _type im.NoticeStatus, data map[st return errors.New("直播信息不存在") } groupId := fmt.Sprintf("%v%d%d", live.LiveRoomId, live.Id, live.ActivityId) - return im.SendGroupSystemNotification(groupId, _type, data, members...) + return im.SendGroupSystemNotification(groupId, int(_type), data, members...) } -func SendGroupCustomMessage(userId string, activityId int64, _type im.NoticeStatus, data map[string]interface{}) error { +func SendGroupCustomMessage(userId interface{}, activityId int64, _type NoticeStatus, data map[string]interface{}) error { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) if err != nil || !exist { @@ -43,13 +55,13 @@ func SendGroupCustomMessage(userId string, activityId int64, _type im.NoticeStat GroupId: groupId, Random: utils.RandomInt(6), MsgPriority: "High", - FromAccount: userId, + FromAccount: fmt.Sprint(userId), MsgBody: []im.MsgBody{ { MsgType: "TIMCustomElem", MsgContent: im.MsgBodyContent{ Data: string(bs), - Desc: strconv.Itoa(int(_type)), + Desc: fmt.Sprint(_type), Ext: "", Sound: "", }, diff --git a/services/pay/handle.go b/services/pay/handle.go index 49efa5a..56c17a3 100644 --- a/services/pay/handle.go +++ b/services/pay/handle.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" - "hudongzhuanjia/libs/im" "hudongzhuanjia/logger" "hudongzhuanjia/models" im_service "hudongzhuanjia/services/im" @@ -100,7 +99,7 @@ func HandleSuccess(param *OrderDelayQueueParam) (err error) { return } - err = im_service.SendGroupCustomMessage(fmt.Sprint(info.UserId), param.Order.ActivityId, im.NoticeLiveRedPackStart, + 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, diff --git a/test/pay_test.go b/test/pay_test.go index 7e4b67d..743660c 100644 --- a/test/pay_test.go +++ b/test/pay_test.go @@ -54,7 +54,7 @@ func TestTimeExpire(t *testing.T) { func TestSendNotice(t *testing.T) { for { - err := im_service.SendGroupCustomMessage(fmt.Sprint(4), 118, im.NoticeReward, + err := im_service.SendGroupCustomMessage(4, 118, im_service.NoticeReward, map[string]interface{}{ "amount": 20.01, "content": "恭喜你发出",