|
|
@ -5,6 +5,7 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
|
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"live-service/common/nerr"
|
|
|
|
"live-service/common/nerr"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -45,20 +46,22 @@ func (m *customUserIntegralModel) InsertTx(ctx context.Context, tx *gorm.DB, dat
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *customUserIntegralModel) UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, change int64) error {
|
|
|
|
func (m *customUserIntegralModel) UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, integral int64) error {
|
|
|
|
if change < 0 {
|
|
|
|
if integral < 0 {
|
|
|
|
return errors.New("无法将积分更新至负数")
|
|
|
|
return errors.New("无法将积分更新至负数")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db := withTx(ctx, m.conn, tx)
|
|
|
|
db := withTx(ctx, m.conn, tx)
|
|
|
|
|
|
|
|
|
|
|
|
result := db.Table(m.table).
|
|
|
|
result := db.Table(m.table).
|
|
|
|
Where("user_id = ?", userId).
|
|
|
|
Where("`user_id` = ?", userId).
|
|
|
|
Update("integral", change)
|
|
|
|
Update("`integral`", integral)
|
|
|
|
if result.Error != nil {
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO 这里得处理一下
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return ErrRowsAffectedZero
|
|
|
|
logx.Statf("更新积分影响行数为0, user_id: %d, integral: %d", userId, integral)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -67,7 +70,7 @@ func (m *customUserIntegralModel) FindIntegral(ctx context.Context, tx *gorm.DB,
|
|
|
|
var resp int64
|
|
|
|
var resp int64
|
|
|
|
err := withTx(ctx, m.conn, tx).Table(m.table).
|
|
|
|
err := withTx(ctx, m.conn, tx).Table(m.table).
|
|
|
|
Select(fmt.Sprintf("%s.integral", m.table)).
|
|
|
|
Select(fmt.Sprintf("%s.integral", m.table)).
|
|
|
|
Where("user_id = ?", userId).Take(&resp).Error
|
|
|
|
Where("`user_id` = ?", userId).Take(&resp).Error
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
return resp, nil
|
|
|
|