123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /**********************************************
- ** @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
- }
|