using AutoMapper; using Model.ModelDto.SysDto; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; 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 { /// /// 部门服务实践 /// public class DepartmentServer : IDepartmentServer { /// /// 依赖注入 /// private readonly IDepartmentRepository _department; readonly IMapper _mapper; private static readonly SqlSugarScope Db = DataContext.Db; private readonly UserManager _userManager; /// /// 构造函数 /// /// 部门仓储接口 /// automapper public DepartmentServer(IDepartmentRepository department, IMapper mapper, UserManager userManager) { _department = department;//部门 _mapper = mapper;//automapper _userManager = userManager; } /// /// 获取部门信息列表 /// /// 部门名称 /// 部门号 /// public async Task> GetDepartmentList(string DepartmentName, string DepartmentNo) { return await Db.Queryable() .LeftJoin((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((a, b) => new DepartmentDto() { CreateUserName = b.RealName }, true) .ToListAsync(); } /// /// 根据id获取部门信息 /// /// 部门id /// public async Task GetDepartmentById(int id) { return await Db.Queryable().FirstAsync(s => s.Id == id); } /// /// 根据部门号获取部门信息 /// /// public async Task GetDepartmentByNo(string DepartmentNo) { return await Db.Queryable().CountAsync(s => s.DepartmentNo == DepartmentNo && s.IsDel == "0"); } /// /// 新增部门信息 /// /// 部门dto /// /// 捕获异常 public async Task AddDepartment(DepartmentDto departmentdto) { //模型映射 SysDepartment department = _mapper.Map(departmentdto); //判断部门号是否唯一 int count = await GetDepartmentByNo(department.DepartmentNo); if (count > 0) { throw Oops.Bah("部门号必须唯一"); } department.CreateTime = DateTime.Now; department.CreateUser = _userManager.UserId; return await Db.Insertable(department).ExecuteCommandAsync(); } /// /// 删除部门信息 /// /// 部门实体模型 /// /// 捕获异常 public async Task DelDepartment(SysDepartment department) { return await Db.Updateable() .Where(s => s.Id == department.Id) .SetColumns(s => s.IsDel == "1") .SetColumns(s => s.UpdateTime == DateTime.Now) .SetColumns(s => s.UpdateUser == _userManager.UserId) .ExecuteCommandAsync(); } /// /// 编辑部门信息 /// /// 部门dto /// /// 捕获异常 public async Task ExitDepartment(DepartmentDto departmentdto) { SysDepartment department = _mapper.Map(departmentdto); int count = await Db.Queryable().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(); } } }