package dao

import (
	"time"
)

// User 用户
type User struct {
	ID         int       `gorm:"primary_key;type:int" json:"id"`                                    //编号
	UserName   string    `gorm:"type:varchar(12)" json:"userName"`                                  //姓名
	Sex        int       `gorm:"type:smallint;default:1" json:"sex"`                                //性别                                      //用户编号
	Account    string    `gorm:"type:varchar(45)" json:"account"`                                   //账号
	Password   string    `gorm:"type:text" json:"password"`                                         //密码
	Avatar     string    `gorm:"type:varchar(500)" json:"avatar"`                                   //头像
	Email      string    `gorm:"type:varchar(45)" json:"email"`                                     //邮箱
	Phone      string    `gorm:"type:varchar(45)" json:"phone"`                                     //手机
	RoleName   string    `gorm:"type:varchar(12)" json:"roleName"`                                  //角色名
	Birthday   time.Time `gorm:"type:datetime" json:"birthday"`                                     //生日
	DeptId     int       `gorm:"type:int" json:"deptId"`                                            //部门id
	Status     int       `gorm:"type:int;default:0" json:"status"`                                  //状态 0=可用，1禁用
	IsDeleted  int       `gorm:"type:int" json:"isDeleted"`                                         //是否删除 0=未删除,1=删除
	CreateTime time.Time `gorm:"autoCreateTime;column:create_time;type:datetime" json:"createTime"` //新增时间
	AuthId     string    `gorm:"type:varchar(12);default:'100'" json:"authId"`                      //权限id
}

func (*User) TableName() string {
	return "user"
}

func (c *User) LoginFindAccount(acc string) (*User, error) {
	var user User
	err := Db.Debug().Model(&user).Where("account = ? and status = 0", acc).First(&user).Error
	return &user, err
}

//func (c *User) GetUser() error {
//	return Db.Model(&c).Where(" is_deleted = 0").Find(&c).Error
//}
//
//func (c *User) GetUserByTenantId() error {
//	return Db.Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).Find(&c).Error
//}
//
//func (c *User) GetUserByPwd() error {
//	return Db.Model(&c).Where("tenant_id = ? and account = ? and password = ? and is_deleted = 0", c.TenantId, c.Account, c.Password).First(&c).Error
//}
//
//func (c *User) GetUsers(offset, limit int) ([]User, int, error) {
//	var users []User
//	var counts int64
//	db := Db.Model(&c)
//
//	if c.Account != "" {
//		db = db.Where("account like ?", "%"+c.Account+"%")
//	}
//	if c.RealName != "" {
//		db = db.Where("real_name like ?", "%"+c.RealName+"%")
//	}
//	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
//	db1 := Db.Model(&c)
//	db1.Where("is_deleted = 0").Count(&counts)
//	return users, int(counts), err
//}
//
//func (c *User) Save() error {
//	return Db.Model(&c).Save(&c).Error
//}
//
//func (c *User) Update() error {
//	return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
//}
//
//func (c *User) Remove() error {
//	return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime, "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
//}
//
//func (c *User) UpdatePwd(pwd string) error {
//	return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
//}
//
//func (c *User) GetAll() ([]User, error) {
//	var users []User
//	err := Db.Model(&c).Where("is_deleted = 0").Find(&users).Error
//	return users, err
//}
//
//func (c *User) UpdateRoles(userIds []string, roleIds string) error {
//	err := Db.Model(&c).Where("id in ?", userIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
//	return err
//}
//
//// IsExist :account是否已存在 true为存在,false不存在
//func (c *User) IsExist() bool {
//	var s string
//	Db.Model(&c).Select("account").Where("account = ?", c.Account).First(&s)
//	return s != ""
//}
//
//// GetAvatar 查 avatar
//func (c *User) GetAvatar() {
//	tx := Db.Model(&c)
//	tx.Select("avatar").Where("tenant_id = ?", c.TenantId).Find(&c)
//}
