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" "strings" "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.GetById(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "摇红包规则不存在") t.JSON(map[string]interface{}{ "status": rule.ShakeRedEnvelopeStatus, }) } // todo:调用支付接口 func (t *ShakeRedEnvelopeCtl) Shake() { customerId := t.MustGetInt64("customer_id") ruleId := t.MustGetInt64("shake_red_envelope_rule_id") userId := t.MustGetUID() // 查询摇红包规则 rule := new(models.ShakeRedEnvelopeRule) exist, err := models.GetById(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "摇红包规则不存在") t.CheckRunning(rule.ShakeRedEnvelopeStatus) area := new(models.AreaStore) exist, err = area.GetAreaStoreById(t.MustGetAreaId()) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在") // 查询摇红包活动主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.GetById(activity, envelope.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) 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, activity.Id, rule.Id, envelope.Id, userId) t.CheckErr(err) t.Assert(!exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "您已经摇过红包了,请等待下一轮.") // 不存在继续往下走 } // done: 增加概率, 修改概率 r := rand.New(rand.NewSource(time.Now().UnixNano())) t.Assert(r.Float64()*100 <= rule.Probability, code.MSG_ERR, "您与红包擦肩而过") // 必须在红包概率以内才能获取 // 增加session 或者乐观锁 exist, err = record.GetByRuleId(ruleId, activity.RehearsalId) t.CheckErr(err) t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "红包已经派发完毕") // 查询用户 user := new(models.User) exist, err = models.GetById(user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") record.IsDraw = 0 // 记录红包 ---> 非彩排才能 if activity.RehearsalId == 0 { //res, err := pay_service.SendRedPack("欧轩互动", user.Openid, fmt.Sprintf("感谢您参加%s", activity.Name), // "", rule.Model, "摇得越快抢得越多", int(record.Amount*100), 1, 1) ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") res, err := pay_service.Transfer("欧轩互动-红包活动", ip[0], user.Openid, int(record.Amount*100)) if err != nil { t.ERROR("您与红包擦肩而过", code.MSG_SHAKERB_NOT_HIT) return } record.TransferType = 1 record.TransferNo = res.PartnerTradeNo record.IsDraw = 1 } record.UserId = user.Id record.AreaName = area.Name record.Name = activity.Name row, err := record.UpdateAllColsById(record.Id) t.CheckErr(err) t.Assert(row == 1, code.MSG_ERR, "您与红包擦肩而过") // 那么 row == 1 为已经成功抢到 // 通知大屏 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) customer := new(models.Customer) exist, err = models.GetById(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id), define.TYPE_H5USER, userId, map[string]interface{}{ "customer_id": customer.Id, "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, customerId, map[string]interface{}{ "customer_id": customer.Id, "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() { records, err := models.GetRedEnvelopesByUserId(t.MustGetUID()) t.CheckErr(err) t.JSON(map[string]interface{}{ "list": records, "total": len(records), }) }