using QM.AuthServer.Models.Roles; using QM.AuthServer.IRepository; using QM.OrmSqlSugar; using System; using System.Linq; using System.Threading.Tasks; using WS; using WS.AutoMapper; namespace QM.AuthServer.Repository { public class SysDepartmentRepository : SqlSugarRepository, ISysDepartmentRepository { /// /// 检查 /// /// /// public SysDepartment CheckId(string bid) { var model = Get(t => t.bid == bid); if (model == null) throw new UseArgumentException("部门不存在,请检查数据是否正确"); return model; } /// /// 创建 /// /// /// public async Task Create(DepartmentInputDto input, string user) { var model = Get(t => t.name == input.name); if (model != null) throw new UseArgumentException("部门已存在,请重新输入"); SysDepartment role = input.MapTo(); role.bid = Guid.NewGuid().ToStringN(); role.cuser = user; await InsertAsync(role); return true; } /// /// 修改 /// /// /// public async Task Update(DepartmentUpdateDto input, string user) { var person = CheckId(input.bid); var model = input.Map(person); await UpdateAsync(model); return true; } /// /// 删除 /// /// /// public async Task Delete(string bid) { var person = CheckId(bid); try { BeginTran(); var gids = Db.Queryable().Where(t => t.bid == bid).Select(t => t.gid).ToList(); await Db.Deleteable(t => gids.Contains(t.gid)).ExecuteCommandAsync(); await Db.Deleteable(t => t.bid == bid).ExecuteCommandAsync(); await Db.Deleteable(t => t.bid == bid).ExecuteCommandAsync(); CommitTran(); return true; } catch (Exception ex) { RollbackTran(); } return false; } } }