实现长效套餐创建逻辑,并整合不同套餐类型的创建流程
This commit is contained in:
@@ -60,6 +60,11 @@ func newBill(db *gorm.DB, opts ...gen.DOOption) bill {
|
||||
}{
|
||||
RelationField: field.NewRelation("Resource.Short", "models.ResourceShort"),
|
||||
},
|
||||
Long: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Resource.Long", "models.ResourceLong"),
|
||||
},
|
||||
}
|
||||
|
||||
_bill.fillFieldMap()
|
||||
@@ -308,6 +313,9 @@ type billBelongsToResource struct {
|
||||
Short struct {
|
||||
field.RelationField
|
||||
}
|
||||
Long struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
|
||||
func (a billBelongsToResource) Where(conds ...field.Expr) *billBelongsToResource {
|
||||
|
||||
@@ -41,6 +41,12 @@ func newResource(db *gorm.DB, opts ...gen.DOOption) resource {
|
||||
RelationField: field.NewRelation("Short", "models.ResourceShort"),
|
||||
}
|
||||
|
||||
_resource.Long = resourceHasOneLong{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Long", "models.ResourceLong"),
|
||||
}
|
||||
|
||||
_resource.fillFieldMap()
|
||||
|
||||
return _resource
|
||||
@@ -54,12 +60,14 @@ type resource struct {
|
||||
UserID field.Int32 // 用户ID
|
||||
ResourceNo field.String // 套餐编号
|
||||
Active field.Bool // 套餐状态
|
||||
Type field.Int32 // 套餐类型:1-动态,2-隧道,3-独享
|
||||
Type field.Int32 // 套餐类型:1-短效动态,2-长效动态
|
||||
CreatedAt field.Field // 创建时间
|
||||
UpdatedAt field.Field // 更新时间
|
||||
DeletedAt field.Field // 删除时间
|
||||
Short resourceHasOneShort
|
||||
|
||||
Long resourceHasOneLong
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
@@ -99,7 +107,7 @@ func (r *resource) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (r *resource) fillFieldMap() {
|
||||
r.fieldMap = make(map[string]field.Expr, 9)
|
||||
r.fieldMap = make(map[string]field.Expr, 10)
|
||||
r.fieldMap["id"] = r.ID
|
||||
r.fieldMap["user_id"] = r.UserID
|
||||
r.fieldMap["resource_no"] = r.ResourceNo
|
||||
@@ -192,6 +200,77 @@ func (a resourceHasOneShortTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
type resourceHasOneLong struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
}
|
||||
|
||||
func (a resourceHasOneLong) Where(conds ...field.Expr) *resourceHasOneLong {
|
||||
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 resourceHasOneLong) WithContext(ctx context.Context) *resourceHasOneLong {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a resourceHasOneLong) Session(session *gorm.Session) *resourceHasOneLong {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a resourceHasOneLong) Model(m *models.Resource) *resourceHasOneLongTx {
|
||||
return &resourceHasOneLongTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
type resourceHasOneLongTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a resourceHasOneLongTx) Find() (result *models.ResourceLong, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a resourceHasOneLongTx) Append(values ...*models.ResourceLong) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a resourceHasOneLongTx) Replace(values ...*models.ResourceLong) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a resourceHasOneLongTx) Delete(values ...*models.ResourceLong) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a resourceHasOneLongTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a resourceHasOneLongTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
type resourceDo struct{ gen.DO }
|
||||
|
||||
func (r resourceDo) Debug() *resourceDo {
|
||||
|
||||
Reference in New Issue
Block a user