package system

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type SysRoleAddReq struct {
	RoleName string `description:"角色名称"`
	RoleType string `description:"角色类型"`
}

type SysRole struct {
	RoleId       int       `gorm:"primaryKey"`
	RoleName     string    `description:"角色名称"`
	RoleType     string    `description:"角色类型"`
	RoleTypeCode string    `description:"角色类型编码"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time
}

type SysRoleItem struct {
	RoleId       int       `orm:"column(role_id);pk" description:"角色ID"`
	RoleName     string    `description:"角色名称"`
	RoleType     string    `description:"角色类型"`
	RoleTypeCode string    `description:"角色类型编码"`
	CreateTime   time.Time `description:"创建时间"`
	RoleLevel    int       `description:"角色等级:0-表示一级角色,每个账号只能绑定一个一级角色 ,1-表示二级角色,每个账号可以绑定多个二级 角色"`
}

func GetSysRoleCount(roleName string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_role WHERE role_name=? `
	err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&count).Error
	return
}

func AddSysRole(item *SysRole) (lastId int64, err error) {
	err = global.DEFAULT_DmSQL.Create(item).Error
	return
}

type SysRoleEditReq struct {
	RoleId   int    `description:"角色ID"`
	RoleName string `description:"角色名称"`
	RoleType string `description:"角色类型"`
}

func GetSysRoleById(roleId int) (item *SysRoleItem, err error) {
	sql := `SELECT * FROM sys_role WHERE role_id=? `
	err = global.DEFAULT_DmSQL.Raw(sql, roleId).First(&item).Error
	return
}

func GetSysRoleByName(roleName string) (item *SysRoleItem, err error) {
	sql := `SELECT * FROM sys_role WHERE role_name=? `
	err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&item).Error
	return
}

func GetSysRoleByRoleTypeCode(roleTypeCode string) (item *SysRoleItem, err error) {
	sql := `SELECT * FROM sys_role WHERE role_type_code=? `
	err = global.DEFAULT_DmSQL.Raw(sql, roleTypeCode).First(&item).Error
	return
}
func ModifySysRole(roleName, roleType, roleTypeCode string, groupId int) (err error) {
	sql := `UPDATE sys_role SET role_name=?,role_type=?,role_type_code=?,modify_time=NOW() WHERE role_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, roleName, roleType, roleTypeCode, groupId).Error
	return
}

type SysRoleDeleteReq struct {
	RoleId int `description:"角色ID"`
}

func DeleteSysRole(roleId int) (err error) {
	sql := `DELETE FROM sys_role WHERE role_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
	return
}

func GetSysRoleListCount(condition string, pars []interface{}) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
	return
}

func GetSysRoleList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleItem, err error) {
	sql := `SELECT * FROM sys_role WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += `ORDER BY modify_time DESC LIMIT ?,?`
	pars = append(pars, startSize)
	pars = append(pars, pageSize)

	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
	return
}

type SysRoleListResp struct {
	List   []*SysRoleItem
	Paging *paging.PagingItem `description:"分页数据"`
}

type RoleMenu struct {
	MenuId int    `description:"导航唯一标识"`
	Name   string `description:"导航名称"`
	NameEn string `description:"导航名称(英文)"`
}

type RoleMenuList struct {
	MenuId    int         `description:"导航唯一标识"`
	Name      string      `description:"导航名称"`
	NameEn    string      `description:"导航名称(英文)"`
	Child     []*RoleMenu `gorm:"-"`
	CheckList []int
}

type RoleMenuListResp struct {
	List []*RoleMenuList
}

func GetRoleMenuList() (items []*RoleMenuList, err error) {
	sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
	err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
	return
}

func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
	sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
	err = global.DEFAULT_DmSQL.Raw(sql, parentId).Find(&items).Error
	return
}

func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
	sql := ` SELECT b.menu_id FROM sys_role_menu AS a
				INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
				WHERE a.role_id=? AND a.type = 0
				AND b.parent_id=? `
	err = global.DEFAULT_DmSQL.Raw(sql, roleId, parentId).Find(&items).Error
	return
}

type RoleMenusAddReq struct {
	RoleId          int    `description:"角色ID"`
	MenuIdStr       string `description:"MenuId,多个用英文逗号隔开"`
	MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
}

func DeleteRoleMenuByRoleId(roleId int) (err error) {
	sql := `DELETE FROM sys_role_menu WHERE role_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
	return

}

type SysRoleMenu struct {
	RoleMenuId int `gorm:"primaryKey"`
	RoleId     int
	MenuId     int
	Type       int `description:"类型:0-全选;1-半选(此字段仅用于前端的回显)"`
}

func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
	err = global.DEFAULT_DmSQL.Create(item).Error
	return
}

func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
	if roleId == 0 {
		return
	}
	tx := global.DEFAULT_DmSQL.Begin()
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()

	sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
	err = tx.Exec(sql, roleId).Error
	if err != nil {
		return
	}

	if len(items) > 0 {
		err = tx.CreateInBatches(items, utils.MultiAddNum).Error
	}
	return
}

func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
	sql := `SELECT * FROM sys_role WHERE 1=1`
	if levelFlag {
		sql += ` and role_level = 0`
	}
	sql += ` ORDER BY create_time DESC `
	err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
	return
}

type SysRoleAllResp struct {
	List []*SysRoleItem
}

func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
	if len(roleCodes) == 0 {
		return
	}
	sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
	err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Scan(&roleIds).Error
	return
}

func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
	sql := `SELECT
				*
			FROM
				sys_role_menu
			WHERE
				role_id = ?`
	err = global.DEFAULT_DmSQL.Raw(sql, roleId).Find(&items).Error
	return
}

func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
	if len(roleCodes) == 0 {
		return
	}
	sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
	err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Find(&items).Error
	return
}