| | |
| | | using WMS.Entity.SysEntity; |
| | | using WMS.IBLL.ISysServer; |
| | | using WMS.IDAL.ISysInterface; |
| | | using SqlSugar; |
| | | using WMS.Entity.Context; |
| | | using Utility; |
| | | using Microsoft.AspNetCore.Identity; |
| | | using System.Data; |
| | | |
| | | namespace WMS.BLL.SysServer |
| | | { |
| | |
| | | /// </summary> |
| | | private readonly IDepartmentRepository _department; |
| | | readonly IMapper _mapper; |
| | | private static readonly SqlSugarScope Db = DataContext.Db; |
| | | private readonly UserManager _userManager; |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="server">部门仓储接口</param> |
| | | /// <param name="mapper">automapper</param> |
| | | public DepartmentServer(IDepartmentRepository department, IMapper mapper) |
| | | public DepartmentServer(IDepartmentRepository department, IMapper mapper, UserManager userManager) |
| | | { |
| | | _department = department;//部门 |
| | | _mapper = mapper;//automapper |
| | | _userManager = userManager; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="DepartmentName">部门名称</param> |
| | | /// <param name="DepartmentNo">部门号</param> |
| | | /// <returns></returns> |
| | | public List<DepartmentDto> GetDepartmentList(string DepartmentName, string DepartmentNo) |
| | | public async Task<List<DepartmentDto>> GetDepartmentList(string DepartmentName, string DepartmentNo) |
| | | { |
| | | List<DepartmentDto> departmentlist = _department.GetDepartmentList(DepartmentName, DepartmentNo); |
| | | return departmentlist; |
| | | return await Db.Queryable<SysDepartment>() |
| | | .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id) |
| | | .WhereIF(!string.IsNullOrEmpty(DepartmentNo), a => a.DepartmentNo.Contains(DepartmentNo)) |
| | | .WhereIF(!string.IsNullOrEmpty(DepartmentName), a => a.DepartmentName.Contains(DepartmentName)) |
| | | .Where(a => a.IsDel == "0") |
| | | .Select<DepartmentDto>((a, b) => new DepartmentDto() { CreateUserName = b.RealName }, true) |
| | | .ToListAsync(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="id">部门id</param> |
| | | /// <returns></returns> |
| | | public SysDepartment GetDepartmentById(int id) |
| | | public async Task<SysDepartment> GetDepartmentById(int id) |
| | | { |
| | | SysDepartment department = _department.GetDepartmentById(id); |
| | | return department; |
| | | return await Db.Queryable<SysDepartment>().FirstAsync(s => s.Id == id); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据部门号获取部门信息 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public int GetDepartmentByNo(string DepartmentNo) |
| | | public async Task<int> GetDepartmentByNo(string DepartmentNo) |
| | | { |
| | | List<SysDepartment> department = _department.GetDepartmentByNo(DepartmentNo); |
| | | return department.Count; |
| | | return await Db.Queryable<SysDepartment>().CountAsync(s => s.DepartmentNo == DepartmentNo && s.IsDel == "0"); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 新增部门信息 |
| | | /// </summary> |
| | |
| | | /// <exception cref="Exception">捕获异常</exception> |
| | | public async Task<int> AddDepartment(DepartmentDto departmentdto) |
| | | { |
| | | //捕获异常 |
| | | try |
| | | //模型映射 |
| | | SysDepartment department = _mapper.Map<SysDepartment>(departmentdto); |
| | | //判断部门号是否唯一 |
| | | int count = await GetDepartmentByNo(department.DepartmentNo); |
| | | if (count > 0) |
| | | { |
| | | //模型映射 |
| | | SysDepartment department = _mapper.Map<SysDepartment>(departmentdto); |
| | | //判断部门号是否唯一 |
| | | int count = GetDepartmentByNo(department.DepartmentNo); |
| | | int i = 0; |
| | | if (count > 0) |
| | | { |
| | | i = 3; |
| | | } |
| | | else if (count == 0) |
| | | { |
| | | i = await _department.AddDepartment(department); |
| | | } |
| | | return i; |
| | | |
| | | throw Oops.Bah("部门号必须唯一"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | //抛出异常 |
| | | throw new Exception("新增部门异常", ex); |
| | | } |
| | | department.CreateTime = DateTime.Now; |
| | | department.CreateUser = _userManager.UserId; |
| | | return await Db.Insertable(department).ExecuteCommandAsync(); |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <exception cref="Exception">捕获异常</exception> |
| | | public async Task<int> DelDepartment(SysDepartment department) |
| | | { |
| | | //捕获异常 |
| | | try |
| | | { |
| | | //删除 |
| | | int i = await _department.DelDepartment(department); |
| | | return i; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | //抛出异常 |
| | | throw new Exception("删除部门异常", ex); |
| | | } |
| | | return await Db.Updateable<SysDepartment>() |
| | | .Where(s => s.Id == department.Id) |
| | | .SetColumns(s => s.IsDel == "1") |
| | | .SetColumns(s => s.UpdateTime == DateTime.Now) |
| | | .SetColumns(s => s.UpdateUser == _userManager.UserId) |
| | | .ExecuteCommandAsync(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <exception cref="Exception">捕获异常</exception> |
| | | public async Task<int> ExitDepartment(DepartmentDto departmentdto) |
| | | { |
| | | //捕获异常 |
| | | try |
| | | { |
| | | //模型映射 |
| | | SysDepartment department = _mapper.Map<SysDepartment>(departmentdto); |
| | | //编辑 |
| | | int i = await _department.ExitDepartment(department); |
| | | return i; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | //抛出异常 |
| | | throw new Exception("编辑部门异常", ex); |
| | | } |
| | | SysDepartment department = _mapper.Map<SysDepartment>(departmentdto); |
| | | int count = await Db.Queryable<SysDepartment>().CountAsync(s => s.DepartmentNo == departmentdto.DepartmentNo && s.Id != departmentdto.Id && s.IsDel == "0"); |
| | | if (count > 0) |
| | | throw Oops.Bah("部门号必须唯一"); |
| | | department.UpdateUser = _userManager.UserId; |
| | | department.UpdateTime = DateTime.Now; |
| | | return await Db.Updateable(department) |
| | | .UpdateColumns(s => new { s.DepartmentNo, s.DepartmentName, s.Demo, s.UpdateUser, s.UpdateTime }) |
| | | .ExecuteCommandAsync(); |
| | | } |
| | | } |
| | | } |