完成部门管理没实现功能

This commit is contained in:
不做码农 2021-12-25 12:05:30 +08:00
parent 27cca0e4a4
commit 412dccf416
4 changed files with 57 additions and 21 deletions

View File

@ -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);
}
/// <summary>
/// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//(");
/// </summary>
/// <param name="input"></param>
/// <param name="oldValue"></param>
/// <param name="newValue"></param>
/// <returns></returns>
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);
}
}
}

View File

@ -7,7 +7,7 @@ namespace ZR.Repository.System
/// <summary>
/// 部门管理
/// </summary>
[AppService(ServiceLifetime= LifeTime.Transient)]
[AppService(ServiceLifetime = LifeTime.Transient)]
public class SysDeptRepository : BaseRepository<SysDept>
{
/// <summary>
@ -22,9 +22,10 @@ namespace ZR.Repository.System
return Context.SqlQueryable<SysDept>(sql).AddParameters(new { @deptId = deptId }).ToList();
}
public int UdateDeptChildren(SysDept dept)
public int UdateDeptChildren(List<SysDept> 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();
}
}
}

View File

@ -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
/// <returns></returns>
public PagedInfo<SysUser> SelectUserList(SysUser user, PagerInfo pager)
{
var exp = Expressionable.Create<SysUser>();
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<SysDept>().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<SysUser>()
.LeftJoin<SysDept>((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(),

View File

@ -86,7 +86,7 @@ namespace ZR.Service.System
/// <returns></returns>
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
/// 修改该部门的父级部门状态
/// </summary>
/// <param name="dept">当前部门</param>
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));
}
/// <summary>
@ -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);
}
}
/// <summary>
/// 获取所有子部门
/// </summary>
/// <param name="depts"></param>
/// <param name="deptId"></param>
/// <returns></returns>
public List<SysDept> GetChildrenDepts(List<SysDept> depts, long deptId)
{
return depts.FindAll(delegate (SysDept item)