func AmountGreaterThan1000(m *gdb.Model) *gdb.Model {
return m.WhereGT("amount", 1000)
}
func PaidWithCreditCard(m *gdb.Model) *gdb.Model {
return m.Where("pay_mode_sign", "credit_card")
}
func PaidWithCod(m *gdb.Model) *gdb.Model {
return m.Where("pay_mode_sign", "cod")
}
func OrderStatus(statuses []string) func(m *gdb.Model) *gdb.Model {
return func(m *gdb.Model) *gdb.Model {
return m.Where("status", statuses)
}
}
var (
m = g.Model("product_order")
)
m.Handler(AmountGreaterThan1000, PaidWithCreditCard).Scan(&orders)
// SELECT * FROM `product_order` WHERE `amount`>1000 AND `pay_mode_sign`='credit_card'
// 查找所有金额大于 1000 的信用卡订单
m.Handler(AmountGreaterThan1000, PaidWithCod).Scan(&orders)
// SELECT * FROM `product_order` WHERE `amount`>1000 AND `pay_mode_sign`='cod'
// 查找所有金额大于 1000 的 COD 订单
m.Handler(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Scan(&orders)
// SELECT * FROM `product_order` WHERE `amount`>1000 AND `status` IN('paid','shipped')
// 查找所有金额大于1000 的已付款或已发货订单
1 Comment
edx
我想写一个自动更新数据库created_time和updated_time的功能 必须借助handler实现吗