互动
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.

284 lines
9.0 KiB

package pc
import (
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
im_service "hudongzhuanjia/services/im"
red_envelope_service "hudongzhuanjia/services/red_envelope"
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"time"
)
type ShakeRedEnvelopeCtl struct {
controllers.AuthorCtl
}
func (t *ShakeRedEnvelopeCtl) Ready() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
rule := new(models.ShakeRedEnvelopeRule)
exist, err := models.Get(rule, ruleId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在")
if rule.ShakeRedEnvelopeStatus != define.StatusNotBegin {
t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR)
}
envelope := new(models.ShakeRedEnvelopeActivity)
exist, err = models.Get(envelope, rule.ShakeRedEnvelopeActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
activity := new(models.Activity)
exist, err = models.Get(activity, envelope.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
// done: 把其他的准备中的状态改为未开始
// 又一个坑, 红包的 轮次根据 activity_id
sids := make([]int64, 0)
err = core.GetXormAuto().Table(new(models.ShakeRedEnvelopeActivity)).Select("id").
Where("is_delete=0 and activity_id=?", activity.Id).Find(&sids)
t.CheckErr(err)
_, err = rule.UpdateToStatusBySids(sids, define.StatusReady, define.StatusNotBegin)
t.CheckErr(err)
_, err = rule.UpdateStatus(ruleId, define.StatusReady)
t.CheckErr(err)
t.SUCCESS("success")
}
//开始摇红包
func (t *ShakeRedEnvelopeCtl) Start() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
uid := t.MustGetUID()
//根据摇红包规则id查找该条记录并且更给状态
rule := new(models.ShakeRedEnvelopeRule)
exist, err := models.Get(rule, ruleId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在")
envelope := new(models.ShakeRedEnvelopeActivity)
exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
activity := new(models.Activity)
exist, err = models.Get(activity, envelope.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
if rule.ShakeRedEnvelopeStatus != define.StatusReady {
t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR)
return
}
_, err = rule.UpdateStatus(ruleId, define.StatusRunning)
t.CheckErr(err)
err = red_envelope_service.GenRedEnvelope(activity.Id, activity.Name, activity.RehearsalId, rule)
t.CheckErr(err)
// 彩排不用发送到直播间
if activity.RehearsalId == 0 {
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activity.Id)
t.CheckErr(err)
if exist && live.HaveModule(define.MODULE_SHAKRB) {
go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackStart,
map[string]interface{}{
"customer_id": uid,
"shake_red_envelope_rule_id": rule.Id,
"timestamp": time.Now().Unix(),
})
}
}
t.SUCCESS("success")
}
//停止摇红包
func (t *ShakeRedEnvelopeCtl) Stop() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
uid := t.MustGetUID()
rule := new(models.ShakeRedEnvelopeRule)
exist, err := models.Get(rule, ruleId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
envelope := new(models.ShakeRedEnvelopeActivity)
exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
activity := new(models.Activity)
exist, err = models.Get(activity, envelope.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
rule.ShakeRedEnvelopeStatus = define.StatusEnding
_, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
t.CheckErr(err)
if activity.RehearsalId == 0 {
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activity.Id)
t.CheckErr(err)
if exist && live.HaveModule(define.MODULE_SHAKRB) {
go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackEnd,
map[string]interface{}{
"customer_id": uid,
"shake_red_envelope_rule_id": rule.Id,
"timestamp": time.Now().Unix(),
})
}
}
t.SUCCESS("success")
}
func (t *ShakeRedEnvelopeCtl) List() {
activityId := t.MustGetInt64("activity_id")
customerId := t.MustGetUID()
customer := new(models.Customer)
exist, err := models.Get(customer, customerId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
activity := new(models.Activity)
exist, err = models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
list := make([]*models.ShakeRedEnvelopeActivity, 0)
total, err := core.GetXormAuto().Where("is_delete=0 and is_pay=1 and activity_id=?", activityId).FindAndCount(&list)
t.CheckErr(err)
aIds := make([]int64, 0)
for _, v := range list {
aIds = append(aIds, v.Id)
}
area := new(models.AreaStore)
if customer.Pid == 0 {
exist, err = area.GetMainAreaById(activityId)
} else {
exist, err = area.GetAreaStoreById(t.MustGetAreaId())
}
t.CheckErr(err)
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
rules := make([]*models.ShakeRedEnvelopeRule, 0)
err = core.GetXormAuto().Where("is_delete=0 and area_id=?", area.Id).In("shake_red_envelope_activity_id", aIds).Find(&rules)
t.CheckErr(err)
rIds := make([]int64, 0)
for _, v := range rules {
rIds = append(rIds, v.Id)
}
ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
core.GetXormAuto().Where("is_delete=0").In("shake_red_envelope_rule_id", rIds).Find(&ladders)
for index := range rules {
for i := range ladders {
if rules[index].Id == ladders[i].ShakeRedEnvelopeRuleId {
rules[index].ShakeRedEnvelopeRuleLadders = append(rules[index].ShakeRedEnvelopeRuleLadders, ladders[i])
}
}
}
for index := range list {
for i := range rules {
if list[index].Id == rules[i].ShakeRedEnvelopeActivityId {
list[index].ShakeRedEnvelopeRule = rules[i]
break
}
}
}
for index := range rules {
rules[index].Des = "根据对应得概率获得红包"
}
t.JSON(map[string]interface{}{
"total": total,
"list": list,
})
}
//二维码
func (t *ShakeRedEnvelopeCtl) Qrcode() {
activityId := t.MustGetInt64("activity_id")
customerId := t.MustGetUID()
rehearsalId := t.MustGetInt64("rehearsal_id")
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
area := new(models.AreaStore)
exist, err := area.GetByCustomerId(customerId, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
//将服务器得地址和activity_id,动态生成二维码
qrcode, err := utils.GenH5Qrcode(define.H5ShakeRb, map[string]interface{}{
"activity_id": activityId,
"area_id": area.Id,
"customer_id": customerId,
"rule_id": ruleId,
"rehearsal_id": rehearsalId,
})
t.CheckErr(err)
t.JSON(map[string]interface{}{
"qrcode": qrcode,
})
}
type ShakeRedEnvelopeRecordAndUser struct {
models.ShakeRedEnvelopeRecord `xorm:"extends"`
User *models.User `json:"user" xorm:"extends"`
}
// 参数优化
func (t *ShakeRedEnvelopeCtl) Winners() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
err := core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
Join("LEFT", models.AliasTableName(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=?",
ruleId, rehearsalId).OrderBy("r.created_at desc").Find(&records)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"total": len(records),
"winners": records,
})
}
// 统计摇红包参与人数和剩余红包数量
func (t *ShakeRedEnvelopeCtl) Count() {
ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
rule := new(models.ShakeRedEnvelopeRule)
exist, err := models.Get(rule, ruleId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包轮次不存在")
numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=?",
ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeUser))
t.CheckErr(err)
numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeRecord))
t.CheckErr(err)
t.JSON(map[string]interface{}{
"red_envelope_num": numR,
"red_envelope_user": numU,
})
}