/********************************************** ** @Des: This file ... ** @Author: haodaquan ** @Date: 2017-09-16 15:42:43 ** @Last Modified by: haodaquan ** @Last Modified time: 2017-09-17 11:48:17 ***********************************************/ package backgroundm import ( "github.com/astaxie/beego/orm" "wuyebaoxiuapi/models" ) type Admin struct { Id int LoginName string RealName string Password string RoleIds string Phone string Email string Salt string LastLogin int64 LastIp string Status int CreateId int UpdateId int CreateTime int64 UpdateTime int64 Gender int NickName string AvatarUrl string Openid string School string DailyAddress string Rating int Types int // 类型(0:管理员,1:维修人员,2:保修用户) } func (a *Admin) TableName() string { return models.TableName("uc_admin") } func init() { orm.RegisterModel(new(Admin)) } func AdminAdd(a *Admin) (int64, error) { return orm.NewOrm().Insert(a) } func AdminGetByName(loginName string) (*Admin, error) { a := new(Admin) err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("login_name", loginName).One(a) if err != nil { return nil, err } return a, nil } func AdminGetList(page, pageSize int, filters ...interface{}) ([]*Admin, int64) { offset := (page - 1) * pageSize list := make([]*Admin, 0) query := orm.NewOrm().QueryTable(models.TableName("uc_admin")) if len(filters) > 0 { l := len(filters) for k := 0; k < l; k += 2 { query = query.Filter(filters[k].(string), filters[k+1]) } } total, _ := query.Count() query.OrderBy("-id").Limit(pageSize, offset).All(&list) return list, total } func AdminGetById(id int) (*Admin, error) { r := new(Admin) err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("id", id).One(r) if err != nil { return nil, err } return r, nil } func (a *Admin) Update(fields ...string) error { if _, err := orm.NewOrm().Update(a, fields...); err != nil { return err } return nil } // func RoleAuthDelete(id int) (int64, error) { // query := orm.NewOrm().QueryTable(TableName("role_auth")) // return query.Filter("role_id", id).Delete() // } // func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) { // query := orm.NewOrm().QueryTable(TableName("role_auth")) // i, _ := query.PrepareInsert() // for _, ra := range ras { // _, err := i.Insert(ra) // if err == nil { // n = n + 1 // } // } // i.Close() // 别忘记关闭 statement // return n, err // } //#########################用户####################################### // 根据openid,查找用户信息。用于用户小程序登录是否已经注册。 func FindUserInfoByOpenid(openid string)(*Admin,error) { a := new(Admin) println(a) err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("openid", openid).One(a) if err != nil { return nil, err } return a, nil } func UserAdd(a *Admin)(int64, error) { return orm.NewOrm().Insert(a) } func FindUserInfoByName(loginName string) (*Admin, error) { a := new(Admin) err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("login_name", loginName).One(a) if err != nil { return nil, err } return a, nil } func (a *Admin) UpdatePassword(fields ...string) error { if _, err := orm.NewOrm().Update(a, fields...); err != nil { return err } return nil } //##################维修工人####################### type RepairWorkerCount struct { Count int64 } // 维修工人 type RepairWorker struct { Id int LoginName string Phone string NickName string AvatarUrl string WRepairCount int64 HRepairCount int64 } // 查找维修工人数量 func (a *Admin)FindRepairWorkerCount()(int64) { o := orm.NewOrm() repairWorkerCount := new(RepairWorkerCount) err := o.Raw("SELECT count(*)" + " as count FROM pp_uc_admin WHERE types = 2").QueryRow(&repairWorkerCount) if err != nil { return 0 } return repairWorkerCount.Count } // 查找维修人列表 func (a *Admin)FindRepairWorkers(page int , pageSize int)([]*Admin,int64) { list := make([]*Admin, 0) qb, _ := orm.NewQueryBuilder("mysql") qBuilder := qb.Select("*").From("pp_uc_admin"). Where("id > 0") //qBuilder.And("types = 2") offset := (page - 1) * pageSize qBuilder.OrderBy("id"). Desc(). Limit(pageSize). Offset(offset) sql := qBuilder.String() o := orm.NewOrm() total, err := o.Raw(sql).QueryRows(&list) if err != nil { return nil,0 } return list,total }