diff --git a/app/models/userinfomodel/user_info.go b/app/models/userinfomodel/user_info.go new file mode 100644 index 0000000..8611a27 --- /dev/null +++ b/app/models/userinfomodel/user_info.go @@ -0,0 +1,47 @@ +package userinfomodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *UserInfoModel +) + +// 实体 +type UserInfo struct { + Id int `xorm:"'Id' int(11)"` + Clientuniqueidentification string `xorm:"'ClientUniqueIdentification' varchar(255)"` + Mobile string `xorm:"'Mobile' varchar(13)"` + Status int `xorm:"'Status' TINYINT"` + Lastupdatetime time.Time `xorm:"'LastUpdateTime' datetime"` + Createtime time.Time `xorm:"'CreateTime' datetime"` +} + +// 表名 +func (m *UserInfo) TableName() string { + return "UserInfo" +} + +// 私有化,防止被外部new +type UserInfoModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *UserInfoModel { + once.Do(func() { + m = new(UserInfoModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} + +func (m *UserInfoModel) GetListByMobile(mobile string, limits ...int) (banners []*UserInfo, err error) { + banners = make([]*UserInfo, 0) + err = m.GetList(&banners, "Mobile like ?", []interface{}{"%" + mobile + "%"}, limits) + return +} diff --git a/app/services/user.go b/app/services/user.go new file mode 100644 index 0000000..75de8d3 --- /dev/null +++ b/app/services/user.go @@ -0,0 +1,55 @@ +package services + +import ( + "fmt" + "github.com/ahmetb/go-linq/v3" + "qteam/app/http/entities/backend" + "qteam/app/models/userinfomodel" + "xorm.io/builder" +) + +func GetListByMobile(mobile string, page int, limit int) (userlist []*userinfomodel.UserInfo, err error) { + limitStart := GetLimitStart(limit, page) + userlist, err = userinfomodel.GetInstance().GetListByMobile(mobile, limitStart...) + return +} + +func GetListByWhere(where map[string]interface{}, page int, limit int) (count int64, UserList []backend.UserListResponse, err error) { + conn := builder.NewCond() + UserListInfo := []userinfomodel.UserInfo{} + for k, v := range where { + if v != "" { + conn = conn.And(builder.Like{k, fmt.Sprint(v)}) + } + } + session := userinfomodel.GetInstance().GetDb().Where(conn) + + if page != 0 && limit != 0 { + session = session.Limit(page, (page-1)*limit) + } + count, err = session.FindAndCount(&UserListInfo) + + linq.From(UserListInfo).SelectT(func(in userinfomodel.UserInfo) (d backend.UserListResponse) { + d = in.ToDomain() + return d + }).ToSlice(&UserList) + + if err != nil { + return + } + return +} + +func GetLimitStart(limit int, page int) (arr []int) { + arr = make([]int, 2) + if limit <= 0 { + limit = 20 + } + arr[0] = limit + if page > 0 { + arr[1] = (page - 1) * limit + } else { + arr[1] = 0 + } + return +}