using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using VOL.Core.Const;
using VOL.Core.Enums;
using VOL.Core.ManageUser;
using VOL.Core.UserManager;
namespace VOL.Core.Infrastructure
{
public static class DictionaryHandler
{
/*2020.05.01增加根据用户信息加载字典数据源sql*/
///
/// 获取自定义数据源sql
///
///
///
///
public static string GetCustomDBSql(string dicNo, string originalSql,string CompanyField)
{
switch (dicNo)
{
case "roles":
originalSql = GetRolesSql(originalSql);
break;
//2020.05.24增加绑定table表时,获取所有的角色列表
//注意,如果是2020.05.24之前获取的数据库脚本
//请在菜单【下拉框绑定设置】添加一个字典编号【t_roles】,除了字典编号,其他内容随便填写
case "t_roles":
originalSql = GetRolesSql();
break;
case "company":
if (string.IsNullOrEmpty(CompanyField))
return originalSql;
break;
default:
break;
}
var curruserinfo = UserContext.Current.UserInfo;
if ( (CompanyField != "" && !string.IsNullOrWhiteSpace(originalSql))|| (!string.IsNullOrWhiteSpace(originalSql)&&(originalSql.IndexOf("##useridfield") >= 0 || originalSql.IndexOf("##companyfield") >= 0 || originalSql.IndexOf("##STOREHOUSEField") >= 0)) )
{
originalSql = originalSql.Replace(Convert.ToChar(10), ' ');
var corpid = curruserinfo.CompanyId;
if (curruserinfo.LimitInCompany)
originalSql = originalSql.Replace("##companyfield", CompanyField)
.Replace("##companyvalue", corpid);
else
originalSql = originalSql.Replace("##companyfield", "'1'")
.Replace("##companyvalue", "1");
if (curruserinfo.LimitInCompany)
{
originalSql = originalSql.Replace("##useridfield", "USERID")
.Replace("##useridvalue", curruserinfo.User_Id.ToString());
}
else
originalSql = originalSql.Replace("##useridfield", "'1'")
.Replace("##useridvalue", "1");
if (curruserinfo.LimitBySTORAGE)
{
originalSql = originalSql.Replace("##STOREHOUSEfield", "STOREHOUSE")
.Replace("##STOREHOUSEvalue", $"select AREACODE from OP_WMS_STOREHOUSE where id in (select pid from OP_WMS_STOREHOUSE_USERLIMIT where userid = '{curruserinfo.User_Id}')");
}
else
originalSql = originalSql.Replace("##STOREHOUSEfield", "'1'")
.Replace("##STOREHOUSEvalue", "'1'");
}
if (originalSql.IndexOf("##useridfield") > 0 || originalSql.IndexOf("##useridvalue") > 0) {
if (originalSql.IndexOf("##useridfield") > 0)
originalSql = originalSql.Replace("##useridfield", "'1'");
if (originalSql.IndexOf("##useridvalue") > 0)
originalSql = originalSql.Replace("##useridvalue", "1");
}
return originalSql;
}
///
/// 2020.05.24增加绑定table表时,获取所有的角色列表
///
///
///
///
public static string GetRolesSql()
{
if (DBType.Name == DbCurrentType.PgSql.ToString())
{
return "SELECT \"Role_Id\" as key,\"RoleName\" as value from Sys_Role_Core";
}
return $@"SELECT Role_Id as 'key',RoleName as 'value' FROM Sys_Role_Core
WHERE Enable=1 ";
}
///
/// 获取解决的数据源,只能看到自己与下级所有角色
///
///
///
///
public static string GetRolesSql(string originalSql)
{
if (UserContext.Current.IsSuperAdmin)
{
return originalSql;
}
string currnetRoleId = UserContext.Current.RoleId;
List roleIds = RoleContext.GetAllChildrenIds(currnetRoleId);
roleIds.Add(currnetRoleId);
string sql = $@"SELECT Role_Id as 'key',RoleName as 'value' FROM Sys_Role_Core
WHERE Enable=1 and Role_Id in ('{string.Join("','", roleIds)}')";
return sql;
}
}
}