123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 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
- {
- /// <summary>
- /// 角色服务
- /// </summary>
- [AutoInject(typeof(IRoleService), InjectType.Scope)]
- public class RoleService : ServiceBase, IRoleService
- {
- /// <summary>
- /// 系统操作仓储中转
- /// </summary>
- private IDataRepositoryContext _dataContext;
- /// <summary>
- /// SQL节点仓储
- /// </summary>
- private ISQLNodeRepository _iSQLNodeRepository;
- /// <summary>
- /// 配置
- /// </summary>
- private IConfiguration _configuration;
- public RoleService(IDataRepositoryContext dataRepositoryContext, IConfiguration configuration, ISQLNodeRepository iSQLNodeRepository)
- {
- this._dataContext = dataRepositoryContext;
- this._configuration = configuration;
- this._iSQLNodeRepository = iSQLNodeRepository;
- }
- public OperateResultInfo<PageQueryResultInfo<RoleResult>> 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<UsedFlag>("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<RoleResult> result;
- IEnumerable<RoleResult> totalResult;
- totalResult = new DataRepository<RoleResult>(_dataContext).Query(sql);
- if (info.PageIndex == 0 || info.PageSize == 0)
- {
- result = totalResult.ToList();
- }
- else
- {
- result = new DataRepository<RoleResult>(_dataContext).QueryPage(sql,
- "CREATE_TIME", info.PageSize, info.PageIndex, true);
- }
- return SuccessStatus(new PageQueryResultInfo<RoleResult>
- {
- 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<SYS_ROLE>(_dataContext).Execute(sql);
- return GetStatus(affectedRows, info.RoleIds.Split(',').Length);
- }
- public OperateResultInfo Remove(RoleCondition info)
- {
- var whereList = GetFieldKeyList(info);
- var result = new DataRepository<SYS_ROLE>(_dataContext).Query(whereList).FirstOrDefault();
- if (result != null)
- {
- var affectedRows = 0;
- if (info.RoleId != null)
- {
- affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Remove("ROLE_ID", info.RoleId.ToString());
- }
- if (!string.IsNullOrWhiteSpace(info.RoleCode))
- {
- affectedRows = new DataRepository<SYS_ROLE>(_dataContext).Remove("ROLE_CODE", info.RoleCode);
- }
- return GetStatus(affectedRows);
- }
- return FailMessageStatus("未查找到数据!");
- }
- public OperateResultInfo Add(RoleCondition info)
- {
- if (info == null)
- {
- return FailStatus();
- }
- //接口中特定错误状态不加了,,既然要简单.
- if (new DataRepository<SYS_ROLE>(_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<SYS_ROLE>(_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<SYS_ROLE>(_dataContext).Add(entity, new string[1] { "ROLE_ID" });
-
- var whereList = new List<FieldKeyInfo>();
- whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), roleId,
- EnumCSharpPropertyType.INT32, DBOperationString._Equal);
- var permissionData = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).
- Query(whereList).ToList();
- permissionData.ForEach(s =>
- {
- s.DEL_FLAG = 1;
- s.UPDATE_BY = info.OperationUserId;
- s.UPDATE_TIME = now;
- });
- affectedRows = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_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<SYS_ROLE_MENU_PERMISSION>(_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<FieldKeyInfo>()
- .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<SYS_ROLE>(_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<SYS_ROLE>(_dataContext).Update(result, "ROLE_ID", "NEWID");
- //菜单权限分配
- whereList = new List<FieldKeyInfo>();
- whereList.AddFieldKeyInfo(nameof(SYS_ROLE_MENU_PERMISSION.ROLE_ID), result.ROLE_ID,
- EnumCSharpPropertyType.INT32, DBOperationString._Equal);
- var permissionData = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_dataContext).
- Query(whereList).ToList();
- permissionData.ForEach(s =>
- {
- s.DEL_FLAG = 1;
- s.UPDATE_BY = info.OperationUserId;
- s.UPDATE_TIME = now;
- });
- affectedRows = new DataRepository<SYS_ROLE_MENU_PERMISSION>(_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<SYS_ROLE_MENU_PERMISSION>(_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<SYS_ROLE_MENU_PERMISSION>(_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("未查找到数据!");
- }
- /// <summary>
- /// 获取角色对应授权信息
- /// </summary>
- /// <param name="info"></param>
- /// <returns></returns>
- public OperateResultInfo<RolePermissionResult> GetPermissionInfo(RolePermissionCondition info)
- {
- if (info.RoleId == null && string.IsNullOrWhiteSpace(info.RoleCode))
- {
- return FailMessageStatus("参数错误!", new RolePermissionResult());
- }
- var whereList = new List<FieldKeyInfo>()
- .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<SYS_ROLE>(_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<RoleMenuInfo>(_dataContext).Query(sql);
- return SuccessStatus(new RolePermissionResult
- {
- RoleCode = result.ROLE_CODE,
- RoleId = result.ROLE_ID,
- RoleName = result.ROLE_NAME,
- RoleMenuList = roleMenuResult.ToList()
- });
- }
- return FailMessageStatus<RolePermissionResult>("未查找到数据!", null);
- }
- #region Private
- /// <summary>
- /// 条件统一下,用统一筛选
- /// </summary>
- /// <param name="RoleCondition"></param>
- /// <returns></returns>
- private List<FieldKeyInfo> GetFieldKeyList(RoleCondition info)
- {
- var whereList = new List<FieldKeyInfo>()
- .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
- }
- }
|