user.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package model
  2. import (
  3. "bytes"
  4. "fmt"
  5. "git.chuangxin1.com/csacred/toolkit"
  6. "git.chuangxin1.com/csacred/toolkit/example/cuser/define"
  7. )
  8. // UserModel user model layer
  9. type UserModel struct {
  10. }
  11. // Get get user info
  12. func (m *UserModel) Get(u define.FormID) (user define.User, err error) {
  13. var db = toolkit.NewDB()
  14. //if err = db.Connect(); err == nil {
  15. //defer db.Close()
  16. var (
  17. buffer bytes.Buffer
  18. where bytes.Buffer
  19. )
  20. buffer.WriteString(`select id, phone, name, password passwd, sex, `)
  21. buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
  22. buffer.WriteString(`signature, category, balance, scale, score, attr, `)
  23. buffer.WriteString(`status, sys_time stime from usr_user`)
  24. where.WriteString(` where id > 0 and id = :id`)
  25. buffer.WriteString(where.String())
  26. err = db.Row(&user, buffer.String(), u)
  27. //}
  28. return
  29. }
  30. // List get users by page
  31. func (m *UserModel) List(
  32. formUser define.FormUser) (users []define.User, total int, err error) {
  33. var db = toolkit.NewDB()
  34. //if err = db.Connect(); err == nil {
  35. //defer db.Close()
  36. var (
  37. buffer bytes.Buffer
  38. where bytes.Buffer
  39. sql bytes.Buffer
  40. )
  41. buffer.WriteString(`select id, phone, name, password passwd, sex, `)
  42. buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
  43. buffer.WriteString(`signature, category, balance, scale, score, attr, `)
  44. buffer.WriteString(`status, sys_time stime `)
  45. where.WriteString(` from usr_user where id > 0`)
  46. if len(formUser.Name) > 0 {
  47. where.WriteString(` and name like :name`)
  48. }
  49. sql.WriteString(`select count(*) total`)
  50. sql.WriteString(where.String())
  51. buffer.WriteString(where.String())
  52. buffer.WriteString(` order by id desc`)
  53. buffer.WriteString(db.Limit(formUser.Page, formUser.PageSize))
  54. fmt.Println(buffer.String(), sql.String())
  55. err = db.Row(&total, sql.String(), formUser)
  56. fmt.Println("total", total)
  57. if err != nil {
  58. return
  59. }
  60. err = db.Rows(&users, buffer.String(), formUser)
  61. fmt.Println("err:", err)
  62. //}
  63. return
  64. }