using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using AutoMapper; using Model.ModelDto.LogDto; using Model.ModelVm.LogVm; using SqlSugar; using Utility; using WMS.DAL; using WMS.Entity.Context; using WMS.Entity.LogEntity; using WMS.Entity.SysEntity; using WMS.IBLL.ILogServer; using WMS.IDAL.ILogInterface; namespace WMS.BLL.LogServer { public class OperationSysServer : DbHelper, IOperationSysServer { private readonly IOperationSysRepository _operation; private readonly IMapper _mapper; private static readonly SqlSugarScope Db = DataContext.Db; private readonly UserManager _userManager; /// /// 构造函数 /// /// 日志 /// automapper public OperationSysServer(IOperationSysRepository operation, IMapper mapper, UserManager userManager) : base(Db) { _operation = operation; //日志 _mapper = mapper; //automapper _userManager = userManager; } /// /// 添加操作日志 /// /// 模块号 /// 菜单号 /// 菜单名称 /// 数据编号 /// 类型 添加 修改 删除 停用 启用 /// 操作 /// /// public async Task AddOperationSys(string parentNo, string menuNo, string menuName, string fkNo, string type, string msg, int userId) { try { var num = await AddAsync(new LogOperationSys() { ParentNo = parentNo, MenuNo = menuNo, MenuName = menuName, FkNo = fkNo, Type = type, Msg = msg, CreateUser = userId }); if (num > 0) { return true; } else { return false; } } catch (Exception e) { throw new Exception(e.Message); } } /// /// 获取系统操作日志数据列表 /// /// 菜单名称 /// 操作类型 /// 操作内容 /// 菜单号 /// 模块号 /// public async Task GetSysOperationList(GetOperationVm model) { return await Db.Queryable() .LeftJoin((a, b) => a.CreateUser == b.Id) .LeftJoin((a, b, c) => a.ParentNo == c.MenuNo) .LeftJoin((a, b, c, d) => SqlFunc.ToInt32(a.Type) == d.Id) .Where(a => a.IsDel == "0") .WhereIF(!string.IsNullOrEmpty(model.MenuName), a => a.MenuName.Contains(model.MenuName)) .WhereIF(!string.IsNullOrEmpty(model.Type), a => a.Type == model.Type) .WhereIF(!string.IsNullOrEmpty(model.Msg), a => a.Msg.Contains(model.Msg)) .WhereIF(!string.IsNullOrEmpty(model.MenuNo), a => a.MenuNo == model.MenuNo) .Where((a, b, c, d) => a.ParentNo == SqlFunc.Subqueryable().Where(e => e.IsDel == "0" && e.MenuName == model.ParentNo).Select(e => e.MenuNo)) .Select((a, b, c, d) => new OperationDto() { Id = a.Id, CreateUserName = b.RealName, TypeName = d.DictName, }, true) .ToPagedListAsync(model.Page, model.Limit); } /// /// 新增系统操作日志 /// /// 模块号 /// 菜单号 /// 数据编号 /// 操作类型 /// 操作内容 /// 创建人 /// public async Task InsertOperation(string parentName, string menuName, string fkNo, string type, string msg, int createuser) { //捕获异常 try { var parentNo = (await Db.Queryable().FirstAsync(s => s.MenuName == parentName && s.IsDel == "0"))?.MenuNo; var menuNo = (await Db.Queryable().FirstAsync(s => s.MenuName == menuName && s.IsDel == "0"))?.MenuNo; int i = await _operation.InsertOperation(parentNo, menuNo, parentName + "-" + menuName, fkNo, type, msg, createuser); return i; } catch (Exception ex) { //抛出异常 throw new Exception("新增操作日志异常", ex); } } /// /// 新增系统操作日志 /// /// 模块号 /// 菜单号 /// 数据编号 /// 操作类型 /// 操作内容 /// 创建人 /// public async Task InsertOperation(OperationInputVm input) { input.ParentNo = (await Db.Queryable().FirstAsync(s => s.MenuName == input.ParentName && s.IsDel == "0"))?.MenuNo; input.MenuNo = (await Db.Queryable().FirstAsync(s => s.MenuName == input.MenuName && s.IsDel == "0"))?.MenuNo; input.Type = (await Db.Queryable().FirstAsync(s => s.DictName == input.TypeName && s.IsDel == "0"))?.Id.ToString(); input.CreateUser = _userManager.UserId; input.CreateTime = DateTime.Now; return await Db.Insertable(input).ExecuteCommandAsync(); } /// /// 获取类型菜单 /// /// 字典名称 /// public async Task> GetDicTypeList(string dicName) { return await Db.Queryable() .Where(s => s.IsDel == "0") .Where(s => s.ParentNo == SqlFunc.Subqueryable().Where(c => c.DictName == dicName).Select(c => c.DictNo)) .ToListAsync(); } } }