using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PMS.BusinessModels.Account;
using PMS.BusinessModels.SysManager;
using PMS.DBService.SysManager;
using PMS.EntityModels.SysManager;
using PMS.Interface.SysManager;
using QWPlatform.IService;
using QWPlatform.SystemLibrary;
using QWPlatform.SystemLibrary.Security;
using QWPlatform.SystemLibrary.Web;
using PMS.BusinessModels.Person;
using PMS.EntityModels.MedicalRecordManager;
namespace PMS.BusinessService.SysManager
{
///
/// 创 建 人:王海洋
/// 创建日期:2018-12-7
/// 功能描述:系统账户业务逻辑处理
///
public class AccountBLLService : IAccount
{
private static System.Collections.Concurrent.ConcurrentDictionary _cacheUsers = new System.Collections.Concurrent.ConcurrentDictionary();
//数据服务
private static AccountDBService db_account;
//锁定对象
private static object _lockobj = new object();
static AccountBLLService()
{
db_account = DataServiceBase.Instance();
}
#region 账户管理
//根据账户ID获取到账户信息
public UserInfo GetAccountInfo(int id)
{
if (_cacheUsers != null && _cacheUsers.ContainsKey(id))
{//从缓存中获取
return _cacheUsers[id];
}
else
{//从数据库查询
var dt = db_account.GetAccountInfo(id);
if (dt != null && dt.Rows.Count > 0)
{
var model = new UserInfo();
model.Account = dt.GetValueByName("账户");
model.ID = dt.GetValueByName("ID");
model.IsSuperAdmin = dt.GetValueByName("类型") == 1;
model.Name = dt.GetValueByName("姓名");
model.PersonID = dt.GetValueByName("人员ID");
model.Email = dt.GetValueByName("邮箱");
model.Company = dt.GetValueByName("渠道名称");
model.CompanyID = dt.GetValueByName("公司ID");
model.OrgID = dt.GetValueByName("机构ID");
model.JobCode = dt.GetValueByName("职务");
model.PersonJob = dt.GetValueByName("职务名称");
model.DefaultProjectID = dt.GetValueByName("项目ID");
model.PersonProperty = dt.GetValueByName("性质");
model.WechatID = dt.GetValueByName("微信id");
model.AuthorizeAll = dt.GetValueByName("渠道所有项目");
model.Roles = new List();
var roleIds = dt.GetValueByName("角色ID")?.Split(',');
if (roleIds != null && roleIds.Length > 0)
{//添加角色列表
model.Roles.AddRange(roleIds);
}
//查询数据权限(已执行的授权目录)
var authDt = db_account.SelectAccountAuthData(id, model.AuthorizeAll, model.CompanyID);
if (authDt != null && authDt.Rows.Count > 0)
{//如果查询到有数据授权,则添加
var list = new List();
foreach (DataRow dr in authDt.Rows)
{//读取权限列表
var itemIds = new string[] { };
var orgid = dr.GetValueByName("公司ID");
var items = dr.GetValueByName("项目ID");
if (!string.IsNullOrEmpty(items))
{
itemIds = items.Split(',');//多个项目使用","区别
}
var authdataModel = new account_authdata_model()
{
uid = id,
orgid = orgid,
items = new List()
};
if (itemIds.Length > 0)
{//如果包括了项目ID
authdataModel.items.AddRange(itemIds);
}
//添加到集合中
list.Add(authdataModel);
}//end foreach
//添加到账户授权信息
model.AuthDats = list;
}
//获取当前账户登录所在渠道(公司)的机构ID
if (model.AuthDats == null)
{//如果没有一个授权的,则实例化一次
model.AuthDats = new List();
}
if (!model.AuthDats.Exists(p => p.orgid == model.CompanyID))
{//如果数据授权信息中不包括当前机构,则将当前机构添加到授权中
model.AuthDats.Add(new account_authdata_model()
{//将当前机构添加到授权体系中
uid = model.ID,
orgid = model.CompanyID
});
}
//放入缓存中,下次不再从数据库读取
if (!_cacheUsers.ContainsKey(id))
{//添加到缓存中
_cacheUsers.TryAdd(id, model);
}
return model;
}
}
return null;
}
//移除缓存用户信息,当用户发生授权变更需要移除
public void RemoveCacheUser(int id)
{
if (_cacheUsers.ContainsKey(id))
{//移除用户
var user = new UserInfo();
_cacheUsers.TryRemove(id, out user);
}
}
///
/// 登录
///
///
///
///
public LoginResult Login(string account, string pwd, string ip)
{
var model = new AccountModel();
model.ZH = account;
model.MM = QWPlatform.SystemLibrary.Utils.Strings.MD5(pwd);
model.SetWhereColumns("账户", "密码");
//登录认证
var qmodel = db_account.Select(model);
if (qmodel != null && qmodel.ID > 0)
{//获取到账户,检查是否被锁定
if (qmodel.ZT == 0)
{//账户已被锁定
return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
}
else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
{
return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
}
else
{//登录成功,更新数据库登录状态
//获取密钥
var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
//写入到登录成功的cookie中(用户ID加密存储),有效一天
CookiesHelper.AddCookie("UserID", secret_key);
model.ZX = 1;
model.DLIP = ip;
model.DLSJ = DateTime.Now;
db_account.Update(model);
var accountId = qmodel.ID.Value;
//登录成功,更新缓存
var r = _cacheUsers.ContainsKey(accountId);
if (r)
{//移除缓存即可
UserInfo userInfo = null;
_cacheUsers.TryRemove(accountId, out userInfo);
}
return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
}
}
else
{//登录失败
return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
}
}
//获取所有机构
public string GetOrgsDataGridJson()
{
var dt = db_account.GetOrgs();
return dt.ToJson();
}
//根据机构ID获取账户目录
public string GetUserListByOrgId(string id, int page, int rows)
{
int total = 0;
DataTable dt = db_account.SelectAccountByOrgId(id, page, rows, out total);
if (dt != null)
{
return dt.ToEasyUIGridJson(total);
}
return string.Empty;
}
//账户设置角色
public bool SaveAuthRoleToAccount(int? uid, string rids)
{
rids = rids.Trim(',', ' ');
//先把所有权限删除
db_account.DeleteUserRole(uid.Value);
if (!string.IsNullOrEmpty(rids))
{//为空时回收所有授权
var rid = rids.Split(',');
for (int i = 0; i < rid.Length; i++)
{
var roleid = 0;
if (int.TryParse(rid[i], out roleid))
{//转换成功
db_account.AddUserRoleAuth(new AccountRoleModel()
{
ZHID = uid,
JSID = roleid
});
}
}
}
//需要更新缓存
RemoveCacheUser(uid.Value);
return true;
}
///
/// 批量授权
///
///
///
public int BatchAuth(string id, IEnumerable models)
{
if (models != null)
{
string[] data = id.Split(',');
for(int i=0;i 0)
{
projectids = string.Join(",", item.items);
}
var GSID = item.orgid;
//添加数据权限
db_account.AddAuth(data[i].ToInt32(),item.orgid,projectids);
}
}
}
return 1;
}
//保存账户的数据授权
public bool SaveAuthDataToAccount(IEnumerable models)
{
if (models != null)
{
//清空原来的权限
db_account.DeleteDataAuth(models.First().uid);
//移除原来的数据授权信息
foreach (var item in models)
{
var projectids = "";
if (item.items != null && item.items.Count > 0)
{
projectids = string.Join(",", item.items);
}
var model = new AccountDataModel()
{
ZHID = item.uid,
GSID = item.orgid,
XMID = projectids
};
db_account.InsertAuthDataToAccount(model);
}
//需要更新缓存
RemoveCacheUser(models.First().uid);
return true;
}
return false;
}
//根据账户ID获取对应的账户授权信息
public List GetAccountDataAuth(int id)
{
DataTable dt = db_account.SelectAccountAuthData(id);
List list = new List();
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{//循环各公司(渠道)
account_authdata_model model = new account_authdata_model();
model.orgid = dr.GetValueByName("公司ID");
model.items = new List();
//项目ID
var items = dr.GetValueByName("项目ID");
if (!string.IsNullOrEmpty(items))
{//包括了项目名称
model.items.AddRange(items.Split(','));
}
list.Add(model);
}//end foreach
return list;
}
return null;
}
//根据当前用户,当前路径,分组名,查询出可用的按钮集合
public string GetButtionsForUserRole(List roles, string menuPath, string groupName, bool isSuperAdmin)
{
var str_roles = string.Join(",", roles);
//执行数据库查询
DataTable dt = db_account.SelectModuleButtonsForUserRole(str_roles, menuPath, groupName, isSuperAdmin);
return dt.ToJson();
}
//将项目授权到指定的账户上
public bool AuthProjectToAccount(int uid, string orgid, string projectid)
{//执行账户授权项目(一般是在用户创建项目时授权项目使用)
//移除缓存(针对指定的账户需要重新从数据库读取)
this.RemoveCacheUser(uid);
return db_account.AuthProjectToAccount(uid, orgid, projectid) > 0;
}
//检查旧密码是否输入正确
public bool CheckOldPwd(int id, string pwd)
{
//密码加密
var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
return db_account.CheckOldPwd(id, md5pwd);
}
//修改密码
public bool ChangePassword(int id, string pwd)
{
//密码加密
var md5pwd = QWPlatform.SystemLibrary.Utils.Strings.StrToMD5(pwd);
return db_account.ChangePassword(id, md5pwd);
}
#endregion
#region 角色管理
//查询到角色列表
public string GetRolesList(int page, int rows)
{
int total = 0;
DataTable dt = db_account.SelectRoles(page, rows, out total);
return dt.ToEasyUIGridJson(total, null);
}
//查询可用的角色列表
public string GetRolesForActiveList(int page, int rows)
{
int total = 0;
DataTable dt = db_account.SelectRolesForActive(page, rows, out total);
return dt.ToEasyUIGridJson(total, null);
}
//获取角色授权
public string GetRoleAuthJson(int roleid)
{
var dt = db_account.SelectRoleAuth(roleid);
return dt.ToJson();
}
//获取角色模块功能列表
public string GetRoleAuthFunGridJson(int roleid, int moduleid)
{
var dt = db_account.SelectRoleAuthFuns(roleid, moduleid);
return dt.ToEasyUIGridJson(0, null);
}
//保存角色的授权信息
public bool SaveRoleAuthorzie(int rid, string mids, string bids)
{
//移除两边的","号
mids = mids.Trim(',', ' ');
bids = bids.Trim(',', ' ');
if (!string.IsNullOrEmpty(mids))
{//模块不为空则解析模块
var moduleids = mids.Split(',');
for (int i = 0; i < moduleids.Length; i++)
{//获取每个模块,进行授权。
var mid = 0;
if (int.TryParse(moduleids[i], out mid))
{//转换获取模块ID
var model = new RoleAuthModel();
model.MKID = mid;
model.JSID = rid;
//删除原来的权限
db_account.DeleteRoleAuth(model);
if (i == 0)
{//第一个节点,是授权的功能子节点
model.GNIDLB = bids;
model.SQJD = 1;
}
//执行一次插入
db_account.InsertRoleAuth(model);
}
}
return true;
}
return false;
}
//根据账户的已授权角色目录
public DataTable GetAccountRoles(int uid)
{
return db_account.SelectAccountRoles(uid);
}
//获取所有渠道下的项目信息
public string GetOrgProjecgTree()
{
var dt = db_account.SelectOrgProjectTree();
return dt.ToEasyUITreeJson("ID", "名称", "渠道ID", null, null, new string[] { "是否渠道" });
}
//根据角色id获取用户信息
public string GetUserByRoleId(int roleid, int rows, int page)
{
int total = 0;
DataTable dt = db_account.GetUserByRoleId(roleid, rows, page, out total);
return DataToEasyUI.ToEasyUIGridJson(dt, total);
}
#endregion
//保存配置文件
public bool SaveConfig(my_configinfo myconfigInfo, int account)
{
var myconfig = new NotefiyConfigInfo();
//解决问题时配置
myconfig.SolveTime = new ConfigInfo()
{
SendEmail = myconfigInfo.SolveTime_Email,
SendSMS = myconfigInfo.SolveTime_SMS,
SendSite = myconfigInfo.SolveTime_Site
};
//被指派时
myconfig.Assign = new ConfigInfo()
{
SendEmail = myconfigInfo.Assign_Email,
SendSMS = myconfigInfo.Assign_SMS,
SendSite = myconfigInfo.Assign_Site
};
//被终止时
myconfig.StopTime = new ConfigInfo()
{
SendEmail = myconfigInfo.StopTime_Email,
SendSMS = myconfigInfo.StopTime_SMS,
SendSite = myconfigInfo.StopTime_Site
};
//被回退时
myconfig.BackTime = new ConfigInfo()
{
SendEmail = myconfigInfo.BackTime_Email,
SendSMS = myconfigInfo.BackTime_SMS,
SendSite = myconfigInfo.BackTime_Site
};
myconfig.ProductID = myconfigInfo.ProductID;
myconfig.ModuleID = myconfigInfo.ModuleID;
//保存配置信息
return db_account.SaveConfig(account, myconfig);
}
//保存消息配置
public bool SaveMessageInfo(Mssage_config Mssage_config, int account)
{
return db_account.SaveMessageInfo(Mssage_config, account);
}
//获取消息配置
public Mssage_config GetMessageInfo(int account)
{
return db_account.GetMessageInfo(account);
}
//获取配置文件
public NotefiyConfigInfo GetConfigInfo(int account)
{
return db_account.GetConfigInfo(account);
}
///
/// 查询结该人员的邮件及配置信息
///
/// 人员信息ID
///
public NotefiyConfigInfo GetNotifyConfigInfoByUserId(string personId)
{
return db_account.GetNotifyConfigInfoByUserId(personId);
}
///
/// 修改个人信息
///
///
public int UpdatePersonInfo(PersonBusinessModel model)
{
return db_account.UpdatePersonInfo(model);
}
///
/// 微信登陆(密码无需再次加密)
///
///
///
///
///
public LoginResult WeChatLogin(string account, string pwd, string ip)
{
var model = new AccountModel();
model.ZH = account;
model.MM = pwd;
model.SetWhereColumns("账户", "密码");
//登录认证
var qmodel = db_account.Select(model);
if (qmodel != null && qmodel.ID > 0)
{//获取到账户,检查是否被锁定
if (qmodel.ZT == 0)
{//账户已被锁定
return new LoginResult() { Success = false, Message = "账户已被锁定,请联系管理员。" };
}
else if (string.IsNullOrEmpty(qmodel.GSID) || string.IsNullOrEmpty(qmodel.RYID))
{
return new LoginResult() { Success = false, Message = "该账户没有关联到人员信息,缺少公司ID或人员ID." };
}
else
{//登录成功,更新数据库登录状态
//获取密钥
var secret_key = DESEncrypt.DesEncrypt(model.ID.ToString());
//写入到登录成功的cookie中(用户ID加密存储),有效五天
CookiesHelper.AddCookie("UserID", secret_key,DateTime.Now.AddDays(5));
model.ZX = 1;
model.DLIP = ip;
model.DLSJ = DateTime.Now;
db_account.Update(model);
var accountId = qmodel.ID.Value;
//登录成功,更新缓存
var r = _cacheUsers.ContainsKey(accountId);
if (r)
{//移除缓存即可
UserInfo userInfo = null;
_cacheUsers.TryRemove(accountId, out userInfo);
}
return new LoginResult() { Success = true, AccountID = model.ID.Value, Message = "登录成功" };
}
}
else
{//登录失败
return new LoginResult() { Success = false, Message = "账户不存在,或密码不正确." };
}
}
///
/// 根据个人id获取账户信息
///
///
///
public DataTable GetAccountInfo(string personId)
{
return db_account.GetAccountInfo(personId);
}
///
/// 根据手机号获取系统账号信息
///
///
///
public DataTable GetTelAccountInfo(string tel)
{
return db_account.GetTelAccountInfo(tel);
}
///
/// 查询字典表
///
///
///
public List SelectNature(string ID)
{
return db_account.SelectNature(ID);
}
}
}