1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package model
- import (
- "bytes"
- "fmt"
- "git.chuangxin1.com/csacred/toolkit"
- "git.chuangxin1.com/csacred/toolkit/example/cuser/define"
- )
- // UserModel user model layer
- type UserModel struct {
- }
- // Get get user info
- func (m *UserModel) Get(u define.FormID) (user define.User, err error) {
- var db = toolkit.NewDB()
- //if err = db.Connect(); err == nil {
- //defer db.Close()
- var (
- buffer bytes.Buffer
- where bytes.Buffer
- )
- buffer.WriteString(`select id, phone, name, password passwd, sex, `)
- buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
- buffer.WriteString(`signature, category, balance, scale, score, attr, `)
- buffer.WriteString(`status, sys_time stime from usr_user`)
- where.WriteString(` where id > 0 and id = :id`)
- buffer.WriteString(where.String())
- err = db.Row(&user, buffer.String(), u)
- //}
- return
- }
- // List get users by page
- func (m *UserModel) List(
- formUser define.FormUser) (users []define.User, total int, err error) {
- var db = toolkit.NewDB()
- //if err = db.Connect(); err == nil {
- //defer db.Close()
- var (
- buffer bytes.Buffer
- where bytes.Buffer
- sql bytes.Buffer
- )
- buffer.WriteString(`select id, phone, name, password passwd, sex, `)
- buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
- buffer.WriteString(`signature, category, balance, scale, score, attr, `)
- buffer.WriteString(`status, sys_time stime `)
- where.WriteString(` from usr_user where id > 0`)
- if len(formUser.Name) > 0 {
- where.WriteString(` and name like :name`)
- }
- sql.WriteString(`select count(*) total`)
- sql.WriteString(where.String())
- buffer.WriteString(where.String())
- buffer.WriteString(` order by id desc`)
- buffer.WriteString(db.Limit(formUser.Page, formUser.PageSize))
- fmt.Println(buffer.String(), sql.String())
- err = db.Row(&total, sql.String(), formUser)
- fmt.Println("total", total)
- if err != nil {
- return
- }
- err = db.Rows(&users, buffer.String(), formUser)
- fmt.Println("err:", err)
- //}
- return
- }
|