using AutoMapper;
|
using DTO;
|
using DTO.Models;
|
using IServices;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Threading.Tasks;
|
using zhengcaioa.IService;
|
using zhengcaioa.Models;
|
|
namespace Services
|
{
|
public class PltPageService : IPltPageService
|
{
|
private readonly zhengcaioaContext _context;
|
private readonly IMapper _mapper;
|
public PltPageService(zhengcaioaContext context, IMapper mapper)
|
{
|
_context = context;
|
_mapper = mapper;
|
}
|
|
|
public ResultEntity save(PltPageDTO pltPageDTO)
|
{
|
ResultEntity resultEntity = new ResultEntity();
|
try
|
{
|
var pltPage = _mapper.Map<PltPage>(pltPageDTO);
|
if (String.IsNullOrEmpty(pltPage.Id))
|
{
|
pltPage.Id = Guid.NewGuid().ToString();
|
_context.PltPages.Add(pltPage);
|
}
|
else
|
{
|
var updatepltPage = _context.PltPages.Find(pltPage.Id);
|
updatepltPage.SystemId = pltPage.SystemId;
|
updatepltPage.PageName = pltPage.PageName;
|
updatepltPage.PageShortcut = pltPage.PageShortcut;
|
updatepltPage.DisplaySeq = pltPage.DisplaySeq;
|
updatepltPage.PagePath = pltPage.PagePath;
|
updatepltPage.PageMethod = pltPage.PageMethod;
|
updatepltPage.PageType = pltPage.PageType;
|
updatepltPage.PageSuperior = pltPage.PageSuperior ;
|
updatepltPage.PageIco = pltPage.PageIco;
|
updatepltPage.OpenType = pltPage.OpenType;
|
|
|
|
|
|
|
|
updatepltPage.RecStatus = pltPage.RecStatus;
|
// updatepltPage.Creater = pltPage.Creater;
|
//updatepltPage.Createtime = pltPage.Createtime;
|
updatepltPage.Modifier = pltPage.Modifier;
|
updatepltPage.Modifytime = pltPage.Modifytime;
|
|
}
|
|
_context.SaveChanges();
|
resultEntity.ReturnID = pltPage.Id;
|
resultEntity.Result = true;
|
}
|
catch (Exception ex)
|
{
|
resultEntity.Result = false;
|
resultEntity.Message = "保存失败,请联系管理员";
|
|
}
|
return resultEntity;
|
}
|
|
public PltPageDTO Get(string id)
|
{
|
PltPage entity = _context.PltPages.Find(id);
|
|
if (entity.RecStatus != "A")
|
{
|
entity = new PltPage();
|
}
|
var pltPageDTO = _mapper.Map<PltPageDTO>(entity);
|
return pltPageDTO;
|
}
|
|
public ResultDataEntity<PltPageDTO> SearchByPaging(PltPageDTOSearch searchEntity)
|
{
|
ResultDataEntity<PltPageDTO> data = new ResultDataEntity<PltPageDTO>();
|
List<PltPageDTO> list = new List<PltPageDTO>();
|
//筛选
|
var query = _context.PltPages
|
.Where(b => b.RecStatus == "A")
|
.ToList();
|
|
if (!string.IsNullOrEmpty(searchEntity.SystemId))
|
{
|
query = query.Where(m => m.SystemId.Contains(searchEntity.SystemId)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PageName))
|
{
|
query = query.Where(m => m.PageName.Contains(searchEntity.PageName)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PageShortcut))
|
{
|
query = query.Where(m => m.PageShortcut.Contains(searchEntity.PageShortcut)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PagePath))
|
{
|
query = query.Where(m => m.PagePath.Contains(searchEntity.PagePath)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PageType))
|
{
|
query = query.Where(m => m.PageType.Contains(searchEntity.PageType)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PageSuperior))
|
{
|
query = query.Where(m => m.PageSuperior==searchEntity.PageSuperior).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.PageIco))
|
{
|
query = query.Where(m => m.PageIco.Contains(searchEntity.PageIco)).ToList();
|
}
|
|
|
|
|
query = query.OrderByDescending(x => x.Modifytime).ToList();
|
//if (searchEntity.totalrows == 0)
|
searchEntity.totalrows = query.Count();
|
var rolelist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
|
list = _mapper.Map<List<PltPageDTO>>(rolelist);
|
data.LoadData(searchEntity, list);
|
return data;
|
}
|
|
public ResultEntity ModifyStatus(string id, string userid)
|
{
|
|
ResultEntity result = new ResultEntity();
|
result.Result = true;
|
|
var model = _context.PltPages.Find(id);
|
if (model != null)
|
{
|
model.RecStatus = "D";
|
model.Modifier = userid;
|
model.Modifytime = DateTime.Now;
|
_context.SaveChanges();
|
}
|
|
return result;
|
}
|
|
|
/// <summary>
|
/// 获取所有权限
|
/// </summary>
|
/// <returns></returns>
|
public List<PageEntity> GePagetList()
|
{
|
List<PageEntity> entitylist = new List<PageEntity>();
|
|
|
var listpage = _context.PltPages.Where(p => p.RecStatus == "A").ToList();
|
if (listpage.Count > 0)
|
{
|
listpage.ForEach(p =>
|
{
|
entitylist.Add(new PageEntity()
|
{
|
Open_Type = p.OpenType!=null?p.OpenType.Value:0,
|
PageID = p.Id,
|
SystemID = p.SystemId,
|
PageName = p.PageName,
|
DisplaySeq = p.DisplaySeq != null ? p.DisplaySeq.Value:0,
|
PagePath = p.PagePath,
|
PageType = p.PageType,
|
PageSuperior = p.PageSuperior,
|
PageIco = (string.IsNullOrEmpty(p.PageIco) || p.PageIco == "00" || p.PageIco == "01") == true ? "fa fa-angle-right" : p.PageIco,
|
OpenType = p.DisplaySeq != null ? p.OpenType.Value : 0,
|
});
|
});
|
}
|
|
return entitylist.OrderBy(it => it.PageSuperior).ThenBy(d => d.DisplaySeq).ToList();
|
}
|
|
|
|
/// <summary>
|
/// 获取所有角色相关权限
|
/// </summary>
|
/// <returns></returns>
|
public List<PltAuthDTO> GetauthList(string roleid = "")
|
{
|
|
|
var listAuth = _context.PltAuths.Where(a => a.RecStatus == "A" &&
|
(roleid == "" ? true : a.RoleId == roleid)
|
).ToList();
|
var list = _mapper.Map<List<PltAuthDTO>>(listAuth);
|
return list;
|
}
|
|
|
/// <summary>
|
/// 设置角色权限
|
/// </summary>
|
/// <param name="roleid"></param>
|
/// <param name="pageid"></param>
|
/// <param name="User_id"></param>
|
/// <returns></returns>
|
public ResultEntity SaveManyEntity(string roleid = "", string pageid = "", string User_id = "")
|
{
|
var result = new ResultEntity();
|
result.Result = true;
|
try
|
{
|
|
#region 删除角色原来设置的权限
|
var listAuthOld = _context.PltAuths.Where(a => a.RoleId == roleid).ToList();
|
if (listAuthOld.Count > 0)
|
{
|
listAuthOld.ForEach(a => {
|
_context.PltAuths.Remove(a);
|
});
|
}
|
#endregion
|
|
#region 设置新的权限
|
var aryPage = pageid.Split(',').ToList();
|
if (aryPage.Count > 0)
|
{
|
aryPage.ForEach(p => {
|
_context.PltAuths.Add(new PltAuth()
|
{
|
Id = Guid.NewGuid().ToString(),
|
RoleId = roleid,
|
AuthType = "1",
|
ItemId =p,
|
RecStatus = "A",
|
Creater = User_id,
|
Createtime = DateTime.Now,
|
Modifier = User_id,
|
Modifytime = DateTime.Now
|
});
|
|
});
|
}
|
#endregion
|
|
_context.SaveChanges();
|
|
|
}
|
catch (Exception ex)
|
{
|
result.Result = false;
|
result.Message = ex.Message;
|
}
|
return result;
|
}
|
|
|
/// <summary>
|
/// 查询当前菜单的下属按钮
|
/// </summary>
|
/// <param name="datadt"></param>
|
/// <returns></returns>
|
public List<PageEntity> GetUserPage(string userid, string page_path)
|
{
|
List<PageEntity> entityList = new List<PageEntity>();
|
|
|
|
#region 超级管理员操作
|
var roleId = _context.PltUserRoles.Where(o => o.UserId == userid).Select(o => o.RoleId).ToList(); ;//获取角色类型
|
var rolename = _context.PltRoles.Where(o => roleId.Contains(o.Id) && o.RoleName == "系统管理员").ToList();
|
|
if (rolename != null && rolename.Count > 0)//超级管理员操作
|
{
|
var adminList = from page in _context.PltPages
|
join pagepa in _context.PltPages
|
on page.PageSuperior equals pagepa.Id
|
where
|
page.RecStatus == "A" &&
|
new List<string> { "B" }.Contains(page.PageType)
|
&& pagepa.PagePath == page_path
|
orderby page.DisplaySeq
|
select new PageEntity()
|
{
|
Open_Type = page.OpenType ?? 0,
|
PageID = page.Id,
|
SystemID = page.SystemId,
|
PageName = page.PageName,
|
PageShortcut = page.PageShortcut,
|
DisplaySeq = page.DisplaySeq ?? 0,
|
PagePath = page.PagePath,
|
PageType = page.PageType,
|
PageSuperior = page.PageSuperior,
|
PageIco = page.PageIco ?? "", //fa fa-angle-right
|
OpenType = page.OpenType ?? 0,
|
PageMethod = page.PageMethod,
|
};
|
entityList = adminList.ToList();
|
}
|
#endregion
|
else
|
{
|
var list = from user in _context.PltUsers
|
join user_role in _context.PltUserRoles on user.Id equals user_role.UserId
|
join role in _context.PltRoles on user_role.RoleId equals role.Id
|
join auth in _context.PltAuths.Distinct() on role.Id equals auth.RoleId
|
join page in _context.PltPages on auth.ItemId equals page.Id
|
join pagepa in _context.PltPages on page.PageSuperior equals pagepa.Id
|
where
|
user.Id == userid &&
|
user.RecStatus == "A" &&
|
user_role.RecStatus == "A" &&
|
role.RecStatus == "A" &&
|
auth.RecStatus == "A" &&
|
page.RecStatus == "A" &&
|
new List<string> { "B"}.Contains(page.PageType)
|
//systemidlist.Contains(page.system_id) &&
|
&& pagepa.PagePath == page_path
|
orderby page.DisplaySeq
|
select new PageEntity()
|
{
|
Open_Type = page.OpenType ?? 0,
|
PageID = page.Id,
|
SystemID = page.SystemId,
|
PageName = page.PageName,
|
PageShortcut = page.PageShortcut,
|
DisplaySeq = page.DisplaySeq ?? 0,
|
PagePath = page.PagePath,
|
PageType = page.PageType,
|
PageSuperior = page.PageSuperior,
|
PageIco = page.PageIco ?? "", //fa fa-angle-right
|
OpenType = page.OpenType ?? 0,
|
PageMethod = page.PageMethod,
|
};
|
entityList = list.ToList().Distinct(new PageEntityIdComparer()).ToList();
|
}
|
|
|
return entityList;
|
}
|
}
|
}
|