kefu/models/users.go

349 lines
9.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package models
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"kefu/common"
"kefu/tools"
"kefu/types"
"math/rand"
"strconv"
"time"
)
type User struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ExpiredAt types.Time `json:"expired_at"`
Name string `json:"name"`
Password string `json:"password"`
Nickname string `json:"nickname"`
Avator string `json:"avator"`
Pid uint `json:"pid"`
RecNum uint `json:"rec_num"`
AgentNum uint `json:"agent_num"`
Status uint `json:"status"`
OnlineStatus uint `json:"online_status"`
EntId string `json:"ent_id" sql:"-"`
RoleName string `json:"role_name" sql:"-"`
RoleId string `json:"role_id" sql:"-"`
Email string `json:"email"`
CompanyPic string `json:"company_pic"`
Tel string `json:"tel"`
Uuid string `json:"uuid"`
orderBy string
}
func CreateUser(name string, password string, avator string, nickname string, pid, agentNum uint, status uint, expired types.Time) uint {
user := &User{
Name: name,
Password: password,
Avator: avator,
Nickname: nickname,
Pid: pid,
RecNum: 0,
Status: status,
OnlineStatus: 1,
AgentNum: agentNum,
ExpiredAt: expired,
}
user.UpdatedAt = time.Now()
DB.Create(user)
return user.ID
}
func UpdateUser(id string, name string, password string, avator string, nickname, email, tel string, agentNum uint) {
user := &User{
Name: name,
Avator: avator,
Nickname: nickname,
AgentNum: agentNum,
Email: email,
Tel: tel,
}
user.UpdatedAt = time.Now()
if password != "" {
user.Password = password
}
DB.Model(&User{}).Where("id = ?", id).Update(user)
}
func UpdateUserRecNum(name string, num interface{}) {
user := &User{}
DB.Model(user).Where("name = ?", name).Update("RecNum", gorm.Expr("rec_num + ?", num))
}
func UpdateUserRecNumZero(name string, num uint) {
values := map[string]uint{
"rec_num": num,
}
DB.Model(&User{}).Where("name = ?", name).Update(values)
}
func UpdateUserStatusWhere(status uint, query interface{}, args ...interface{}) {
values := map[string]uint{
"Status": status,
}
DB.Model(&User{}).Where(query, args...).Update(values)
}
func UpdateUserExpiredWhere(expiredTime string, query interface{}, args ...interface{}) {
values := map[string]types.Time{
"ExpiredAt": {tools.TimeStrToTime(expiredTime)},
}
DB.Model(&User{}).Where(query, args...).Update(values)
}
func UpdateUserPass(name string, pass string) {
user := &User{
Password: pass,
}
user.UpdatedAt = time.Now()
DB.Model(user).Where("name = ?", name).Update("Password", pass)
}
func UpdateUserAvator(name string, avator string) {
user := &User{
Avator: avator,
}
user.UpdatedAt = time.Now()
DB.Model(user).Where("name = ?", name).Update("Avator", avator)
}
func UpdateUserTel(name string, phone string) {
user := &User{
Tel: phone,
}
user.UpdatedAt = time.Now()
DB.Model(user).Where("name = ?", name).Update("Tel", phone)
}
func UpdateKefuInfoByName(name, avator, nickname string) {
user := &User{
Avator: avator,
Nickname: nickname,
}
user.UpdatedAt = time.Now()
DB.Model(user).Where("name = ?", name).Update(user)
}
func FindUser(username string) User {
var user User
DB.Where("name = ?", username).First(&user)
return user
}
func FindUserByUid(id interface{}) User {
var user User
DB.Where("id = ?", id).First(&user)
return user
}
func FindUserRoleById(id interface{}) User {
var user User
DB.Select("user.*,user_role.role_id role_id").Joins("join user_role on user.id=user_role.user_id").Where("user.id = ?", id).First(&user)
return user
}
func FindUserById(id interface{}) User {
var user User
DB.Select("user.*,role.name role_name,role.id role_id").Joins("join user_role on user.id=user_role.user_id").Joins("join role on user_role.role_id=role.id").Where("user.id = ?", id).First(&user)
return user
}
func FindUserByIdPid(pid interface{}, id interface{}) User {
var user User
DB.Select("user.*,role.name role_name,role.id role_id").Joins("join user_role on user.id=user_role.user_id").Joins("join role on user_role.role_id=role.id").Where("user.id = ? and user.pid = ?", id, pid).First(&user)
return user
}
func FindUserWhere(query interface{}, args ...interface{}) User {
var user User
DB.Where(query, args...).First(&user)
return user
}
func DeleteUserById(id string) {
DB.Where("id = ?", id).Delete(User{})
}
func DeleteUserByIdPid(id string, pid interface{}) {
DB.Where("id = ? and pid=?", id, pid).Delete(User{})
}
func FindUsers() []User {
var users []User
DB.Select("user.*,role.name role_name").Joins("left join user_role on user.id=user_role.user_id").Joins("left join role on user_role.role_id=role.id").Order("user.id desc").Find(&users)
return users
}
func FindUsersByEntId(entId interface{}) []User {
var users []User
users = FindUsersByPid(entId)
return users
}
func FindUsersByPid(pid interface{}) []User {
var users []User
DB.Where("id=? or pid=?", pid, pid).Order("rec_num asc").Find(&users)
return users
}
func FindUsersWhere(query interface{}, args ...interface{}) []User {
var users []User
DB.Where(query, args...).Order("rec_num asc").Find(&users)
return users
}
func FindUserRole(query interface{}, id interface{}) User {
var user User
DB.Select(query).Where("user.id = ?", id).Joins("join user_role on user.id=user_role.user_id").Joins("join role on user_role.role_id=role.id").First(&user)
return user
}
// 查询条数
func CountUsers() uint {
var count uint
DB.Model(&User{}).Count(&count)
return count
}
// 根据where查询条数
func CountUsersWhere(query interface{}, args ...interface{}) uint {
var count uint
DB.Model(&User{}).Where(query, args...).Count(&count)
return count
}
// 根据where分页查询
func FindUsersOwn(page uint, pagesize uint, query interface{}, args ...interface{}) []User {
if pagesize == 0 {
pagesize = common.PageSize
}
offset := (page - 1) * pagesize
if offset < 0 {
offset = 0
}
var users []User
DB.Select("user.*,role.name role_name,role.id role_id").Joins("left join user_role on user.id=user_role.user_id").Joins("left join role on user_role.role_id=role.id").Where(query, args...).Offset(offset).Order("user.updated_at desc").Limit(pagesize).Find(&users)
return users
}
func FindUsersPages(page uint, pagesize uint) []User {
offset := (page - 1) * pagesize
if offset < 0 {
offset = 0
}
var users []User
DB.Select("user.*,role.name role_name").Joins("left join user_role on user.id=user_role.user_id").Joins("left join role on user_role.role_id=role.id").Offset(offset).Order("user.id desc").Limit(pagesize).Find(&users)
return users
}
// 获取一条用户信息
func (user *User) AddUser() (uint, error) {
db := DB.Create(user)
return user.ID, db.Error
}
// 获取一条用户信息
func (user *User) GetOneUser(fields string) User {
var dUser User
var userRole User_role
myDB := user.buildQuery()
myDB.Select(fields).First(&dUser)
if dUser.ID != 0 {
userRole = FindRoleByUserId(dUser.ID)
entId := ""
if userRole.RoleId == 3 {
entId = fmt.Sprintf("%v", dUser.Pid)
} else {
entId = fmt.Sprintf("%v", dUser.ID)
}
dUser.EntId = entId
dUser.RoleId = strconv.Itoa(int(userRole.RoleId))
}
if userRole.RoleId != 0 {
role := FindRole(userRole.RoleId)
dUser.RoleName = role.Name
}
return dUser
}
// 获取多条用户信息
func (user *User) GetUsers(fields string) []User {
var users []User
myDB := user.buildQuery()
myDB.Select(fields).Find(&users)
return users
}
// 更新user
func (user *User) UpdateUser() {
user.UpdatedAt = time.Now()
user.buildQuery().Model(&User{}).Update(user)
}
// 更新user
func UpdateUser2(result map[string]string, query interface{}, args ...interface{}) error {
result["updated_at"] = tools.GetNowTime()
db := DB.Table("user").Where(query, args...).Update(result)
return db.Error
}
// 更新user
func (user *User) SaveUser(query interface{}, args ...interface{}) error {
user.UpdatedAt = time.Now()
db := DB.Table("user").Where(query, args...).Update(user)
return db.Error
}
// 查询构造
func (user *User) buildQuery() *gorm.DB {
userDB := DB
userDB.Model(user)
if user.ID != 0 {
userDB = userDB.Where("id = ?", user.ID)
}
if user.Pid != 0 {
userDB = userDB.Where("pid = ?", user.Pid)
}
if user.Name != "" {
userDB = userDB.Where("name = ?", user.Name)
}
if user.Tel != "" {
userDB = userDB.Where("tel = ?", user.Tel)
}
if user.Email != "" {
userDB = userDB.Where("email = ?", user.Email)
}
if user.orderBy != "" {
userDB = userDB.Order(user.orderBy)
}
return userDB
}
// 设置属性
func (this *User) SetOrder(orderBy string) {
this.orderBy = orderBy
}
// 获检查用户的状态
func (this *User) CheckStatusExpired() (bool, uint, string) {
if this.ID == 0 || this.Status == 0 {
return false, types.ApiCode.ACCOUNT_NO_EXIST, types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_NO_EXIST)
}
if int64(this.Status) == types.Constant.AccountForbidden {
return false, types.ApiCode.ACCOUNT_FORBIDDEN, types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_FORBIDDEN)
}
//查看过期时间
nowSecond := time.Now().Unix()
expireSecond := this.ExpiredAt.Unix()
if expireSecond != 0 && expireSecond < nowSecond {
return false, types.ApiCode.ACCOUNT_EXPIRED, types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_EXPIRED)
}
return true, 0, ""
}
// golang实现排序效果类似 mysql ORDER BY rec_num asc ,rand()
type ByRecNumRand []User
func (a ByRecNumRand) Len() int {
return len(a)
}
func (a ByRecNumRand) Less(i, j int) bool {
if a[i].RecNum < a[j].RecNum {
return true
} else if a[i].RecNum > a[j].RecNum {
return false
} else {
// 如果rec_num相同则使用随机数进行排序
return rand.Intn(2) == 0
}
}
func (a ByRecNumRand) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}