完善定价与套餐关联表结构

This commit is contained in:
2026-03-24 16:25:21 +08:00
parent c9995ef566
commit 5ffa151f58
28 changed files with 1550 additions and 112 deletions

View File

@@ -35,16 +35,49 @@ func newResource(db *gorm.DB, opts ...gen.DOOption) resource {
_resource.ResourceNo = field.NewString(tableName, "resource_no")
_resource.Active = field.NewBool(tableName, "active")
_resource.Type = field.NewInt(tableName, "type")
_resource.Code = field.NewString(tableName, "code")
_resource.Short = resourceHasOneShort{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Short", "models.ResourceShort"),
Sku: struct {
field.RelationField
Product struct {
field.RelationField
}
Discount struct {
field.RelationField
}
}{
RelationField: field.NewRelation("Short.Sku", "models.ProductSku"),
Product: struct {
field.RelationField
}{
RelationField: field.NewRelation("Short.Sku.Product", "models.Product"),
},
Discount: struct {
field.RelationField
}{
RelationField: field.NewRelation("Short.Sku.Discount", "models.ProductDiscount"),
},
},
}
_resource.Long = resourceHasOneLong{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Long", "models.ResourceLong"),
Sku: struct {
field.RelationField
}{
RelationField: field.NewRelation("Long.Sku", "models.ProductSku"),
},
}
_resource.Product = resourceHasOneProduct{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Product", "models.Product"),
}
_resource.User = resourceBelongsToUser{
@@ -135,10 +168,13 @@ type resource struct {
ResourceNo field.String
Active field.Bool
Type field.Int
Code field.String
Short resourceHasOneShort
Long resourceHasOneLong
Product resourceHasOneProduct
User resourceBelongsToUser
fieldMap map[string]field.Expr
@@ -164,6 +200,7 @@ func (r *resource) updateTableName(table string) *resource {
r.ResourceNo = field.NewString(table, "resource_no")
r.Active = field.NewBool(table, "active")
r.Type = field.NewInt(table, "type")
r.Code = field.NewString(table, "code")
r.fillFieldMap()
@@ -180,7 +217,7 @@ func (r *resource) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (r *resource) fillFieldMap() {
r.fieldMap = make(map[string]field.Expr, 11)
r.fieldMap = make(map[string]field.Expr, 13)
r.fieldMap["id"] = r.ID
r.fieldMap["created_at"] = r.CreatedAt
r.fieldMap["updated_at"] = r.UpdatedAt
@@ -189,6 +226,7 @@ func (r *resource) fillFieldMap() {
r.fieldMap["resource_no"] = r.ResourceNo
r.fieldMap["active"] = r.Active
r.fieldMap["type"] = r.Type
r.fieldMap["code"] = r.Code
}
@@ -198,6 +236,8 @@ func (r resource) clone(db *gorm.DB) resource {
r.Short.db.Statement.ConnPool = db.Statement.ConnPool
r.Long.db = db.Session(&gorm.Session{Initialized: true})
r.Long.db.Statement.ConnPool = db.Statement.ConnPool
r.Product.db = db.Session(&gorm.Session{Initialized: true})
r.Product.db.Statement.ConnPool = db.Statement.ConnPool
r.User.db = db.Session(&gorm.Session{Initialized: true})
r.User.db.Statement.ConnPool = db.Statement.ConnPool
return r
@@ -207,6 +247,7 @@ func (r resource) replaceDB(db *gorm.DB) resource {
r.resourceDo.ReplaceDB(db)
r.Short.db = db.Session(&gorm.Session{})
r.Long.db = db.Session(&gorm.Session{})
r.Product.db = db.Session(&gorm.Session{})
r.User.db = db.Session(&gorm.Session{})
return r
}
@@ -215,6 +256,16 @@ type resourceHasOneShort struct {
db *gorm.DB
field.RelationField
Sku struct {
field.RelationField
Product struct {
field.RelationField
}
Discount struct {
field.RelationField
}
}
}
func (a resourceHasOneShort) Where(conds ...field.Expr) *resourceHasOneShort {
@@ -296,6 +347,10 @@ type resourceHasOneLong struct {
db *gorm.DB
field.RelationField
Sku struct {
field.RelationField
}
}
func (a resourceHasOneLong) Where(conds ...field.Expr) *resourceHasOneLong {
@@ -373,6 +428,87 @@ func (a resourceHasOneLongTx) Unscoped() *resourceHasOneLongTx {
return &a
}
type resourceHasOneProduct struct {
db *gorm.DB
field.RelationField
}
func (a resourceHasOneProduct) Where(conds ...field.Expr) *resourceHasOneProduct {
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 resourceHasOneProduct) WithContext(ctx context.Context) *resourceHasOneProduct {
a.db = a.db.WithContext(ctx)
return &a
}
func (a resourceHasOneProduct) Session(session *gorm.Session) *resourceHasOneProduct {
a.db = a.db.Session(session)
return &a
}
func (a resourceHasOneProduct) Model(m *models.Resource) *resourceHasOneProductTx {
return &resourceHasOneProductTx{a.db.Model(m).Association(a.Name())}
}
func (a resourceHasOneProduct) Unscoped() *resourceHasOneProduct {
a.db = a.db.Unscoped()
return &a
}
type resourceHasOneProductTx struct{ tx *gorm.Association }
func (a resourceHasOneProductTx) Find() (result *models.Product, err error) {
return result, a.tx.Find(&result)
}
func (a resourceHasOneProductTx) Append(values ...*models.Product) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a resourceHasOneProductTx) Replace(values ...*models.Product) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a resourceHasOneProductTx) Delete(values ...*models.Product) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a resourceHasOneProductTx) Clear() error {
return a.tx.Clear()
}
func (a resourceHasOneProductTx) Count() int64 {
return a.tx.Count()
}
func (a resourceHasOneProductTx) Unscoped() *resourceHasOneProductTx {
a.tx = a.tx.Unscoped()
return &a
}
type resourceBelongsToUser struct {
db *gorm.DB