feat:调整数据库Id生成
This commit is contained in:
parent
d2541661e9
commit
d4fa337e3f
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"database/sql"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
|
@ -27,6 +28,7 @@ func (m *AppRepo) AppList(conn builder.Cond, pageFilter entities.PageRequest, ap
|
|||
}
|
||||
|
||||
func (m *AppRepo) AppInsertOne(app *appmodel.App) (int64, error) {
|
||||
app.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(app)
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1,55 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/merchantmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"database/sql"
|
||||
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type MerchantRepo struct {
|
||||
repo xorm.Interface
|
||||
}
|
||||
|
||||
func NewMerchantRepo(repo xorm.Interface) *MerchantRepo {
|
||||
return &MerchantRepo{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantList(conn builder.Cond, pageFilter entities.PageRequest, merchantList *[]merchantmodel.Merchant) (int64, error) {
|
||||
repo := m.repo.Where(conn)
|
||||
if pageFilter.Page > 0 {
|
||||
repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1))
|
||||
}
|
||||
return repo.Desc("create_time").FindAndCount(merchantList)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantInsertOne(merchant *merchantmodel.Merchant) (int64, error) {
|
||||
merchant.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantDelete(merchant *merchantmodel.Merchant, conn builder.Cond) (int64, error) {
|
||||
return m.repo.Where(conn).Delete(merchant)
|
||||
}
|
||||
|
||||
// columns 参数为要更新的字段
|
||||
func (m *MerchantRepo) MerchantUpdate(merchant *merchantmodel.Merchant, conn builder.Cond, columns ...string) (int64, error) {
|
||||
return m.repo.Where(conn).MustCols(columns...).Update(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantDetail(merchant *merchantmodel.Merchant, conn builder.Cond) (bool, error) {
|
||||
return m.repo.Where(conn).Get(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantFindOne(merchant *merchantmodel.Merchant, conn builder.Cond, columns ...string) (*merchantmodel.Merchant, error) {
|
||||
has, err := m.repo.Where(conn).Get(merchant)
|
||||
if !has {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return merchant, err
|
||||
}
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/merchantmodel"
|
||||
"database/sql"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type MerchantRepo struct {
|
||||
repo xorm.Interface
|
||||
}
|
||||
|
||||
func NewMerchantRepo(repo xorm.Interface) *MerchantRepo {
|
||||
return &MerchantRepo{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantList(conn builder.Cond, pageFilter entities.PageRequest, merchantList *[]merchantmodel.Merchant) (int64, error) {
|
||||
repo := m.repo.Where(conn)
|
||||
if pageFilter.Page > 0 {
|
||||
repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1))
|
||||
}
|
||||
return repo.Desc("create_time").FindAndCount(merchantList)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantInsertOne(merchant *merchantmodel.Merchant) (int64, error) {
|
||||
return m.repo.InsertOne(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantDelete(merchant *merchantmodel.Merchant, conn builder.Cond) (int64, error) {
|
||||
return m.repo.Where(conn).Delete(merchant)
|
||||
}
|
||||
|
||||
// columns 参数为要更新的字段
|
||||
func (m *MerchantRepo) MerchantUpdate(merchant *merchantmodel.Merchant, conn builder.Cond, columns ...string) (int64, error) {
|
||||
return m.repo.Where(conn).MustCols(columns...).Update(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantDetail(merchant *merchantmodel.Merchant, conn builder.Cond) (bool, error) {
|
||||
return m.repo.Where(conn).Get(merchant)
|
||||
}
|
||||
|
||||
func (m *MerchantRepo) MerchantFindOne(merchant *merchantmodel.Merchant, conn builder.Cond, columns ...string) (*merchantmodel.Merchant, error) {
|
||||
has, err := m.repo.Where(conn).Get(merchant)
|
||||
if !has {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return merchant, err
|
||||
}
|
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/ordercallbacklogmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
@ -26,6 +27,7 @@ func (m *OrderCallbackLogRepo) OrderCallbackLogList(conn builder.Cond, pageFilte
|
|||
}
|
||||
|
||||
func (m *OrderCallbackLogRepo) OrderCallbackLogInsertOne(orderLog *ordercallbacklogmodel.OrderCallbackLog) (int64, error) {
|
||||
orderLog.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(orderLog)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/orderrequestlogmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
@ -26,6 +27,7 @@ func (m *OrderRequestLogRepo) OrderRequestLogList(conn builder.Cond, pageFilter
|
|||
}
|
||||
|
||||
func (m *OrderRequestLogRepo) OrderRequestLogInsertOne(orderLog *orderrequestlogmodel.OrderRequestLog) (int64, error) {
|
||||
orderLog.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(orderLog)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/orderthirdpaylogmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
@ -26,6 +27,7 @@ func (m *OrderThirdPayLogRepo) OrderThirdPayLogList(conn builder.Cond, pageFilte
|
|||
}
|
||||
|
||||
func (m *OrderThirdPayLogRepo) OrderThirdPayLogInsertOne(orderLog *orderthirdpaylogmodel.OrderThirdPayLog) (int64, error) {
|
||||
orderLog.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(orderLog)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/ordersmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"database/sql"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
|
@ -27,6 +28,7 @@ func (m *OrderRepo) OrderList(conn builder.Cond, pageFilter entities.PageRequest
|
|||
}
|
||||
|
||||
func (m *OrderRepo) OrderInsertOne(order *ordersmodel.Orders) (int64, error) {
|
||||
order.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(order)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package data
|
|||
import (
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/models/paychannelmodel"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"database/sql"
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm"
|
||||
|
@ -27,6 +28,7 @@ func (m *PayChannelRepo) PayChannelList(conn builder.Cond, pageFilter entities.P
|
|||
}
|
||||
|
||||
func (m *PayChannelRepo) PayChannelInsertOne(payChannel *paychannelmodel.PayChannel) (int64, error) {
|
||||
payChannel.Id = snowflake.GetID()
|
||||
return m.repo.InsertOne(payChannel)
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ var (
|
|||
|
||||
// 实体
|
||||
type OrderCallbackLog struct {
|
||||
Id int64 `xorm:"'id' bigint(20) pk autoincr"`
|
||||
Id int64
|
||||
OrderId int64 `xorm:"'order_id' bigint(20)"`
|
||||
MerchantRequest string `xorm:"'merchant_request' JSON"`
|
||||
Status int `xorm:"'status' int(11)"`
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -85,10 +84,11 @@ func PaymentService(c context.Context, payOrderRequest PayOrderRequest) PayOrder
|
|||
// 记录日志
|
||||
go func() {
|
||||
orderId := payOrderRequest.OrderId
|
||||
payCallback := fmt.Sprintf("{%s}", info)
|
||||
payCallback, _ := json.Marshal(info)
|
||||
//payCallback := fmt.Sprintf("{%s}", info)
|
||||
payParam, _ := json.Marshal(payOrderRequest)
|
||||
merchantCallback, _ := json.Marshal(payOrderResponse)
|
||||
SaveLog(orderId, common.THIRD_ORDER_TYPE_PAY, payCallback, string(payParam), string(merchantCallback), logStatus)
|
||||
SaveLog(orderId, common.THIRD_ORDER_TYPE_PAY, string(payCallback), string(payParam), string(merchantCallback), logStatus)
|
||||
}()
|
||||
return payOrderResponse
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package snowflake
|
||||
|
||||
import (
|
||||
sf "github.com/go-pay/util/snowflake"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var once sync.Once
|
||||
var snowflake *sf.Node
|
||||
|
||||
func InitSnowflake(workerID int64) (err error) {
|
||||
// 创建雪花ID生成器
|
||||
if snowflake == nil {
|
||||
once.Do(func() {
|
||||
snowflake, err = sf.NewNode(workerID)
|
||||
})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func GetID() int64 {
|
||||
if snowflake == nil {
|
||||
return 0
|
||||
}
|
||||
return snowflake.Generate().Int64()
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package snowflake
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInitSnowflake(t *testing.T) {
|
||||
err := InitSnowflake(1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(GetID())
|
||||
}
|
|
@ -3,6 +3,7 @@ package bootstrap
|
|||
import (
|
||||
"PaymentCenter/app/jobs"
|
||||
"PaymentCenter/app/jobs/basejob"
|
||||
"PaymentCenter/app/utils/snowflake"
|
||||
"PaymentCenter/config"
|
||||
"github.com/qit-team/snow-core/db"
|
||||
"github.com/qit-team/snow-core/kernel/close"
|
||||
|
@ -74,6 +75,12 @@ func Bootstrap(conf *config.Config) (err error) {
|
|||
//
|
||||
//utils.Log(nil, "nacos err", err)
|
||||
|
||||
// 目前使用单机,后续分布式需要改
|
||||
err = snowflake.InitSnowflake(1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//注册job register,为了非job模式的消息入队调用
|
||||
basejob.SetJobRegister(jobs.RegisterWorker)
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue