|
|
package client
import ( "fmt" "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" "hudongzhuanjia/utils/code" "time" )
type NoticeRedPackEvent struct { OutTradeNo string `json:"out_trade_no"` Prompt string `json:"prompt"` RedPackInfoId int64 `json:"red_pack_info_id"` ActivityId int64 `json:"activity_id"` Status int `json:"status"` }
type LiveCtl struct { controllers.AuthorCtl }
// 详情
func (t *LiveCtl) Detail() { activityId := t.MustGetInt64("activity_id") areaId, exist := t.GetInt64("area_id")
if !exist { // 假设不存地区,那就设置为主地区
area := new(models.AreaStore) exist, err := area.GetMainAreaById(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") areaId = area.Id }
userId := t.MustGetUID()
err := new(models.LiveViewer).Record(userId, activityId) t.CheckErr(err)
live := new(models.LiveConfig) exist, err = live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
config := new(models.LiveConfigArea) exist, err = config.GetByActivityIdAndAreaId(activityId, areaId) t.CheckErr(err) if exist { live.SharePosterImg = config.MergeSharePoster }
live.AdminLiveUrl = "" live.AreaId = areaId t.JSON(live) }
func (t *LiveCtl) Like() { activityId := t.MustGetInt64("activity_id") //userId := t.MustGetUID()
_, err := new(models.LiveConfig).Like(activityId) t.CheckErr(err)
live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在") t.JSON(map[string]interface{}{ "like": live.LikeNum, "watch": live.WatchNum, }) }
func (t *LiveCtl) LoopQuery() { activityId := t.MustGetInt64("activity_id") live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在") t.JSON(map[string]interface{}{ "like": live.LikeNum, "watch": live.WatchNum, }) }
// 下单发送红包
// 维护一个队列进行循环, 遍历是否付款成功
func (t *LiveCtl) SendLiveRedPack() { userId := t.MustGetUID() // 用户 uid
activityId := t.MustGetInt64("activity_id") // activity_id
num := t.MustGetInt("num") // 红包数量
prompt := t.MustGet("prompt") // 提示
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) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
area := new(models.AreaStore) exist, err = models.GetById(area, areaId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
user := models.User{} exist, err = models.GetById(&user, userId) 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) }
rule := new(models.LiveRedEnvelopeRule) rule.OutTradeNo = res["out_trade_no"].(string) // 彩排为空
rule.ActivityId = activityId rule.RehearsalId = activity.RehearsalId rule.AreaId = areaId 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 = 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) record.ActivityId = activityId record.RehearsalId = activity.RehearsalId record.AreaId = area.Id record.AreaName = area.Name record.RedEnvelopeType = 1 record.LiveRedEnvelopeRuleId = rule.Id record.Name = user.Nickname + "发红包" record.UserId = 0 record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2) record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err = models.Add(record) t.CheckErr(err) }
res["rehearsal_id"] = activity.RehearsalId res["live_red_envelope_rule_id"] = rule.Id t.JSON(res) }
// 领取红包
func (t *LiveCtl) GetLiveRedPack() { ruleId := t.MustGetInt64("live_red_envelope_rule_id") userId := t.MustGetUID()
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) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
redPack := new(models.ShakeRedEnvelopeRecord) exist, err = redPack.GetLiveRedPackRecord(ruleId) t.CheckErr(err) if !exist { // 通知其他的人
t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT) return }
// 乐观锁 ==> 防止并发
redPack.UserId = user.Id redPack.TransferType = 1 redPack.PartnerTradeNo = utils.RandomStr(32) row, err := redPack.UpdateAllColsById(redPack.Id) t.CheckErr(err) if row != 1 { t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT) return }
if rule.RehearsalId == 0 { // 非彩排
go pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60) }
t.JSON(redPack) }
|