using DapperORMCore.Context.DataContext;
using DapperORMCore.Context.Extend;
using DapperORMCore.Model.BaseModel;
using DapperORMCore.Model.CoreModel;
using DapperORMCore.String.Consts;
using DapperORMCore.String.Enums;
using NXWMS.IService.NXWMS;
using NXWMS.Model.AppModels.Condition;
using System;
using System.Collections.Generic;
using System.Text;
using WestDistance.DapperORM.Repository.Repositorys;
using DapperORMCore.Context;
using NXWMS.Code;
using System.Linq;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Logging;
using Microsoft.Extensions.Configuration;
using NXWMS.String.Enums;
using System.Data;
using NXWMS.Code.Converter;
using NXWMS.DataAccess.Entity;
using DapperORMCore.Repository.IRepositorys;
using DapperORMCore.Dapper.BaseModel;
using NXWMS.IService.NXWMS.SysSettings;
using NXWMS.Model.AppModels.Condition.SysSettings;
using NXWMS.Model.AppModels.Result.SysSettings;
using NXWMS.Code.Extends;
using NXWMS.Model.Common;
namespace NXWMS.Service.NXWMS.SysSettings
{
///
/// 角色服务
///
[AutoInject(typeof(IRoleService), InjectType.Scope)]
public class RoleService : ServiceBase, IRoleService
{
///
/// 系统操作仓储中转
///
private IDataRepositoryContext _dataContext;
///
/// SQL节点仓储
///
private ISQLNodeRepository _iSQLNodeRepository;
///
/// 配置
///
private IConfiguration _configuration;
public RoleService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
{
this._dataContext = dataRepositoryContext;
this._configuration = configuration;
this._iSQLNodeRepository = iSQLNodeRepository;
}
public OperateResultInfo> GetList(RoleSearchCondition info)
{
var sqlAndBuilder = new StringBuilder();
var sql = $@"SELECT
CreateName = (SELECT USER_NAME FROM SYS_USER A WHERE A.USER_ID=CREATE_BY),
UpdateName = (SELECT USER_NAME FROM SYS_USER B WHERE B.USER_ID=UPDATE_BY),
UsedFlagName = {_iSQLNodeRepository.GetEnumIntCaseString("USED_FLAG")},
{info.ItemSQL} FROM SYS_ROLE WHERE 1=1 AND DEL_FLAG = 0 ";
sqlAndBuilder = info.Id != null ?
info.Id > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.Id, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = info.IsUsed == null ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("USED_FLAG", info.IsUsed, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_CODE", info.RoleCode, DBOperationString._ContainIn));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleName) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_NAME", info.RoleName, DBOperationString._ContainIn));
sql = sql + (sqlAndBuilder.Length > 0 ? _iSQLNodeRepository.GetAndString(sqlAndBuilder, false) : "");
IEnumerable result;
IEnumerable totalResult;
totalResult = new DataRepository(_dataContext).Query(sql);
if (info.PageIndex == 0 || info.PageSize == 0)
{
result = totalResult.ToList();
}
else
{
result = new DataRepository(_dataContext).QueryPage(sql,
"CREATE_TIME", info.PageSize, info.PageIndex, true);
}
return SuccessStatus(new PageQueryResultInfo
{
RowData = result,
PageConditionInfo = info,
TotalCount = totalResult.Count(),
TotalPageCount = (int)Math.Ceiling((double)totalResult.Count() / info.PageSize)
});
}
public OperateResultInfo Deleted(RoleCondition info)
{
var sqlAndBuilder = new StringBuilder();
sqlAndBuilder = info.RoleId != null ?
info.RoleId > 0 ?
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.RoleId, DBOperationString._Equal)) :
sqlAndBuilder : sqlAndBuilder;
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleCode) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_CODE", info.RoleCode, DBOperationString._Equal));
sqlAndBuilder = string.IsNullOrWhiteSpace(info.RoleIds) ?
sqlAndBuilder :
sqlAndBuilder.Append(_iSQLNodeRepository.GetAddCondition("ROLE_ID", info.RoleIds, DBOperationString._In));
if (sqlAndBuilder.Length == 0)
{
return FailMessageStatus("参数错误!");
}
var now = DateTime.Now;
var sql = $@"UPDATE SYS_ROLE SET DEL_FLAG=1,UPDATE_BY={info.OperationUserId},UPDATE_TIME='{now}'
WHERE 1=1 {sqlAndBuilder}";
var affectedRows = new DataRepository(_dataContext).Execute(sql);
return GetStatus(affectedRows, info.RoleIds.Split(',').Length);
}
public OperateResultInfo Remove(RoleCondition info)
{
var whereList = GetFieldKeyList(info);
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
var affectedRows = 0;
if (info.RoleId != null)
{
affectedRows = new DataRepository(_dataContext).Remove("ROLE_ID", info.RoleId.ToString());
}
if (!string.IsNullOrWhiteSpace(info.RoleCode))
{
affectedRows = new DataRepository(_dataContext).Remove("ROLE_CODE", info.RoleCode);
}
return GetStatus(affectedRows);
}
return FailMessageStatus("未查找到数据!");
}
public OperateResultInfo Add(RoleCondition info)
{
if (info == null)
{
return FailStatus();
}
//接口中特定错误状态不加了,,既然要简单.
if (new DataRepository(_dataContext).Query().Where(m => m.ROLE_CODE == info.RoleCode
&& m.DEL_FLAG == 0 && m.USED_FLAG == 1).Any())
{
return FailMessageStatus("角色编码已经存在,请输入其它编码!");
}
_dataContext.BeginTran();
var now = DateTime.Now;
var entity = new SYS_ROLE();
var roleId = new DataRepository(_dataContext).Query().Select(s => s.ROLE_ID).Max() + 1;
entity.CREATE_BY = info.OperationUserId;
entity.CREATE_TIME = now;
entity.UPDATE_BY = info.OperationUserId;
entity.UPDATE_TIME = now;
entity.ROLE_CODE = info.RoleCode;
entity.ROLE_NAME = info.RoleName;
entity.DESCRIBE = info.Describe;
entity.ROLE_ID = roleId;
var affectedRows = new DataRepository(_dataContext).Add(entity, new string[1] { "ROLE_ID" });
var whereList = new List();
whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), roleId,
EnumCSharpPropertyType.INT32, DBOperationString._Equal);
var permissionData = new DataRepository(_dataContext).
Query(whereList).ToList();
permissionData.ForEach(s =>
{
s.DEL_FLAG = 1;
s.UPDATE_BY = info.OperationUserId;
s.UPDATE_TIME = now;
});
affectedRows = new DataRepository(_dataContext).Update(permissionData, "ROLE_MENU_PERMISSION_ID");
if (affectedRows != permissionData.Count)
{
_dataContext.Rollback();
return GetStatus(affectedRows, permissionData.Count);
}
affectedRows = 0;
foreach (int item in info.PermissionMenuIdList)
{
var permission = new SYS_ROLE_MENU_PERMISSION
{
CREATE_TIME = now,
UPDATE_TIME = now,
CREATE_BY = info.OperationUserId,
UPDATE_BY = info.OperationUserId,
MENU_ID = item,
ROLE_ID = roleId,
USED_FLAG = 1,
};
affectedRows = affectedRows + new DataRepository(_dataContext).Add(permission, new string[1] { "ROLE_MENU_PERMISSION_ID" });
}
if (affectedRows != info.PermissionMenuIdList.Count)
{
_dataContext.Rollback();
return GetStatus(affectedRows, info.PermissionMenuIdList.Count);
}
_dataContext.Commit();
return SuccessStatus();
}
public OperateResultInfo Edit(RoleCondition info)
{
if (info.RoleId == null && string.IsNullOrWhiteSpace(info.RoleIds))
{
return FailMessageStatus("参数错误!");
}
var whereList = new List()
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
info.RoleId != null)
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
!string.IsNullOrWhiteSpace(info.RoleCode));
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
_dataContext.BeginTran();
var now = DateTime.Now;
result.USED_FLAG = Convert.ToInt32(info.IsUsed);
result.ROLE_ID = result.ROLE_ID;
result.UPDATE_BY = info.OperationUserId;
result.UPDATE_TIME = now;
result.ROLE_CODE = info.RoleCode;
result.ROLE_NAME = info.RoleName;
result.DESCRIBE = info.Describe;
var affectedRows = new DataRepository(_dataContext).Update(result, "ROLE_ID", "NEWID");
//菜单权限分配
whereList = new List();
whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), result.ROLE_ID,
EnumCSharpPropertyType.INT32, DBOperationString._Equal);
var permissionData = new DataRepository(_dataContext).
Query(whereList).ToList();
permissionData.ForEach(s =>
{
s.DEL_FLAG = 1;
s.UPDATE_BY = info.OperationUserId;
s.UPDATE_TIME = now;
});
affectedRows = new DataRepository(_dataContext).Update(permissionData, "ROLE_MENU_PERMISSION_ID");
if (affectedRows != permissionData.Count)
{
_dataContext.Rollback();
return GetStatus(affectedRows, permissionData.Count);
}
affectedRows = 0;
if (info.PermissionMenuIdList.Any())
{
foreach (int item in info.PermissionMenuIdList)
{
if (!new DataRepository(_dataContext).Query().
Where(s => s.ROLE_ID == result.ROLE_ID && s.MENU_ID == item && s.DEL_FLAG==0).Any())
{
var permission = new SYS_ROLE_MENU_PERMISSION
{
CREATE_TIME = now,
UPDATE_TIME = now,
CREATE_BY = info.OperationUserId,
UPDATE_BY = info.OperationUserId,
MENU_ID = item,
ROLE_ID = result.ROLE_ID,
USED_FLAG = 1,
};
affectedRows = affectedRows + new DataRepository(_dataContext).Add(permission, new string[1] { "ROLE_MENU_PERMISSION_ID" });
}
}
}
if (affectedRows != info.PermissionMenuIdList.Count)
{
_dataContext.Rollback();
return GetStatus(affectedRows, info.PermissionMenuIdList.Count);
}
_dataContext.Commit();
return SuccessStatus();
}
return FailMessageStatus("未查找到数据!");
}
///
/// 获取角色对应授权信息
///
///
///
public OperateResultInfo GetPermissionInfo(RolePermissionCondition info)
{
if (info.RoleId == null && string.IsNullOrWhiteSpace(info.RoleCode))
{
return FailMessageStatus("参数错误!", new RolePermissionResult());
}
var whereList = new List()
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
info.RoleId != null)
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
!string.IsNullOrWhiteSpace(info.RoleCode));
var result = new DataRepository(_dataContext).Query(whereList).FirstOrDefault();
if (result != null)
{
var sql = string.Format(@"SELECT
MainMenuId = a.MENU_ID, MainMenuCode = a.MENU_CODE,MainMenuName=a.MENU_NAME,MainMenuOrder=a.MENU_ORDER,MainMenuURL=a.MENU_URL
FROM SYS_ROLE_MENU_PERMISSION T
JOIN SYS_ROLE R ON T.ROLE_ID = R.ROLE_ID AND R.DEL_FLAG = 0 AND R.USED_FLAG = 1 AND T.DEL_FLAG = 0 AND T.USED_FLAG = 1
JOIN SYS_MENU A ON T.MENU_ID = A.MENU_ID AND A.DEL_FLAG = 0 AND A.USED_FLAG = 1 AND A.DEL_FLAG = 0
LEFT JOIN SYS_MENU B ON T.MENU_ID = B.MENU_ID AND B.USED_FLAG = 1 AND B.DEL_FLAG = 0
LEFT JOIN SYS_MENU C ON C.USED_FLAG = 1 AND C.DEL_FLAG = 0 AND C.MENU_PID != -1
AND T.MENU_ID = C.MENU_ID
WHERE 1 = 1 AND T.ROLE_ID = {0}", result.ROLE_ID);
var roleMenuResult = new DataRepository(_dataContext).Query(sql);
return SuccessStatus(new RolePermissionResult
{
RoleCode = result.ROLE_CODE,
RoleId = result.ROLE_ID,
RoleName = result.ROLE_NAME,
RoleMenuList = roleMenuResult.ToList()
});
}
return FailMessageStatus("未查找到数据!", null);
}
#region Private
///
/// 条件统一下,用统一筛选
///
///
///
private List GetFieldKeyList(RoleCondition info)
{
var whereList = new List()
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleId, EnumCSharpPropertyType.INT32, DBOperationString._Equal,
info.RoleId != null)
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_ID), info.RoleIds, EnumCSharpPropertyType.STRING, DBOperationString._In,
!string.IsNullOrWhiteSpace(info.RoleIds))
.AddFieldKeyInfo(nameof(SYS_ROLE.ROLE_CODE), info.RoleCode, EnumCSharpPropertyType.STRING, DBOperationString._Equal,
!string.IsNullOrWhiteSpace(info.RoleCode));
return whereList;
}
#endregion
}
}