#pragma once
#include "IAuthorityManager.h"
class CSqliteAuthorityManager : public IAuthorityManager
{
public:
	std::string GetPermissionTag(int iPermissionId) override;


	int GetPermissionId(std::string sPermissionTag) override;


	bool HasPermission(int iPermissionId) override;


	bool HasPermission(std::string sPermissionTag) override;


	bool HasPermission(std::vector<std::string> vecPermissionList) override;


	bool Login(const std::string loginName, const std::string password) override;


	bool HasLogin() override;


	User GetLoginUser() override;


	std::map<int, std::string> GetPermissionList() override;


	std::string GetLastError() override;

public:
	std::map<int, Permission> GetPermissions() override;
	std::map<int, User> GetUsers(bool bAll=false) override;
	std::map<int, Role> GetRoles() override;

	std::map<int, int> GetRolesByUser(int userId) override;
	std::map<int, Permission> GetPermissionsByUser(int userId) override;
	std::map<int, int> GetPermissionsByRole(int roleId);

	bool AddUser(const User& user) override;
	bool DeleteUser(int userId, bool bSetRemoveTag=false) override;
	bool DeleteUser(const std::string loginName) override;
	bool EnableUser(int userId, bool bEnable) override;
	bool MotifyUser(const User& user, bool bPassword = false) override;

	bool AddRole(const Role& role) override;
	bool DeleteRole(int roleId) override;

	bool MotifyRole(const Role& role) override;

	bool AddPermission(const Permission& permission) override;
	bool DeletePermission(int permissionId) override;
	bool MotifyPermission(const Permission& permission) override;

	bool UpdateUserRoles(int userId, std::vector<int> roleIdList) override;
	bool UpdateRolePermissions(int roleId, std::vector<int> permissionIdList) override;
public:
	bool Init();
	bool CheckExistUser(std::string loginName, bool& bExist);
	bool CheckExistRole(std::string roleName, bool& bExist);
	bool CheckExistPermission(std::string tagName, bool& bExist);
private:
	//! 拥有的权限列表
	std::vector<Permission> m_permissionList;
	//! 用户缓存
	std::map<int, User> m_mapUsers;
	//! 角色缓存
	std::map<int, Role> m_mapRoles;

	//!权限缓存
	std::map<int, Permission> m_mapPermission;

	//!当前登录用户拥有的权限
	std::map<std::string, Permission> m_mapPermissionTag;
	QSqlDatabase m_db;
	QSqlTableModel* m_ModelUser;
	QSqlTableModel* m_ModelRole;
	QSqlTableModel* m_ModelPermission;
	User m_LoginUser;
	bool m_bHasLogin = false;
	std::string m_LastError;
	int m_ErrCode = 0;
};
