diff --git a/internal/model/card_wallet.go b/internal/model/asset_wallet.go similarity index 85% rename from internal/model/card_wallet.go rename to internal/model/asset_wallet.go index 7bdbc3f..97aad5e 100644 --- a/internal/model/card_wallet.go +++ b/internal/model/asset_wallet.go @@ -6,9 +6,9 @@ import ( "gorm.io/gorm" ) -// CardWallet 卡钱包模型 +// AssetWallet 资产钱包模型 // 管理物联网卡和设备级别的钱包 -type CardWallet struct { +type AssetWallet struct { ID uint `gorm:"column:id;primaryKey" json:"id"` ResourceType string `gorm:"column:resource_type;type:varchar(20);not null;index;comment:资源类型(iot_card-物联网卡 | device-设备)" json:"resource_type"` ResourceID uint `gorm:"column:resource_id;not null;index;comment:资源ID(关联tb_iot_card.id或tb_device.id)" json:"resource_id"` @@ -25,20 +25,20 @@ type CardWallet struct { } // TableName 指定表名 -func (CardWallet) TableName() string { - return "tb_card_wallet" +func (AssetWallet) TableName() string { + return "tb_asset_wallet" } // GetAvailableBalance 获取可用余额 = balance - frozen_balance -func (w *CardWallet) GetAvailableBalance() int64 { +func (w *AssetWallet) GetAvailableBalance() int64 { return w.Balance - w.FrozenBalance } -// CardWalletTransaction 卡钱包交易记录模型 -// 记录所有卡钱包余额变动 -type CardWalletTransaction struct { +// AssetWalletTransaction 资产钱包交易记录模型 +// 记录所有资产钱包余额变动 +type AssetWalletTransaction struct { ID uint `gorm:"column:id;primaryKey" json:"id"` - CardWalletID uint `gorm:"column:card_wallet_id;not null;index;comment:卡钱包ID" json:"card_wallet_id"` + AssetWalletID uint `gorm:"column:asset_wallet_id;not null;index;comment:资产钱包ID" json:"asset_wallet_id"` ResourceType string `gorm:"column:resource_type;type:varchar(20);not null;index;comment:资源类型(冗余字段,便于查询)" json:"resource_type"` ResourceID uint `gorm:"column:resource_id;not null;index;comment:资源ID(冗余字段,便于查询)" json:"resource_id"` UserID uint `gorm:"column:user_id;not null;comment:操作人用户ID" json:"user_id"` @@ -47,8 +47,8 @@ type CardWalletTransaction struct { BalanceBefore int64 `gorm:"column:balance_before;type:bigint;not null;comment:变动前余额(单位:分)" json:"balance_before"` BalanceAfter int64 `gorm:"column:balance_after;type:bigint;not null;comment:变动后余额(单位:分)" json:"balance_after"` Status int `gorm:"column:status;type:int;not null;default:1;comment:交易状态(1-成功 2-失败 3-处理中)" json:"status"` - ReferenceType *string `gorm:"column:reference_type;type:varchar(50);comment:关联业务类型(order | topup)" json:"reference_type,omitempty"` - ReferenceID *uint `gorm:"column:reference_id;comment:关联业务ID" json:"reference_id,omitempty"` + ReferenceType *string `gorm:"column:reference_type;type:varchar(50);comment:关联业务类型(order | recharge)" json:"reference_type,omitempty"` + ReferenceNo *string `gorm:"column:reference_no;type:varchar(50);comment:关联业务编号(充值单号CRCH…或订单号ORD…)" json:"reference_no,omitempty"` Remark *string `gorm:"column:remark;type:text;comment:备注" json:"remark,omitempty"` Metadata *string `gorm:"column:metadata;type:jsonb;comment:扩展信息(如套餐信息、支付方式等)" json:"metadata,omitempty"` Creator uint `gorm:"column:creator;not null;comment:创建人ID" json:"creator"` @@ -60,16 +60,16 @@ type CardWalletTransaction struct { } // TableName 指定表名 -func (CardWalletTransaction) TableName() string { - return "tb_card_wallet_transaction" +func (AssetWalletTransaction) TableName() string { + return "tb_asset_wallet_transaction" } -// CardRechargeRecord 卡充值记录模型 -// 记录所有卡充值操作 -type CardRechargeRecord struct { +// AssetRechargeRecord 资产充值记录模型 +// 记录所有资产钱包充值操作 +type AssetRechargeRecord struct { ID uint `gorm:"column:id;primaryKey" json:"id"` UserID uint `gorm:"column:user_id;not null;index;comment:操作人用户ID" json:"user_id"` - CardWalletID uint `gorm:"column:card_wallet_id;not null;comment:卡钱包ID" json:"card_wallet_id"` + AssetWalletID uint `gorm:"column:asset_wallet_id;not null;comment:资产钱包ID" json:"asset_wallet_id"` ResourceType string `gorm:"column:resource_type;type:varchar(20);not null;index;comment:资源类型(冗余字段)" json:"resource_type"` ResourceID uint `gorm:"column:resource_id;not null;index;comment:资源ID(冗余字段)" json:"resource_id"` RechargeNo string `gorm:"column:recharge_no;type:varchar(50);not null;uniqueIndex;comment:充值订单号(格式:CRCH+时间戳+随机数)" json:"recharge_no"` @@ -88,6 +88,6 @@ type CardRechargeRecord struct { } // TableName 指定表名 -func (CardRechargeRecord) TableName() string { - return "tb_card_recharge_record" +func (AssetRechargeRecord) TableName() string { + return "tb_asset_recharge_record" } diff --git a/internal/model/dto/asset_wallet_dto.go b/internal/model/dto/asset_wallet_dto.go new file mode 100644 index 0000000..621b832 --- /dev/null +++ b/internal/model/dto/asset_wallet_dto.go @@ -0,0 +1,52 @@ +package dto + +import "time" + +// AssetWalletResponse 资产钱包概况响应 +type AssetWalletResponse struct { + WalletID uint `json:"wallet_id" description:"钱包数据库ID"` + ResourceType string `json:"resource_type" description:"资源类型:iot_card 或 device"` + ResourceID uint `json:"resource_id" description:"对应卡或设备的数据库ID"` + Balance int64 `json:"balance" description:"总余额(分)"` + FrozenBalance int64 `json:"frozen_balance" description:"冻结余额(分)"` + AvailableBalance int64 `json:"available_balance" description:"可用余额 = balance - frozen_balance(分)"` + Currency string `json:"currency" description:"币种,目前固定 CNY"` + Status int `json:"status" description:"钱包状态:1-正常 2-冻结 3-关闭"` + StatusText string `json:"status_text" description:"状态文本"` + CreatedAt time.Time `json:"created_at" description:"创建时间(RFC3339)"` + UpdatedAt time.Time `json:"updated_at" description:"更新时间(RFC3339)"` +} + +// AssetWalletTransactionListRequest 资产钱包流水列表请求(路径参数 + 查询参数) +type AssetWalletTransactionListRequest struct { + AssetType string `path:"asset_type" description:"资产类型:card 或 device" required:"true"` + ID uint `path:"id" description:"资产ID" required:"true"` + Page int `json:"page" query:"page" description:"页码,默认1"` + PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" description:"每页数量,默认20,最大100"` + TransactionType *string `json:"transaction_type" query:"transaction_type" validate:"omitempty,oneof=recharge deduct refund" description:"交易类型过滤:recharge/deduct/refund"` + StartTime *time.Time `json:"start_time" query:"start_time" description:"开始时间(RFC3339)"` + EndTime *time.Time `json:"end_time" query:"end_time" description:"结束时间(RFC3339)"` +} + +// AssetWalletTransactionItem 单条流水记录 +type AssetWalletTransactionItem struct { + ID uint `json:"id" description:"流水记录ID"` + TransactionType string `json:"transaction_type" description:"交易类型:recharge/deduct/refund"` + TransactionTypeText string `json:"transaction_type_text" description:"交易类型文本:充值/扣款/退款"` + Amount int64 `json:"amount" description:"变动金额(分),充值为正数,扣款/退款为负数"` + BalanceBefore int64 `json:"balance_before" description:"变动前余额(分)"` + BalanceAfter int64 `json:"balance_after" description:"变动后余额(分)"` + ReferenceType *string `json:"reference_type,omitempty" description:"关联业务类型:recharge 或 order(可空)"` + ReferenceNo *string `json:"reference_no,omitempty" description:"关联业务编号:充值单号(CRCH…)或订单号(ORD…)(可空)"` + Remark *string `json:"remark,omitempty" description:"备注(可空)"` + CreatedAt time.Time `json:"created_at" description:"流水创建时间(RFC3339)"` +} + +// AssetWalletTransactionListResponse 资产钱包流水列表响应 +type AssetWalletTransactionListResponse struct { + List []*AssetWalletTransactionItem `json:"list" description:"流水列表"` + Total int64 `json:"total" description:"总记录数"` + Page int `json:"page" description:"当前页码"` + PageSize int `json:"page_size" description:"每页数量"` + TotalPages int `json:"total_pages" description:"总页数"` +}