package models

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type SysUser struct {
	SysUserId        int       `orm:"pk" description:"系统用户id"`
	SysUserName      string    `description:"账号"`
	SysRealName      string    `description:"姓名"`
	Password         string    `description:"密码"`
	Email            string    `description:"邮箱"`
	Phone            string    `description:"手机号"`
	AreaCode         string    `description:"手机区号"`
	SysRoleId        int       `description:"角色id"`
	SysRoleName      string    `description:"角色名称"`
	SysDepartmentId  int       `description:"所属部门id"`
	SysDepartmentId1 int       `description:"所属部门一级id"`
	SysDepartmentId2 int       `description:"所属部门二级id"`
	SysDepartmentId3 int       `description:"所属部门三级id"`
	Province         string    `description:"省"`
	City             string    `description:"市"`
	PositionName     string    `description:"职位名称"`
	PositionCode     int       `description:"职位编码"`
	IsEnabled        bool      `description:"是否启用"`
	CreateTime       time.Time `description:"创建时间"`
	ModifyTime       time.Time `description:"更新时间"`
}

type SysUserView struct {
	SysUserId         int       `orm:"pk" description:"系统用户id"`
	SysUserName       string    `description:"账号"`
	SysRealName       string    `description:"姓名"`
	Password          string    `description:"密码"`
	Email             string    `description:"邮箱"`
	Phone             string    `description:"手机号"`
	AreaCode          string    `description:"手机区号"`
	SysRoleId         int       `description:"角色id"`
	SysRoleName       string    `description:"角色名称"`
	SysDepartmentId   int       `description:"所属部门id"`
	SysDepartmentName string    `description:"所属部门全路径"`
	Province          string    `description:"省"`
	City              string    `description:"市"`
	IsEnabled         bool      `description:"是否启用"`
	PositionName      string    `description:"职位名称"`
	PositionCode      int       `description:"职位编码"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"更新时间"`
}

type SysUserMapping struct {
	SysUserId          int       `orm:"pk" description:"系统用户id"`
	SysUserName        string    `description:"账号"`
	SysRealName        string    `description:"姓名"`
	Password           string    `description:"密码"`
	Email              string    `description:"邮箱"`
	Phone              string    `description:"手机号"`
	AreaCode           string    `description:"手机区号"`
	SysRoleId          int       `description:"角色id"`
	SysRoleName        string    `description:"角色名称"`
	SysDepartmentId    int       `description:"所属部门id"`
	SysDepartmentName1 string    `description:"所属部门一级名称"`
	SysDepartmentName2 string    `description:"所属部门二级名称"`
	SysDepartmentName3 string    `description:"所属部门三级名称"`
	Province           string    `description:"省"`
	City               string    `description:"市"`
	PositionName       string    `description:"职位名称"`
	PositionCode       int       `description:"职位编码"`
	IsEnabled          bool      `description:"是否启用"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"更新时间"`
}

func (s *SysUser) Save() (err error) {
	o := orm.NewOrm()
	_, err = o.InsertOrUpdate(s, "sys_user_id")
	return
}

func (s *SysUser) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(s, cols...)
	return
}

func SaveSysUser(sysUser *SysUser, sysDepartmendPathIds []int) (err error) {
	o := orm.NewOrm()
	sysUser.SysDepartmentId1 = sysDepartmendPathIds[0]
	sysUser.SysDepartmentId2 = sysDepartmendPathIds[1]
	sysUser.SysDepartmentId3 = sysDepartmendPathIds[2]
	_, err = o.InsertOrUpdate(sysUser)
	return
}

func GetSysUserBySysUserName(sysUserName string) (item *SysUser, err error) {
	sql := `SELECT * FROM sys_user WHERE sys_user_name=?`
	o := orm.NewOrm()
	err = o.Raw(sql, sysUserName).QueryRow(&item)
	return
}

func GetSysUserByDepartmentId(sysDepartmentId int, enabled bool) (items []*SysUser, err error) {
	sql := `SELECT * FROM sys_user WHERE sys_department_id=? AND is_enabled=?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, sysDepartmentId, enabled).QueryRows(&items)
	return
}

func GetSysUserById(sysUserId int) (item *SysUser, err error) {
	sql := `SELECT * FROM sys_user WHERE sys_user_id=?`
	o := orm.NewOrm()
	err = o.Raw(sql, sysUserId).QueryRow(&item)
	return
}

func GetSysUserCountById(sysUserId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_id=?`
	o := orm.NewOrm()
	err = o.Raw(sql, sysUserId).QueryRow(&count)
	return
}

func GetSysUserCountBySysUserName(sysUserName string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_name=?`
	o := orm.NewOrm()
	err = o.Raw(sql, sysUserName).QueryRow(&count)
	return
}

func GetSysUserCount(condition string, pars []interface{}) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE 1=1`
	if condition != "" {
		sql += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sql, pars...).QueryRow(&count)
	return
}

func GetSysUserCountByRoleId(roleId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_role_id=?`
	o := orm.NewOrm()
	err = o.Raw(sql, roleId).QueryRow(&count)
	return
}

func GetSysUserListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUser, err error) {
	sql := `SELECT * FROM sys_user u WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY u.is_enabled DESC, u.modify_time DESC LIMIT ?,?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetSysUserListByIsEnabled(isEnabled bool) (items []*SysUser, err error) {
	sql := `SELECT * FROM sys_user u WHERE 1=1 AND is_enabled=?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, isEnabled).QueryRows(&items)
	return
}

func GetSysUserIdList() (items []int, err error) {
	sql := ` SELECT sys_user_id FROM sys_user WHERE 1=1 `
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
