|
|
package pc
import ( "fmt" "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"
"github.com/ouxuanserver/osmanthuswine/src/core" )
type ShakeRedEnvelopeCtl struct { controllers.AuthorCtl }
func (t *ShakeRedEnvelopeCtl) Ready() { ruleId := t.MustGetInt("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)
sids := make([]int, 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.MustGetInt("shake_red_envelope_rule_id") uid := t.GetAccountId()
//根据摇红包规则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, activity.ArchId) 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.MustGetInt("shake_red_envelope_rule_id") uid := t.GetAccountId()
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.MustGetInt("activity_id") customerId := t.GetAccountId()
area := new(models.AreaStore) exist, err := area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_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([]int, 0) for _, v := range list { aIds = append(aIds, v.Id) }
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([]int, 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.MustGetInt("activity_id") customerId := t.GetAccountId() rehearsalId := t.MustGetInt("rehearsal_id") ruleId := t.MustGetInt("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.MustGetInt("shake_red_envelope_rule_id")
rule := &models.ShakeRedEnvelopeRule{} exist, err := models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
redEnvelope := &models.ShakeRedEnvelopeActivity{} exist, err = models.Get(redEnvelope, rule.ShakeRedEnvelopeActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包活动不存在")
activity := &models.Activity{} exist, err = models.Get(activity, redEnvelope.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
records := make([]*ShakeRedEnvelopeRecordAndUser, 0) err = core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r"). Join("LEFT", models.Alias(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=? and r.arch_id=?", ruleId, activity.RehearsalId, activity.ArchId).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.MustGetInt("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, "摇红包轮次不存在")
redEnvelope := &models.ShakeRedEnvelopeActivity{} exist, err = models.Get(redEnvelope, rule.ShakeRedEnvelopeActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包活动不存在")
activity := &models.Activity{} exist, err = models.Get(activity, redEnvelope.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=? and arch_id=?", ruleId, activity.RehearsalId, activity.ArchId).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=? and arch_id=?", ruleId, activity.RehearsalId, activity.ArchId). Count(new(models.ShakeRedEnvelopeRecord)) t.CheckErr(err)
t.JSON(map[string]interface{}{ "red_envelope_num": numR, "red_envelope_user": numU, }) }
|