|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using AutoMapper;
- using Microsoft.EntityFrameworkCore;
- using Permission.Entity.DbContext;
- using Permission.Entity.System;
- using Permission.Infrastructure.Repositories;
- using Permission.Infrastructure.WebControls;
- using Permission.Service.DTO;
- using Permission.Service.DTO.ApiModels;
- using Permission.Service.IServices;
- using Permission.Utils.Validate;
-
- namespace Permission.Service.Services
- {
- public class SystemInfoService : ISystemInfoService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly PermissionContext _dbContext;
- private readonly IMapper _mapper;
- private readonly IBaseRepository<SystemInfo> _systemInfoRepository;
- private readonly IBaseRepository<SysUser> _sysUserRepository;
-
- public SystemInfoService(IUnitOfWork unitOfWork, PermissionContext dbContext, IMapper mapper, IBaseRepository<SystemInfo> systemInfoRepository, IBaseRepository<SysUser> sysUserRepository)
- {
- this._unitOfWork = unitOfWork;
- this._dbContext = dbContext;
- this._mapper = mapper;
- this._systemInfoRepository = systemInfoRepository;
- this._sysUserRepository = sysUserRepository;
- }
-
- public bool AddModel(SystemInfo model)
- {
- try
- {
- model.GuidId = Guid.NewGuid().ToString();
- model.IsEnabled = model.IsEnabled != null;
- model.IsManagerPage = model.IsManagerPage == 1 ? 1 : 0;
- model.DeleteMark = false;
- model.CreateTime = DateTime.Now;
- var res = _systemInfoRepository.Save(model);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public bool ModifyModel(SystemInfo model)
- {
- try
- {
- var systemInfo = _systemInfoRepository.Get(model.Id);
- systemInfo.SysName = model.SysName;
- systemInfo.SysLogo = model.SysLogo;
- systemInfo.SystemTypeId = model.SystemTypeId;
- systemInfo.SysHost = model.SysHost;
- systemInfo.SysInterface = model.SysInterface;
- systemInfo.ModifyTime = DateTime.Now;
- systemInfo.ModifyUser = model.ModifyUser;
- systemInfo.Remark = model.Remark;
- systemInfo.IsEnabled = model.IsEnabled;
- systemInfo.SortCode = model.SortCode;
- var res = _systemInfoRepository.Update(systemInfo);
- //_dbContext.SaveChanges();
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public bool DeleteModel(int key)
- {
- bool flag = false;
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var system = _customerById(_dbContext, key);
- if (system.SystemUsers.Any())
- {
- system.SystemUsers.Clear();
- var model = _systemInfoRepository.Get(key);
- _systemInfoRepository.Delete(model);
- }
- else
- {
- var model = _systemInfoRepository.Get(key);
- _systemInfoRepository.Delete(model);
- }
- flag = true;
- }
- else
- {
- flag = false;
- }
- return flag;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public SystemInfo GetSystemByKey(int key)
- {
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var data = _systemInfoRepository.Get(key);
- return data;
- }
- return null;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public Page<SystemInfo> PageList(int typeid, string keyword, int pagesize, int pageindex)
- {
- try
- {
- int total = 0;
- if (keyword.IsEmpty())
- {
- var data = _systemInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
- u => u,
- out total,
- u => u.SystemTypeId == typeid && u.DeleteMark == false);
-
- return data;
- }
- else
- {
- var data = _systemInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
- u => u,
- out total,
- u => u.SysName.Contains(keyword) && u.SystemTypeId == typeid && u.DeleteMark == false);
- return data;
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public List<SystemUsersModel> GetUsersBySystemId(int systemId)
- {
- if (systemId.IsEmpty() || systemId <= 0)
- {
- return null;
- }
- else
- {
- var group = _customerById(_dbContext, systemId);
- var gruopusers = group.SystemUsers;
- var allusers = _sysUserRepository.QueryEntity<SysUser, DateTime, SysUser>(m => m.IsEnabled == true && m.DeleteMark == false, ms => ms.CreateTime.Value, null, true).ToList();
- var data = allusers.Select(u => new SystemUsersModel
- {
- UserId = u.Id,
- UserName = u.Account,
- RealName = u.RealName,
- IsChecked = gruopusers.Select(us => us.UserId).Contains(u.Id) ? 1 : 0
- }).ToList();
- return data;
- }
- }
-
- public bool UpdateUsersSystem(int systemId, string userIds)
- {
- bool flag = false;
- if (!systemId.IsEmpty() || systemId > 0)
- {
-
- List<SystemUser> newUsers = new List<SystemUser>();
- //删除原先旧数据
- try
- {
- var system = _customerById(_dbContext, systemId);
- system.SystemUsers.Clear();
-
- if (!userIds.IsEmpty())
- {
- SystemUser sysuser = null;
- foreach (var uid in userIds.Split(',').ToList())
- {
- sysuser = new SystemUser
- {
- UserId = int.Parse(uid),
- SystemId = systemId,
- GuidId = Guid.NewGuid().ToString("N"),
- CreateUser = "",
- CreateTime = DateTime.Now
- };
-
- newUsers.Add(sysuser);
- }
- system.SystemUsers.AddRange(newUsers);
- }
- var res = _unitOfWork.SaveChanges() > 0;
- flag = res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- {
- flag = false;
- }
- return flag;
- }
-
- #region 私有方法
-
- private static Func<PermissionContext, int, SystemInfo> _customerById =
- EF.CompileQuery((PermissionContext db, int systemId) => db.SystemInfos
- .Include(u => u.SystemUsers)
- .FirstOrDefault(c => c.Id == systemId));
-
- private static Func<PermissionContext, int, SystemInfo> _allitemsById =
- EF.CompileQuery((PermissionContext db, int systemId) => db.SystemInfos
- .Include(u => u.SystemType)
- .FirstOrDefault(c => c.Id == systemId));
-
- private static Func<PermissionContext, int, SysUser> _customerByUserId =
- EF.CompileQuery((PermissionContext db, int userId) => db.SysUsers
- .Include(u => u.SystemUsers)
- .FirstOrDefault(c => c.Id == userId));
-
- #endregion
-
- #region api
-
- public IEnumerable<SystemInfoApiViewModel> GetSystemsByUserIdApi(int userId)
- {
- try
- {
- if (!userId.IsEmpty() || userId > 0)
- {
- /*
- string sqla = @"select SystemId from SystemUser where UserId=@UserId";
- IEnumerable<string> sysidlist = _query.QueryList<string>(sqla, new { UserId = userId });
- string sysids = string.Join(",", sysidlist.ToArray());
- //string sqlb = string.Format("select * from SystemInfo where Status=1 and DeleteFlag=0 and SystemId in('{0}') order by SortCode asc", sysids.Trim(',').Replace(",", "','"));
- 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(",", "','"));
- IEnumerable<dynamic> syslist = _query.QueryList<dynamic>(sqlb, null);
- return syslist;
- */
-
- var user = _customerByUserId(_dbContext, userId);
- var sysidlist = user.SystemUsers.Select(m => m.SystemId).ToList();
- var data =
- _systemInfoRepository.LoadAll(
- m => sysidlist.Contains(m.Id) && m.IsEnabled == true && m.DeleteMark == false).Select(m => _allitemsById(_dbContext, m.Id)).ToList().OrderBy(s => s.SortCode);
- var syslist = _mapper.Map<List<SystemInfoApiViewModel>>(data).OrderBy(m=>m.TypeSortCode);
- return syslist;
- }
- else
- {
- return null;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- #endregion
- }
- }
|