package model

import "time"

const (
	SyncTypeUserCreate      = "user_create"
	SyncTypeQuotaChange     = "quota_change"
	SyncTypePreConsumeQuery = "pre_consume_query"
	SyncTypeBatchSync       = "batch_sync"
	SyncTypeManualSync      = "manual_sync"
	SyncTypeStartupSync     = "startup_sync"
	SyncTypeSettle          = "settle"
)

const (
	SyncDirectionCnToOv = "cn_to_ov"
	SyncDirectionOvToCn = "ov_to_cn"
)

const (
	SyncStatusSuccess = "success"
	SyncStatusFailed  = "failed"
)

// QuotaSyncLog 余额同步日志
type QuotaSyncLog struct {
	Id           int    `json:"id" gorm:"primaryKey"`
	UserId       int    `json:"user_id" gorm:"index:idx_user_id"`
	RemoteUserId int    `json:"remote_user_id" gorm:"index"`
	SyncType     string `json:"sync_type" gorm:"size:32;index:idx_sync_type"`
	BeforeQuota  int    `json:"before_quota"`
	AfterQuota   int    `json:"after_quota"`
	ChangeAmount int    `json:"change_amount"`
	MasterQuota  int    `json:"master_quota"`
	PendingQuota int    `json:"pending_quota"`
	RequestId    string `json:"request_id"`
	Model        string `json:"model"`
	Direction    string `json:"direction" gorm:"size:16"`
	Status       string `json:"status" gorm:"size:16;default:'success';index:idx_status"`
	ErrorMsg     string `json:"error_msg"`
	CreatedAt    int64  `json:"created_at" gorm:"index:idx_created_at"`
	SyncedAt     int64  `json:"synced_at"`
}

func (QuotaSyncLog) TableName() string {
	return "quota_sync_logs"
}

// CreateSyncLog 创建同步日志
func CreateSyncLog(log *QuotaSyncLog) error {
	if log.CreatedAt == 0 {
		log.CreatedAt = time.Now().Unix()
	}
	return DB.Create(log).Error
}
