重构 orm 代码生成逻辑,实现 bill 接口,优化请求字段检查与 list total 查询逻辑
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user