using DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace zhengcaioa.Models
{
public class UserPage
{
///
/// 数据实体转换
///
///
///
public static List GetUserPage(string userid, List systemidlist, string page_method)
{
List entityList = new List();
var connectionString = "";
try
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json");
connectionString = builder.Build().GetValue("ConnectionStrings:" + "DefaultConnection");
}
catch
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
connectionString = builder.Build().GetValue("ConnectionStrings:" + "DefaultConnection");
}
var dbContextOptionBuilder = new DbContextOptionsBuilder();
using (var db = new zhengcaioaContext(dbContextOptionBuilder.UseSqlServer(connectionString).Options))
{
#region 超级管理员操作
var roleId = db.PltUserRoles.Where(o => o.UserId == userid).Select(o => o.RoleId).ToList(); ;//获取角色类型
var rolename = db.PltRoles.Where(o => roleId.Contains(o.Id) && o.RoleName == "系统管理员").ToList();
if (rolename!=null&& rolename.Count>0)//超级管理员操作
{
var adminList = from page in db.PltPages
where
page.RecStatus == "A" &&
new List { "M", "P" }.Contains(page.PageType)
orderby page.DisplaySeq
select new PageEntity()
{
Open_Type = page.OpenType ?? 0,
PageID = page.Id,
SystemID = page.SystemId,
PageName = page.PageName,
DisplaySeq = page.DisplaySeq ?? 0,
PagePath = page.PagePath,
PageType = page.PageType,
PageSuperior = page.PageSuperior,
PageIco = page.PageIco ?? "", //fa fa-angle-right
OpenType = page.OpenType ?? 0,
};
entityList = adminList.ToList();
}
#endregion
else
{
var list = from user in db.PltUsers
join user_role in db.PltUserRoles on user.Id equals user_role.UserId
join role in db.PltRoles on user_role.RoleId equals role.Id
join auth in db.PltAuths.Distinct() on role.Id equals auth.RoleId
join page in db.PltPages on auth.ItemId equals page.Id
where
user.Id == userid &&
user.RecStatus == "A" &&
user_role.RecStatus == "A" &&
role.RecStatus == "A" &&
auth.RecStatus == "A" &&
page.RecStatus == "A" &&
new List { "M", "P" }.Contains(page.PageType) &&
//systemidlist.Contains(page.system_id) &&
(string.IsNullOrEmpty(page_method) ? true : page.PageMethod == page_method)
orderby page.DisplaySeq
select new PageEntity()
{
Open_Type = page.OpenType ?? 0,
PageID = page.Id,
SystemID = page.SystemId,
PageName = page.PageName,
DisplaySeq = page.DisplaySeq ?? 0,
PagePath = page.PagePath,
PageType = page.PageType,
PageSuperior = page.PageSuperior,
PageIco = page.PageIco ?? "", //fa fa-angle-right
OpenType = page.OpenType ?? 0,
};
entityList = list.ToList().Distinct(new PageEntityIdComparer()).ToList();
}
}
return entityList;
}
///
/// 获取菜单信息
///
///
public static string GetMeum(string userid)
{
StringBuilder sbm = new StringBuilder();
int count = 0;
List list = GetUserPage(userid, new List { 1 }, "").OrderBy(D => D.DisplaySeq).ToList();
list.Where(d => d.PageType == "M").Where(d => d.PageSuperior == "0").OrderBy(o => o.DisplaySeq).ToList().ForEach(f =>
{
count++;
string exp = "";
if (count == 1)
{
exp = "class=\"active\"";
}
var leve2 = list.Where(d => d.PageSuperior == f.PageID && d.PageType == "M").ToList();
sbm.Append("");
sbm.Append(" ");
sbm.Append("" + f.PageName + "");
if (leve2.Count > 0)
{
sbm.Append("");
}
sbm.Append(" ");
#region 二级
if (leve2.Count > 0)
{
leve2.ForEach(t =>
{
var level3 = list.Where(d => d.PageSuperior == t.PageID && d.PageType == "M").OrderBy(o => o.DisplaySeq).ToList();
sbm.Append(" ");
sbm.Append(" - ");
string arrow3 = "";
if (level3.Count > 0)
{
arrow3 = " ";
}
sbm.Append(" ");
#region 三级
if (level3.Count > 0)
{
sbm.Append("
");
level3.ForEach(t3 =>
{
sbm.Append(" -
");
});
sbm.Append("
");
}
#endregion
sbm.Append(" ");
sbm.Append("
");
});
}
#endregion
sbm.Append("");
});
return sbm.ToString();
}
}
}