飞星
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

282 行
10 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using AutoMapper;
  5. using Microsoft.EntityFrameworkCore;
  6. using Permission.Entity.DbContext;
  7. using Permission.Entity.System;
  8. using Permission.Infrastructure.Repositories;
  9. using Permission.Infrastructure.WebControls;
  10. using Permission.Service.DTO;
  11. using Permission.Service.DTO.ApiModels;
  12. using Permission.Service.IServices;
  13. using Permission.Utils.Validate;
  14. namespace Permission.Service.Services
  15. {
  16. public class SystemInfoService : ISystemInfoService
  17. {
  18. private readonly IUnitOfWork _unitOfWork;
  19. private readonly PermissionContext _dbContext;
  20. private readonly IMapper _mapper;
  21. private readonly IBaseRepository<SystemInfo> _systemInfoRepository;
  22. private readonly IBaseRepository<SysUser> _sysUserRepository;
  23. public SystemInfoService(IUnitOfWork unitOfWork, PermissionContext dbContext, IMapper mapper, IBaseRepository<SystemInfo> systemInfoRepository, IBaseRepository<SysUser> sysUserRepository)
  24. {
  25. this._unitOfWork = unitOfWork;
  26. this._dbContext = dbContext;
  27. this._mapper = mapper;
  28. this._systemInfoRepository = systemInfoRepository;
  29. this._sysUserRepository = sysUserRepository;
  30. }
  31. public bool AddModel(SystemInfo model)
  32. {
  33. try
  34. {
  35. model.GuidId = Guid.NewGuid().ToString();
  36. model.IsEnabled = model.IsEnabled != null;
  37. model.IsManagerPage = model.IsManagerPage == 1 ? 1 : 0;
  38. model.DeleteMark = false;
  39. model.CreateTime = DateTime.Now;
  40. var res = _systemInfoRepository.Save(model);
  41. return res;
  42. }
  43. catch (Exception ex)
  44. {
  45. throw ex;
  46. }
  47. }
  48. public bool ModifyModel(SystemInfo model)
  49. {
  50. try
  51. {
  52. var systemInfo = _systemInfoRepository.Get(model.Id);
  53. systemInfo.SysName = model.SysName;
  54. systemInfo.SysLogo = model.SysLogo;
  55. systemInfo.SystemTypeId = model.SystemTypeId;
  56. systemInfo.SysHost = model.SysHost;
  57. systemInfo.SysInterface = model.SysInterface;
  58. systemInfo.ModifyTime = DateTime.Now;
  59. systemInfo.ModifyUser = model.ModifyUser;
  60. systemInfo.Remark = model.Remark;
  61. systemInfo.IsEnabled = model.IsEnabled;
  62. systemInfo.SortCode = model.SortCode;
  63. var res = _systemInfoRepository.Update(systemInfo);
  64. //_dbContext.SaveChanges();
  65. return res;
  66. }
  67. catch (Exception ex)
  68. {
  69. throw ex;
  70. }
  71. }
  72. public bool DeleteModel(int key)
  73. {
  74. bool flag = false;
  75. try
  76. {
  77. if (!key.IsEmpty() || key > 0)
  78. {
  79. var system = _customerById(_dbContext, key);
  80. if (system.SystemUsers.Any())
  81. {
  82. system.SystemUsers.Clear();
  83. var model = _systemInfoRepository.Get(key);
  84. _systemInfoRepository.Delete(model);
  85. }
  86. else
  87. {
  88. var model = _systemInfoRepository.Get(key);
  89. _systemInfoRepository.Delete(model);
  90. }
  91. flag = true;
  92. }
  93. else
  94. {
  95. flag = false;
  96. }
  97. return flag;
  98. }
  99. catch (Exception ex)
  100. {
  101. throw ex;
  102. }
  103. }
  104. public SystemInfo GetSystemByKey(int key)
  105. {
  106. try
  107. {
  108. if (!key.IsEmpty() || key > 0)
  109. {
  110. var data = _systemInfoRepository.Get(key);
  111. return data;
  112. }
  113. return null;
  114. }
  115. catch (Exception e)
  116. {
  117. throw e;
  118. }
  119. }
  120. public Page<SystemInfo> PageList(int typeid, string keyword, int pagesize, int pageindex)
  121. {
  122. try
  123. {
  124. int total = 0;
  125. if (keyword.IsEmpty())
  126. {
  127. var data = _systemInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
  128. u => u,
  129. out total,
  130. u => u.SystemTypeId == typeid && u.DeleteMark == false);
  131. return data;
  132. }
  133. else
  134. {
  135. var data = _systemInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
  136. u => u,
  137. out total,
  138. u => u.SysName.Contains(keyword) && u.SystemTypeId == typeid && u.DeleteMark == false);
  139. return data;
  140. }
  141. }
  142. catch (Exception e)
  143. {
  144. throw e;
  145. }
  146. }
  147. public List<SystemUsersModel> GetUsersBySystemId(int systemId)
  148. {
  149. if (systemId.IsEmpty() || systemId <= 0)
  150. {
  151. return null;
  152. }
  153. else
  154. {
  155. var group = _customerById(_dbContext, systemId);
  156. var gruopusers = group.SystemUsers;
  157. var allusers = _sysUserRepository.QueryEntity<SysUser, DateTime, SysUser>(m => m.IsEnabled == true && m.DeleteMark == false, ms => ms.CreateTime.Value, null, true).ToList();
  158. var data = allusers.Select(u => new SystemUsersModel
  159. {
  160. UserId = u.Id,
  161. UserName = u.Account,
  162. RealName = u.RealName,
  163. IsChecked = gruopusers.Select(us => us.UserId).Contains(u.Id) ? 1 : 0
  164. }).ToList();
  165. return data;
  166. }
  167. }
  168. public bool UpdateUsersSystem(int systemId, string userIds)
  169. {
  170. bool flag = false;
  171. if (!systemId.IsEmpty() || systemId > 0)
  172. {
  173. List<SystemUser> newUsers = new List<SystemUser>();
  174. //删除原先旧数据
  175. try
  176. {
  177. var system = _customerById(_dbContext, systemId);
  178. system.SystemUsers.Clear();
  179. if (!userIds.IsEmpty())
  180. {
  181. SystemUser sysuser = null;
  182. foreach (var uid in userIds.Split(',').ToList())
  183. {
  184. sysuser = new SystemUser
  185. {
  186. UserId = int.Parse(uid),
  187. SystemId = systemId,
  188. GuidId = Guid.NewGuid().ToString("N"),
  189. CreateUser = "",
  190. CreateTime = DateTime.Now
  191. };
  192. newUsers.Add(sysuser);
  193. }
  194. system.SystemUsers.AddRange(newUsers);
  195. }
  196. var res = _unitOfWork.SaveChanges() > 0;
  197. flag = res;
  198. }
  199. catch (Exception ex)
  200. {
  201. throw ex;
  202. }
  203. }
  204. else
  205. {
  206. flag = false;
  207. }
  208. return flag;
  209. }
  210. #region 私有方法
  211. private static Func<PermissionContext, int, SystemInfo> _customerById =
  212. EF.CompileQuery((PermissionContext db, int systemId) => db.SystemInfos
  213. .Include(u => u.SystemUsers)
  214. .FirstOrDefault(c => c.Id == systemId));
  215. private static Func<PermissionContext, int, SystemInfo> _allitemsById =
  216. EF.CompileQuery((PermissionContext db, int systemId) => db.SystemInfos
  217. .Include(u => u.SystemType)
  218. .FirstOrDefault(c => c.Id == systemId));
  219. private static Func<PermissionContext, int, SysUser> _customerByUserId =
  220. EF.CompileQuery((PermissionContext db, int userId) => db.SysUsers
  221. .Include(u => u.SystemUsers)
  222. .FirstOrDefault(c => c.Id == userId));
  223. #endregion
  224. #region api
  225. public IEnumerable<SystemInfoApiViewModel> GetSystemsByUserIdApi(int userId)
  226. {
  227. try
  228. {
  229. if (!userId.IsEmpty() || userId > 0)
  230. {
  231. /*
  232. string sqla = @"select SystemId from SystemUser where UserId=@UserId";
  233. IEnumerable<string> sysidlist = _query.QueryList<string>(sqla, new { UserId = userId });
  234. string sysids = string.Join(",", sysidlist.ToArray());
  235. //string sqlb = string.Format("select * from SystemInfo where Status=1 and DeleteFlag=0 and SystemId in('{0}') order by SortCode asc", sysids.Trim(',').Replace(",", "','"));
  236. string sqlb = string.Format("select a.SysTypeName as SysTypeName,b.* from SystemType as a left join SystemInfo as b on a.SysTypeId = b.TypeCode where a.Status=1 and a.DeleteFlag=0 and b.Status=1 and b.DeleteFlag=0 and b.SystemId in('{0}') order by b.SortCode asc", sysids.Trim(',').Replace(",", "','"));
  237. IEnumerable<dynamic> syslist = _query.QueryList<dynamic>(sqlb, null);
  238. return syslist;
  239. */
  240. var user = _customerByUserId(_dbContext, userId);
  241. var sysidlist = user.SystemUsers.Select(m => m.SystemId).ToList();
  242. var data =
  243. _systemInfoRepository.LoadAll(
  244. m => sysidlist.Contains(m.Id) && m.IsEnabled == true && m.DeleteMark == false).Select(m => _allitemsById(_dbContext, m.Id)).ToList().OrderBy(s => s.SortCode);
  245. var syslist = _mapper.Map<List<SystemInfoApiViewModel>>(data).OrderBy(m=>m.TypeSortCode);
  246. return syslist;
  247. }
  248. else
  249. {
  250. return null;
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. throw ex;
  256. }
  257. }
  258. #endregion
  259. }
  260. }