admin.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**********************************************
  2. ** @Des: This file ...
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-16 15:42:43
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 11:48:17
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "github.com/astaxie/beego/orm"
  11. "wuyebaoxiuapi/models"
  12. )
  13. type Admin struct {
  14. Id int
  15. LoginName string
  16. RealName string
  17. Password string
  18. RoleIds string
  19. Phone string
  20. Email string
  21. Salt string
  22. LastLogin int64
  23. LastIp string
  24. Status int
  25. CreateId int
  26. UpdateId int
  27. CreateTime int64
  28. UpdateTime int64
  29. Gender int
  30. NickName string
  31. AvatarUrl string
  32. Openid string
  33. School string
  34. DailyAddress string
  35. Rating int
  36. Types int // 类型(0:管理员,1:维修人员,2:保修用户)
  37. }
  38. func (a *Admin) TableName() string {
  39. return models.TableName("uc_admin")
  40. }
  41. func init() {
  42. orm.RegisterModel(new(Admin))
  43. }
  44. func AdminAdd(a *Admin) (int64, error) {
  45. return orm.NewOrm().Insert(a)
  46. }
  47. func AdminGetByName(loginName string) (*Admin, error) {
  48. a := new(Admin)
  49. err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("login_name", loginName).One(a)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return a, nil
  54. }
  55. func AdminGetList(page, pageSize int, filters ...interface{}) ([]*Admin, int64) {
  56. offset := (page - 1) * pageSize
  57. list := make([]*Admin, 0)
  58. query := orm.NewOrm().QueryTable(models.TableName("uc_admin"))
  59. if len(filters) > 0 {
  60. l := len(filters)
  61. for k := 0; k < l; k += 2 {
  62. query = query.Filter(filters[k].(string), filters[k+1])
  63. }
  64. }
  65. total, _ := query.Count()
  66. query.OrderBy("-id").Limit(pageSize, offset).All(&list)
  67. return list, total
  68. }
  69. func AdminGetById(id int) (*Admin, error) {
  70. r := new(Admin)
  71. err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("id", id).One(r)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return r, nil
  76. }
  77. func (a *Admin) Update(fields ...string) error {
  78. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. // func RoleAuthDelete(id int) (int64, error) {
  84. // query := orm.NewOrm().QueryTable(TableName("role_auth"))
  85. // return query.Filter("role_id", id).Delete()
  86. // }
  87. // func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) {
  88. // query := orm.NewOrm().QueryTable(TableName("role_auth"))
  89. // i, _ := query.PrepareInsert()
  90. // for _, ra := range ras {
  91. // _, err := i.Insert(ra)
  92. // if err == nil {
  93. // n = n + 1
  94. // }
  95. // }
  96. // i.Close() // 别忘记关闭 statement
  97. // return n, err
  98. // }
  99. //#########################用户#######################################
  100. // 根据openid,查找用户信息。用于用户小程序登录是否已经注册。
  101. func FindUserInfoByOpenid(openid string)(*Admin,error) {
  102. a := new(Admin)
  103. println(a)
  104. err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("openid", openid).One(a)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return a, nil
  109. }
  110. func UserAdd(a *Admin)(int64, error) {
  111. return orm.NewOrm().Insert(a)
  112. }
  113. func FindUserInfoByName(loginName string) (*Admin, error) {
  114. a := new(Admin)
  115. err := orm.NewOrm().QueryTable(models.TableName("uc_admin")).Filter("login_name", loginName).One(a)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return a, nil
  120. }
  121. func (a *Admin) UpdatePassword(fields ...string) error {
  122. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  123. return err
  124. }
  125. return nil
  126. }
  127. //##################维修工人#######################
  128. type RepairWorkerCount struct {
  129. Count int64
  130. }
  131. // 维修工人
  132. type RepairWorker struct {
  133. Id int
  134. LoginName string
  135. Phone string
  136. NickName string
  137. AvatarUrl string
  138. WRepairCount int64
  139. HRepairCount int64
  140. }
  141. // 查找维修工人数量
  142. func (a *Admin)FindRepairWorkerCount()(int64) {
  143. o := orm.NewOrm()
  144. repairWorkerCount := new(RepairWorkerCount)
  145. err := o.Raw("SELECT count(*)" +
  146. " as count FROM pp_uc_admin WHERE types = 2").QueryRow(&repairWorkerCount)
  147. if err != nil {
  148. return 0
  149. }
  150. return repairWorkerCount.Count
  151. }
  152. // 查找维修人列表
  153. func (a *Admin)FindRepairWorkers(page int , pageSize int)([]*Admin,int64) {
  154. list := make([]*Admin, 0)
  155. qb, _ := orm.NewQueryBuilder("mysql")
  156. qBuilder := qb.Select("*").From("pp_uc_admin").
  157. Where("id > 0")
  158. //qBuilder.And("types = 2")
  159. offset := (page - 1) * pageSize
  160. qBuilder.OrderBy("id").
  161. Desc().
  162. Limit(pageSize).
  163. Offset(offset)
  164. sql := qBuilder.String()
  165. o := orm.NewOrm()
  166. total, err := o.Raw(sql).QueryRows(&list)
  167. if err != nil {
  168. return nil,0
  169. }
  170. return list,total
  171. }