package dao

import (
	"fmt"
	"time"
)

// User 用户
type User struct {
	ID                      int64     `gorm:"primary_key;type:bigint" json:"id"`                                  //编号
	TenantId                string    `gorm:"type:varchar(12)" json:"tenantId"`                                   //租户ID
	Code                    string    `gorm:"type:varchar(12)" json:"code"`                                       //用户编号
	Account                 string    `gorm:"type:varchar(45)" json:"account"`                                    //账号
	Password                string    `gorm:"type:varchar(45)" json:"password"`                                   //密码
	Name                    string    `gorm:"type:varchar(20)" json:"name"`                                       //昵称
	RealName                string    `gorm:"type:varchar(12)" json:"realName"`                                   //真名
	Avatar                  string    `gorm:"type:varchar(500)" json:"avatar"`                                    //头像
	Email                   string    `gorm:"type:varchar(45)" json:"email"`                                      //邮箱
	Phone                   string    `gorm:"type:varchar(45)" json:"phone"`                                      //手机
	Birthday                string    `gorm:"type:string" json:"birthday"`                                        //生日
	Sex                     int       `gorm:"type:smallint" json:"sex"`                                           //生日
	RoleId                  int64     `gorm:"type:bigint" json:"roleId"`                                          //角色id 数组,分隔
	CreateUser              int64     `gorm:"type:bigint" json:"createUser"`                                      //创建人
	CreateDept              int64     `gorm:"type:bigint" json:"createDept"`                                      //创建部门
	CreateTime              time.Time `gorm:"autoCreateTime;column:create_time;type:datetime;" json:"createTime"` //新增时间
	UpdateUser              int64     `gorm:"type:bigint" json:"updateUser"`                                      //修改人
	UpdateTime              time.Time `gorm:"autoUpdateTime;column:update_time;type:datetime;" json:"updateTime"` //修改时间
	Status                  int       `gorm:"type:int  " json:"status"`                                           //状态
	IsDeleted               int       `gorm:"type:int" json:"isDeleted"`                                          //是否删除 0=未删除,1=删除
	GroupId                 int       `gorm:"type:int" json:"groupId"`                                            //用户分组id
	BigScreenIndexCameraIds string    `gorm:"type:varchar(255)" json:"bigScreenIndexCameraIds"`                   //数据大屏中摄像头保存位置
	SecuritySixScreen       string    `gorm:"type:varchar(255)" json:"securitySixScreen"`                         //安防页面六分屏
}
type JsonInt64 int64

func (ji *JsonInt64) UnmarshalJSON() {

}

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

func (c *User) GetUser() error {
	return Db.Debug().Model(&c).Where(" is_deleted = 0").Find(&c).Error
}

func (c *User) GetUserByTenantId() error {
	return Db.Debug().Model(&c).Where("tenant_id = ? and is_deleted = 0", c.TenantId).Find(&c).Error
}

func (c *User) GetUserByPwd() error {
	return Db.Debug().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, int64, error) {
	var users []User
	var counts int64
	db := Db.Debug().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.Debug().Model(&c)
	db1.Debug().Where("is_deleted = 0").Count(&counts)
	return users, counts, err
}

func (c *User) Save() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *User) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *User) Remove() error {
	return Db.Debug().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.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
}

func (c *User) GetAll() ([]User, error) {
	var users []User
	err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&users).Error
	return users, err
}

func (c *User) UpdateRoles(userIds []string, roleIds string) error {
	err := Db.Debug().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
	r := Db.Debug().Model(&c).Select("account").Where("account = ?", c.Account).First(&s).Error
	fmt.Printf("ERROR:%v\n account:%v\n return:%v\n", r, s, s != "")
	return s != ""
}

// GetAvatar 查 avatar
func (c *User) GetAvatar() {
	tx := Db.Debug().Model(&c)
	tx.Select("avatar").Where("tenant_id = ?", c.TenantId).Find(&c)
}
