Browse Source

发送红包

master
黄梓健 5 years ago
parent
commit
4760714523
  1. 22
      controllers/client/live.go
  2. 39
      logger/logger.go
  3. 18
      models/shake_red_envelope_record.go
  4. 15
      services/pay/transfer.go

22
controllers/client/live.go

@ -10,6 +10,7 @@ import (
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"strings"
"time"
)
@ -158,6 +159,7 @@ func (t *LiveCtl) SendLiveRedPack() {
record.LiveRedEnvelopeRuleId = rule.Id
record.Name = user.Nickname + "发红包"
record.UserId = 0
record.IsDraw = -1 // 未被提现
record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2)
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
@ -200,16 +202,28 @@ func (t *LiveCtl) GetLiveRedPack() {
// 乐观锁 ==> 防止并发
redPack.UserId = user.Id
redPack.TransferType = 1
redPack.PartnerTradeNo = utils.RandomStr(32)
row, err := redPack.UpdateAllColsById(redPack.Id)
redPack.IsDraw = 0
row, err := redPack.UpdateById(redPack.Id, "version, user_id, is_draw")
t.CheckErr(err)
if row != 1 {
t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT)
return
}
//go pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60)
addr := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
result, err := pay_service.SendRedPack("欧轩互动", user.Openid, rule.Prompt, addr[0], "直播抢红包活动",
"抢的多,赚得多", int(redPack.Amount*100), 1, 2)
if err != nil {
redPack.UserId = 0
redPack.IsDraw = -1
redPack.Version += 1
redPack.UpdateById(redPack.Id, "version, user_id, is_draw")
t.ERROR("红包已被领完", code.MSG_SHAKERB_RECORD_NOT_HIT)
return
}
redPack.MchBillno = result.MchBillno
redPack.Version += 1
redPack.UpdateById(redPack.Id, "version, mch_billno")
t.JSON(redPack)
}

39
logger/logger.go

@ -8,8 +8,8 @@ import (
"time"
)
var Sugar *zap.SugaredLogger
var logger *zap.Logger
//var Sugar *zap.SugaredLogger
var logger *zap.SugaredLogger
func init() {
InitZap()
@ -25,8 +25,7 @@ func InitZap() {
zapcore.NewConsoleEncoder(NewEncoderConfig()),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), w),
zap.DebugLevel)
logger = zap.New(core, zap.AddCaller())
Sugar = logger.Sugar()
logger = zap.New(core, zap.AddCaller()).Sugar()
}
func NewEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
@ -68,34 +67,22 @@ func level(level string) zapcore.Level {
}
}
func Info(msg string, fields ...zap.Field) {
logger.Info(msg, fields...)
func Info(args ...interface{}) {
logger.Info(args...)
}
func Debug(msg string, fields ...zap.Field) {
logger.Debug(msg, fields...)
func Debug(args ...interface{}) {
logger.Debug(args...)
}
func Panic(msg string, fields ...zap.Field) {
logger.Debug(msg, fields...)
func Panic(args ...interface{}) {
logger.Panic(args...)
}
func Warn(msg string, fields ...zap.Field) {
logger.Warn(msg, fields...)
func Warn(args ...interface{}) {
logger.Warn(args...)
}
func DPanic(msg string, fields ...zap.Field) {
logger.DPanic(msg, fields...)
}
func Error(msg string, fields ...zap.Field) {
logger.Error(msg, fields...)
}
func With(fields ...zap.Field) {
logger.With(fields...)
}
func WithOptions(opts ...zap.Option) {
logger.WithOptions(opts...)
func Error(args ...interface{}) {
logger.Error(args...)
}

18
models/shake_red_envelope_record.go

@ -24,15 +24,13 @@ type ShakeRedEnvelopeRecord struct {
AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(18)"`
Name string `json:"name" xorm:"not null default '' comment('红包名字') VARCHAR(255)"`
UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
Amount float64 `json:"amount" xorm:"not null default '0' comment('金额') DECIMAL(18)"`
IsDraw int `json:"is_draw" xorm:"not null default(0) comment('-1未被摇中,0已被摇中,1已被提现,2提现失败')"`
TransferType int `json:"transfer_type" xorm:"not null default 0 comment('0微信红包 1 微信转账') TINYINT(1)"`
PartnerTradeNo string `json:"partner_trade_no" xorm:"not null default '' comment('转账账单no') VARCHAR(128)"`
Amount float64 `json:"amount" xorm:"not null default 0.00 comment('金额') DECIMAL(18)"`
IsValid bool `json:"is_valid,omitempty" xorm:"-"`
IsDraw int `json:"is_draw" xorm:"not null default 0 comment('-1未被摇中,0已被摇中')"`
MchBillno string `json:"mch_billno" xorm:"not null default"`
Version int `json:"version" xorm:"not null version comment('乐观锁') INT(11)"`
// 无关变量
// Status string `json:"status" xorm:"not null default '' comment('红包状态') VARCHAR(16)"`
IsValid bool `json:"is_valid,omitempty" xorm:"-"`
}
func (t *ShakeRedEnvelopeRecord) TableName() string {
@ -71,8 +69,8 @@ func (t *ShakeRedEnvelopeRecord) GetByRuleId(ruleId, rehearsalId int64) (bool, e
ruleId, rehearsalId).Get(t)
}
func (t *ShakeRedEnvelopeRecord) UpdateAllColsById(id int64) (int64, error) {
return core.GetXormAuto().Where("is_delete=0 and id=?", id).AllCols().Update(t)
func (t *ShakeRedEnvelopeRecord) UpdateById(id int64, field ...string) (int64, error) {
return core.GetXormAuto().Where("is_delete=0 and id=?", id).Cols(field...).Update(t)
}
func GetRedEnvelopesByUserId(userId int64) ([]*ShakeRedEnvelopeRecord, error) {
@ -86,6 +84,6 @@ func GetRedEnvelopesByUserId(userId int64) ([]*ShakeRedEnvelopeRecord, error) {
}
func (t *ShakeRedEnvelopeRecord) GetLiveRedPackRecord(id, rehearsalId interface{}) (bool, error) {
return core.GetXormAuto().Where("is_delete=0 and red_envelope_type = 1 and user_id = 0 and "+
"live_red_envelope_rule_id=? and rehearsal_id=?", id, rehearsalId).Get(t)
return core.GetXormAuto().Where("is_delete=0 and red_envelope_type = 1 and user_id = 0 "+
"and is_draw=0 and live_red_envelope_rule_id=? and rehearsal_id=?", id, rehearsalId).Get(t)
}

15
services/pay/transfer.go

@ -268,6 +268,7 @@ type RedPackResult struct {
func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmount, totalNum, scene int) (*RedPackResult, error) {
client, err := Client()
if err != nil {
logger.Error("client init error: %v", err)
return nil, err
}
@ -301,20 +302,6 @@ func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmo
SendListid: m["send_listid"],
}
userRedPack := new(models.UserRedPack)
userRedPack.MchBillno = mchBillNo
userRedPack.ReOpenid = openId
userRedPack.SceneId = scene
userRedPack.SendListid = res.SendListid
userRedPack.TotalAmount = totalAmount
userRedPack.TotalNum = totalNum
userRedPack.IsDelete = false
userRedPack.UpdatedAt = time.Now()
userRedPack.CreatedAt = time.Now()
_, err = userRedPack.AddRedPack()
if err != nil {
return nil, err
}
return res, nil
}

Loading…
Cancel
Save