重构 orm 代码生成逻辑,实现 bill 接口,优化请求字段检查与 list total 查询逻辑

This commit is contained in:
2025-04-11 17:36:34 +08:00
parent c1b4f8d605
commit 636bdcdb87
36 changed files with 794 additions and 193 deletions

View File

@@ -3,15 +3,20 @@
核心流程:
- [x] 注册与登录
- [ ] 对接短信接口
- [ ] 人机风险分级验证
- [ ] jwt 签发
- [ ] 对接接口
- [ ] 人机风险分级验证
- [ ] jwt 签发
- [x] 鉴权
- [ ] 实名认证
- [ ] 充值余额
- [ ] 选择套餐
- [X] 提取 IP
- [ ] 长效提取
- [x] 实名认证
- [ ] 对接接口
- [x] 充值余额
- [ ] 对接接口
- [x] 选择套餐
- [ ] 对接接口
- [ ] 提取记录
- [ ] 提取 IP
- [ ] 长效提取
- [ ] 使用记录
- [ ] 连接
中间件:
@@ -20,6 +25,11 @@
- [ ] Limiter
- [ ] Compress
实现开发时迁移脚本:
- 检查 sql 注释
- 执行迁移
- 如果有必要则填充数据
查端口需要通过外部接口实现,防止不同环境下的端口覆盖。提供一个额外的简便方法用来实现端口覆盖
业务代码和测试代码共用的控制变量可以优化为环境变量
@@ -36,18 +46,12 @@ remote 令牌问题
考虑将鉴权逻辑放到 handler 里,统一动静态鉴权以及解耦服务层
重新手动实现 model 层
环境变量配置默认会话配置
oauth token 验证授权范围
保存 session 到数据库
账单数据表结构修改
短信发送日志
## 环境变量和脚本
在 init/env 中有定义和默认值

View File

