tycg_carviolation_BE/Traffic.Service/Implements/RoleService.cs

210 lines
7.0 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.Models;
using Traffic.Data.ViewModels;
using Traffic.Repository.Infrastructures;
using Traffic.Service.Interfaces;
namespace Traffic.Service.Implements
{
public class RoleService : IRoleService
{
private readonly IUnitOfWork _unitOfWork;
public RoleService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// 新增權限
/// </summary>
/// <param name="roleData"></param>
/// <returns></returns>
public TradeResultModel PostRoleDataList(int byWho, RoleData roleData)
{
var result = new TradeResultModel { Success = false };
try
{
roleData.RoleName = roleData.RoleName;
roleData.CreatedOn = roleData.CreatedOn;
roleData.CreatedOn = DateTime.UtcNow.AddHours(8);
roleData.Status = roleData.Status;
var role = new AccountRole()
{
RoleName = roleData.RoleName,
CreatorId = byWho,
CreatedOn = roleData.CreatedOn,
};
var id = _unitOfWork.AccountRepository.InsertAccountRole(role);
_unitOfWork.Complete();
}
catch (Exception ex)
{
result.Message = ex.Message;
return result;
}
result.Success = true;
return result;
}
/// <summary>
/// 取得所有權限清單
/// </summary>
/// <returns></returns>
public PageViewModel<IEnumerable<RoleDataQuery>> GetRoleDataList(SearchModel searchModel)
{
var roles = _unitOfWork.AccountRepository.GetAccountRoles();
if (!string.IsNullOrEmpty(searchModel.Term))
{
roles = roles.Where(x => x.RoleName.Contains(searchModel.Term));
}
int count = -1;
int pageCount = -1;
if (searchModel.Page == 1)
{
count = roles.Count();
if (count % 10 != 0)
{
pageCount = (count / searchModel.PageSize) + 1;
}
else
{
pageCount = count / searchModel.PageSize;
}
}
switch (searchModel.Order)
{
case "Id":
roles = searchModel.IsAsc ? roles.OrderBy(x => x.Id) : roles.OrderByDescending(x => x.Id);
break;
case "RoleName":
roles = searchModel.IsAsc ? roles.OrderBy(x => x.RoleName).ThenBy(x => x.Id) : roles.OrderByDescending(x => x.RoleName).ThenByDescending(x => x.Id);
break;
default:
goto case "Id";
}
roles = roles.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
var data = roles.ToList().Select(x => new RoleDataQuery
{
Id = x.Id,
RoleName = x.RoleName,
CreateID = x.CreatorId,
CreateTime = x.CreatedOn.ToString(),
});
return new PageViewModel<IEnumerable<RoleDataQuery>>
{
RowCount = count,
PageCount = pageCount,
CurrentPage = searchModel.Page,
Data = data,
};
}
/// <summary>
/// 取得群組對應碼
/// </summary>
/// <returns></returns>
public IEnumerable<RoleCodeInfo> GetRoleInfoList()
{
var roles = _unitOfWork.AccountRepository.GetAccountRoles();
var result = roles.ToList().Select(x => new RoleCodeInfo
{
Id = x.Id,
RoleName = x.RoleName
});
return result;
}
/// <summary>
/// 取得權限設定
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<RolePermissionDataQuery> GetRolePermissionInfo(int? roleId)
{
var rolePageList = _unitOfWork.RolePageRepository.GetRolePermissionInfo(roleId);
var result = rolePageList.ToList().Select(x => new RolePermissionDataQuery
{
RoleId = x.RoleId,
RoleName = x.RoleName,
PageId = x.PageId,
PageName = x.PageName
});
return result.ToList();
}
public List<PagePermissionDataQuery> GetAllPagePermission(int roleId)
{
var pages = _unitOfWork.RolePageRepository.GetPageLists();
var PagePermission = new List<PagePermissionDataQuery>();
var hasPermission = _unitOfWork.RolePageRepository.GetRolePageByRoleId(roleId);
foreach (var item in pages)
{
if (hasPermission.Where(i=>i.PageId == item.Id).Any())
{
PagePermission.Add(new PagePermissionDataQuery
{
PageId = item.Id,
PageName = item.PageName,
PageModule = item.PageModule,
RoleId = roleId,
PageUrl = item.PageURL,
Enable = true
});
}
else
{
PagePermission.Add(new PagePermissionDataQuery
{
PageId = item.Id,
PageName = item.PageName,
PageModule = item.PageModule,
RoleId = roleId,
PageUrl = item.PageURL,
Enable = false
});
}
}
return PagePermission.ToList();
}
/// </summary>
/// 修改權限狀態
/// </summary>
/// <param name="roleData"></param>
/// <returns></returns>
public bool PutPageRoleStatus(PagePermissionData pagePermission)
{
if (pagePermission.Enable)
{
_unitOfWork.RolePageRepository.InsertRolePage(new RolePage()
{
PageId = pagePermission.PageId,
RoleId = pagePermission.RoleId
});
_unitOfWork.Complete();
return true;
}
var rolePage = _unitOfWork.RolePageRepository.GetRolePages().Where(x => x.PageId == pagePermission.PageId && x.RoleId == pagePermission.RoleId).FirstOrDefault();
if (rolePage != null)
{
_unitOfWork.RolePageRepository.DeleteRolePage(rolePage.Id);
_unitOfWork.Complete();
return true;
}
return false;
}
}
}