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 PltRoleService: IPltRoleService
|
{
|
|
private readonly zhengcaioaContext _context;
|
private readonly IMapper _mapper;
|
public PltRoleService(zhengcaioaContext context, IMapper mapper)
|
{
|
_context = context;
|
_mapper = mapper;
|
}
|
|
|
public ResultEntity save(PltRoleDTO pltRoleDTO)
|
{
|
ResultEntity resultEntity = new ResultEntity();
|
try
|
{
|
var pltRole = _mapper.Map<PltRole>(pltRoleDTO);
|
if (String.IsNullOrEmpty(pltRole.Id))
|
{
|
pltRole.Id = Guid.NewGuid().ToString();
|
_context.PltRoles.Add(pltRole);
|
}
|
else
|
{
|
var updatepltRole = _context.PltRoles.Find(pltRole.Id);
|
updatepltRole.RoleName = pltRole.RoleName;
|
updatepltRole.RoleGroup = pltRole.RoleGroup;
|
|
|
updatepltRole.RecStatus = pltRole.RecStatus;
|
// updatepltRole.Creater = pltRole.Creater;
|
//updatepltRole.Createtime = pltRole.Createtime;
|
updatepltRole.Modifier = pltRole.Modifier;
|
updatepltRole.Modifytime = pltRole.Modifytime;
|
|
}
|
|
_context.SaveChanges();
|
resultEntity.ReturnID = pltRole.Id;
|
resultEntity.Result = true;
|
}
|
catch (Exception ex)
|
{
|
resultEntity.Result = false;
|
resultEntity.Message = "保存失败,请联系管理员";
|
|
}
|
return resultEntity;
|
}
|
|
public PltRoleDTO Get(string id)
|
{
|
PltRole entity = _context.PltRoles.Find(id);
|
if (entity.RecStatus != "A")
|
{
|
entity = new PltRole();
|
}
|
|
var pltRoleDTO = _mapper.Map<PltRoleDTO>(entity);
|
return pltRoleDTO;
|
}
|
|
public ResultDataEntity<PltRoleDTO> SearchByPaging(PltRoleDTOSearch searchEntity)
|
{
|
ResultDataEntity<PltRoleDTO> data = new ResultDataEntity<PltRoleDTO>();
|
List<PltRoleDTO> list = new List<PltRoleDTO>();
|
|
var listCode = (from a in _context.SysCodeDtls
|
join b in _context.SysCodes
|
on a.CodeId equals b.Id
|
where a.RecStatus == "A"
|
&& b.RecStatus == "A"
|
select new CodeDataEntity()
|
{
|
CodeId = b.Id,
|
CodeTable = b.CodeTable,
|
CodeField = b.CodeField,
|
CodeSn = a.CodeSn,
|
Comments = a.Comments,
|
Contents = a.Contents,
|
RecStatus = a.RecStatus,
|
Sort = a.Sort
|
}
|
);
|
|
//筛选
|
var query = (from a in _context.PltRoles
|
|
join b in listCode.Where(x => x.CodeTable == "plt_role" && x.CodeField == "role_group")
|
on a.RoleGroup equals b.CodeSn
|
into bsss
|
from bbb in bsss.DefaultIfEmpty()
|
where a.RecStatus == "A"
|
&& (string.IsNullOrWhiteSpace(searchEntity.RoleGroup) || a.RoleGroup == searchEntity.RoleGroup.Trim())
|
&& (string.IsNullOrWhiteSpace(searchEntity.RoleName) || a.RoleName.Contains(searchEntity.RoleName.Trim()))
|
select new PltRoleDTO
|
{
|
Id = a.Id,
|
RoleGroup = a.RoleGroup,
|
|
RoleName = a.RoleName,
|
RoleGroupName = bbb.Comments,
|
Creater = a.Creater,
|
Createtime = a.Createtime,
|
|
RecStatus = a.RecStatus,
|
Modifier = a.Modifier,
|
Modifytime = a.Modifytime,
|
}
|
).OrderByDescending(x => x.Modifytime).ToList();
|
|
//if (!string.IsNullOrEmpty(searchEntity.RoleName))
|
//{
|
// query = query.Where(m => m.RoleName.Contains(searchEntity.RoleName)).ToList();
|
//}
|
//if (!string.IsNullOrEmpty(searchEntity.RoleGroup))
|
//{
|
// query = query.Where(m => m.RoleGroup.Contains(searchEntity.RoleGroup)).ToList();
|
//}
|
|
|
|
query = query.OrderByDescending(x => x.Modifytime).ToList();
|
//if (searchEntity.totalrows == 0)
|
searchEntity.totalrows = query.Count();
|
list = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
|
//list = _mapper.Map<List<PltRoleDTO>>(rolelist);
|
data.LoadData(searchEntity, list);
|
return data;
|
}
|
|
public ResultEntity ModifyStatus(string id, string userid)
|
{
|
|
ResultEntity result = new ResultEntity();
|
result.Result = true;
|
|
var model = _context.PltRoles.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<PltRoleDTO> listRole()
|
{
|
|
|
var listRole = _context.PltRoles.Where(r => r.RecStatus == "A").ToList();
|
|
var list = _mapper.Map<List<PltRoleDTO>>(listRole);
|
return list;
|
}
|
|
|
/// <summary>
|
/// 获取所有人员角色配置信息
|
/// </summary>
|
/// <returns></returns>
|
public List<PltUserRoleDTO> GetList()
|
{
|
var listUserRole = _context.PltUserRoles.Where(u => u.RecStatus == "A").ToList();
|
var list = _mapper.Map<List<PltUserRoleDTO>>(listUserRole);
|
return list;
|
}
|
|
|
/// <summary>
|
/// 保存角色配置(角色选择账号)
|
/// </summary>
|
/// <param name="roleid"></param>
|
/// <param name="pageid"></param>
|
/// <param name="User_id"></param>
|
/// <returns></returns>
|
public ResultEntity SaveMoreEntity(string roleid = "", string userids = "", string User_id = "")
|
{
|
var result = new ResultEntity();
|
result.Result = true;
|
#region 删除角色原来设置的人员
|
try
|
{
|
|
#region 删除角色原来设置的人员
|
var listUserRoleOld = _context.PltUserRoles.Where(u => u.RoleId == roleid).ToList();
|
if (listUserRoleOld.Count > 0)
|
{
|
listUserRoleOld.ForEach(u => {
|
_context.PltUserRoles.Remove(u);
|
});
|
}
|
#endregion
|
#region 设置角色新配置的人员
|
var aryUser = userids.Split(',').ToList();
|
if (aryUser.Count > 0)
|
{
|
aryUser.ForEach(u => {
|
_context.PltUserRoles.Add(new PltUserRole()
|
{
|
Id = Guid.NewGuid().ToString(),
|
UserId = u,
|
RoleId = roleid,
|
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;
|
}
|
|
#endregion
|
|
|
return result;
|
}
|
|
|
public ResultEntity SaveUserRole(string roleid, string userid, string User_id)
|
{
|
var result = new ResultEntity();
|
result.Result = true;
|
#region 删除角色原来设置的人员
|
try
|
{
|
|
|
#region 设置角色新配置的人员
|
|
_context.PltUserRoles.Add(new PltUserRole()
|
{
|
Id = Guid.NewGuid().ToString(),
|
UserId = userid,
|
RoleId = roleid,
|
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;
|
}
|
|
#endregion
|
|
|
return result;
|
}
|
|
|
|
}
|
}
|