package models import ( "fmt" "github.com/astaxie/beego/orm" ) type UserCar struct { Id string `orm:"pk;size(50);column(id)" json:"id"` UserId string `orm:"column(userId)" json:"userId"` //人员id PlateNo string `orm:"column(plateNo)" json:"plateNo"` //车牌号 } func (uc *UserCar) TableName() string { return "user_car" } func (uc *UserCar) Insert() (int64,error) { o := orm.NewOrm() return o.Insert(uc) } func (uc *UserCar) GetByUserId(userId string) (all []UserCar,err error) { o := orm.NewOrm() _, err = o.QueryTable(uc.TableName()).Filter("userId", userId).All(&all) if err != nil { return nil, err } return all,nil } func (uc *UserCar) Exist(userId string, plateNo string) bool { var list []UserCar o := orm.NewOrm() o.Raw("select * from ? where userId=? and plateNo=?", uc.TableName(), userId, plateNo).QueryRows(&list) if len(list) >0 { return true } return false } func (uc *UserCar) Delete(userId string, plateNo string) (int64, error) { o := orm.NewOrm() sql := fmt.Sprintf("delete from "+uc.TableName()+" where userId='%s' and plateNo='%s'", userId, plateNo) result, err := o.Raw(sql).Exec() if err != nil { return 0, err } return result.RowsAffected() }