public override department ToDto(Department value) { department d = new department(); d.description = value.Description; d.id = value.Id; d.code = value.Code; d.name = value.Name; if (value.JobPositions != null) { d.jobPositions = new List <jobPosition>(); JobPositionConverter jpConverter = new JobPositionConverter(DbContext); var jps = value.JobPositions.OrderBy(j => j.Code); foreach (var jp in jps) { d.jobPositions.Add(jpConverter.ToDto(jp)); } } d.parentDepartmentId = value.ParentDepartment?.Id; if (value.Leader != null) { // EmployeeConverter emConverter = new EmployeeConverter(); // d.leader = emConverter.Convert(value.Leader); } return(d); }
public override employee ToDto(Employee model) { employee em = new employee() { address = model.Address, birthday = model.BirthDay, code = model.Code, birthdayType = model.BirthDayType, description = model.Description, education = model.Education, email = model.Email, id = model.Id, idCardCode = model.IdCardCode, isVirtual = model.IsVirtual, mobileNumber = model.MobileNumber, name = model.Name, passWord = model.Passwrod, serveState = model.ServeState, sex = model.Sex, nationality = model.Nationality, userId = model.UserId, pictureUrl = model.PictureUrl, QQ = model.QQ, WeChat = model.WeChat, userRoleIds = model.UserRoleIds == null?null:model.UserRoleIds.Split(','), permissionIds = model.PermissionIds == null ? null : model.PermissionIds.Split(','), }; if (model.Department != null) { em.departmentId = model.Department.Id; em.departmentName = model.Department.Name; } if (model.ServingRecords != null) { var jpC = new JobPositionConverter(DbContext); List <servingRecord> list = new List <servingRecord>(); foreach (var item in model.ServingRecords) { servingRecord record = new servingRecord { beginTime = item.BeginTime, employeeId = item.Employee.Id, endTime = item.EndTime, id = item.Id, isConcurrent = item.IsConcurrent, isCurrent = item.IsCurrent, jobPosition = jpC.ToDto(item.JobPosition) }; list.Add(record); } em.servingRecords = list.ToArray(); } if (model.Image != null) { em.image = Convert.ToBase64String(model.Image); } return(em); }
public override Department FromDto(department d) { Department target = DbContext.Departments.Find(d.id); if (target == null) { target = new Department(); target.Id = d.id; } target.Code = d.code; target.Description = d.description; if (d.leader != null) { target.Leader = DbContext.Employees.Find(target.Leader.Id); } target.Name = d.name; if (!string.IsNullOrEmpty(d.parentDepartmentId)) { target.ParentDepartment = DbContext.Departments.Find(d.parentDepartmentId); } if (d.jobPositions != null) { if (target.JobPositions == null) { target.JobPositions = new List <JobPosition>(); } string[] jobPositonIds = d.jobPositions.Select(p => p.id).ToArray(); IEnumerable <JobPosition> toDeletes = target.JobPositions.Where(p => !jobPositonIds.Contains(p.Id)); DbContext.JobPositions.RemoveRange(toDeletes); List <JobPosition> jpList = new List <JobPosition>(); foreach (var jp in d.jobPositions) { JobPositionConverter jpCvt = new JobPositionConverter(DbContext); JobPosition jobP = jpCvt.FromDto(jp); jobP.Department = target; jpList.Add(jobP); } foreach (var item in jpList) { var entity = DbContext.JobPositions.Find(item.Id); if (entity == null) { DbContext.JobPositions.Add(item); } else { DbContext.Entry(entity).CurrentValues.SetValues(item); } } } return(target); }