diff --git a/Infrastructure/Extensions/StringExtension.cs b/Infrastructure/Extensions/StringExtension.cs index 75885c8..60315e2 100644 --- a/Infrastructure/Extensions/StringExtension.cs +++ b/Infrastructure/Extensions/StringExtension.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.RegularExpressions; namespace Infrastructure.Extensions { @@ -26,5 +27,19 @@ namespace Infrastructure.Extensions { return !string.IsNullOrEmpty(str); } + + /// + /// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//("); + /// + /// + /// + /// + /// + public static string ReplaceFirst(this string input, string oldValue, string newValue) + { + Regex regEx = new Regex(oldValue, RegexOptions.Multiline); + return regEx.Replace(input, newValue == null ? "" : newValue, 1); + + } } } diff --git a/ZR.Repository/System/SysDeptRepository.cs b/ZR.Repository/System/SysDeptRepository.cs index b41871a..af18347 100644 --- a/ZR.Repository/System/SysDeptRepository.cs +++ b/ZR.Repository/System/SysDeptRepository.cs @@ -7,7 +7,7 @@ namespace ZR.Repository.System /// /// 部门管理 /// - [AppService(ServiceLifetime= LifeTime.Transient)] + [AppService(ServiceLifetime = LifeTime.Transient)] public class SysDeptRepository : BaseRepository { /// @@ -22,9 +22,10 @@ namespace ZR.Repository.System return Context.SqlQueryable(sql).AddParameters(new { @deptId = deptId }).ToList(); } - public int UdateDeptChildren(SysDept dept) + public int UdateDeptChildren(List dept) { - return Context.Updateable(dept).UpdateColumns(it => new { it.Ancestors }).ExecuteCommand(); + return Context.Updateable(dept).WhereColumns(f => new { f.DeptId }) + .UpdateColumns(it => new { it.Ancestors }).ExecuteCommand(); } } } diff --git a/ZR.Repository/System/SysUserRepository.cs b/ZR.Repository/System/SysUserRepository.cs index 57423c1..6c263a4 100644 --- a/ZR.Repository/System/SysUserRepository.cs +++ b/ZR.Repository/System/SysUserRepository.cs @@ -3,6 +3,7 @@ using Infrastructure.Extensions; using SqlSugar; using System; using System.Collections.Generic; +using System.Linq; using ZR.Model; using ZR.Model.System; @@ -22,15 +23,24 @@ namespace ZR.Repository.System /// public PagedInfo SelectUserList(SysUser user, PagerInfo pager) { + var exp = Expressionable.Create(); + exp.AndIF(!string.IsNullOrEmpty(user.UserName), u => u.UserName.Contains(user.UserName)); + exp.AndIF(!string.IsNullOrEmpty(user.Status), u => u.Status == user.Status); + exp.AndIF(user.BeginTime != DateTime.MinValue && user.BeginTime != null, u => u.Create_time >= user.BeginTime); + exp.AndIF(user.EndTime != DateTime.MinValue && user.EndTime != null, u => u.Create_time <= user.EndTime); + exp.AndIF(!user.Phonenumber.IsEmpty(), u => u.Phonenumber == user.Phonenumber); + exp.And(u => u.DelFlag == "0"); + + if (user.DeptId != 0) + { + SysDept dept = Context.Queryable().First(f => f.DeptId == user.DeptId); + string[] deptArr = dept?.Ancestors.Split(",").ToArray(); + + exp.AndIF(user.DeptId != 0, u => u.DeptId == user.DeptId);// || deptArr.Contains(u.DeptId.ToString())); + } var query = Context.Queryable() .LeftJoin((u, dept) => u.DeptId == dept.DeptId) - .WhereIF(!string.IsNullOrEmpty(user.UserName), u => u.UserName.Contains(user.UserName)) - .WhereIF(!string.IsNullOrEmpty(user.Status), u => u.Status == user.Status) - .WhereIF(user.BeginTime != DateTime.MinValue && user.BeginTime != null, u => u.Create_time >= user.BeginTime) - .WhereIF(user.EndTime != DateTime.MinValue && user.EndTime != null, u => u.Create_time <= user.EndTime) - .WhereIF(!user.Phonenumber.IsEmpty(), u => u.Phonenumber == user.Phonenumber) - .WhereIF(user.DeptId != 0, u => u.DeptId == user.DeptId) - .Where(u => u.DelFlag == "0") + .Where(exp.ToExpression()) .Select((u, dept) => new SysUser { UserId = u.UserId.SelectAll(), diff --git a/ZR.Service/System/SysDeptService.cs b/ZR.Service/System/SysDeptService.cs index 14da4b7..9a996e9 100644 --- a/ZR.Service/System/SysDeptService.cs +++ b/ZR.Service/System/SysDeptService.cs @@ -86,7 +86,7 @@ namespace ZR.Service.System /// public int UpdateDept(SysDept dept) { - SysDept newParentDept = DeptRepository.GetFirst(it => it.ParentId == dept.ParentId); + SysDept newParentDept = DeptRepository.GetFirst(it => it.DeptId == dept.ParentId); SysDept oldDept = DeptRepository.GetFirst(m => m.DeptId == dept.DeptId); if (newParentDept != null && oldDept != null) { @@ -96,10 +96,11 @@ namespace ZR.Service.System UpdateDeptChildren(dept.DeptId, newAncestors, oldAncestors); } int result = DeptRepository.Context.Updateable(dept).ExecuteCommand(); - if (UserConstants.DEPT_NORMAL.Equals(dept.Status)) + if (UserConstants.DEPT_NORMAL.Equals(dept.Status) && dept.Ancestors.IfNotEmpty() + && !"0".Equals(dept.Ancestors)) { // 如果该部门是启用状态,则启用该部门的所有上级部门 - //UpdateParentDeptStatus(dept); + UpdateParentDeptStatusNormal(dept); } return result; } @@ -108,12 +109,13 @@ namespace ZR.Service.System /// 修改该部门的父级部门状态 /// /// 当前部门 - private void UpdateParentDeptStatus(SysDept dept) + private void UpdateParentDeptStatusNormal(SysDept dept) { - string updateBy = dept.Update_by; - dept = DeptRepository.GetFirst(it => it.DeptId == dept.DeptId); - dept.Update_by = updateBy; - //DeptRepository.UpdateParentDeptStatus(dept); + long[] depts = Tools.SpitLongArrary(dept.Ancestors); + dept.Status = "0"; + dept.Update_time = DateTime.Now; + + DeptRepository.Update(dept, it => new { it.Update_by, it.Update_time, it.Status }, f => depts.Contains(f.DeptId)); } /// @@ -128,14 +130,22 @@ namespace ZR.Service.System foreach (var child in children) { - child.Ancestors = child.Ancestors.Replace(oldAncestors, newAncestors); + string ancestors = child.Ancestors.ReplaceFirst(oldAncestors, newAncestors); + long[] ancestorsArr = Tools.SpitLongArrary(ancestors).Distinct().ToArray(); + child.Ancestors = string.Join(",", ancestorsArr); } - if (children.Count > 0) + if (children.Any()) { - //DeptRepository.UdateDeptChildren(child); + DeptRepository.UdateDeptChildren(children); } } + /// + /// 获取所有子部门 + /// + /// + /// + /// public List GetChildrenDepts(List depts, long deptId) { return depts.FindAll(delegate (SysDept item)