package models

import (
	"context"
	"time"

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

type SysRole struct {
	SysRoleId   int       `orm:"pk" description:"角色id"`
	SysRoleName string    `description:"姓名"`
	CreateTime  time.Time `description:"创建时间"`
	ModifyTime  time.Time `description:"更新时间"`
}

func (s *SysRole) Add() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(s)
	return
}

func DeleteSysRoleById(roleId int) (err error) {
	sql := `DELETE FROM sys_role WHERE sys_role_id=? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, roleId).Exec()
	return
}

func GetSysRoleByRoleName(roleName string) (item *SysRole, err error) {
	sql := `SELECT * FROM sys_role WHERE sys_role_name=? `
	o := orm.NewOrm()
	err = o.Raw(sql, roleName).QueryRow(&item)
	return
}

func GetSysRoleCountByRoleName(roleName string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_role WHERE sys_role_name=? `
	o := orm.NewOrm()
	err = o.Raw(sql, roleName).QueryRow(&count)
	return
}

func UpdateSysUserRoleByRoleId(roleId int, roleName string) (err error) {
	o := orm.NewOrm()
	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
		role := &SysRole{}
		role.SysRoleId = roleId
		role.SysRoleName = roleName
		role.ModifyTime = time.Now()
		_, e := txOrm.Update(role, "sys_role_id", "sys_role_name", "modify_time")
		if e != nil {
			return e
		}
		sql := `UPDATE sys_user SET sys_role_name=? WHERE sys_role_id=?`
		_, e = txOrm.Raw(sql, roleName, roleId).Exec()
		return e
	})
	return
}

func GetSysRoleById(id int) (item *SysRole, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role WHERE sys_role_id=?`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func GetSysRoleListCount() (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM sys_role `
	err = o.Raw(sql).QueryRow(&count)
	return
}

func GetSysRoleList(startSize, pageSize int) (items []*SysRole, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role ORDER BY modify_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
	return
}

func GetSysRoleListNoPage() (items []*SysRole, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role ORDER BY modify_time DESC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// CreateMultiSysRoleMenu 删除并新增角色权限
func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenuMapping) (err error) {
	if roleId == 0 {
		return
	}
	o := orm.NewOrm()
	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
		sql := `DELETE FROM sys_role_menu_mapping WHERE sys_role_id = ?`
		_, e := txOrm.Raw(sql, roleId).Exec()
		if e != nil {
			return e
		}

		if len(items) > 0 {
			_, e = txOrm.InsertMulti(len(items), items)
			if e != nil {
				return e
			}
		}
		return nil
	})
	return
}

// GetSysRoleMenuByRoleId 获取角色关联菜单
func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenuMapping, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role_menu_mapping WHERE sys_role_id = ?`
	_, err = o.Raw(sql, roleId).QueryRows(&items)
	return
}