@@ -1,8 +1,6 @@
package main
import (
m "platform/web/models"
"gorm.io/driver/postgres"
"gorm.io/gen"
"gorm.io/gen/field"
@@ -10,7 +8,12 @@ import (
"gorm.io/gorm/schema"
)
var g *gen.Generator
func main() {
// 初始化
db, _ := gorm.Open(
postgres.Open("host=localhost user=test password=test dbname=app port=5432 sslmode=disable TimeZone=Asia/Shanghai"),
&gorm.Config{
@@ -20,32 +23,87 @@ func main() {
},
)
g := gen.NewGenerator(gen.Config{
g = gen.NewGenerator(gen.Config{
OutPath: "web/queries",
ModelPkgPath: "models",
Mode: gen.WithDefaultQuery | gen.WithoutContext,
})
g.UseDB(db)
models := g.GenerateAllTable(
gen.FieldType("created_at", "common.LocalDateTime"),
gen.FieldType("updated_at", "common.LocalDateTime"),
gen.FieldType("deleted_at", "common.LocalDateTime"),
)
// 生成模型
common := []gen.ModelOpt{
gen.FieldModify(func(field gen.Field) gen.Field {
if field.Type == "time.Time" {
field.Type = "common.LocalDateTime"
}
return field
}),
}
customs := make(map[string]any)
resourcePss := g.GenerateModel("resource_pss", common...)
customs["resource_pss"] = resourcePss
resource := g.GenerateModel("resource", append(common,
gen.FieldRelate(field.HasOne, "Pss", resourcePss, &field.RelateConfig{
RelatePointer: true,
GORMTag: field.GormTag{
"foreignKey": []string{"ResourceID"},
"references": []string{"ID"},
},
}),
)...)
customs["resource"] = resource
trade := g.GenerateModel("trade", common...)
customs["trade"] = trade
refund := g.GenerateModel("refund", common...)
customs["refund"] = refund
bill := g.GenerateModel("bill", append(common,
gen.FieldRelate(field.BelongsTo, "Trade", trade, &field.RelateConfig{
RelatePointer: true,
GORMTag: field.GormTag{
"foreignKey": []string{"TradeID"},
},
}),
gen.FieldRelate(field.BelongsTo, "Refund", refund, &field.RelateConfig{
RelatePointer: true,
GORMTag: field.GormTag{
"foreignKey": []string{"RefundID"},
},
}),
gen.FieldRelate(field.BelongsTo, "Resource", resource, &field.RelateConfig{
RelatePointer: true,
GORMTag: field.GormTag{
"foreignKey": []string{"ResourceID"},
},
}),
)...)
customs["bill"] = bill
tables, _ := db.Migrator().GetTables()
models := make([]interface{}, len(tables))
for i, name := range tables {
if customs[name] != nil {
models[i] = customs[name]
continue
}
models[i] = genBasic(name, common...)
}
g.ApplyBasic(models...)
modelChannel := g.GenerateModel("channel",
gen.FieldRelateModel(field.BelongsTo, "Node", &m.Node{}, &field.RelateConfig{
RelatePointer: true,
}),
gen.FieldRelateModel(field.BelongsTo, "User", &m.User{}, &field.RelateConfig{
RelatePointer: true,
}),
gen.FieldRelateModel(field.BelongsTo, "Proxy", &m.Proxy{}, &field.RelateConfig{
RelatePointer: true,
}),
)
g.ApplyBasic(modelChannel)
// 生成查询
g.Execute()
}
func genBasic(name string, opts ...gen.ModelOpt) any {
return g.GenerateModel(name,
gen.FieldType("created_at", "common.LocalDateTime"),
gen.FieldType("updated_at", "common.LocalDateTime"),
)
}

View File

@@ -1,14 +1,11 @@
package main
import (
"fmt"
"time"
"platform/web/services"
)
func main() {
parse, err := time.Parse(time.RFC3339, "2025-04-16T16:00:00.000Z")
if err != nil {
return
for range 12 {
println(services.ID.GenReadable("res"))
}
fmt.Printf("%x\n", parse)
}

View File

@@ -587,16 +587,18 @@ comment on column product.deleted_at is '删除时间';
-- resource
drop table if exists resource cascade;
create table resource (
id serial primary key,
user_id int not null references "user" (id)
id serial primary key,
user_id int not null references "user" (id)
on update cascade
on delete cascade,
active bool not null default true,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
deleted_at timestamp
resource_no varchar(255) unique,
active bool not null default true,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
deleted_at timestamp
);
create index resource_user_id_index on resource (user_id);
create index resource_resource_no_index on resource (resource_no);
create index resource_active_index on resource (active);
create index resource_deleted_at_index on resource (deleted_at);
@@ -604,6 +606,7 @@ create index resource_deleted_at_index on resource (deleted_at);
comment on table resource is '套餐表';
comment on column resource.id is '套餐ID';
comment on column resource.user_id is '用户ID';
comment on column resource.resource_no is '套餐编号';
comment on column resource.active is '套餐状态';
comment on column resource.created_at is '创建时间';
comment on column resource.updated_at is '更新时间';
@@ -763,6 +766,8 @@ create table refund (
on update cascade --
on delete set null,
amount decimal(12, 2) not null default 0,
reason varchar(255),
status int not null default 0,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
deleted_at timestamp
@@ -777,6 +782,8 @@ comment on column refund.id is '退款ID';
comment on column refund.trade_id is '订单ID';
comment on column refund.product_id is '产品ID';
comment on column refund.amount is '退款金额';
comment on column refund.reason is '退款原因';
comment on column refund.status is '退款状态0-待处理1-已退款2-已拒绝';
comment on column refund.created_at is '创建时间';
comment on column refund.updated_at is '更新时间';
comment on column refund.deleted_at is '删除时间';
@@ -785,24 +792,25 @@ comment on column refund.deleted_at is '删除时间';
drop table if exists bill cascade;
create table bill (
id serial primary key,
user_id int not null references "user" (id)
user_id int not null references "user" (id)
on update cascade
on delete cascade,
trade_id int references trade (id) --
on update cascade --
on delete set null,
resource_id int references resource (id) --
on update cascade --
on delete set null,
refund_id int references refund (id) --
on update cascade --
on delete set null,
bill_no varchar(255) not null unique,
trade_id int references trade (id) --
on update cascade --
on delete set null,
resource_id int references resource (id) --
on update cascade --
on delete set null,
refund_id int references refund (id) --
on update cascade --
on delete set null,
bill_no varchar(255) not null unique,
info varchar(255),
type int not null,
status int not null,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
type int not null,
status int not null,
amount decimal(12, 2) not null default 0,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
deleted_at timestamp
);
create index bill_user_id_index on bill (user_id);
@@ -825,6 +833,7 @@ comment on column bill.bill_no is '易读账单号';
comment on column bill.info is '产品可读信息';
comment on column bill.type is '账单类型0-充值1-消费2-退款';
comment on column bill.status is '账单状态0-未完成1-已完成2-已作废';
comment on column bill.amount is '账单金额';
comment on column bill.created_at is '创建时间';
comment on column bill.updated_at is '更新时间';
comment on column bill.deleted_at is '删除时间';

View File

@@ -1 +1,96 @@
package handlers
import (
"platform/web/auth"
"platform/web/common"
q "platform/web/queries"
"platform/web/services"
"time"
"github.com/gofiber/fiber/v2"
)
// region ListBill
type ListBillReq struct {
Page int `json:"page" validate:"required"`
Size int `json:"size" validate:"required"`
BillNo *string `json:"bill_no"`
Type *int `json:"type"`
Status *int `json:"status"`
CreateAfter *time.Time `json:"create_after"`
CreateBefore *time.Time `json:"create_before"`
}
// ListBill 获取账单列表
func ListBill(c *fiber.Ctx) error {
// 检查权限
authContext, err := auth.Protect(c, []services.PayloadType{services.PayloadUser}, []string{})
if err != nil {
return err
}
// 解析请求参数
req := new(ListBillReq)
if err := c.BodyParser(req); err != nil {
return err
}
if req.Page < 1 {
req.Page = 1
}
if req.Size < 1 {
req.Size = 10
}
var offset = (req.Page - 1) * req.Size
var limit = req.Size
// 查询账单列表
do := q.Bill.
Where(q.Bill.UserID.Eq(authContext.Payload.Id))
if req.Status != nil {
do = do.Where(q.Bill.Status.Eq(int32(*req.Status)))
}
if req.Type != nil {
do = do.Where(q.Bill.Type.Eq(int32(*req.Type)))
}
if req.CreateAfter != nil {
do = do.Where(q.Bill.CreatedAt.Gte(common.LocalDateTime(*req.CreateAfter)))
}
if req.CreateBefore != nil {
do = do.Where(q.Bill.CreatedAt.Lte(common.LocalDateTime(*req.CreateBefore)))
}
if req.BillNo != nil && *req.BillNo != "" {
do = do.Where(q.Bill.BillNo.Eq(*req.BillNo))
}
bills, err := do.Debug().
Preload(q.Bill.Resource, q.Bill.Trade, q.Bill.Refund).
Preload(q.Bill.Resource.Pss).
Order(q.Bill.CreatedAt.Desc()).
Offset(offset).
Limit(limit).
Find()
if err != nil {
return err
}
var total int64
if len(bills) < limit {
total = int64(len(bills) + offset)
} else {
total, err = do.Count()
if err != nil {
return err
}
}
return c.JSON(common.PageResp{
Total: int(total),
Page: req.Page,
Size: req.Size,
List: bills,
})
}
// endregion

View File

@@ -17,6 +17,7 @@ import (
type ListResourcePssReq struct {
Page int `json:"page" validate:"required"`
Size int `json:"size" validate:"required"`
ResourceNo *string `json:"resource_no"`
Active *bool `json:"active"`
Type *int `json:"type"`
CreateAfter *time.Time `json:"create_after"`
@@ -38,16 +39,27 @@ func ListResourcePss(c *fiber.Ctx) error {
if err := c.BodyParser(req); err != nil {
return err
}
if req.Page < 1 {
req.Page = 1
}
if req.Size < 1 {
req.Size = 10
}
var offset = (req.Page - 1) * req.Size
var limit = req.Size
// 查询资源列表
do := q.Resource.
Join(q.ResourcePss, q.ResourcePss.ResourceID.EqCol(q.Resource.ID)).
Joins(q.Resource.Pss).
Where(q.Resource.UserID.Eq(authContext.Payload.Id))
if req.ResourceNo != nil && *req.ResourceNo != "" {
do = do.Where(q.Resource.ResourceNo.Eq(*req.ResourceNo))
}
if req.Active != nil {
do = do.Where(q.Resource.Active.Is(*req.Active))
}
if req.Type != nil {
do = do.Where(q.ResourcePss.Type.Eq(int32(*req.Type)))
do = do.Where(q.ResourcePss.As(q.Resource.Pss.Name()).Type.Eq(int32(*req.Type)))
}
if req.CreateAfter != nil {
do = do.Where(q.Resource.CreatedAt.Gte(common.LocalDateTime(*req.CreateAfter)))
@@ -56,34 +68,37 @@ func ListResourcePss(c *fiber.Ctx) error {
do = do.Where(q.Resource.CreatedAt.Lte(common.LocalDateTime(*req.CreateBefore)))
}
if req.ExpireAfter != nil {
do = do.Where(q.ResourcePss.Expire.Gte(*req.ExpireAfter))
do = do.Where(q.ResourcePss.As(q.Resource.Pss.Name()).Expire.Gte(common.LocalDateTime(*req.ExpireAfter)))
}
if req.ExpireBefore != nil {
do = do.Where(q.ResourcePss.Expire.Lte(*req.ExpireBefore))
do = do.Where(q.ResourcePss.As(q.Resource.Pss.Name()).Expire.Lte(common.LocalDateTime(*req.ExpireBefore)))
}
total, err := do.Debug().
Count()
if err != nil {
return err
}
var resourcePss []*m.ResourcePss
var resource []*m.Resource
err = do.Debug().
Select(q.ResourcePss.ALL).
Order(q.ResourcePss.CreatedAt.Desc()).
Offset((req.Page - 1) * req.Size).
Limit(req.Size).
Scan(&resourcePss)
Order(q.ResourcePss.As(q.Resource.Pss.Name()).CreatedAt.Desc()).
Offset(offset).
Limit(limit).
Scan(&resource)
if err != nil {
return err
}
var total int64
if len(resource) < limit {
total = int64(len(resource) + offset)
} else {
total, err = do.Count()
if err != nil {
return err
}
}
return c.JSON(common.PageResp{
Total: int(total),
Page: req.Page,
Size: req.Size,
List: resourcePss,
List: resource,
})
}
@@ -141,9 +156,10 @@ func CreateResourceByBalance(c *fiber.Ctx) error {
// 创建资源
resource := m.Resource{
UserID: authContext.Payload.Id,
UserID: authContext.Payload.Id,
ResourceNo: services.ID.GenReadable("res"),
}
err = q.Resource.Save(&resource)
err = q.Resource.Create(&resource)
if err != nil {
return err
}
@@ -152,10 +168,10 @@ func CreateResourceByBalance(c *fiber.Ctx) error {
Type: req.Type,
Live: req.Live,
Quota: req.Quota,
Expire: time.Now().Add(time.Duration(req.Expire) * time.Second),
Expire: common.LocalDateTime(time.Now().Add(time.Duration(req.Expire) * time.Second)),
DailyLimit: req.DailyLimit,
}
err = q.ResourcePss.Save(&resourcePss)
err = q.ResourcePss.Create(&resourcePss)
if err != nil {
return err
}

View File

@@ -2,6 +2,7 @@ package handlers
import (
"platform/web/auth"
"platform/web/common"
m "platform/web/models"
q "platform/web/queries"
"platform/web/services"
@@ -35,38 +36,44 @@ func ListWhitelist(c *fiber.Ctx) error {
if err := c.BodyParser(req); err != nil {
return err
}
var page = req.Page
if page < 1 {
page = 1
if req.Page < 1 {
req.Page = 1
}
var size = req.Size
if size < 1 {
size = 10
if req.Size < 1 {
req.Size = 10
}
var offset = (req.Page - 1) * req.Size
var limit = req.Size
// 获取用户信息
list, err := q.Whitelist.
Where(q.Whitelist.UserID.Eq(authContext.Payload.Id)).
Offset((page - 1) * size).
Limit(size).
do := q.Whitelist.
Where(q.Whitelist.UserID.Eq(authContext.Payload.Id))
list, err := do.
Offset(offset).
Limit(limit).
Order(q.Whitelist.CreatedAt.Desc()).
Find()
if err != nil {
return err
}
count, err := q.Whitelist.
Where(q.Whitelist.UserID.Eq(authContext.Payload.Id)).
Count()
if err != nil {
return err
var total int64
if len(list) < limit {
total = int64(len(list) + offset)
} else {
total, err = do.Count()
if err != nil {
return err
}
}
// 返回结果
return c.Status(fiber.StatusOK).JSON(ListWhitelistResp{
Total: count,
return c.JSON(common.PageResp{
Total: int(total),
List: list,
Page: page,
Size: size,
Page: req.Page,
Size: req.Size,
})
}

View File

@@ -7,6 +7,8 @@ package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
)
const TableNameAdmin = "admin"
@@ -26,7 +28,7 @@ type Admin struct {
LastLoginAgent string `gorm:"column:last_login_agent;comment:最后登录代理" json:"last_login_agent"` // 最后登录代理
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Admin's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameAdminRole = "admin_role"
@@ -17,7 +21,7 @@ type AdminRole struct {
Sort int32 `gorm:"column:sort;comment:排序" json:"sort"` // 排序
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName AdminRole's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameAdminRoleLink = "admin_role_link"
@@ -15,7 +19,7 @@ type AdminRoleLink struct {
RoleID int32 `gorm:"column:role_id;not null;comment:角色ID" json:"role_id"` // 角色ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName AdminRoleLink's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameAdminRolePermissionLink = "admin_role_permission_link"
@@ -15,7 +19,7 @@ type AdminRolePermissionLink struct {
PermissionID int32 `gorm:"column:permission_id;not null;comment:权限ID" json:"permission_id"` // 权限ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName AdminRolePermissionLink's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameBill = "bill"
@@ -15,13 +19,17 @@ type Bill struct {
Info string `gorm:"column:info;comment:产品可读信息" json:"info"` // 产品可读信息
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
TradeID int32 `gorm:"column:trade_id" json:"trade_id"`
ResourceID int32 `gorm:"column:resource_id" json:"resource_id"`
Type int32 `gorm:"column:type;not null" json:"type"`
BillNo string `gorm:"column:bill_no;not null" json:"bill_no"`
RefundID int32 `gorm:"column:refund_id" json:"refund_id"`
Status int32 `gorm:"column:status;not null" json:"status"`
Amount float64 `gorm:"column:amount;not null" json:"amount"`
Trade *Trade `gorm:"foreignKey:TradeID" json:"trade"`
Refund *Refund `gorm:"foreignKey:RefundID" json:"refund"`
Resource *Resource `gorm:"foreignKey:ResourceID" json:"resource"`
}
// TableName Bill's table name

View File

@@ -5,6 +5,7 @@
package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
@@ -14,26 +15,23 @@ const TableNameChannel = "channel"
// Channel mapped from table <channel>
type Channel struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:通道ID" json:"id"` // 通道ID
UserID int32 `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID
ProxyID int32 `gorm:"column:proxy_id;not null;comment:代理ID" json:"proxy_id"` // 代理ID
NodeID int32 `gorm:"column:node_id;comment:节点ID" json:"node_id"` // 节点ID
ProxyPort int32 `gorm:"column:proxy_port;not null;comment:转发端口" json:"proxy_port"` // 转发端口
Protocol string `gorm:"column:protocol;comment:协议" json:"protocol"` // 协议
UserHost string `gorm:"column:user_host;comment:用户地址" json:"user_host"` // 用户地址
NodeHost string `gorm:"column:node_host;comment:节点地址" json:"node_host"` // 节点地址
AuthIP bool `gorm:"column:auth_ip;not null;comment:IP认证" json:"auth_ip"` // IP认证
AuthPass bool `gorm:"column:auth_pass;not null;comment:密码认证" json:"auth_pass"` // 密码认证
Username string `gorm:"column:username;comment:用户名" json:"username"` // 用户名
Password string `gorm:"column:password;comment:密码" json:"password"` // 密码
Expiration time.Time `gorm:"column:expiration;not null;comment:过期时间" json:"expiration"` // 过期时间
CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
ProxyHost string `gorm:"column:proxy_host;not null" json:"proxy_host"`
Node *Node `json:"node"`
User *User `json:"user"`
Proxy *Proxy `json:"proxy"`
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:通道ID" json:"id"` // 通道ID
UserID int32 `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID
ProxyID int32 `gorm:"column:proxy_id;not null;comment:代理ID" json:"proxy_id"` // 代理ID
NodeID int32 `gorm:"column:node_id;comment:节点ID" json:"node_id"` // 节点ID
ProxyPort int32 `gorm:"column:proxy_port;not null;comment:转发端口" json:"proxy_port"` // 转发端口
Protocol string `gorm:"column:protocol;comment:协议" json:"protocol"` // 协议
UserHost string `gorm:"column:user_host;comment:用户地址" json:"user_host"` // 用户地址
NodeHost string `gorm:"column:node_host;comment:节点地址" json:"node_host"` // 节点地址
AuthIP bool `gorm:"column:auth_ip;not null;comment:IP认证" json:"auth_ip"` // IP认证
AuthPass bool `gorm:"column:auth_pass;not null;comment:密码认证" json:"auth_pass"` // 密码认证
Username string `gorm:"column:username;comment:用户名" json:"username"` // 用户名
Password string `gorm:"column:password;comment:密码" json:"password"` // 密码
Expiration time.Time `gorm:"column:expiration;not null;comment:过期时间" json:"expiration"` // 过期时间
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
ProxyHost string `gorm:"column:proxy_host;not null" json:"proxy_host"`
}
// TableName Channel's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameClient = "client"
@@ -23,7 +27,7 @@ type Client struct {
Status int32 `gorm:"column:status;not null;default:1;comment:状态1-正常0-禁用" json:"status"` // 状态1-正常0-禁用
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Client's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameClientPermissionLink = "client_permission_link"
@@ -15,7 +19,7 @@ type ClientPermissionLink struct {
PermissionID int32 `gorm:"column:permission_id;not null;comment:权限ID" json:"permission_id"` // 权限ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName ClientPermissionLink's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameNode = "node"
@@ -24,7 +28,7 @@ type Node struct {
Loss int32 `gorm:"column:loss;comment:丢包率" json:"loss"` // 丢包率
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Node's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNamePermission = "permission"
@@ -16,7 +20,7 @@ type Permission struct {
Description string `gorm:"column:description;comment:权限描述" json:"description"` // 权限描述
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Permission's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameProduct = "product"
@@ -18,7 +22,7 @@ type Product struct {
Status int32 `gorm:"column:status;not null;default:1;comment:产品状态1-正常0-禁用" json:"status"` // 产品状态1-正常0-禁用
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Product's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameProxy = "proxy"
@@ -18,7 +22,7 @@ type Proxy struct {
Secret string `gorm:"column:secret;comment:代理服务密钥" json:"secret"` // 代理服务密钥
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName Proxy's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameRefund = "refund"
@@ -15,7 +19,7 @@ type Refund struct {
Amount float64 `gorm:"column:amount;not null;comment:退款金额" json:"amount"` // 退款金额
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
TradeID int32 `gorm:"column:trade_id;not null" json:"trade_id"`
}

View File

@@ -4,18 +4,24 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameResource = "resource"
// Resource mapped from table <resource>
type Resource struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:套餐ID" json:"id"` // 套餐ID
UserID int32 `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID
Active bool `gorm:"column:active;not null;default:true;comment:套餐状态" json:"active"` // 套餐状态
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:套餐ID" json:"id"` // 套餐ID
UserID int32 `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID
Active bool `gorm:"column:active;not null;default:true;comment:套餐状态" json:"active"` // 套餐状态
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
ResourceNo string `gorm:"column:resource_no" json:"resource_no"`
Pss *ResourcePss `gorm:"foreignKey:ResourceID;references:ID" json:"pss"`
}
// TableName Resource's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameResourcePps = "resource_pps"
@@ -14,7 +18,7 @@ type ResourcePps struct {
ResourceID int32 `gorm:"column:resource_id;not null;comment:套餐ID" json:"resource_id"` // 套餐ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName ResourcePps's table name

View File

@@ -7,6 +7,8 @@ package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
)
const TableNameResourcePsr = "resource_psr"
@@ -21,7 +23,7 @@ type ResourcePsr struct {
Used bool `gorm:"column:used;comment:是否已使用" json:"used"` // 是否已使用
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName ResourcePsr's table name

View File

@@ -6,7 +6,8 @@ package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
)
const TableNameResourcePss = "resource_pss"
@@ -18,14 +19,14 @@ type ResourcePss struct {
Type int32 `gorm:"column:type;comment:套餐类型1-包时2-包量" json:"type"` // 套餐类型1-包时2-包量
Live int32 `gorm:"column:live;comment:可用时长(秒)" json:"live"` // 可用时长(秒)
Quota int32 `gorm:"column:quota;comment:配额数量" json:"quota"` // 配额数量
Expire time.Time `gorm:"column:expire;comment:过期时间" json:"expire"` // 过期时间
Expire common.LocalDateTime `gorm:"column:expire;comment:过期时间" json:"expire"` // 过期时间
Used int32 `gorm:"column:used;not null;comment:已用数量" json:"used"` // 已用数量
DailyLimit int32 `gorm:"column:daily_limit;not null;comment:每日限制" json:"daily_limit"` // 每日限制
DailyUsed int32 `gorm:"column:daily_used;not null;comment:今日已用数量" json:"daily_used"` // 今日已用数量
DailyLast time.Time `gorm:"column:daily_last;comment:今日最后使用时间" json:"daily_last"` // 今日最后使用时间
DailyLast common.LocalDateTime `gorm:"column:daily_last;comment:今日最后使用时间" json:"daily_last"` // 今日最后使用时间
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName ResourcePss's table name

View File

@@ -6,7 +6,8 @@ package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
)
const TableNameTrade = "trade"
@@ -25,10 +26,10 @@ type Trade struct {
Status int32 `gorm:"column:status;not null;comment:订单状态0-待支付1-已支付2-已取消3-已退款" json:"status"` // 订单状态0-待支付1-已支付2-已取消3-已退款
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
Type int32 `gorm:"column:type;not null" json:"type"`
CancelAt time.Time `gorm:"column:cancel_at" json:"cancel_at"`
PaidAt time.Time `gorm:"column:paid_at" json:"paid_at"`
CancelAt common.LocalDateTime `gorm:"column:cancel_at" json:"cancel_at"`
PaidAt common.LocalDateTime `gorm:"column:paid_at" json:"paid_at"`
}
// TableName Trade's table name

View File

@@ -7,6 +7,8 @@ package models
import (
"platform/web/common"
"time"
"gorm.io/gorm"
)
const TableNameUser = "user"
@@ -33,7 +35,7 @@ type User struct {
LastLoginAgent string `gorm:"column:last_login_agent;comment:最后登录代理" json:"last_login_agent"` // 最后登录代理
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName User's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameUserRole = "user_role"
@@ -17,7 +21,7 @@ type UserRole struct {
Sort int32 `gorm:"column:sort;comment:排序" json:"sort"` // 排序
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName UserRole's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameUserRoleLink = "user_role_link"
@@ -15,7 +19,7 @@ type UserRoleLink struct {
RoleID int32 `gorm:"column:role_id;not null;comment:角色ID" json:"role_id"` // 角色ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName UserRoleLink's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameUserRolePermissionLink = "user_role_permission_link"
@@ -15,7 +19,7 @@ type UserRolePermissionLink struct {
PermissionID int32 `gorm:"column:permission_id;not null;comment:权限ID" json:"permission_id"` // 权限ID
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
}
// TableName UserRolePermissionLink's table name

View File

@@ -4,7 +4,11 @@
package models
import "platform/web/common"
import (
"platform/web/common"
"gorm.io/gorm"
)
const TableNameWhitelist = "whitelist"
@@ -15,7 +19,7 @@ type Whitelist struct {
Host string `gorm:"column:host;not null;comment:IP地址" json:"host"` // IP地址
CreatedAt common.LocalDateTime `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt common.LocalDateTime `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt common.LocalDateTime `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间
Remark string `gorm:"column:remark" json:"remark"`
}

View File

@@ -39,6 +39,29 @@ func newBill(db *gorm.DB, opts ...gen.DOOption) bill {
_bill.BillNo = field.NewString(tableName, "bill_no")
_bill.RefundID = field.NewInt32(tableName, "refund_id")
_bill.Status = field.NewInt32(tableName, "status")
_bill.Amount = field.NewFloat64(tableName, "amount")
_bill.Trade = billBelongsToTrade{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Trade", "models.Trade"),
}
_bill.Refund = billBelongsToRefund{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Refund", "models.Refund"),
}
_bill.Resource = billBelongsToResource{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Resource", "models.Resource"),
Pss: struct {
field.RelationField
}{
RelationField: field.NewRelation("Resource.Pss", "models.ResourcePss"),
},
}
_bill.fillFieldMap()
@@ -61,6 +84,12 @@ type bill struct {
BillNo field.String
RefundID field.Int32
Status field.Int32
Amount field.Float64
Trade billBelongsToTrade
Refund billBelongsToRefund
Resource billBelongsToResource
fieldMap map[string]field.Expr
}
@@ -89,6 +118,7 @@ func (b *bill) updateTableName(table string) *bill {
b.BillNo = field.NewString(table, "bill_no")
b.RefundID = field.NewInt32(table, "refund_id")
b.Status = field.NewInt32(table, "status")
b.Amount = field.NewFloat64(table, "amount")
b.fillFieldMap()
@@ -105,7 +135,7 @@ func (b *bill) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (b *bill) fillFieldMap() {
b.fieldMap = make(map[string]field.Expr, 12)
b.fieldMap = make(map[string]field.Expr, 16)
b.fieldMap["id"] = b.ID
b.fieldMap["user_id"] = b.UserID
b.fieldMap["info"] = b.Info
@@ -118,6 +148,8 @@ func (b *bill) fillFieldMap() {
b.fieldMap["bill_no"] = b.BillNo
b.fieldMap["refund_id"] = b.RefundID
b.fieldMap["status"] = b.Status
b.fieldMap["amount"] = b.Amount
}
func (b bill) clone(db *gorm.DB) bill {
@@ -130,6 +162,223 @@ func (b bill) replaceDB(db *gorm.DB) bill {
return b
}
type billBelongsToTrade struct {
db *gorm.DB
field.RelationField
}
func (a billBelongsToTrade) Where(conds ...field.Expr) *billBelongsToTrade {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a billBelongsToTrade) WithContext(ctx context.Context) *billBelongsToTrade {
a.db = a.db.WithContext(ctx)
return &a
}
func (a billBelongsToTrade) Session(session *gorm.Session) *billBelongsToTrade {
a.db = a.db.Session(session)
return &a
}
func (a billBelongsToTrade) Model(m *models.Bill) *billBelongsToTradeTx {
return &billBelongsToTradeTx{a.db.Model(m).Association(a.Name())}
}
type billBelongsToTradeTx struct{ tx *gorm.Association }
func (a billBelongsToTradeTx) Find() (result *models.Trade, err error) {
return result, a.tx.Find(&result)
}
func (a billBelongsToTradeTx) Append(values ...*models.Trade) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a billBelongsToTradeTx) Replace(values ...*models.Trade) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a billBelongsToTradeTx) Delete(values ...*models.Trade) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a billBelongsToTradeTx) Clear() error {
return a.tx.Clear()
}
func (a billBelongsToTradeTx) Count() int64 {
return a.tx.Count()
}
type billBelongsToRefund struct {
db *gorm.DB
field.RelationField
}
func (a billBelongsToRefund) Where(conds ...field.Expr) *billBelongsToRefund {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a billBelongsToRefund) WithContext(ctx context.Context) *billBelongsToRefund {
a.db = a.db.WithContext(ctx)
return &a
}
func (a billBelongsToRefund) Session(session *gorm.Session) *billBelongsToRefund {
a.db = a.db.Session(session)
return &a
}
func (a billBelongsToRefund) Model(m *models.Bill) *billBelongsToRefundTx {
return &billBelongsToRefundTx{a.db.Model(m).Association(a.Name())}
}
type billBelongsToRefundTx struct{ tx *gorm.Association }
func (a billBelongsToRefundTx) Find() (result *models.Refund, err error) {
return result, a.tx.Find(&result)
}
func (a billBelongsToRefundTx) Append(values ...*models.Refund) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a billBelongsToRefundTx) Replace(values ...*models.Refund) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a billBelongsToRefundTx) Delete(values ...*models.Refund) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a billBelongsToRefundTx) Clear() error {
return a.tx.Clear()
}
func (a billBelongsToRefundTx) Count() int64 {
return a.tx.Count()
}
type billBelongsToResource struct {
db *gorm.DB
field.RelationField
Pss struct {
field.RelationField
}
}
func (a billBelongsToResource) Where(conds ...field.Expr) *billBelongsToResource {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a billBelongsToResource) WithContext(ctx context.Context) *billBelongsToResource {
a.db = a.db.WithContext(ctx)
return &a
}
func (a billBelongsToResource) Session(session *gorm.Session) *billBelongsToResource {
a.db = a.db.Session(session)
return &a
}
func (a billBelongsToResource) Model(m *models.Bill) *billBelongsToResourceTx {
return &billBelongsToResourceTx{a.db.Model(m).Association(a.Name())}
}
type billBelongsToResourceTx struct{ tx *gorm.Association }
func (a billBelongsToResourceTx) Find() (result *models.Resource, err error) {
return result, a.tx.Find(&result)
}
func (a billBelongsToResourceTx) Append(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a billBelongsToResourceTx) Replace(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a billBelongsToResourceTx) Delete(values ...*models.Resource) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a billBelongsToResourceTx) Clear() error {
return a.tx.Clear()
}
func (a billBelongsToResourceTx) Count() int64 {
return a.tx.Count()
}
type billDo struct{ gen.DO }
func (b billDo) Debug() *billDo {

View File

@@ -33,6 +33,12 @@ func newResource(db *gorm.DB, opts ...gen.DOOption) resource {
_resource.CreatedAt = field.NewField(tableName, "created_at")
_resource.UpdatedAt = field.NewField(tableName, "updated_at")
_resource.DeletedAt = field.NewField(tableName, "deleted_at")
_resource.ResourceNo = field.NewString(tableName, "resource_no")
_resource.Pss = resourceHasOnePss{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Pss", "models.ResourcePss"),
}
_resource.fillFieldMap()
@@ -42,13 +48,15 @@ func newResource(db *gorm.DB, opts ...gen.DOOption) resource {
type resource struct {
resourceDo
ALL field.Asterisk
ID field.Int32 // 套餐ID
UserID field.Int32 // 用户ID
Active field.Bool // 套餐状态
CreatedAt field.Field // 创建时间
UpdatedAt field.Field // 更新时间
DeletedAt field.Field // 删除时间
ALL field.Asterisk
ID field.Int32 // 套餐ID
UserID field.Int32 // 用户ID
Active field.Bool // 套餐状态
CreatedAt field.Field // 创建时间
UpdatedAt field.Field // 更新时间
DeletedAt field.Field // 删除时间
ResourceNo field.String
Pss resourceHasOnePss
fieldMap map[string]field.Expr
}
@@ -71,6 +79,7 @@ func (r *resource) updateTableName(table string) *resource {
r.CreatedAt = field.NewField(table, "created_at")
r.UpdatedAt = field.NewField(table, "updated_at")
r.DeletedAt = field.NewField(table, "deleted_at")
r.ResourceNo = field.NewString(table, "resource_no")
r.fillFieldMap()
@@ -87,13 +96,15 @@ func (r *resource) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (r *resource) fillFieldMap() {
r.fieldMap = make(map[string]field.Expr, 6)
r.fieldMap = make(map[string]field.Expr, 8)
r.fieldMap["id"] = r.ID
r.fieldMap["user_id"] = r.UserID
r.fieldMap["active"] = r.Active
r.fieldMap["created_at"] = r.CreatedAt
r.fieldMap["updated_at"] = r.UpdatedAt
r.fieldMap["deleted_at"] = r.DeletedAt
r.fieldMap["resource_no"] = r.ResourceNo
}
func (r resource) clone(db *gorm.DB) resource {
@@ -106,6 +117,77 @@ func (r resource) replaceDB(db *gorm.DB) resource {
return r
}
type resourceHasOnePss struct {
db *gorm.DB
field.RelationField
}
func (a resourceHasOnePss) Where(conds ...field.Expr) *resourceHasOnePss {
if len(conds) == 0 {
return &a
}
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
exprs = append(exprs, cond.BeCond().(clause.Expression))
}
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
return &a
}
func (a resourceHasOnePss) WithContext(ctx context.Context) *resourceHasOnePss {
a.db = a.db.WithContext(ctx)
return &a
}
func (a resourceHasOnePss) Session(session *gorm.Session) *resourceHasOnePss {
a.db = a.db.Session(session)
return &a
}
func (a resourceHasOnePss) Model(m *models.Resource) *resourceHasOnePssTx {
return &resourceHasOnePssTx{a.db.Model(m).Association(a.Name())}
}
type resourceHasOnePssTx struct{ tx *gorm.Association }
func (a resourceHasOnePssTx) Find() (result *models.ResourcePss, err error) {
return result, a.tx.Find(&result)
}
func (a resourceHasOnePssTx) Append(values ...*models.ResourcePss) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a resourceHasOnePssTx) Replace(values ...*models.ResourcePss) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a resourceHasOnePssTx) Delete(values ...*models.ResourcePss) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a resourceHasOnePssTx) Clear() error {
return a.tx.Clear()
}
func (a resourceHasOnePssTx) Count() int64 {
return a.tx.Count()
}
type resourceDo struct{ gen.DO }
func (r resourceDo) Debug() *resourceDo {

View File

@@ -32,11 +32,11 @@ func newResourcePss(db *gorm.DB, opts ...gen.DOOption) resourcePss {
_resourcePss.Type = field.NewInt32(tableName, "type")
_resourcePss.Live = field.NewInt32(tableName, "live")
_resourcePss.Quota = field.NewInt32(tableName, "quota")
_resourcePss.Expire = field.NewTime(tableName, "expire")
_resourcePss.Expire = field.NewField(tableName, "expire")
_resourcePss.Used = field.NewInt32(tableName, "used")
_resourcePss.DailyLimit = field.NewInt32(tableName, "daily_limit")
_resourcePss.DailyUsed = field.NewInt32(tableName, "daily_used")
_resourcePss.DailyLast = field.NewTime(tableName, "daily_last")
_resourcePss.DailyLast = field.NewField(tableName, "daily_last")
_resourcePss.CreatedAt = field.NewField(tableName, "created_at")
_resourcePss.UpdatedAt = field.NewField(tableName, "updated_at")
_resourcePss.DeletedAt = field.NewField(tableName, "deleted_at")
@@ -55,11 +55,11 @@ type resourcePss struct {
Type field.Int32 // 套餐类型1-包时2-包量
Live field.Int32 // 可用时长(秒)
Quota field.Int32 // 配额数量
Expire field.Time // 过期时间
Expire field.Field // 过期时间
Used field.Int32 // 已用数量
DailyLimit field.Int32 // 每日限制
DailyUsed field.Int32 // 今日已用数量
DailyLast field.Time // 今日最后使用时间
DailyLast field.Field // 今日最后使用时间
CreatedAt field.Field // 创建时间
UpdatedAt field.Field // 更新时间
DeletedAt field.Field // 删除时间
@@ -84,11 +84,11 @@ func (r *resourcePss) updateTableName(table string) *resourcePss {
r.Type = field.NewInt32(table, "type")
r.Live = field.NewInt32(table, "live")
r.Quota = field.NewInt32(table, "quota")
r.Expire = field.NewTime(table, "expire")
r.Expire = field.NewField(table, "expire")
r.Used = field.NewInt32(table, "used")
r.DailyLimit = field.NewInt32(table, "daily_limit")
r.DailyUsed = field.NewInt32(table, "daily_used")
r.DailyLast = field.NewTime(table, "daily_last")
r.DailyLast = field.NewField(table, "daily_last")
r.CreatedAt = field.NewField(table, "created_at")
r.UpdatedAt = field.NewField(table, "updated_at")
r.DeletedAt = field.NewField(table, "deleted_at")

View File

@@ -41,8 +41,8 @@ func newTrade(db *gorm.DB, opts ...gen.DOOption) trade {
_trade.UpdatedAt = field.NewField(tableName, "updated_at")
_trade.DeletedAt = field.NewField(tableName, "deleted_at")
_trade.Type = field.NewInt32(tableName, "type")
_trade.CancelAt = field.NewTime(tableName, "cancel_at")
_trade.PaidAt = field.NewTime(tableName, "paid_at")
_trade.CancelAt = field.NewField(tableName, "cancel_at")
_trade.PaidAt = field.NewField(tableName, "paid_at")
_trade.fillFieldMap()
@@ -67,8 +67,8 @@ type trade struct {
UpdatedAt field.Field // 更新时间
DeletedAt field.Field // 删除时间
Type field.Int32
CancelAt field.Time
PaidAt field.Time
CancelAt field.Field
PaidAt field.Field
fieldMap map[string]field.Expr
}
@@ -99,8 +99,8 @@ func (t *trade) updateTableName(table string) *trade {
t.UpdatedAt = field.NewField(table, "updated_at")
t.DeletedAt = field.NewField(table, "deleted_at")
t.Type = field.NewInt32(table, "type")
t.CancelAt = field.NewTime(table, "cancel_at")
t.PaidAt = field.NewTime(table, "paid_at")
t.CancelAt = field.NewField(table, "cancel_at")
t.PaidAt = field.NewField(table, "paid_at")
t.fillFieldMap()

View File

@@ -41,6 +41,10 @@ func ApplyRouters(app *fiber.App) {
trade := api.Group("/trade")
trade.Post("/create", handlers.CreateTrade)
// 账单
bill := api.Group("/bill")
bill.Post("/list", handlers.ListBill)
// 临时
app.Get("/collect", handlers.CreateChannelGet)
app.Get("/temp", handlers.Temp)

View File

@@ -311,7 +311,7 @@ func (s *channelService) CreateChannel(
toUpdate := models.ResourcePss{
Used: resource.Used + int32(count),
DailyLast: now,
DailyLast: common.LocalDateTime(now),
}
last := resource.DailyLast
if now.Year() != last.Year() || now.Month() != last.Month() || now.Day() != last.Day() {