package client import ( "fmt" "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" pay_service "hudongzhuanjia/services/pay" ws_send_service "hudongzhuanjia/services/ws_send" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "math/rand" "time" ) type ShakeRedEnvelopeCtl struct { controllers.AuthorCtl } func (t *ShakeRedEnvelopeCtl) Status() { 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_SHAKERB_RECORD_NOT_EXIST, "摇红包规则不存在") t.JSON(map[string]interface{}{ "status": rule.ShakeRedEnvelopeStatus, }) } func (t *ShakeRedEnvelopeCtl) Shake() { ruleId := t.MustGetInt64("shake_red_envelope_rule_id") userId := t.MustGetUID() areaId := t.MustGetAreaId() // 查询摇红包规则 rule := new(models.ShakeRedEnvelopeRule) exist, err := models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "摇红包规则不存在") t.CheckRunning(rule.ShakeRedEnvelopeStatus) // 查询摇红包活动主id, is_pay=1给钱了,给摇 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) area := models.AreaStore{} exist, err = models.Get(&area, areaId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") moduleService, exist, err := activity_service.GetModuleService(define.MODULE_SHAKRB, activity.Id) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在") record := new(models.ShakeRedEnvelopeRecord) /// 之后使用, 存入乐观锁 if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { // 剔除摇过红包的用户 exist, err = record.ExistRecord(activity.RehearsalId, rule.Id, userId) t.CheckErr(err) t.Assert(!exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "您已经摇过红包了,请等待下一轮.") // 不存在继续往下走 } // done: 增加概率, 修改概率 r := rand.New(rand.NewSource(time.Now().UnixNano())) t.Assert(r.Float64()*100 <= rule.Probability, code.MSG_SHAKERB_RECORD_NOT_HIT, "您与红包擦肩而过") // 必须在红包概率以内才能获取 // 增加session 或者乐观锁 exist, err = record.GetByRuleId(ruleId, activity.RehearsalId, 0) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包已经派发完毕") // 查询用户 user := new(models.User) exist, err = models.Get(user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") record.IsDraw = 0 record.UserId = userId record.AreaId = areaId row, err := models.Update(record.Id, record, "is_draw", "user_id", "area_id") t.CheckErr(err) t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "您与红包擦肩而过") // 那么 row == 1 为已经成功抢到 // 记录红包 ---> 非彩排才能 if activity.RehearsalId == 0 { result, err := pay_service.SendRedPack("欧轩互动", user.Openid, "红包只会越抢越多", "直播抢红包活动", "抢的多,赚得多", int(record.Amount*100), 1, 2) t.CheckErr(err) record.MchBillno = result.MchBillno models.Update(record.Id, record, "mch_billno") } // 通知大屏 total, err := new(models.ShakeRedEnvelopeRecord).Total(activity.Id, activity.RehearsalId, envelope.Id, rule.Id) remaining, err := new(models.ShakeRedEnvelopeRecord).Count(activity.Id, activity.RehearsalId, envelope.Id, rule.Id, -1) t.CheckErr(err) go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id), define.TYPE_H5USER, userId, map[string]interface{}{ "customer_id": area.CustomerId, "user_id": user.Id, "type": "shake_rb", "data": map[string]interface{}{ "amount": record.Amount, "nickname": user.Nickname, "avatar": user.Avatar, "total": total - remaining, // "remaining": remaining, // }, }) go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id), define.TYPE_CUSTOMER, area.CustomerId, map[string]interface{}{ "customer_id": area.CustomerId, "user_id": user.Id, "type": "shake_rb", "data": map[string]interface{}{ "amount": record.Amount, "nickname": user.Nickname, "avatar": user.Avatar, "total": total - remaining, // "remaining": remaining, // }, }) t.JSON(map[string]interface{}{ "amount": record.Amount, "shake_red_envelope_record_id": record.Id, }) } // 用户红包 func (t *ShakeRedEnvelopeCtl) UserRedEnvelopes() { uid := t.MustGetUID() activityId := t.MustGetInt64("activity_id") activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "活动不存在") records, err := models.GetRedEnvelopesByUserId(uid, activityId, activity.RehearsalId) t.CheckErr(err) t.JSON(map[string]interface{}{ "list": records, "total": len(records), }) }